Interface TypeConverter


public interface TypeConverter
Type Converter SPI 类型转换器SPI

Provides a service provider interface for type conversion.

为类型转换提供服务提供者接口。

Features | 主要功能:

  • SPI for custom type conversion logic - 用于自定义类型转换逻辑的SPI
  • Convertibility check before conversion - 转换前的可转换性检查
  • Generic type-safe conversion method - 泛型类型安全转换方法

Usage Examples | 使用示例:

public class MoneyTypeConverter implements TypeConverter {
    @Override
    public boolean canConvert(Class<?> source, Class<?> target) {
        return target == Money.class && source == String.class;
    }

    @Override
    public <T> T convert(Object value, Class<T> targetType) {
        return targetType.cast(Money.parse((String) value));
    }
}

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Null-safe: Depends on implementation - 空值安全: 取决于实现

Performance | 性能特性:

  • Time complexity: O(1) for canConvert and convert in typical implementations - 时间复杂度: 典型实现中 canConvert 和 convert 均为 O(1)
  • Space complexity: O(1) - 空间复杂度: O(1)
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canConvert(Class<?> sourceType, Class<?> targetType)
    Check if conversion is possible 检查是否可以转换
    <T> T
    convert(Object value, Class<T> targetType)
    Convert value to target type 将值转换为目标类型
  • Method Details

    • canConvert

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

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