增加重载GenericBuilder的of接口,支持有参构造(最多5个)的调用;增加重载with接口,支持无参方法调用

This commit is contained in:
TomXin 2022-01-30 14:06:43 +08:00
parent 9d5c2ffc1a
commit c0d2b8c9e3
2 changed files with 262 additions and 18 deletions

View File

@ -11,25 +11,32 @@ import java.util.function.Supplier;
* <p>使用方法如下</p>
* <pre>
* 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();
*
* </pre>
*
* <p> 我们也可以对已创建的对象进行修改</p>
* <pre>
* Box boxModified = GenericBuilder
* .of(() -&gt; box)
* .with(Box::setTitle, "Hello Friend!")
* .with(Box::setLength, 3)
* .with(Box::setWidth, 4)
* .with(Box::setHeight, 5)
* .build();
* .of(() -&gt; box)
* .with(Box::setTitle, "Hello Friend!")
* .with(Box::setLength, 3)
* .with(Box::setWidth, 4)
* .with(Box::setHeight, 5)
* .build();
* </pre>
* <p> 我们还可以对这样调用有参构造这对于创建一些在有参构造中包含初始化函数的对象是有意义的</p>
* <pre>
* Box box1 = GenericBuilder
* .of(Box::new, 2048L, "Hello Partner!", 222, 333, 444)
* .with(Box::alis)
* .build();
* </pre>
*
* @author TomXin
@ -43,7 +50,7 @@ public class GenericBuilder<T> implements Builder<T> {
private final Supplier<T> instantiator;
/**
* 修改器集合
* 修改器列表
*/
private final List<Consumer<T>> modifiers = new ArrayList<>();
@ -57,7 +64,7 @@ public class GenericBuilder<T> implements Builder<T> {
}
/**
* 通过实例化器创建GenericBuilder
* 通过无参数实例化器创建GenericBuilder
*
* @param instant 实例化器
* @param <T> 目标类型
@ -67,6 +74,109 @@ public class GenericBuilder<T> implements Builder<T> {
return new GenericBuilder<>(instant);
}
/**
* 通过1参数实例化器创建GenericBuilder
*
* @param instant 实例化器
* @param p1 参数一
* @param <T> 目标类型
* @param <P1> 参数一类型
* @return GenericBuilder对象
*/
public static <T, P1> GenericBuilder<T> of(Supplier1<T, P1> instant, P1 p1) {
Supplier<T> s = () -> instant.get(p1);
return new GenericBuilder<>(s);
}
/**
* 通过2参数实例化器创建GenericBuilder
*
* @param instant 实例化器
* @param p1 参数一
* @param p2 参数二
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @return GenericBuilder对象
*/
public static <T, P1, P2> GenericBuilder<T> of(Supplier2<T, P1, P2> instant, P1 p1, P2 p2) {
Supplier<T> s = () -> instant.get(p1, p2);
return new GenericBuilder<>(s);
}
/**
* 通过3参数实例化器创建GenericBuilder
*
* @param instant 实例化器
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @param <P3> 参数三类型
* @return GenericBuilder对象
*/
public static <T, P1, P2, P3> GenericBuilder<T> of(Supplier3<T, P1, P2, P3> instant, P1 p1, P2 p2, P3 p3) {
Supplier<T> s = () -> instant.get(p1, p2, p3);
return new GenericBuilder<>(s);
}
/**
* 通过4参数实例化器创建GenericBuilder
*
* @param instant 实例化器
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param p4 参数四
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @param <P3> 参数三类型
* @param <P4> 参数四类型
* @return GenericBuilder对象
*/
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) {
Supplier<T> s = () -> instant.get(p1, p2, p3, p4);
return new GenericBuilder<>(s);
}
/**
* 通过5参数实例化器创建GenericBuilder
*
* @param instant 实例化器
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param p4 参数四
* @param p5 参数五
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @param <P3> 参数三类型
* @param <P4> 参数四类型
* @param <P5> 参数五类型
* @return GenericBuilder对象
*/
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) {
Supplier<T> s = () -> instant.get(p1, p2, p3, p4, p5);
return new GenericBuilder<>(s);
}
/**
* 调用无参数方法
*
* @param consumer 无参数Consumer
* @return GenericBuilder对象
*/
public GenericBuilder<T> with(Consumer<T> consumer) {
modifiers.add(consumer);
return this;
}
/**
* 调用1参数方法
*
@ -170,6 +280,118 @@ public class GenericBuilder<T> implements Builder<T> {
return value;
}
/**
* 1参数Supplier
*
* @param <T> 目标类型
* @param <P1> 参数一类型
*/
@FunctionalInterface
public interface Supplier1<T, P1> {
/**
* 生成实例的方法
*
* @param p1 参数一
* @return 目标对象
*/
T get(P1 p1);
}
/**
* 2参数Supplier
*
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
*/
@FunctionalInterface
public interface Supplier2<T, P1, P2> {
/**
* 生成实例的方法
*
* @param p1 参数一
* @param p2 参数二
* @return 目标对象
*/
T get(P1 p1, P2 p2);
}
/**
* 3参数Supplier
*
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @param <P3> 参数三类型
*/
@FunctionalInterface
public interface Supplier3<T, P1, P2, P3> {
/**
* 生成实例的方法
*
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @return 目标对象
*/
T get(P1 p1, P2 p2, P3 p3);
}
/**
* 4参数Supplier
*
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @param <P3> 参数三类型
* @param <P4> 参数四类型
*/
@FunctionalInterface
public interface Supplier4<T, P1, P2, P3, P4> {
/**
* 生成实例的方法
*
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param p4 参数四
* @return 目标对象
*/
T get(P1 p1, P2 p2, P3 p3, P4 p4);
}
/**
* 5参数Supplier
*
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @param <P3> 参数三类型
* @param <P4> 参数四类型
* @param <P5> 参数五类型
*/
@FunctionalInterface
public interface Supplier5<T, P1, P2, P3, P4, P5> {
/**
* 生成实例的方法
*
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param p4 参数四
* @param p5 参数五
* @return 目标对象
*/
T get(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
}
/**
* 1参数Consumer
*/

View File

@ -1,8 +1,7 @@
package cn.hutool.core.builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import cn.hutool.core.util.StrUtil;
import lombok.*;
import org.junit.Test;
/**
@ -31,6 +30,11 @@ public class GenericBuilderTest {
.with(Box::setHeight, 5)
.build();
System.out.println(boxModified);
Box box1 = GenericBuilder
.of(Box::new, 2048L, "Hello Partner!", 222, 333, 444)
.with(Box::alis)
.build();
System.out.println(box1);
}
@Getter
@ -42,6 +46,24 @@ public class GenericBuilderTest {
private Integer length;
private Integer width;
private Integer height;
private String titleAlias;
public Box() {
}
public Box(Long id, String title, Integer length, Integer width, Integer height) {
this.id = id;
this.title = title;
this.length = length;
this.width = width;
this.height = height;
}
public void alis() {
if (StrUtil.isNotBlank(this.title)) {
this.titleAlias = "TomXin:\"" + title + "\"";
}
}
}
}