From 9d5c2ffc1a9037efe0a9e46f06feff0554ed87e6 Mon Sep 17 00:00:00 2001 From: TomXin <766781886@qq.com> Date: Sat, 29 Jan 2022 17:34:52 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9EGenericBuilder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hutool/core/builder/GenericBuilder.java | 252 ++++++++++++++++++ .../core/builder/GenericBuilderTest.java | 47 ++++ 2 files changed, 299 insertions(+) create mode 100644 hutool-core/src/main/java/cn/hutool/core/builder/GenericBuilder.java create mode 100644 hutool-core/src/test/java/cn/hutool/core/builder/GenericBuilderTest.java diff --git a/hutool-core/src/main/java/cn/hutool/core/builder/GenericBuilder.java b/hutool-core/src/main/java/cn/hutool/core/builder/GenericBuilder.java new file mode 100644 index 000000000..b8da21486 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/builder/GenericBuilder.java @@ -0,0 +1,252 @@ +package cn.hutool.core.builder; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Supplier; + +/** + *
通用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(); + *+ * + * @author TomXin + * @since jdk1.8 + */ +public class GenericBuilder
使用方法如下:
** 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(); + * .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(); + * .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(); ** * @author TomXin @@ -43,7 +50,7 @@ public class GenericBuilder
我们还可以对这样调用有参构造,这对于创建一些在有参构造中包含初始化函数的对象是有意义的
+ *我们还可以对这样调用有参构造,这对于创建一些在有参构造中包含初始化函数的对象是有意义的:
** Box box1 = GenericBuilder * .of(Box::new, 2048L, "Hello Partner!", 222, 333, 444) * .with(Box::alis) * .build(); *+ *
注意:本工具类支持调用的方法的参数数量不超过5个,更多的参数不利于阅读和维护。 + * 关于Java方法的参数个数限制似乎并没有明确统一的规范,网络上众说纷纭,这里取个相对平均的数5。 + * 特殊需要求可以基于此类进行拓展. + *
* * @author TomXin * @since jdk1.8