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:
-
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 | 为指定类创建 BeanBuilderSets a property value | 设置属性值<V> BeanBuilder<T> Type-safe property setting (using getter method reference) | 类型安全设置属性(使用 getter 方法引用)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
Type-safe property setting (using getter method reference) | 类型安全设置属性(使用 getter 方法引用)- Type Parameters:
V- the value type | 值类型- Parameters:
getter- the getter reference | getter 引用value- the value | 值- Returns:
- this builder | 此构建器
-
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
-