Class BeanBuilder<T>

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

public class BeanBuilder<T> extends Object implements 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 Details

    • BeanBuilder

      public BeanBuilder(Class<T> beanClass)
      Creates a BeanBuilder for the given class | 为指定类创建 BeanBuilder
      Parameters:
      beanClass - the target bean class | 目标 Bean 类
  • Method Details

    • of

      public static <T> BeanBuilder<T> of(Class<T> beanClass)
      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

      public static <T> BeanBuilder<T> from(T source)
      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

      public BeanBuilder<T> set(String propertyName, Object value)
      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. Use set(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 getter is actually a serializable method reference, property name resolution works normally. Otherwise, an OpenIllegalArgumentException is 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

      public <V> BeanBuilder<T> set(BeanBuilder.SerializableFunction<T,V> getter, V value)
      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

      public BeanBuilder<T> setIfNotNull(String propertyName, Object value)
      Conditionally sets a property (sets when non-null) | 条件设置属性(非 null 时设置)
      Parameters:
      propertyName - the property name | 属性名
      value - the value | 值
      Returns:
      this builder | 此构建器
    • setIf

      public BeanBuilder<T> setIf(boolean condition, String propertyName, Object value)
      Conditionally sets a property | 条件设置属性
      Parameters:
      condition - the condition | 条件
      propertyName - the property name | 属性名
      value - the value | 值
      Returns:
      this builder | 此构建器
    • setAll

      public BeanBuilder<T> setAll(Map<String,Object> props)
      Sets multiple properties in batch | 批量设置属性
      Parameters:
      props - the property map | 属性映射
      Returns:
      this builder | 此构建器
    • configure

      public BeanBuilder<T> configure(Consumer<BeanBuilder<T>> consumer)
      Configuration callback | 配置回调
      Parameters:
      consumer - the configuration consumer | 配置消费者
      Returns:
      this builder | 此构建器
    • build

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

      public T buildAndValidate(Consumer<T> validator)
      Builds and validates the bean | 构建并验证 Bean
      Parameters:
      validator - the validation consumer | 验证消费者
      Returns:
      the built bean | 构建的 Bean