add GenericBuilder

This commit is contained in:
Looly 2022-02-07 10:56:50 +08:00
parent 18a16eb48f
commit 3a9f90c3e2
15 changed files with 445 additions and 236 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.7.21 (2022-02-04)
# 5.7.21 (2022-02-07)
### 🐣新特性
* 【extra 】 增加jetbrick模板支持
@ -16,6 +16,7 @@
* 【core 】 DateTime增加setUseJdkToStringStyle方法
* 【core 】 CharSequenceUtil增加replace重载(issue#2122@Github)
* 【core 】 IntMap和LongMap使用位运算快速求解取余运算(pr#2123@Github)
* 【core 】 新增通用builder类GenericBuilder(pr#526@Gitee)
### 🐞Bug修复
* 【core 】 修复ChineseDate农历获取正月出现数组越界BUGissue#2112@Github

View File

@ -1,5 +1,16 @@
package cn.hutool.core.builder;
import cn.hutool.core.lang.func.Consumer1;
import cn.hutool.core.lang.func.Consumer2;
import cn.hutool.core.lang.func.Consumer3;
import cn.hutool.core.lang.func.Consumer4;
import cn.hutool.core.lang.func.Consumer5;
import cn.hutool.core.lang.func.Supplier1;
import cn.hutool.core.lang.func.Supplier2;
import cn.hutool.core.lang.func.Supplier3;
import cn.hutool.core.lang.func.Supplier4;
import cn.hutool.core.lang.func.Supplier5;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
@ -44,14 +55,14 @@ import java.util.function.Supplier;
* </p>
*
* @author TomXin
* @since jdk1.8
* @since 5.7.21
*/
public class GenericBuilder<T> implements Builder<T> {
/**
* 实例化器
*/
private final Supplier<T> instantiator;
private final Supplier<T> instant;
/**
* 修改器列表
@ -64,7 +75,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @param instant 实例化器
*/
public GenericBuilder(Supplier<T> instant) {
this.instantiator = instant;
this.instant = instant;
}
/**
@ -88,8 +99,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @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);
return of(instant.toSupplier(p1));
}
/**
@ -104,8 +114,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @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);
return of(instant.toSupplier(p1, p2));
}
/**
@ -122,8 +131,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @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);
return of(instant.toSupplier(p1, p2, p3));
}
/**
@ -142,8 +150,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @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);
return of(instant.toSupplier(p1, p2, p3, p4));
}
/**
@ -164,8 +171,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @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);
return of(instant.toSupplier(p1, p2, p3, p4, p5));
}
@ -190,8 +196,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @return GenericBuilder对象
*/
public <P1> GenericBuilder<T> with(Consumer1<T, P1> consumer, P1 p1) {
Consumer<T> c = instance -> consumer.accept(instance, p1);
modifiers.add(c);
modifiers.add(consumer.toConsumer(p1));
return this;
}
@ -206,8 +211,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @return GenericBuilder对象
*/
public <P1, P2> GenericBuilder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
Consumer<T> c = instance -> consumer.accept(instance, p1, p2);
modifiers.add(c);
modifiers.add(consumer.toConsumer(p1, p2));
return this;
}
@ -224,8 +228,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @return GenericBuilder对象
*/
public <P1, P2, P3> GenericBuilder<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {
Consumer<T> c = instance -> consumer.accept(instance, p1, p2, p3);
modifiers.add(c);
modifiers.add(consumer.toConsumer(p1, p2, p3));
return this;
}
@ -244,8 +247,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @return GenericBuilder对象
*/
public <P1, P2, P3, P4> GenericBuilder<T> with(Consumer4<T, P1, P2, P3, P4> consumer, P1 p1, P2 p2, P3 p3, P4 p4) {
Consumer<T> c = instance -> consumer.accept(instance, p1, p2, p3, p4);
modifiers.add(c);
modifiers.add(consumer.toConsumer(p1, p2, p3, p4));
return this;
}
@ -266,8 +268,7 @@ public class GenericBuilder<T> implements Builder<T> {
* @return GenericBuilder对象
*/
public <P1, P2, P3, P4, P5> GenericBuilder<T> with(Consumer5<T, P1, P2, P3, P4, P5> consumer, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
Consumer<T> c = instance -> consumer.accept(instance, p1, p2, p3, p4, p5);
modifiers.add(c);
modifiers.add(consumer.toConsumer(p1, p2, p3, p4, p5));
return this;
}
@ -278,201 +279,9 @@ public class GenericBuilder<T> implements Builder<T> {
*/
@Override
public T build() {
T value = instantiator.get();
T value = instant.get();
modifiers.forEach(modifier -> modifier.accept(value));
modifiers.clear();
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
*/
@FunctionalInterface
public interface Consumer1<T, P1> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数二
*/
void accept(T t, P1 p1);
}
/**
* 2参数Consumer
*/
@FunctionalInterface
public interface Consumer2<T, P1, P2> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
*/
void accept(T t, P1 p1, P2 p2);
}
/**
* 3参数Consumer
*/
@FunctionalInterface
public interface Consumer3<T, P1, P2, P3> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
*/
void accept(T t, P1 p1, P2 p2, P3 p3);
}
/**
* 4参数Consumer
*/
@FunctionalInterface
public interface Consumer4<T, P1, P2, P3, P4> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param p4 参数四
*/
void accept(T t, P1 p1, P2 p2, P3 p3, P4 p4);
}
/**
* 5参数Consumer
*/
@FunctionalInterface
public interface Consumer5<T, P1, P2, P3, P4, P5> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param p4 参数四
* @param p5 参数五
*/
void accept(T t, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
}
}

View File

@ -51,7 +51,7 @@ public class CheckedUtil {
* @param expression Lambda表达式
* @param <P> 运行时传入的参数类型
* @param <R> 最终返回的数据类型
* @return cn.hutool.core.lang.func.Func
* @return {@link FuncRt}
*/
public static <P, R> FuncRt<P, R> uncheck(Func<P, R> expression) {
return uncheck(expression, new RuntimeException());
@ -63,7 +63,7 @@ public class CheckedUtil {
*
* @param expression 运行时传入的参数类型
* @param <R> 最终返回的数据类型
* @return cn.hutool.core.lang.func.Func0
* @return {@link Func0Rt}
*/
public static <R> Func0Rt<R> uncheck(Func0<R> expression) {
return uncheck(expression, new RuntimeException());
@ -76,7 +76,7 @@ public class CheckedUtil {
* @param expression 运行时传入的参数类型
* @param <P> 运行时传入的参数类型
* @param <R> 最终返回的数据类型
* @return cn.hutool.core.lang.func.Func1
* @return {@link Func1Rt}
*/
public static <P, R> Func1Rt<P, R> uncheck(Func1<P, R> expression) {
return uncheck(expression, new RuntimeException());
@ -89,7 +89,7 @@ public class CheckedUtil {
*
* @param expression 运行时传入的参数类型
* @param <P> 运行时传入的参数类型
* @return cn.hutool.core.lang.func.VoidFunc
* @return {@link VoidFuncRt}
*/
public static <P> VoidFuncRt<P> uncheck(VoidFunc<P> expression) {
return uncheck(expression, new RuntimeException());
@ -100,7 +100,7 @@ public class CheckedUtil {
* 如此一来代码中就不用显示的try-catch转化成运行时异常
*
* @param expression 运行时传入的参数类型
* @return cn.hutool.core.lang.func.VoidFunc0
* @return {@link VoidFunc0Rt}
*/
public static VoidFunc0Rt uncheck(VoidFunc0 expression) {
return uncheck(expression, new RuntimeException());
@ -112,7 +112,7 @@ public class CheckedUtil {
*
* @param expression 运行时传入的参数类型
* @param <P> 运行时传入的参数类型
* @return cn.hutool.core.lang.func.VoidFunc1
* @return {@link VoidFunc1Rt}
*/
public static <P> VoidFunc1Rt<P> uncheck(VoidFunc1<P> expression) {
return uncheck(expression, new RuntimeException());
@ -127,7 +127,7 @@ public class CheckedUtil {
* @param rte 期望抛出的运行时异常
* @param <P> 运行时传入的参数类型
* @param <R> 最终返回的数据类型
* @return cn.hutool.core.lang.func.Func
* @return {@link FuncRt}
*/
public static <P, R> FuncRt<P, R> uncheck(Func<P, R> expression, RuntimeException rte) {
Objects.requireNonNull(expression, "expression can not be null");
@ -152,7 +152,7 @@ public class CheckedUtil {
* @param expression Lambda表达式
* @param rte 期望抛出的运行时异常
* @param <R> 最终返回的数据类型
* @return cn.hutool.core.lang.func.Func0
* @return {@link Func0Rt}
*/
public static <R> Func0Rt<R> uncheck(Func0<R> expression, RuntimeException rte) {
Objects.requireNonNull(expression, "expression can not be null");
@ -178,7 +178,7 @@ public class CheckedUtil {
* @param rte 期望抛出的运行时异常
* @param <P> 运行时传入的参数类型
* @param <R> 最终返回的数据类型
* @return cn.hutool.core.lang.func.Func1
* @return {@link Func1Rt}
*/
public static <P, R> Func1Rt<P, R> uncheck(Func1<P, R> expression, RuntimeException rte) {
Objects.requireNonNull(expression, "expression can not be null");
@ -203,7 +203,7 @@ public class CheckedUtil {
* @param expression Lambda表达式
* @param rte 期望抛出的运行时异常
* @param <P> 运行时传入的参数类型
* @return cn.hutool.core.lang.func.VoidFunc
* @return {@link VoidFuncRt}
*/
public static <P> VoidFuncRt<P> uncheck(VoidFunc<P> expression, RuntimeException rte) {
Objects.requireNonNull(expression, "expression can not be null");
@ -228,7 +228,7 @@ public class CheckedUtil {
*
* @param expression Lambda表达式
* @param rte 期望抛出的运行时异常
* @return cn.hutool.core.lang.func.VoidFunc0
* @return {@link VoidFunc0Rt}
*/
public static VoidFunc0Rt uncheck(VoidFunc0 expression, RuntimeException rte) {
Objects.requireNonNull(expression, "expression can not be null");
@ -254,7 +254,7 @@ public class CheckedUtil {
* @param expression Lambda表达式
* @param rte 期望抛出的运行时异常
* @param <P> 运行时传入的参数类型
* @return cn.hutool.core.lang.func.VoidFunc1
* @return {@link VoidFunc1Rt}
*/
public static <P> VoidFunc1Rt<P> uncheck(VoidFunc1<P> expression, RuntimeException rte) {
Objects.requireNonNull(expression, "expression can not be null");

View File

@ -0,0 +1,32 @@
package cn.hutool.core.lang.func;
import java.util.function.Consumer;
/**
* 1参数Consumer
*
* @param <T> 目标类型
* @param <P1> 参数一类型
* @author TomXin
* @since 5.7.21
*/
@FunctionalInterface
public interface Consumer1<T, P1> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数1
*/
void accept(T t, P1 p1);
/**
* 将带有参数的Consumer转换为无参{@link Consumer}
*
* @param p1 参数1
* @return {@link Consumer}
*/
default Consumer<T> toConsumer(P1 p1) {
return instant -> accept(instant, p1);
}
}

View File

@ -0,0 +1,36 @@
package cn.hutool.core.lang.func;
import java.util.function.Consumer;
/**
* 2参数Consumer
*
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @author TomXin
* @since 5.7.21
*/
@FunctionalInterface
public interface Consumer2<T, P1, P2> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
*/
void accept(T t, P1 p1, P2 p2);
/**
* 将带有参数的Consumer转换为无参{@link Consumer}
*
* @param p1 参数1
* @param p2 参数2
* @return {@link Consumer}
*/
default Consumer<T> toConsumer(P1 p1, P2 p2) {
return instant -> accept(instant, p1, p2);
}
}

View File

@ -0,0 +1,38 @@
package cn.hutool.core.lang.func;
import java.util.function.Consumer;
/**
* 3参数Consumer
*
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @param <P3> 参数三类型
* @author TomXin
* @since 5.7.21
*/
@FunctionalInterface
public interface Consumer3<T, P1, P2, P3> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
*/
void accept(T t, P1 p1, P2 p2, P3 p3);
/**
* 将带有参数的Consumer转换为无参{@link Consumer}
*
* @param p1 参数1
* @param p2 参数2
* @param p3 参数3
* @return {@link Consumer}
*/
default Consumer<T> toConsumer(P1 p1, P2 p2, P3 p3) {
return instant -> accept(instant, p1, p2, p3);
}
}

View File

@ -0,0 +1,41 @@
package cn.hutool.core.lang.func;
import java.util.function.Consumer;
/**
* 4参数Consumer
*
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @param <P3> 参数三类型
* @param <P4> 参数四类型
* @author TomXin
* @since 5.7.21
*/
@FunctionalInterface
public interface Consumer4<T, P1, P2, P3, P4> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param p4 参数四
*/
void accept(T t, P1 p1, P2 p2, P3 p3, P4 p4);
/**
* 将带有参数的Consumer转换为无参{@link Consumer}
*
* @param p1 参数1
* @param p2 参数2
* @param p3 参数3
* @param p4 参数4
* @return {@link Consumer}
*/
default Consumer<T> toConsumer(P1 p1, P2 p2, P3 p3, P4 p4) {
return instant -> accept(instant, p1, p2, p3, p4);
}
}

View File

@ -0,0 +1,44 @@
package cn.hutool.core.lang.func;
import java.util.function.Consumer;
/**
* 5参数Consumer
*
* @param <T> 目标 类型
* @param <P1> 参数一 类型
* @param <P2> 参数二 类型
* @param <P3> 参数三 类型
* @param <P4> 参数四 类型
* @param <P5> 参数五 类型
* @author TomXin
* @since 5.7.21
*/
@FunctionalInterface
public interface Consumer5<T, P1, P2, P3, P4, P5> {
/**
* 接收参数方法
*
* @param t 对象
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @param p4 参数四
* @param p5 参数五
*/
void accept(T t, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5);
/**
* 将带有参数的Consumer转换为无参{@link Consumer}
*
* @param p1 参数1
* @param p2 参数2
* @param p3 参数3
* @param p4 参数4
* @param p5 参数5
* @return {@link Consumer}
*/
default Consumer<T> toConsumer(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
return instant -> accept(instant, p1, p2, p3, p4, p5);
}
}

View File

@ -0,0 +1,32 @@
package cn.hutool.core.lang.func;
import java.util.function.Supplier;
/**
* 1参数Supplier
*
* @param <T> 目标 类型
* @param <P1> 参数一 类型
* @author TomXin
* @since 5.7.21
*/
@FunctionalInterface
public interface Supplier1<T, P1> {
/**
* 生成实例的方法
*
* @param p1 参数一
* @return 目标对象
*/
T get(P1 p1);
/**
* 将带有参数的Supplier转换为无参{@link Supplier}
*
* @param p1 参数1
* @return {@link Supplier}
*/
default Supplier<T> toSupplier(P1 p1) {
return () -> get(p1);
}
}

View File

@ -0,0 +1,36 @@
package cn.hutool.core.lang.func;
import java.util.function.Supplier;
/**
* 两个参数的Supplier
*
* @param <T> 目标 类型
* @param <P1> 参数一 类型
* @param <P2> 参数二 类型
* @author TomXin
* @since 5.7.21
*/
@FunctionalInterface
public interface Supplier2<T, P1, P2> {
/**
* 生成实例的方法
*
* @param p1 参数一
* @param p2 参数二
* @return 目标对象
*/
T get(P1 p1, P2 p2);
/**
* 将带有参数的Supplier转换为无参{@link Supplier}
*
* @param p1 参数1
* @param p2 参数2
* @return {@link Supplier}
*/
default Supplier<T> toSupplier(P1 p1, P2 p2) {
return () -> get(p1, p2);
}
}

View File

@ -0,0 +1,39 @@
package cn.hutool.core.lang.func;
import java.util.function.Supplier;
/**
* 3参数Supplier
*
* @param <T> 目标类型
* @param <P1> 参数一类型
* @param <P2> 参数二类型
* @param <P3> 参数三类型
* @author TomXin
* @since 5.7.21
*/
@FunctionalInterface
public interface Supplier3<T, P1, P2, P3> {
/**
* 生成实例的方法
*
* @param p1 参数一
* @param p2 参数二
* @param p3 参数三
* @return 目标对象
*/
T get(P1 p1, P2 p2, P3 p3);
/**
* 将带有参数的Supplier转换为无参{@link Supplier}
*
* @param p1 参数1
* @param p2 参数2
* @param p3 参数3
* @return {@link Supplier}
*/
default Supplier<T> toSupplier(P1 p1, P2 p2, P3 p3) {
return () -> get(p1, p2, p3);
}
}

View File

@ -0,0 +1,42 @@
package cn.hutool.core.lang.func;
import java.util.function.Supplier;
/**
* 4参数Supplier
*
* @param <T> 目标 类型
* @param <P1> 参数一 类型
* @param <P2> 参数二 类型
* @param <P3> 参数三 类型
* @param <P4> 参数四 类型
* @author TomXin
* @since 5.7.21
*/
@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);
/**
* 将带有参数的Supplier转换为无参{@link Supplier}
*
* @param p1 参数1
* @param p2 参数2
* @param p3 参数3
* @param p4 参数4
* @return {@link Supplier}
*/
default Supplier<T> toSupplier(P1 p1, P2 p2, P3 p3, P4 p4) {
return () -> get(p1, p2, p3, p4);
}
}

View File

@ -0,0 +1,45 @@
package cn.hutool.core.lang.func;
import java.util.function.Supplier;
/**
* 5参数Supplier
*
* @param <T> 目标 类型
* @param <P1> 参数一 类型
* @param <P2> 参数二 类型
* @param <P3> 参数三 类型
* @param <P4> 参数四 类型
* @param <P5> 参数五 类型
* @author TomXin
* @since 5.7.21
*/
@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);
/**
* 将带有参数的Supplier转换为无参{@link Supplier}
*
* @param p1 参数1
* @param p2 参数2
* @param p3 参数3
* @param p4 参数4
* @param p5 参数5
* @return {@link Supplier}
*/
default Supplier<T> toSupplier(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
return () -> get(p1, p2, p3, p4, p5);
}
}

View File

@ -2,6 +2,7 @@ package cn.hutool.core.builder;
import cn.hutool.core.util.StrUtil;
import lombok.*;
import org.junit.Assert;
import org.junit.Test;
/**
@ -21,7 +22,13 @@ public class GenericBuilderTest {
.with(Box::setWidth, 8)
.with(Box::setHeight, 7)
.build();
System.out.println(box);
Assert.assertEquals(1024L, box.getId().longValue());
Assert.assertEquals("Hello World!", box.getTitle());
Assert.assertEquals(9, box.getLength().intValue());
Assert.assertEquals(8, box.getWidth().intValue());
Assert.assertEquals(7, box.getHeight().intValue());
Box boxModified = GenericBuilder
.of(() -> box)
.with(Box::setTitle, "Hello Friend!")
@ -29,12 +36,23 @@ public class GenericBuilderTest {
.with(Box::setWidth, 4)
.with(Box::setHeight, 5)
.build();
System.out.println(boxModified);
Assert.assertEquals(1024L, boxModified.getId().longValue());
Assert.assertEquals("Hello Friend!", box.getTitle());
Assert.assertEquals(3, boxModified.getLength().intValue());
Assert.assertEquals(4, boxModified.getWidth().intValue());
Assert.assertEquals(5, boxModified.getHeight().intValue());
Box box1 = GenericBuilder
.of(Box::new, 2048L, "Hello Partner!", 222, 333, 444)
.with(Box::alis)
.build();
System.out.println(box1);
Assert.assertEquals(2048L, box1.getId().longValue());
Assert.assertEquals("Hello Partner!", box1.getTitle());
Assert.assertEquals(222, box1.getLength().intValue());
Assert.assertEquals(333, box1.getWidth().intValue());
Assert.assertEquals(444, box1.getHeight().intValue());
}
@Getter

View File

@ -21,11 +21,8 @@ public class CheckedUtilTest {
@Test
public void sleepTest() {
VoidFunc0 func = () -> Thread.sleep(1000L);
func.callWithRuntimeException();
}
@ -39,7 +36,6 @@ public class CheckedUtilTest {
} catch (Exception re) {
Assert.assertTrue(re instanceof RuntimeException);
}
}
@SuppressWarnings("ConstantConditions")