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

cn.wjybxx.dson.codec.dsonlite.DsonLiteConverterUtils Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/*
 * Copyright 2023-2024 wjybxx([email protected])
 *
 * 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 cn.wjybxx.dson.codec.dsonlite;

import cn.wjybxx.dson.codec.ConverterUtils;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Map;

/**
 * @author wjybxx
 * date 2023/4/1
 */
public class DsonLiteConverterUtils extends ConverterUtils {

    /** 默认codec注册表 */
    private static final DsonLiteCodecRegistry CODEC_REGISTRY;

    static {
        Map, DsonLiteCodecImpl> codecMap = DsonLiteCodecRegistries.newCodecMap(BUILTIN_CODECS.stream()
                .map(DsonLiteCodecImpl::new)
                .toList());
        CODEC_REGISTRY = new DefaultCodecRegistry(codecMap);
    }

    public static DsonLiteCodecRegistry getDefaultCodecRegistry() {
        return CODEC_REGISTRY;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    private static class DefaultCodecRegistry implements DsonLiteCodecRegistry {

        final Map, DsonLiteCodecImpl> codecMap;

        final DsonLiteCodecImpl objectArrayCodec;
        final DsonLiteCodecImpl collectionCodec;
        final DsonLiteCodecImpl mapCodec;

        private DefaultCodecRegistry(Map, DsonLiteCodecImpl> codecMap) {
            this.codecMap = codecMap;

            this.objectArrayCodec = getCodec(codecMap, Object[].class);
            this.collectionCodec = getCodec(codecMap, Collection.class);
            this.mapCodec = getCodec(codecMap, Map.class);
        }

        private static  DsonLiteCodecImpl getCodec(Map, DsonLiteCodecImpl> codecMap, Class clazz) {
            DsonLiteCodecImpl codec = (DsonLiteCodecImpl) codecMap.get(clazz);
            if (codec == null) throw new IllegalArgumentException(clazz.getName());
            return codec;
        }

        @Nullable
        @Override
        public  DsonLiteCodecImpl get(Class clazz) {
            DsonLiteCodecImpl codec = codecMap.get(clazz);
            if (codec != null) return (DsonLiteCodecImpl) codec;

            if (clazz.isArray()) return (DsonLiteCodecImpl) objectArrayCodec;
            if (Collection.class.isAssignableFrom(clazz)) return (DsonLiteCodecImpl) collectionCodec;
            if (Map.class.isAssignableFrom(clazz)) return (DsonLiteCodecImpl) mapCodec;
            return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy