Class ConverterRegistry

java.lang.Object
org.aoju.bus.core.convert.ConverterRegistry

public class ConverterRegistry extends Object
转换器登记中心,将各种类型Convert对象放入登记中心,通过convert方法查找 目标类型对应的转换器,将被转换对象转换之,在此类中,存放着默认转换器和自定义 转换器,默认转换器预定义的一些转换器,自定义转换器存放用户自定的转换器
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • ConverterRegistry

      public ConverterRegistry()
  • Method Details

    • getInstance

      public static ConverterRegistry getInstance()
      获得单例的 ConverterRegistry
      Returns:
      ConverterRegistry
    • putCustom

      public ConverterRegistry putCustom(Type type, Class<? extends Converter<?>> converterClass)
      登记自定义转换器
      Parameters:
      type - 转换的目标类型
      converterClass - 转换器类,必须有默认构造方法
      Returns:
      ConverterRegistry
    • putCustom

      public ConverterRegistry putCustom(Type type, Converter<?> converter)
      登记自定义转换器
      Parameters:
      type - 转换的目标类型
      converter - 转换器
      Returns:
      ConverterRegistry
    • getConverter

      public <T> Converter<T> getConverter(Type type, boolean isCustomFirst)
      获得转换器
      Type Parameters:
      T - 转换的目标类型
      Parameters:
      type - 类型
      isCustomFirst - 是否自定义转换器优先
      Returns:
      转换器
    • getDefaultConverter

      public <T> Converter<T> getDefaultConverter(Type type)
      获得默认转换器
      Type Parameters:
      T - 转换的目标类型(转换器转换到的类型)
      Parameters:
      type - 类型
      Returns:
      转换器
    • getCustomConverter

      public <T> Converter<T> getCustomConverter(Type type)
      获得自定义转换器
      Type Parameters:
      T - 转换的目标类型(转换器转换到的类型)
      Parameters:
      type - 类型
      Returns:
      转换器
    • convert

      public <T> T convert(Type type, Object value, T defaultValue, boolean isCustomFirst) throws ConvertException
      转换值为指定类型
      Type Parameters:
      T - 转换的目标类型(转换器转换到的类型)
      Parameters:
      type - 类型目标
      value - 被转换值
      defaultValue - 默认值
      isCustomFirst - 是否自定义转换器优先
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换器不存在
    • convert

      public <T> T convert(Type type, Object value, T defaultValue) throws ConvertException
      转换值为指定类型 自定义转换器优先
      Type Parameters:
      T - 转换的目标类型(转换器转换到的类型)
      Parameters:
      type - 类型
      value - 值
      defaultValue - 默认值
      Returns:
      转换后的值
      Throws:
      ConvertException - 转换器不存在
    • convert

      public <T> T convert(Type type, Object value) throws ConvertException
      转换值为指定类型
      Type Parameters:
      T - 转换的目标类型(转换器转换到的类型)
      Parameters:
      type - 类型
      value - 值
      Returns:
      转换后的值, 默认为null
      Throws:
      ConvertException - 转换器不存在