Class RecordMapper<S,T>

java.lang.Object
cloud.opencode.base.reflect.record.RecordMapper<S,T>
Type Parameters:
S - the source type | 源类型
T - the target type | 目标类型

public final class RecordMapper<S,T> extends Object
Record Mapper for advanced Record/Bean mapping Record映射器 - 高级Record/Bean映射

Provides advanced mapping between Records and Beans with support for field renaming, exclusion, type conversion, and batch operations.

提供Record和Bean之间的高级映射,支持字段重命名、排除、类型转换和批量操作。

Features | 主要功能:

  • Record to Record mapping - Record到Record映射
  • Record to Bean mapping - Record到Bean映射
  • Bean to Record mapping - Bean到Record映射
  • Bean to Bean mapping - Bean到Bean映射
  • Field renaming via map() - 通过map()重命名字段
  • Field exclusion via exclude() - 通过exclude()排除字段
  • Custom type converters - 自定义类型转换器
  • Null value handling - 空值处理
  • Batch mapping - 批量映射

Usage Examples | 使用示例:

// Simple Record to Record mapping
RecordMapper<UserDTO, User> mapper = RecordMapper.builder(UserDTO.class, User.class)
    .map("userName", "name")
    .exclude("password")
    .convert("age", v -> Integer.parseInt(v.toString()))
    .ignoreNulls(true)
    .build();

User user = mapper.map(dto);
List<User> users = mapper.mapAll(dtos);

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
  • Null-safe: No (source must not be null) - 空值安全: 否(源不能为null)

Performance | 性能特性:

  • Time complexity: O(n) per mapping where n is the number of fields - 时间复杂度: 每次映射O(n),n为字段数
  • Space complexity: O(n) for the field mapping configuration - 空间复杂度: O(n)用于字段映射配置
Since:
JDK 25, opencode-base-reflect V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Builder for RecordMapper RecordMapper构建器
  • Method Summary

    Modifier and Type
    Method
    Description
    static <S,T> RecordMapper.Builder<S,T>
    builder(Class<S> sourceType, Class<T> targetType)
    Creates a new RecordMapper builder 创建新的RecordMapper构建器
    map(S source)
    Maps a source object to a target object 将源对象映射为目标对象
    mapAll(Collection<? extends S> sources)
    Maps a collection of source objects to a list of target objects 将源对象集合映射为目标对象列表

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • builder

      public static <S,T> RecordMapper.Builder<S,T> builder(Class<S> sourceType, Class<T> targetType)
      Creates a new RecordMapper builder 创建新的RecordMapper构建器
      Type Parameters:
      S - the source type | 源类型
      T - the target type | 目标类型
      Parameters:
      sourceType - the source type | 源类型
      targetType - the target type | 目标类型
      Returns:
      the builder | 构建器
      Throws:
      NullPointerException - if sourceType or targetType is null | 如果sourceType或targetType为null
    • map

      public T map(S source)
      Maps a source object to a target object 将源对象映射为目标对象
      Parameters:
      source - the source object | 源对象
      Returns:
      the mapped target object | 映射后的目标对象
      Throws:
      NullPointerException - if source is null | 如果源对象为null
      OpenReflectException - if mapping fails | 如果映射失败
    • mapAll

      public List<T> mapAll(Collection<? extends S> sources)
      Maps a collection of source objects to a list of target objects 将源对象集合映射为目标对象列表
      Parameters:
      sources - the source collection | 源集合
      Returns:
      the mapped target list | 映射后的目标列表
      Throws:
      NullPointerException - if sources is null | 如果源集合为null