Class RecordBuilder<T extends Record>

java.lang.Object
cloud.opencode.base.core.builder.RecordBuilder<T>
Type Parameters:
T - Record type - Record 类型
All Implemented Interfaces:
Builder<T>

public class RecordBuilder<T extends Record> extends Object implements Builder<T>
Record Builder - Fluent builder for Java Records Record 构建器 - Java Record 的流式构建器

Creates Java Record instances with fluent API despite immutability.

使用流式 API 创建 Java Record 实例,绕过不可变性限制。

Features | 主要功能:

  • Component setting (set, setIfNotNull, setIf) - 组件设置
  • Batch setting (setAll) - 批量设置
  • Copy from existing record (from) - 从现有 Record 复制
  • Build with validation (buildAndValidate) - 构建并验证

Usage Examples | 使用示例:

record User(String name, int age) {}

User user = RecordBuilder.of(User.class)
    .set("name", "John")
    .set("age", 25)
    .build();

User updated = RecordBuilder.from(user)
    .set("age", 26)
    .build();

Security | 安全性:

  • Thread-safe: No (builder instance not thread-safe) - 线程安全: 否
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(n) where n = number of record components - O(n), n为记录组件数
  • Space complexity: O(n) for component values - 组件值 O(n)
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • RecordBuilder

      public RecordBuilder(Class<T> recordClass)
  • Method Details

    • of

      public static <T extends Record> RecordBuilder<T> of(Class<T> recordClass)
      Creates a new builder for the given record class 创建构建器
    • from

      public static <T extends Record> RecordBuilder<T> from(T record)
      Creates a builder from an existing record instance. Uses RecordComponent.getName() which reliably returns real component names regardless of the -parameters compiler option. 从现有 Record 创建构建器。 使用 RecordComponent.getName() 获取真实组件名,不依赖 -parameters 编译选项。
      Type Parameters:
      T - the record type | Record 类型
      Parameters:
      record - the source record instance | 源 Record 实例
      Returns:
      a new builder pre-populated with the record's component values | 预填充了 Record 组件值的新构建器
      Throws:
      IllegalStateException - if record component names appear to be synthetic (e.g. "arg0"), which would indicate a JVM bug or bytecode manipulation | 如果组件名为合成名(如 "arg0"),表明 JVM 异常或字节码被篡改
    • set

      public RecordBuilder<T> set(String componentName, Object value)
      Sets a component value 设置组件值
    • setIfNotNull

      public RecordBuilder<T> setIfNotNull(String componentName, Object value)
      Sets a component value only if the value is not null 条件设置组件值(非 null 时设置)
    • setIf

      public RecordBuilder<T> setIf(boolean condition, String componentName, Object value)
      Conditionally sets a component value 条件设置组件值
    • setAll

      public RecordBuilder<T> setAll(Map<String,Object> props)
      Sets multiple component values at once 批量设置组件值
    • configure

      public RecordBuilder<T> configure(Consumer<RecordBuilder<T>> consumer)
      Applies a configuration callback to this builder 配置回调
    • build

      public T build()
      Description copied from interface: Builder
      Builds the target object. 构建目标对象。
      Specified by:
      build in interface Builder<T extends Record>
      Returns:
      the built object | 构建的对象
    • buildAndValidate

      public T buildAndValidate(Consumer<T> validator)
      Builds the record and validates it with the given validator 构建并验证