Class PropertyConverter

java.lang.Object
cloud.opencode.base.reflect.bean.PropertyConverter

public final class PropertyConverter extends Object
Property Type Converter 属性类型转换器

Converts property values between different types.

在不同类型之间转换属性值。

Features | 主要功能:

  • String/Number/Boolean type conversions - String/Number/Boolean类型转换
  • Date/Time type conversions - 日期/时间类型转换
  • Enum and Collection conversions - 枚举和集合转换
  • Custom converter registration - 自定义转换器注册

Usage Examples | 使用示例:

int num = PropertyConverter.convert("42", int.class);
String str = PropertyConverter.convert(42, String.class);
boolean canConvert = PropertyConverter.canConvert(String.class, Integer.class);

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap for converter registry) - 线程安全: 是(使用ConcurrentHashMap存储转换器)
  • Null-safe: Yes (null input returns type default) - 空值安全: 是(null输入返回类型默认值)

Performance | 性能特性:

  • Time complexity: O(1) for convert (hash map lookup) and canConvert - 时间复杂度: convert(哈希映射查找)和 canConvert 均为 O(1)
  • Space complexity: O(1) per conversion - 空间复杂度: 每次转换 O(1)
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • convert

      public static <T> T convert(Object value, Class<T> targetType)
      Converts a value to the target type 将值转换为目标类型
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      value - the value to convert | 要转换的值
      targetType - the target type | 目标类型
      Returns:
      the converted value | 转换后的值
    • convertSafe

      public static <T> T convertSafe(Object value, Class<T> targetType)
      Converts a value, returning null if conversion fails 转换值,如果转换失败返回null
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      value - the value to convert | 要转换的值
      targetType - the target type | 目标类型
      Returns:
      the converted value or null | 转换后的值或null
    • convertOrDefault

      public static <T> T convertOrDefault(Object value, Class<T> targetType, T defaultValue)
      Converts a value with a default value if conversion fails 转换值,如果转换失败返回默认值
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      value - the value to convert | 要转换的值
      targetType - the target type | 目标类型
      defaultValue - the default value | 默认值
      Returns:
      the converted value or default | 转换后的值或默认值
    • canConvert

      public static boolean canConvert(Class<?> sourceType, Class<?> targetType)
      Checks if conversion is possible 检查是否可以转换
      Parameters:
      sourceType - the source type | 源类型
      targetType - the target type | 目标类型
      Returns:
      true if conversion is possible | 如果可以转换返回true
    • registerConverter

      public static <S,T> void registerConverter(Class<S> sourceType, Class<T> targetType, Function<Object,Object> converter)
      Registers a custom converter 注册自定义转换器
      Type Parameters:
      S - the source type | 源类型
      T - the target type | 目标类型
      Parameters:
      sourceType - the source type | 源类型
      targetType - the target type | 目标类型
      converter - the converter function | 转换器函数