类 DsonCodecConfig

java.lang.Object
cn.wjybxx.dsoncodec.DsonCodecConfig

public final class DsonCodecConfig extends Object

泛型类Codec

由于泛型类的Codec不能被直接构造,因此只能先将其类型信息存储下来,在运行时根据泛型参数动态构造。

1. Codec编码的类型和要编码的类型有相同的泛型参数列表。且必须包含接收TypeInfo的构造函数 -- 可参考CollectionCodec。 2. 如果Codec的是面向接口或抽象类的,构造函数还可接收一个Supplier的参数。 3. 不会频繁查询,因此不必太在意匹配算法的效率。 4. 数组和泛型是不同的,数组都对应ArrayCodec,因此不需要再这里存储。 5. 请避免运行时修改数据,否则可能造成线程安全问题。 6. 反射难以确定泛型擦参数除后的类型,为避免增加Codec和运行时的复杂度,泛型原型的信息在注册时指定。

与TypeMetaConfig的关系

Codec与TypeMete在配置和运行时都是分离的,它们属于不同的体系; 但Codec关联的encoderType必须在TypeMetaConfig中存在。

合并规则

多个Config合并时,越靠近用户,优先级越高 -- 因为这一定能解决冲突。
作者:
wjybxx date - 2024/10/11
  • 字段详细资料

  • 构造器详细资料

    • DsonCodecConfig

      public DsonCodecConfig()
  • 方法详细资料

    • getEncoderDic

      public Map<cn.wjybxx.base.TypeInfo,DsonCodec<?>> getEncoderDic()
    • getDecoderDic

      public Map<cn.wjybxx.base.TypeInfo,DsonCodec<?>> getDecoderDic()
    • getGenericEncoderDic

      public Map<Class<?>,GenericCodecInfo> getGenericEncoderDic()
    • getGenericDecoderDic

      public Map<Class<?>,GenericCodecInfo> getGenericDecoderDic()
    • getCasters

      public List<DsonCodecCaster> getCasters()
    • getOptimizedTypes

      public Map<ClassPair,Boolean> getOptimizedTypes()
    • getGenericHelpers

      public List<GenericHelper> getGenericHelpers()
    • fromCodecs

      public static DsonCodecConfig fromCodecs(DsonCodec<?>... codecs)
      根据codecs创建一个Config -- 返回的实例不可变
    • fromCodecs

      public static DsonCodecConfig fromCodecs(Collection<? extends DsonCodec<?>> codecs)
      根据codecs创建一个Config -- 返回的实例不可变
    • fromConfigs

      public static DsonCodecConfig fromConfigs(Collection<? extends DsonCodecConfig> configs)
      合并多个Config为单个Config -- 返回的实例不可变
    • toImmutable

      public DsonCodecConfig toImmutable()
      转换为不可变实例
    • clear

      public void clear()
      清理数据
    • mergeFrom

      public DsonCodecConfig mergeFrom(DsonCodecConfig other)
      合并配置
    • addCodecs

      public DsonCodecConfig addCodecs(DsonCodec<?>... codecs)
      配置编解码器
    • addCodecs

      public DsonCodecConfig addCodecs(Collection<? extends DsonCodec<?>> codecs)
      配置编解码器
    • addCodec

      public DsonCodecConfig addCodec(DsonCodec<?> codec)
      配置编解码器
    • addCodec

      public <T> DsonCodecConfig addCodec(Class<T> clazz, DsonCodec<? super T> codec)
      配置编解码器 适用超类Codec的默认解码实例可赋值给当前类型的情况,eg:IntList => IntCollectionCodec。
    • addCodec

      public DsonCodecConfig addCodec(cn.wjybxx.base.TypeInfo typeInfo, DsonCodec<?> codec)
      配置编解码器
    • addEncoder

      public <T> DsonCodecConfig addEncoder(Class<T> clazz, DsonCodec<? super T> codec)
      配置编码器
    • addEncoder

      public DsonCodecConfig addEncoder(cn.wjybxx.base.TypeInfo typeInfo, DsonCodec<?> codec)
      配置编码器 -- 适用已构造泛型
    • addDecoder

      public <T> DsonCodecConfig addDecoder(Class<T> clazz, DsonCodec<? extends T> codec)
      配置解码器
    • addDecoder

      public DsonCodecConfig addDecoder(cn.wjybxx.base.TypeInfo typeInfo, DsonCodec<?> codec)
      配置解码器 -- 适用已构造泛型
    • removeEncoder

      public DsonCodec<?> removeEncoder(Class<?> clazz)
      删除编码器 -- 用于解决冲突
    • removeEncoder

      public DsonCodec<?> removeEncoder(cn.wjybxx.base.TypeInfo typeInfo)
      删除编码器 -- 适用已构造泛型
    • removeDecoder

      public DsonCodec<?> removeDecoder(Class<?> clazz)
      删除解码器 -- 用于解决冲突
    • removeDecoder

      public DsonCodec<?> removeDecoder(cn.wjybxx.base.TypeInfo typeInfo)
      删除解码器 -- 适用已构造泛型
    • addGenericCodec

      public DsonCodecConfig addGenericCodec(cn.wjybxx.base.TypeInfo genericType, Class<? extends DsonCodec> codecType)
      参数:
      genericType - 泛型定义类
      codecType - 解码器类型
      返回:
      this
    • addGenericCodec

      public DsonCodecConfig addGenericCodec(cn.wjybxx.base.TypeInfo genericType, Class<? extends DsonCodec> codecType, Class<?> implType)
      参数:
      genericType - 泛型定义类
      codecType - 解码器类型
      implType - 实现类类型
      返回:
      this
    • addGenericCodec

      public DsonCodecConfig addGenericCodec(cn.wjybxx.base.TypeInfo genericType, Class<? extends DsonCodec> codecType, Supplier<?> factory)
      参数:
      genericType - 泛型定义类
      codecType - 解码器类型
      factory - 实例工厂
      返回:
      this
    • addGenericCodec

      public DsonCodecConfig addGenericCodec(GenericCodecInfo genericCodecInfo)
      增加一个codec配置
      参数:
      genericCodecInfo - 编解码器类的信息
    • addGenericEncoder

      public DsonCodecConfig addGenericEncoder(cn.wjybxx.base.TypeInfo genericType, Class<? extends DsonCodec> codecType)
      参数:
      genericType - 泛型定义类
      codecType - 解码器类型
      返回:
      this
    • addGenericEncoder

      public DsonCodecConfig addGenericEncoder(cn.wjybxx.base.TypeInfo genericType, Class<? extends DsonCodec> codecType, Class<?> implType)
      参数:
      genericType - 泛型定义类
      codecType - 解码器类型
      implType - 实现类类型
      返回:
      this
    • addGenericEncoder

      public DsonCodecConfig addGenericEncoder(cn.wjybxx.base.TypeInfo genericType, Class<? extends DsonCodec> codecType, Supplier<?> factory)
      参数:
      genericType - 泛型定义类
      codecType - 解码器类型
      factory - 实例工厂
      返回:
      this
    • addGenericEncoder

      public DsonCodecConfig addGenericEncoder(GenericCodecInfo genericCodecInfo)
      添加编码器
      参数:
      genericCodecInfo - 编解码器类的信息
      返回:
      this
    • addGenericDecoder

      public DsonCodecConfig addGenericDecoder(cn.wjybxx.base.TypeInfo genericType, Class<? extends DsonCodec> codecType)
      参数:
      genericType - 泛型定义类
      codecType - 解码器类型
      返回:
      this
    • addGenericDecoder

      public DsonCodecConfig addGenericDecoder(cn.wjybxx.base.TypeInfo genericType, Class<? extends DsonCodec> codecType, Class<?> implType)
      参数:
      genericType - 泛型定义类
      codecType - 解码器类型
      implType - 实现类类型
      返回:
      this
    • addGenericDecoder

      public DsonCodecConfig addGenericDecoder(cn.wjybxx.base.TypeInfo genericType, Class<? extends DsonCodec> codecType, Supplier<?> factory)
      参数:
      genericType - 泛型定义类
      codecType - 解码器类型
      factory - 实例工厂
      返回:
      this
    • addGenericDecoder

      public DsonCodecConfig addGenericDecoder(GenericCodecInfo genericCodecInfo)
      添加解码器
      参数:
      genericCodecInfo - 编解码器类的信息
      返回:
      this
    • addCaster

      public DsonCodecConfig addCaster(DsonCodecCaster caster)
      添加类型转换器
    • addCasters

      public DsonCodecConfig addCasters(Collection<? extends DsonCodecCaster> casters)
    • addOptimizedType

      public <T> DsonCodecConfig addOptimizedType(Class<T> encoderType, Class<? super T> declaredType)
      添加可优化编码类型(无需写入类型信息的情况)
      参数:
      encoderType - 编码类型
      declaredType - 声明类型
    • addOptimizedType

      public <T> DsonCodecConfig addOptimizedType(Class<T> encoderType, Class<? super T> declaredType, boolean val)
      添加可优化编码类型(无需写入类型信息的情况) 泛型类请配置泛型原型类
      参数:
      encoderType - 编码类型
      declaredType - 声明类型
      val - 是否可优化
    • addGenericHelper

      public DsonCodecConfig addGenericHelper(GenericHelper genericHelper)
      添加泛型工具类
    • addGenericHelpers

      public DsonCodecConfig addGenericHelpers(Collection<? extends GenericHelper> genericHelpers)
      添加泛型工具类
    • getEncoder

      public DsonCodec<?> getEncoder(cn.wjybxx.base.TypeInfo typeInfo)
    • getDecoder

      public DsonCodec<?> getDecoder(cn.wjybxx.base.TypeInfo typeInfo)
    • getGenericEncoderInfo

      public GenericCodecInfo getGenericEncoderInfo(Class<?> genericTypeDefine)
    • getGenericDecoderInfo

      public GenericCodecInfo getGenericDecoderInfo(Class<?> genericTypeDefine)
    • newDefaultRegistry

      public static DsonCodecConfig newDefaultRegistry()
      创建一个默认配置