类 GenericBuilder<T>

java.lang.Object
spring.turbo.bean.GenericBuilder<T>

public class GenericBuilder<T> extends Object

通用Builder

参考: 一看就会的java8通用Builder

使用方法如下:

 Box box = GenericBuilder.of(Box::new).with(Box::setId, 1024L).with(Box::setTitle, "Hello World!")
         .with(Box::setLength, 9).with(Box::setWidth, 8).with(Box::setHeight, 7).build();

 

我们也可以对已创建的对象进行修改:

 Box boxModified = GenericBuilder.of(() -> box).with(Box::setTitle, "Hello Friend!").with(Box::setLength, 3)
         .with(Box::setWidth, 4).with(Box::setHeight, 5).build();
 

我们还可以对这样调用有参构造,这对于创建一些在有参构造中包含初始化函数的对象是有意义的:

 Box box1 = GenericBuilder.of(Box::new, 2048L, "Hello Partner!", 222, 333, 444).with(Box::alis).build();
 

还可能这样构建Map对象:

HashMap<String, String> colorMap = GenericBuilder .of(HashMap<String,String>::new) .with(Map::put, "red", "#FF0000") .with(Map::put, "yellow", "#FFFF00") .with(Map::put, "blue", "#0000FF") .build();

注意:本工具类支持调用的构造方法的参数数量不超过5个,一般方法的参数数量不超过2个,更多的参数不利于阅读和维护。

作者:
应卓
  • 构造器详细资料

    • GenericBuilder

      public GenericBuilder(Supplier<T> instant)
      构造
      参数:
      instant - 实例化器
  • 方法详细资料

    • of

      public static <T> GenericBuilder<T> of(Supplier<T> instant)
      通过无参数实例化器创建GenericBuilder
      类型参数:
      T - 目标类型
      参数:
      instant - 实例化器
      返回:
      GenericBuilder对象
    • of

      public static <T, P1> GenericBuilder<T> of(Supplier1<T,P1> instant, P1 p1)
      通过1参数实例化器创建GenericBuilder
      类型参数:
      T - 目标类型
      P1 - 参数一类型
      参数:
      instant - 实例化器
      p1 - 参数一
      返回:
      GenericBuilder对象
    • of

      public static <T, P1, P2> GenericBuilder<T> of(Supplier2<T,P1,P2> instant, P1 p1, P2 p2)
      通过2参数实例化器创建GenericBuilder
      类型参数:
      T - 目标类型
      P1 - 参数一类型
      P2 - 参数二类型
      参数:
      instant - 实例化器
      p1 - 参数一
      p2 - 参数二
      返回:
      GenericBuilder对象
    • of

      public static <T, P1, P2, P3> GenericBuilder<T> of(Supplier3<T,P1,P2,P3> instant, P1 p1, P2 p2, P3 p3)
      通过3参数实例化器创建GenericBuilder
      类型参数:
      T - 目标类型
      P1 - 参数一类型
      P2 - 参数二类型
      P3 - 参数三类型
      参数:
      instant - 实例化器
      p1 - 参数一
      p2 - 参数二
      p3 - 参数三
      返回:
      GenericBuilder对象
    • of

      public static <T, P1, P2, P3, P4> GenericBuilder<T> of(Supplier4<T,P1,P2,P3,P4> instant, P1 p1, P2 p2, P3 p3, P4 p4)
      通过4参数实例化器创建GenericBuilder
      类型参数:
      T - 目标类型
      P1 - 参数一类型
      P2 - 参数二类型
      P3 - 参数三类型
      P4 - 参数四类型
      参数:
      instant - 实例化器
      p1 - 参数一
      p2 - 参数二
      p3 - 参数三
      p4 - 参数四
      返回:
      GenericBuilder对象
    • of

      public static <T, P1, P2, P3, P4, P5> GenericBuilder<T> of(Supplier5<T,P1,P2,P3,P4,P5> instant, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
      通过5参数实例化器创建GenericBuilder
      类型参数:
      T - 目标类型
      P1 - 参数一类型
      P2 - 参数二类型
      P3 - 参数三类型
      P4 - 参数四类型
      P5 - 参数五类型
      参数:
      instant - 实例化器
      p1 - 参数一
      p2 - 参数二
      p3 - 参数三
      p4 - 参数四
      p5 - 参数五
      返回:
      GenericBuilder对象
    • with

      public GenericBuilder<T> with(Consumer<T> consumer)
      调用无参数方法
      参数:
      consumer - 无参数Consumer
      返回:
      GenericBuilder对象
    • with

      public <P1> GenericBuilder<T> with(BiConsumer<T,P1> consumer, P1 p1)
      调用1参数方法
      类型参数:
      P1 - 参数一类型
      参数:
      consumer - 1参数Consumer
      p1 - 参数一
      返回:
      GenericBuilder对象
    • with

      public <P1, P2> GenericBuilder<T> with(Consumer3<T,P1,P2> consumer, P1 p1, P2 p2)
      调用2参数方法
      类型参数:
      P1 - 参数一类型
      P2 - 参数二类型
      参数:
      consumer - 2参数Consumer
      p1 - 参数一
      p2 - 参数二
      返回:
      GenericBuilder对象
    • build

      public T build()
      构建
      返回:
      目标对象