Interface AttributeConverter<X,Y>

Type Parameters:
X - the attribute type | 属性类型
Y - the storage type | 存储类型

public interface AttributeConverter<X,Y>
AttributeConverter - SPI for bidirectional type conversion between two types. 属性转换器 - 用于两种类型之间双向转换的 SPI。

Implement this interface to define custom conversion logic between an attribute type and a storage type. Commonly used for entity field to database column type mapping.

实现此接口以定义属性类型和存储类型之间的自定义转换逻辑。 常用于实体字段到数据库列类型的映射。

Example | 示例:

public class JsonConverter implements AttributeConverter<MyObject, String> {
    public String convertTo(MyObject attribute) {
        return JsonUtils.toJson(attribute);
    }
    public MyObject convertFrom(String stored) {
        return JsonUtils.fromJson(stored, MyObject.class);
    }
}

Features | 主要功能:

  • Bidirectional type conversion SPI - 双向类型转换SPI
  • Generic type parameters for type safety - 泛型参数保证类型安全
  • Suitable for entity-to-storage mapping - 适用于实体到存储的映射

Usage Examples | 使用示例:

AttributeConverter<LocalDate, String> conv = new AttributeConverter<>() {
    public String convertTo(LocalDate date) { return date.toString(); }
    public LocalDate convertFrom(String s) { return LocalDate.parse(s); }
};

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Null-safe: Depends on implementation, null inputs possible - 空值安全: 取决于实现,可能接收null输入
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    convertFrom(Y stored)
    Converts the storage value back into the attribute value.
    convertTo(X attribute)
    Converts the attribute value into the storage representation.
  • Method Details

    • convertTo

      Y convertTo(X attribute)
      Converts the attribute value into the storage representation. 将属性值转换为存储表示形式。
      Parameters:
      attribute - the attribute value to be converted; may be null | 要转换的属性值,可以为 null
      Returns:
      the converted storage value | 转换后的存储值
    • convertFrom

      X convertFrom(Y stored)
      Converts the storage value back into the attribute value. 将存储值转换回属性值。
      Parameters:
      stored - the storage value to be converted; may be null | 要转换的存储值,可以为 null
      Returns:
      the converted attribute value | 转换后的属性值