Class BeanBuilder<T>
java.lang.Object
cloud.opencode.base.core.builder.BeanBuilder<T>
- Type Parameters:
T- Bean type - Bean 类型
- All Implemented Interfaces:
Builder<T>
Bean Builder - Fluent builder for JavaBeans
JavaBean 构建器 - JavaBean 的流式构建器
Creates JavaBean instances with fluent API and automatic type conversion.
使用流式 API 和自动类型转换创建 JavaBean 实例。
Features | 主要功能:
- Property setting (set, setIfNotNull, setIf) - 属性设置
- Batch setting (setAll) - 批量设置
- Copy from existing instance (from) - 从现有实例复制
- Automatic type conversion - 自动类型转换
- Build with validation (buildAndValidate) - 构建并验证
Usage Examples | 使用示例:
User user = BeanBuilder.of(User.class)
.set("name", "John")
.set("age", 25)
.setIfNotNull("email", email)
.build();
User copy = BeanBuilder.from(existingUser)
.set("name", "NewName")
.build();
Security | 安全性:
- Thread-safe: No (builder instance not thread-safe) - 线程安全: 否
- Null-safe: Yes - 空值安全: 是
Performance | 性能特性:
- Time complexity: O(1) per property set, O(n) for build - 每次属性设置 O(1), 构建 O(n)
- Space complexity: O(n) where n = number of properties - O(n), n为属性数量
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceSerializable Function - A function interface that supports serialization for method reference resolution 可序列化函数 - 支持序列化的函数接口,用于方法引用解析 -
Constructor Summary
ConstructorsConstructorDescriptionBeanBuilder(Class<T> beanClass) Creates a BeanBuilder for the given class | 为指定类创建 BeanBuilder -
Method Summary
Modifier and TypeMethodDescriptionbuild()Builds the target object.buildAndValidate(Consumer<T> validator) Builds and validates the bean | 构建并验证 Beanconfigure(Consumer<BeanBuilder<T>> consumer) Configuration callback | 配置回调static <T> BeanBuilder<T> from(T source) Creates a builder from an existing instance | 从现有实例创建构建器static <T> BeanBuilder<T> Creates a BeanBuilder for the given class | 为指定类创建 BeanBuilder<V> BeanBuilder<T> set(BeanBuilder.SerializableFunction<T, V> getter, V value) Type-safe property setting (using getter method reference) | 类型安全设置属性(使用 getter 方法引用)Sets a property value | 设置属性值<V> BeanBuilder<T> Deprecated, for removal: This API element is subject to removal in a future version.since 1.0.3, for removal.Sets multiple properties in batch | 批量设置属性Conditionally sets a property | 条件设置属性setIfNotNull(String propertyName, Object value) Conditionally sets a property (sets when non-null) | 条件设置属性(非 null 时设置)
-
Constructor Details
-
BeanBuilder
-
-
Method Details
-
of
Creates a BeanBuilder for the given class | 为指定类创建 BeanBuilder- Type Parameters:
T- the bean type | Bean 类型- Parameters:
beanClass- the target bean class | 目标 Bean 类- Returns:
- a new BeanBuilder | 新的 BeanBuilder
-
from
Creates a builder from an existing instance | 从现有实例创建构建器- Type Parameters:
T- the bean type | Bean 类型- Parameters:
source- the source instance | 源实例- Returns:
- a new BeanBuilder | 新的 BeanBuilder
-
set
Sets a property value | 设置属性值- Parameters:
propertyName- the property name | 属性名value- the value | 值- Returns:
- this builder | 此构建器
-
set
@Deprecated(since="1.0.3", forRemoval=true) public <V> BeanBuilder<T> set(Function<T, V> getter, V value) Deprecated, for removal: This API element is subject to removal in a future version.since 1.0.3, for removal. Useset(SerializableFunction, Object)instead. 自 1.0.3 起废弃,将被移除。请改用set(SerializableFunction, Object)。Sets a property value using a getter function reference (deprecated) | 使用 getter 函数引用设置属性值(已废弃)This method is retained for binary/source compatibility with 1.0.2. Use
set(SerializableFunction, Object)instead, which provides the same functionality with proper type-safe property name resolution.此方法为兼容 1.0.2 版本而保留。 请改用
set(SerializableFunction, Object),它提供相同的功能并支持类型安全的属性名解析。If the passed
getteris actually a serializable method reference, property name resolution works normally. Otherwise, anOpenIllegalArgumentExceptionis thrown with a clear migration hint.如果传入的
getter实际上是可序列化的方法引用,属性名解析正常工作。 否则抛出OpenIllegalArgumentException并给出明确的迁移提示。- Type Parameters:
V- the value type | 值类型- Parameters:
getter- the getter function | getter 函数value- the value | 值- Returns:
- this builder | 此构建器
- Throws:
OpenIllegalArgumentException- if property name cannot be resolved 如果无法解析属性名则抛出异常
-
set
Type-safe property setting (using getter method reference) | 类型安全设置属性(使用 getter 方法引用)Usage example | 使用示例:
BeanBuilder.of(User.class) .set(User::getName, "John") .set(User::getAge, 25) .build();- Type Parameters:
V- the value type | 值类型- Parameters:
getter- the getter method reference (must be a method reference, not a lambda expression) getter 方法引用(必须是方法引用,不能是 lambda 表达式)value- the value | 值- Returns:
- this builder | 此构建器
- Throws:
OpenIllegalArgumentException- if the getter is not a valid method reference 如果 getter 不是有效的方法引用则抛出异常
-
setIfNotNull
Conditionally sets a property (sets when non-null) | 条件设置属性(非 null 时设置)- Parameters:
propertyName- the property name | 属性名value- the value | 值- Returns:
- this builder | 此构建器
-
setIf
Conditionally sets a property | 条件设置属性- Parameters:
condition- the condition | 条件propertyName- the property name | 属性名value- the value | 值- Returns:
- this builder | 此构建器
-
setAll
Sets multiple properties in batch | 批量设置属性- Parameters:
props- the property map | 属性映射- Returns:
- this builder | 此构建器
-
configure
Configuration callback | 配置回调- Parameters:
consumer- the configuration consumer | 配置消费者- Returns:
- this builder | 此构建器
-
build
-
buildAndValidate
-