Class RecordBuilder<T extends Record>

java.lang.Object
cloud.opencode.base.reflect.record.RecordBuilder<T>
Type Parameters:
T - the record type | record类型

public class RecordBuilder<T extends Record> extends Object
Record Builder Record构建器

Fluent builder for creating record instances.

用于创建record实例的流式构建器。

Features | 主要功能:

  • Fluent record instance creation - 流式record实例创建
  • Named component value setting - 命名组件值设置
  • Type-safe record construction - 类型安全的record构造

Usage Examples | 使用示例:

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

Security | 安全性:

  • Thread-safe: No (builder pattern, not thread-safe during construction) - 线程安全: 否(构建器模式,构建期间非线程安全)
  • Null-safe: No (record class must be non-null) - 空值安全: 否(record类须非空)

Performance | 性能特性:

  • Time complexity: build() O(c) where c is the number of record components - 时间复杂度: build() 为 O(c),c为 record 组件数量
  • Space complexity: O(c) for storing component values - 空间复杂度: O(c),存储组件值
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • RecordBuilder

      public RecordBuilder(Class<T> recordClass)
      Creates a RecordBuilder 创建RecordBuilder
      Parameters:
      recordClass - the record class | record类
  • Method Details

    • of

      public static <T extends Record> RecordBuilder<T> of(Class<T> recordClass)
      Creates a RecordBuilder for a record class 为record类创建RecordBuilder
      Type Parameters:
      T - the record type | record类型
      Parameters:
      recordClass - the record class | record类
      Returns:
      the builder | 构建器
    • from

      public static <T extends Record> RecordBuilder<T> from(T record)
      Creates a RecordBuilder copying from an existing record 从现有record复制创建RecordBuilder
      Type Parameters:
      T - the record type | record类型
      Parameters:
      record - the record to copy | 要复制的record
      Returns:
      the builder | 构建器
    • set

      public RecordBuilder<T> set(String name, Object value)
      Sets a component value by name 按名称设置组件值
      Parameters:
      name - the component name | 组件名
      value - the value | 值
      Returns:
      this builder | 此构建器
    • set

      public RecordBuilder<T> set(int index, Object value)
      Sets a component value by index 按索引设置组件值
      Parameters:
      index - the component index | 组件索引
      value - the value | 值
      Returns:
      this builder | 此构建器
    • setAll

      public RecordBuilder<T> setAll(Map<String,?> map)
      Sets multiple values from a map 从map设置多个值
      Parameters:
      map - the values map | 值映射
      Returns:
      this builder | 此构建器
    • copyFrom

      public RecordBuilder<T> copyFrom(T record)
      Sets all values from an existing record 从现有record设置所有值
      Parameters:
      record - the record | record
      Returns:
      this builder | 此构建器
    • setIfAbsent

      public RecordBuilder<T> setIfAbsent(String name, Object value)
      Sets a value if absent 如果不存在则设置值
      Parameters:
      name - the component name | 组件名
      value - the value | 值
      Returns:
      this builder | 此构建器
    • setIfNotNull

      public RecordBuilder<T> setIfNotNull(String name, Object value)
      Sets a value if not null 如果非null则设置值
      Parameters:
      name - the component name | 组件名
      value - the value | 值
      Returns:
      this builder | 此构建器
    • clear

      public RecordBuilder<T> clear(String name)
      Clears a specific value 清除特定值
      Parameters:
      name - the component name | 组件名
      Returns:
      this builder | 此构建器
    • clearAll

      public RecordBuilder<T> clearAll()
      Clears all values 清除所有值
      Returns:
      this builder | 此构建器
    • getValue

      public Object getValue(String name)
      Gets the current value for a component 获取组件的当前值
      Parameters:
      name - the component name | 组件名
      Returns:
      the value or null | 值或null
    • hasValue

      public boolean hasValue(String name)
      Checks if a value is set 检查是否已设置值
      Parameters:
      name - the component name | 组件名
      Returns:
      true if set | 如果已设置返回true
    • getRecordClass

      public Class<T> getRecordClass()
      Gets the record class 获取record类
      Returns:
      the record class | record类
    • getComponents

      public List<RecordComponent> getComponents()
      Gets the components 获取组件
      Returns:
      list of components | 组件列表
    • build

      public T build()
      Builds the record instance 构建record实例
      Returns:
      the record | record
    • buildValidated

      public T buildValidated()
      Builds the record with validation 构建record(带验证)
      Returns:
      the record | record
      Throws:
      IllegalStateException - if required values are missing | 如果缺少必需值