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>
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds the target object.buildAndValidate(Consumer<T> validator) Builds the record and validates it with the given validator 构建并验证configure(Consumer<RecordBuilder<T>> consumer) Applies a configuration callback to this builder 配置回调static <T extends Record>
RecordBuilder<T> from(T record) Creates a builder from an existing record instance.static <T extends Record>
RecordBuilder<T> Creates a new builder for the given record class 创建构建器Sets a component value 设置组件值Sets multiple component values at once 批量设置组件值Conditionally sets a component value 条件设置组件值setIfNotNull(String componentName, Object value) Sets a component value only if the value is not null 条件设置组件值(非 null 时设置)
-
Constructor Details
-
RecordBuilder
-
-
Method Details
-
of
Creates a new builder for the given record class 创建构建器 -
from
Creates a builder from an existing record instance. UsesRecordComponent.getName()which reliably returns real component names regardless of the-parameterscompiler 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
Sets a component value 设置组件值 -
setIfNotNull
Sets a component value only if the value is not null 条件设置组件值(非 null 时设置) -
setIf
Conditionally sets a component value 条件设置组件值 -
setAll
Sets multiple component values at once 批量设置组件值 -
configure
Applies a configuration callback to this builder 配置回调 -
build
-
buildAndValidate
-