org.redisson.codec.DefaultReferenceCodecProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
/**
* Copyright (c) 2013-2024 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson.codec;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.redisson.api.RObject;
import org.redisson.api.annotation.REntity;
import org.redisson.api.annotation.RObjectField;
import org.redisson.client.codec.Codec;
import org.redisson.config.Config;
import org.redisson.liveobject.misc.ClassUtils;
/**
*
* @author Rui Gu (https://github.com/jackygurui)
*/
public class DefaultReferenceCodecProvider implements ReferenceCodecProvider {
private final ConcurrentMap, Codec> codecCache = new ConcurrentHashMap<>();
@Override
public T getCodec(Class codecClass) {
Codec codec = codecCache.get(codecClass);
if (codec == null) {
try {
codec = codecClass.getDeclaredConstructor().newInstance();
codecCache.putIfAbsent(codecClass, codec);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
return (T) codec;
}
@Override
public T getCodec(REntity anno, Class> cls, Config config) {
if (!ClassUtils.isAnnotationPresent(cls, anno.annotationType())) {
throw new IllegalArgumentException("Annotation REntity does not present on type [" + cls.getCanonicalName() + "]");
}
Class> codecClass;
if (anno.codec() == REntity.DEFAULT.class) {
codecClass = config.getCodec().getClass();
} else {
codecClass = anno.codec();
}
return this.getCodec((Class) codecClass);
}
@Override
public T getCodec(RObjectField anno, Class> cls, Class rObjectClass, String fieldName, Config config) {
if (!ClassUtils.isAnnotationPresent(cls, anno.annotationType())) {
throw new IllegalArgumentException("Annotation RObjectField does not present on field " + fieldName + " of type [" + cls.getCanonicalName() + "]");
}
if (rObjectClass.isInterface()) {
throw new IllegalArgumentException("Cannot lookup an interface class of RObject [" + rObjectClass.getCanonicalName() + "]. Concrete class only.");
}
Class> codecClass;
if (anno.codec() == RObjectField.DEFAULT.class) {
codecClass = config.getCodec().getClass();
} else {
codecClass = anno.codec();
}
return this.getCodec((Class) codecClass);
}
@Override
public void registerCodec(Class cls, T codec) {
if (!cls.isInstance(codec)) {
throw new IllegalArgumentException("codec is not an instance of the class [" + cls.getCanonicalName() + "]");
}
codecCache.putIfAbsent(cls, codec);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy