All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.lontime.shaded.org.redisson.codec.DefaultReferenceCodecProvider Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2013-2021 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 com.github.lontime.shaded.org.redisson.codec;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.github.lontime.shaded.org.redisson.api.RObject;
import com.github.lontime.shaded.org.redisson.api.annotation.REntity;
import com.github.lontime.shaded.org.redisson.api.annotation.RObjectField;
import com.github.lontime.shaded.org.redisson.client.codec.Codec;
import com.github.lontime.shaded.org.redisson.config.Config;
import com.github.lontime.shaded.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) {
        try {
            if (!ClassUtils.getDeclaredField(cls, fieldName).isAnnotationPresent(anno.getClass())) {
                throw new IllegalArgumentException("Annotation RObjectField does not present on field " + fieldName + " of type [" + cls.getCanonicalName() + "]");
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        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