From f732d14809162665f0a0c9e1ac17a279a63a099a Mon Sep 17 00:00:00 2001 From: achao Date: Wed, 7 Sep 2022 15:08:11 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E9=87=8D=E6=9E=84Jdk=E8=87=AA=E5=B8=A6?= =?UTF-8?q?=E7=9A=84Lambda=E4=BD=93=E7=B3=BB=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96+=E5=8C=85=E8=A3=B9=E5=8F=97?= =?UTF-8?q?=E6=A3=80=E5=BC=82=E5=B8=B8=20=E6=94=B9=E5=8A=A8=E5=A6=82?= =?UTF-8?q?=E4=B8=8B:=201.=20AnnotationUtil=20115=E8=A1=8C=20=E7=AE=80?= =?UTF-8?q?=E5=8C=96=20predicate::test=20=E4=B8=BA=20predicate=202.=20?= =?UTF-8?q?=E8=B0=83=E6=95=B4=20Func1=20=E4=B8=BA=20SerFunction=203.=20?= =?UTF-8?q?=E8=B0=83=E6=95=B4=20Func0=20=E4=B8=BA=20SerSupplier=204.=20?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=20GenericBuilder=20=E5=AF=B9=E4=BA=8E?= =?UTF-8?q?=E5=A4=9A=E5=8F=82=E6=95=B0=E6=9E=84=E9=80=A0=E5=8F=8C=E5=86=92?= =?UTF-8?q?=E5=8F=B7=E7=AE=80=E5=86=99=E6=94=AF=E6=8C=81=EF=BC=8C=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E9=87=87=E7=94=A8lambda=E6=96=B9=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E4=BE=8B=E5=A6=82GenericBuilder.of(Box::new,=202048L,=20"Hello?= =?UTF-8?q?=20Partner!",=20222,=20333,=20444)=E6=94=B9=E4=B8=BAGenericBuil?= =?UTF-8?q?der.of(()=20->=20new=20Box(2048L,=20"Hello=20Partner!",=20222,?= =?UTF-8?q?=20333,=20444))=205.=20=E7=A7=BB=E9=99=A4=20CheckedUtil?= =?UTF-8?q?=EF=BC=8C=E7=8E=B0=E6=9C=89=E9=87=8D=E6=9E=84=E5=90=8E=E7=9A=84?= =?UTF-8?q?Lambda=20=E6=94=AF=E6=8C=81=E5=8C=85=E8=A3=B9=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=206.=20=E7=A7=BB=E9=99=A4=20Func=EF=BC=8C=E8=AF=A5=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=BC=8F=E6=8E=A5=E5=8F=A3=E5=B1=9E=E4=BA=8E=E6=B3=9B?= =?UTF-8?q?=E5=9E=8B=E5=8F=AF=E5=8F=98=E5=8F=82=E6=95=B0=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E6=8E=A8=E8=8D=90=E4=BD=BF=E7=94=A8=207.=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=20Supplier1=EF=BC=8C1=E5=8F=82=E6=95=B0Supplier=E5=BA=94?= =?UTF-8?q?=E8=AF=A5=E4=BD=BF=E7=94=A8SerFunction=E6=9B=BF=E4=BB=A3=208.?= =?UTF-8?q?=20=E7=A7=BB=E9=99=A4=20Supplier2=EF=BC=8C2=E5=8F=82=E6=95=B0Su?= =?UTF-8?q?pplier=E5=BA=94=E8=AF=A5=E4=BD=BF=E7=94=A8SerBiFunction?= =?UTF-8?q?=E6=9B=BF=E4=BB=A3=209.=20=E7=A7=BB=E9=99=A4=20Supplier3?= =?UTF-8?q?=EF=BC=8C3=E5=8F=82=E6=95=B0Supplier=E5=BA=94=E8=AF=A5=E4=BD=BF?= =?UTF-8?q?=E7=94=A8SerFunction3=E6=9B=BF=E4=BB=A3(=E5=9B=A0=E7=AC=AC4?= =?UTF-8?q?=E6=9D=A1=E6=9B=B4=E6=94=B9=E6=80=9D=E8=B7=AF=EF=BC=8C=E8=AF=A5?= =?UTF-8?q?SerFunction3=E5=B9=B6=E6=9C=AA=E6=B7=BB=E5=8A=A0)=2010.=20?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=20Supplier4=EF=BC=8C4=E5=8F=82=E6=95=B0Suppl?= =?UTF-8?q?ier=E5=BA=94=E8=AF=A5=E4=BD=BF=E7=94=A8SerFunction4=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3(=E5=9B=A0=E7=AC=AC4=E6=9D=A1=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E6=80=9D=E8=B7=AF=EF=BC=8C=E8=AF=A5SerFunction4=E5=B9=B6?= =?UTF-8?q?=E6=9C=AA=E6=B7=BB=E5=8A=A0)=2011.=20=E7=A7=BB=E9=99=A4=20Suppl?= =?UTF-8?q?ier5=EF=BC=8C5=E5=8F=82=E6=95=B0Supplier=E5=BA=94=E8=AF=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8SerFunction5=E6=9B=BF=E4=BB=A3(=E5=9B=A0?= =?UTF-8?q?=E7=AC=AC4=E6=9D=A1=E6=9B=B4=E6=94=B9=E6=80=9D=E8=B7=AF?= =?UTF-8?q?=EF=BC=8C=E8=AF=A5SerFunction5=E5=B9=B6=E6=9C=AA=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0)=2012.=20=E7=A7=BB=E9=99=A4=20VoidFunc=EF=BC=8C?= =?UTF-8?q?=E8=AF=A5=E5=87=BD=E6=95=B0=E5=BC=8F=E6=8E=A5=E5=8F=A3=E5=B1=9E?= =?UTF-8?q?=E4=BA=8E=E6=B3=9B=E5=9E=8B=E5=8F=AF=E5=8F=98=E5=8F=82=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E6=8E=A8=E8=8D=90=E4=BD=BF=E7=94=A8=2013.=20?= =?UTF-8?q?=E8=B0=83=E6=95=B4=20VoidFunc0=20=E4=B8=BA=20SerRunnable=2014.?= =?UTF-8?q?=20=E8=B0=83=E6=95=B4=20VoidFunc1=20=E4=B8=BA=20SerConsumer=201?= =?UTF-8?q?5.=20=E8=B0=83=E6=95=B4=20EntryStream=20=E6=B3=9B=E5=9E=8B?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E3=80=81=E5=AE=8C=E5=96=84javadoc=2016.=20En?= =?UTF-8?q?umUtil=20273=E8=A1=8C=20=E7=AE=80=E5=8C=96=20field::callWithRun?= =?UTF-8?q?timeException=20=E4=B8=BA=20field?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/annotation/AnnotationUtil.java | 2 +- .../cn/hutool/core/bean/BeanDescCache.java | 6 +- .../cn/hutool/core/bean/BeanInfoCache.java | 6 +- .../hutool/core/bean/copier/CopyOptions.java | 4 +- .../hutool/core/builder/GenericBuilder.java | 99 +----- .../main/java/cn/hutool/core/cache/Cache.java | 14 +- .../cn/hutool/core/cache/SimpleCache.java | 22 +- .../hutool/core/cache/impl/AbstractCache.java | 20 +- .../cn/hutool/core/cache/impl/NoCache.java | 14 +- .../hutool/core/exceptions/CheckedUtil.java | 332 ------------------ .../main/java/cn/hutool/core/lang/Opt.java | 12 +- .../java/cn/hutool/core/lang/Singleton.java | 16 +- .../cn/hutool/core/lang/func/Consumer3.java | 23 -- .../java/cn/hutool/core/lang/func/Func.java | 43 --- .../java/cn/hutool/core/lang/func/Func0.java | 39 -- .../java/cn/hutool/core/lang/func/Func1.java | 43 --- .../hutool/core/lang/func/SerBiConsumer.java | 85 +++++ .../hutool/core/lang/func/SerBiFunction.java | 63 ++++ .../hutool/core/lang/func/SerBiPredicate.java | 103 ++++++ .../core/lang/func/SerBinaryOperator.java | 96 +++++ .../cn/hutool/core/lang/func/SerConsumer.java | 83 +++++ .../hutool/core/lang/func/SerConsumer3.java | 65 ++++ .../cn/hutool/core/lang/func/SerFunction.java | 63 ++++ .../hutool/core/lang/func/SerPredicate.java | 139 ++++++++ .../cn/hutool/core/lang/func/SerRunnable.java | 63 ++++ .../cn/hutool/core/lang/func/SerSupplier.java | 53 +++ .../core/lang/func/SerUnaryOperator.java | 68 ++++ .../cn/hutool/core/lang/func/Supplier1.java | 32 -- .../cn/hutool/core/lang/func/Supplier2.java | 36 -- .../cn/hutool/core/lang/func/Supplier3.java | 39 -- .../cn/hutool/core/lang/func/Supplier4.java | 42 --- .../cn/hutool/core/lang/func/Supplier5.java | 45 --- .../cn/hutool/core/lang/func/VoidFunc.java | 41 --- .../cn/hutool/core/lang/func/VoidFunc0.java | 37 -- .../cn/hutool/core/lang/func/VoidFunc1.java | 39 -- .../main/java/cn/hutool/core/map/Dict.java | 8 +- .../core/map/multi/CollectionValueMap.java | 12 +- .../java/cn/hutool/core/map/multi/Table.java | 4 +- .../java/cn/hutool/core/regex/ReUtil.java | 17 +- .../cn/hutool/core/stream/EntryStream.java | 105 +++--- .../cn/hutool/core/text/CharSequenceUtil.java | 10 +- .../java/cn/hutool/core/util/EnumUtil.java | 10 +- .../core/builder/GenericBuilderTest.java | 6 +- .../core/exceptions/CheckedUtilTest.java | 62 ---- .../hutool/core/lang/func/LambdaUtilTest.java | 46 +-- ...ncMapTest.java => SerFunctionMapTest.java} | 2 +- .../main/java/cn/hutool/db/AbstractDb.java | 4 +- .../main/java/cn/hutool/db/ActiveEntity.java | 4 +- hutool-db/src/main/java/cn/hutool/db/Db.java | 6 +- .../src/main/java/cn/hutool/db/Entity.java | 4 +- .../src/main/java/cn/hutool/db/Session.java | 8 +- .../java/cn/hutool/db/sql/SqlExecutor.java | 6 +- 52 files changed, 1064 insertions(+), 1137 deletions(-) delete mode 100755 hutool-core/src/main/java/cn/hutool/core/exceptions/CheckedUtil.java delete mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/Consumer3.java delete mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/Func.java delete mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/Func0.java delete mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/Func1.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiConsumer.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiFunction.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiPredicate.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerBinaryOperator.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerConsumer.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerConsumer3.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerFunction.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerPredicate.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerRunnable.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerSupplier.java create mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/SerUnaryOperator.java delete mode 100755 hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier1.java delete mode 100755 hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier2.java delete mode 100755 hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier3.java delete mode 100755 hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier4.java delete mode 100755 hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier5.java delete mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc.java delete mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc0.java delete mode 100644 hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc1.java delete mode 100644 hutool-core/src/test/java/cn/hutool/core/exceptions/CheckedUtilTest.java rename hutool-core/src/test/java/cn/hutool/core/map/{FuncMapTest.java => SerFunctionMapTest.java} (92%) mode change 100755 => 100644 diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java b/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java index a7c33e5f3..632f4d20c 100755 --- a/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/annotation/AnnotationUtil.java @@ -112,7 +112,7 @@ public class AnnotationUtil { if (null == predicate) { return result; } - return ArrayUtil.filter(result, predicate::test); + return ArrayUtil.filter(result, predicate); } /** diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/BeanDescCache.java b/hutool-core/src/main/java/cn/hutool/core/bean/BeanDescCache.java index e220e7f11..b9b7739b3 100755 --- a/hutool-core/src/main/java/cn/hutool/core/bean/BeanDescCache.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/BeanDescCache.java @@ -1,6 +1,6 @@ package cn.hutool.core.bean; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import cn.hutool.core.map.WeakConcurrentMap; /** @@ -22,8 +22,8 @@ public enum BeanDescCache { * @return 属性名和{@link BeanDesc}映射 * @since 5.4.2 */ - public BeanDesc getBeanDesc(final Class beanClass, final Func0 supplier) { - return bdCache.computeIfAbsent(beanClass, (key)->supplier.callWithRuntimeException()); + public BeanDesc getBeanDesc(final Class beanClass, final SerSupplier supplier) { + return bdCache.computeIfAbsent(beanClass, (key) -> supplier.get()); } /** diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/BeanInfoCache.java b/hutool-core/src/main/java/cn/hutool/core/bean/BeanInfoCache.java index fb2edeb29..23864073f 100755 --- a/hutool-core/src/main/java/cn/hutool/core/bean/BeanInfoCache.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/BeanInfoCache.java @@ -1,6 +1,6 @@ package cn.hutool.core.bean; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import cn.hutool.core.map.ReferenceConcurrentMap; import cn.hutool.core.map.WeakConcurrentMap; @@ -42,8 +42,8 @@ public enum BeanInfoCache { public Map getPropertyDescriptorMap( final Class beanClass, final boolean ignoreCase, - final Func0> supplier) { - return getCache(ignoreCase).computeIfAbsent(beanClass, (key)->supplier.callWithRuntimeException()); + final SerSupplier> supplier) { + return getCache(ignoreCase).computeIfAbsent(beanClass, (key) -> supplier.get()); } /** diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/copier/CopyOptions.java b/hutool-core/src/main/java/cn/hutool/core/bean/copier/CopyOptions.java index d8422e4c6..cea6f8b9f 100755 --- a/hutool-core/src/main/java/cn/hutool/core/bean/copier/CopyOptions.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/copier/CopyOptions.java @@ -2,8 +2,8 @@ package cn.hutool.core.bean.copier; import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Converter; -import cn.hutool.core.lang.func.Func1; import cn.hutool.core.lang.func.LambdaUtil; +import cn.hutool.core.lang.func.SerFunction; import cn.hutool.core.lang.mutable.MutableEntry; import cn.hutool.core.util.ArrayUtil; @@ -178,7 +178,7 @@ public class CopyOptions implements Serializable { * @since 5.8.0 */ @SuppressWarnings("unchecked") - public CopyOptions setIgnoreProperties(final Func1... funcs) { + public CopyOptions setIgnoreProperties(final SerFunction... funcs) { final Set ignoreProperties = ArrayUtil.mapToSet(funcs, LambdaUtil::getFieldName); return setPropertiesFilter((field, o) -> false == ignoreProperties.contains(field.getName())); } 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 index 481cb004d..27fb66156 100644 --- a/hutool-core/src/main/java/cn/hutool/core/builder/GenericBuilder.java +++ b/hutool-core/src/main/java/cn/hutool/core/builder/GenericBuilder.java @@ -1,11 +1,6 @@ package cn.hutool.core.builder; -import cn.hutool.core.lang.func.Consumer3; -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 cn.hutool.core.lang.func.SerConsumer3; import java.util.ArrayList; import java.util.List; @@ -58,7 +53,7 @@ import java.util.function.Supplier; * *

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

* - * @author TomXin + * @author TomXin VampireAchao * @since 5.7.21 */ public class GenericBuilder implements Builder { @@ -84,7 +79,7 @@ public class GenericBuilder implements Builder { } /** - * 通过无参数实例化器创建GenericBuilder + * 通过Supplier创建GenericBuilder * * @param instant 实例化器 * @param 目标类型 @@ -94,92 +89,6 @@ public class GenericBuilder implements Builder { return new GenericBuilder<>(instant); } - /** - * 通过1参数实例化器创建GenericBuilder - * - * @param instant 实例化器 - * @param p1 参数一 - * @param 目标类型 - * @param 参数一类型 - * @return GenericBuilder对象 - */ - public static GenericBuilder of(final Supplier1 instant, final P1 p1) { - return of(instant.toSupplier(p1)); - } - - /** - * 通过2参数实例化器创建GenericBuilder - * - * @param instant 实例化器 - * @param p1 参数一 - * @param p2 参数二 - * @param 目标类型 - * @param 参数一类型 - * @param 参数二类型 - * @return GenericBuilder对象 - */ - public static GenericBuilder of(final Supplier2 instant, final P1 p1, final P2 p2) { - return of(instant.toSupplier(p1, p2)); - } - - /** - * 通过3参数实例化器创建GenericBuilder - * - * @param instant 实例化器 - * @param p1 参数一 - * @param p2 参数二 - * @param p3 参数三 - * @param 目标类型 - * @param 参数一类型 - * @param 参数二类型 - * @param 参数三类型 - * @return GenericBuilder对象 - */ - public static GenericBuilder of(final Supplier3 instant, final P1 p1, final P2 p2, final P3 p3) { - return of(instant.toSupplier(p1, p2, p3)); - } - - /** - * 通过4参数实例化器创建GenericBuilder - * - * @param instant 实例化器 - * @param p1 参数一 - * @param p2 参数二 - * @param p3 参数三 - * @param p4 参数四 - * @param 目标类型 - * @param 参数一类型 - * @param 参数二类型 - * @param 参数三类型 - * @param 参数四类型 - * @return GenericBuilder对象 - */ - public static GenericBuilder of(final Supplier4 instant, final P1 p1, final P2 p2, final P3 p3, final P4 p4) { - return of(instant.toSupplier(p1, p2, p3, p4)); - } - - /** - * 通过5参数实例化器创建GenericBuilder - * - * @param instant 实例化器 - * @param p1 参数一 - * @param p2 参数二 - * @param p3 参数三 - * @param p4 参数四 - * @param p5 参数五 - * @param 目标类型 - * @param 参数一类型 - * @param 参数二类型 - * @param 参数三类型 - * @param 参数四类型 - * @param 参数五类型 - * @return GenericBuilder对象 - */ - public static GenericBuilder of(final Supplier5 instant, final P1 p1, final P2 p2, final P3 p3, final P4 p4, final P5 p5) { - return of(instant.toSupplier(p1, p2, p3, p4, p5)); - } - - /** * 调用无参数方法 * @@ -215,7 +124,7 @@ public class GenericBuilder implements Builder { * @param 参数二类型 * @return GenericBuilder对象 */ - public GenericBuilder with(final Consumer3 consumer, final P1 p1, final P2 p2) { + public GenericBuilder with(final SerConsumer3 consumer, final P1 p1, final P2 p2) { modifiers.add(instant -> consumer.accept(instant, p1, p2)); return this; } diff --git a/hutool-core/src/main/java/cn/hutool/core/cache/Cache.java b/hutool-core/src/main/java/cn/hutool/core/cache/Cache.java index 87d8c80ce..5ac3d6ce7 100755 --- a/hutool-core/src/main/java/cn/hutool/core/cache/Cache.java +++ b/hutool-core/src/main/java/cn/hutool/core/cache/Cache.java @@ -1,7 +1,7 @@ package cn.hutool.core.cache; import cn.hutool.core.cache.impl.CacheObj; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import java.io.Serializable; import java.util.Iterator; @@ -11,7 +11,7 @@ import java.util.Iterator; * * @param 键类型 * @param 值类型 - * @author Looly, jodd + * @author Looly, jodd, VampireAchao */ public interface Cache extends Iterable, Serializable { @@ -64,7 +64,7 @@ public interface Cache extends Iterable, Serializable { } /** - * 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象 + * 从缓存中获得对象,当对象不在缓存中或已经过期返回SerSupplier回调产生的对象 *

* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回{@code null},否则返回值。 *

@@ -74,12 +74,12 @@ public interface Cache extends Iterable, Serializable { * @param supplier 如果不存在回调方法,用于生产值对象 * @return 值对象 */ - default V get(final K key, final Func0 supplier) { + default V get(final K key, final SerSupplier supplier) { return get(key, true, supplier); } /** - * 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象 + * 从缓存中获得对象,当对象不在缓存中或已经过期返回SerSupplier回调产生的对象 *

* 调用此方法时,会检查上次调用时间,如果与当前时间差值大于超时时间返回{@code null},否则返回值。 *

@@ -90,7 +90,7 @@ public interface Cache extends Iterable, Serializable { * @param supplier 如果不存在回调方法,用于生产值对象 * @return 值对象 */ - V get(K key, boolean isUpdateLastAccess, Func0 supplier); + V get(K key, boolean isUpdateLastAccess, SerSupplier supplier); /** * 从缓存中获得对象,当对象不在缓存中或已经过期返回{@code null} @@ -168,7 +168,7 @@ public interface Cache extends Iterable, Serializable { * @return this * @since 5.5.2 */ - default Cache setListener(final CacheListener listener){ + default Cache setListener(final CacheListener listener) { return this; } } diff --git a/hutool-core/src/main/java/cn/hutool/core/cache/SimpleCache.java b/hutool-core/src/main/java/cn/hutool/core/cache/SimpleCache.java index 7f1695be6..034a2334b 100644 --- a/hutool-core/src/main/java/cn/hutool/core/cache/SimpleCache.java +++ b/hutool-core/src/main/java/cn/hutool/core/cache/SimpleCache.java @@ -1,7 +1,7 @@ package cn.hutool.core.cache; import cn.hutool.core.collection.iter.TransIter; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import cn.hutool.core.lang.mutable.Mutable; import cn.hutool.core.lang.mutable.MutableObj; import cn.hutool.core.map.WeakConcurrentMap; @@ -22,7 +22,7 @@ import java.util.function.Predicate; * * @param 键类型 * @param 值类型 - * @author Looly + * @author Looly, VampireAchao */ public class SimpleCache implements Iterable>, Serializable { private static final long serialVersionUID = 1L; @@ -75,18 +75,18 @@ public class SimpleCache implements Iterable>, Serializabl } /** - * 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象 + * 从缓存中获得对象,当对象不在缓存中或已经过期返回SerSupplier回调产生的对象 * * @param key 键 * @param supplier 如果不存在回调方法,用于生产值对象 * @return 值对象 */ - public V get(final K key, final Func0 supplier) { + public V get(final K key, final SerSupplier supplier) { return get(key, null, supplier); } /** - * 从缓存中获得对象,当对象不在缓存中或已经过期返回Func0回调产生的对象 + * 从缓存中获得对象,当对象不在缓存中或已经过期返回SerSupplier回调产生的对象 * * @param key 键 * @param validPredicate 检查结果对象是否可用,如是否断开连接等 @@ -94,9 +94,9 @@ public class SimpleCache implements Iterable>, Serializabl * @return 值对象 * @since 5.7.9 */ - public V get(final K key, final Predicate validPredicate, final Func0 supplier) { + public V get(final K key, final Predicate validPredicate, final SerSupplier supplier) { V v = get(key); - if((null != validPredicate && null != v && false == validPredicate.test(v))){ + if ((null != validPredicate && null != v && false == validPredicate.test(v))) { v = null; } if (null == v && null != supplier) { @@ -107,11 +107,7 @@ public class SimpleCache implements Iterable>, Serializabl // 双重检查,防止在竞争锁的过程中已经有其它线程写入 v = get(key); if (null == v || (null != validPredicate && false == validPredicate.test(v))) { - try { - v = supplier.call(); - } catch (final Exception e) { - throw new RuntimeException(e); - } + v = supplier.get(); put(key, v); } } finally { @@ -172,7 +168,7 @@ public class SimpleCache implements Iterable>, Serializabl @Override public Iterator> iterator() { - return new TransIter<>(this.rawMap.entrySet().iterator(), (entry)-> new Map.Entry() { + return new TransIter<>(this.rawMap.entrySet().iterator(), (entry) -> new Map.Entry() { @Override public K getKey() { return entry.getKey().get(); diff --git a/hutool-core/src/main/java/cn/hutool/core/cache/impl/AbstractCache.java b/hutool-core/src/main/java/cn/hutool/core/cache/impl/AbstractCache.java index 34c5da784..d64387d82 100755 --- a/hutool-core/src/main/java/cn/hutool/core/cache/impl/AbstractCache.java +++ b/hutool-core/src/main/java/cn/hutool/core/cache/impl/AbstractCache.java @@ -2,7 +2,7 @@ package cn.hutool.core.cache.impl; import cn.hutool.core.cache.Cache; import cn.hutool.core.cache.CacheListener; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import cn.hutool.core.lang.mutable.Mutable; import cn.hutool.core.lang.mutable.MutableObj; @@ -92,6 +92,7 @@ public abstract class AbstractCache implements Cache { // ---------------------------------------------------------------- put end // ---------------------------------------------------------------- get start + /** * @return 命中数 */ @@ -107,7 +108,7 @@ public abstract class AbstractCache implements Cache { } @Override - public V get(final K key, final boolean isUpdateLastAccess, final Func0 supplier) { + public V get(final K key, final boolean isUpdateLastAccess, final SerSupplier supplier) { V v = get(key, isUpdateLastAccess); if (null == v && null != supplier) { //每个key单独获取一把锁,降低锁的粒度提高并发能力,see pr#1385@Github @@ -117,11 +118,7 @@ public abstract class AbstractCache implements Cache { // 双重检查锁,防止在竞争锁的过程中已经有其它线程写入 final CacheObj co = getWithoutLock(key); if (null == co || co.isExpired()) { - try { - v = supplier.call(); - } catch (final Exception e) { - throw new RuntimeException(e); - } + v = supplier.get(); put(key, v, this.timeout); } else { v = co.get(isUpdateLastAccess); @@ -136,11 +133,12 @@ public abstract class AbstractCache implements Cache { /** * 获取键对应的{@link CacheObj} + * * @param key 键,实际使用时会被包装为{@link MutableObj} * @return {@link CacheObj} * @since 5.8.0 */ - protected CacheObj getWithoutLock(final K key){ + protected CacheObj getWithoutLock(final K key) { return this.cacheMap.get(MutableObj.of(key)); } // ---------------------------------------------------------------- get end @@ -151,6 +149,7 @@ public abstract class AbstractCache implements Cache { return new CacheValuesIterator<>(copiedIterator); } // ---------------------------------------------------------------- prune start + /** * 清理实现
* 子类实现此方法时无需加锁 @@ -224,7 +223,7 @@ public abstract class AbstractCache implements Cache { * @return 所有键 * @since 5.5.9 */ - public Set keySet(){ + public Set keySet() { return this.cacheMap.keySet().stream().map(Mutable::get).collect(Collectors.toSet()); } @@ -260,10 +259,11 @@ public abstract class AbstractCache implements Cache { /** * 获取所有{@link CacheObj}值的{@link Iterator}形式 + * * @return {@link Iterator} * @since 5.8.0 */ - protected Iterator> cacheObjIter(){ + protected Iterator> cacheObjIter() { return this.cacheMap.values().iterator(); } } diff --git a/hutool-core/src/main/java/cn/hutool/core/cache/impl/NoCache.java b/hutool-core/src/main/java/cn/hutool/core/cache/impl/NoCache.java index 93836ab4e..6bc800962 100755 --- a/hutool-core/src/main/java/cn/hutool/core/cache/impl/NoCache.java +++ b/hutool-core/src/main/java/cn/hutool/core/cache/impl/NoCache.java @@ -1,7 +1,7 @@ package cn.hutool.core.cache.impl; import cn.hutool.core.cache.Cache; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import java.util.Iterator; @@ -10,7 +10,7 @@ import java.util.Iterator; * * @param 键类型 * @param 值类型 - * @author Looly,jodd + * @author Looly, jodd, VampireAchao */ public class NoCache implements Cache { private static final long serialVersionUID = 1L; @@ -51,17 +51,13 @@ public class NoCache implements Cache { } @Override - public V get(final K key, final Func0 supplier) { + public V get(final K key, final SerSupplier supplier) { return get(key, true, supplier); } @Override - public V get(final K key, final boolean isUpdateLastAccess, final Func0 supplier) { - try { - return (null == supplier) ? null : supplier.call(); - } catch (final Exception e) { - throw new RuntimeException(e); - } + public V get(final K key, final boolean isUpdateLastAccess, final SerSupplier supplier) { + return (null == supplier) ? null : supplier.get(); } @Override diff --git a/hutool-core/src/main/java/cn/hutool/core/exceptions/CheckedUtil.java b/hutool-core/src/main/java/cn/hutool/core/exceptions/CheckedUtil.java deleted file mode 100755 index ef7962ea8..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/exceptions/CheckedUtil.java +++ /dev/null @@ -1,332 +0,0 @@ -package cn.hutool.core.exceptions; - -import cn.hutool.core.lang.func.Func; -import cn.hutool.core.lang.func.Func0; -import cn.hutool.core.lang.func.Func1; -import cn.hutool.core.lang.func.Supplier1; -import cn.hutool.core.lang.func.VoidFunc; -import cn.hutool.core.lang.func.VoidFunc0; -import cn.hutool.core.lang.func.VoidFunc1; - -import java.util.Objects; - -/** - * TODO 按照https://github.com/dromara/hutool/issues/2549修改。 - * 方便的执行会抛出受检查类型异常的方法调用或者代码段 - *

- * 该工具通过函数式的方式将那些需要抛出受检查异常的表达式或者代码段转化成一个 cn.hutool.core.lang.func.Func* 对象 - *

- *

- * {@code - *

- *      //代码中如果遇到一个方法调用声明了受检查异常那么我们的代码就必须这样写
- *         Map describedObject = null;
- *         try {
- *             describe = BeanUtils.describe(new Object());
- *         } catch (IllegalAccessException e) {
- *             throw new RuntimeException(e);
- *         } catch (InvocationTargetException e) {
- *             throw new RuntimeException(e);
- *         } catch (NoSuchMethodException e) {
- *             throw new RuntimeException(e);
- *         }
- *         // use describedObject ...
- *
- *       //上面的代码增加了异常块使得代码不那么流畅,现在可以这样写:
- *       Map describedObject = CheckedUtil.uncheck(BeanUtils::describe).call(new Object());
- *       // use describedObject ...
- *
- *       CheckedUtil.uncheck 方法接受任意可以转化成 cn.hutool.core.lang.func.Func* 函数式接口的 Lambda 表达式。返回对应的函数式对象。
- *       上述代码可以理解为:
- *        Func0> aFunc = CheckedUtil.uncheck(BeanUtils::describe);
- *        Map describedObject = aFunc.call(传入参数);
- *        该aFunc对象代表的就是BeanUtils::describe这个表达式,且在内部转化了检查类型异常,不需要代码里面显示处理。
- *
- *
- * 
- * } - * - * @author conder - * @since 5.7.19 - */ -public class CheckedUtil { - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.Func 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression Lambda表达式 - * @param

运行时传入的参数类型 - * @param 最终返回的数据类型 - * @return {@link FuncRt} - */ - public static FuncRt uncheck(final Func expression) { - return uncheck(expression, RuntimeException::new); - } - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.Func0 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression 运行时传入的参数类型 - * @param 最终返回的数据类型 - * @return {@link Func0Rt} - */ - public static Func0Rt uncheck(final Func0 expression) { - return uncheck(expression, RuntimeException::new); - } - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.Func1 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression 运行时传入的参数类型 - * @param

运行时传入的参数类型 - * @param 最终返回的数据类型 - * @return {@link Func1Rt} - */ - public static Func1Rt uncheck(final Func1 expression) { - return uncheck(expression, RuntimeException::new); - } - - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression 运行时传入的参数类型 - * @param

运行时传入的参数类型 - * @return {@link VoidFuncRt} - */ - public static

VoidFuncRt

uncheck(final VoidFunc

expression) { - return uncheck(expression, RuntimeException::new); - } - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc0 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression 运行时传入的参数类型 - * @return {@link VoidFunc0Rt} - */ - public static VoidFunc0Rt uncheck(final VoidFunc0 expression) { - return uncheck(expression, RuntimeException::new); - } - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc1 的Lambda表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression 运行时传入的参数类型 - * @param

运行时传入的参数类型 - * @return {@link VoidFunc1Rt} - */ - public static

VoidFunc1Rt

uncheck(final VoidFunc1

expression) { - return uncheck(expression, RuntimeException::new); - } - - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.Func的Lambda表达式,和一个可以把Exception转化成RuntimeExceptionde的表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression Lambda表达式 - * @param rteSupplier 转化运行时异常的表达式 - * @param

运行时传入的参数类型 - * @param 最终返回的数据类型 - * @return {@link FuncRt} - */ - public static FuncRt uncheck(final Func expression, final Supplier1 rteSupplier) { - Objects.requireNonNull(expression, "expression can not be null"); - return t -> { - try { - return expression.call(t); - } catch (final Exception e) { - if (rteSupplier == null) { - throw new RuntimeException(e); - } else { - throw rteSupplier.get(e); - } - } - }; - } - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.Func0的Lambda表达式,和一个可以把Exception转化成RuntimeExceptionde的表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression Lambda表达式 - * @param rteSupplier 转化运行时异常的表达式 - * @param 最终返回的数据类型 - * @return {@link Func0Rt} - */ - public static Func0Rt uncheck(final Func0 expression, final Supplier1 rteSupplier) { - Objects.requireNonNull(expression, "expression can not be null"); - return () -> { - try { - return expression.call(); - } catch (final Exception e) { - if (rteSupplier == null) { - throw new RuntimeException(e); - } else { - throw rteSupplier.get(e); - } - } - }; - } - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.Func1的Lambda表达式,和一个可以把Exception转化成RuntimeExceptionde的表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression Lambda表达式 - * @param rteSupplier 转化运行时异常的表达式 - * @param

运行时传入的参数类型 - * @param 最终返回的数据类型 - * @return {@link Func1Rt} - */ - public static Func1Rt uncheck(final Func1 expression, final Supplier1 rteSupplier) { - Objects.requireNonNull(expression, "expression can not be null"); - return t -> { - try { - return expression.call(t); - } catch (final Exception e) { - if (rteSupplier == null) { - throw new RuntimeException(e); - } else { - throw rteSupplier.get(e); - } - } - }; - } - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc的Lambda表达式,和一个可以把Exception转化成RuntimeExceptionde的表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression Lambda表达式 - * @param rteSupplier 转化运行时异常的表达式 - * @param

运行时传入的参数类型 - * @return {@link VoidFuncRt} - */ - public static

VoidFuncRt

uncheck(final VoidFunc

expression, final Supplier1 rteSupplier) { - Objects.requireNonNull(expression, "expression can not be null"); - return t -> { - try { - expression.call(t); - } catch (final Exception e) { - if (rteSupplier == null) { - throw new RuntimeException(e); - } else { - throw rteSupplier.get(e); - } - } - }; - } - - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc0的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression Lambda表达式 - * @param rte 期望抛出的运行时异常 - * @return {@link VoidFunc0Rt} - */ - public static VoidFunc0Rt uncheck(final VoidFunc0 expression, final RuntimeException rte) { - Objects.requireNonNull(expression, "expression can not be null"); - return () -> { - try { - expression.call(); - } catch (final Exception e) { - if (rte == null) { - throw new RuntimeException(e); - } else { - rte.initCause(e); - throw rte; - } - } - }; - } - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc0的Lambda表达式,和一个可以把Exception转化成RuntimeExceptionde的表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression Lambda表达式 - * @param rteSupplier 转化运行时异常的表达式 - * @return {@link VoidFunc0Rt} - */ - public static VoidFunc0Rt uncheck(final VoidFunc0 expression, final Supplier1 rteSupplier) { - Objects.requireNonNull(expression, "expression can not be null"); - return () -> { - try { - expression.call(); - } catch (final Exception e) { - if (rteSupplier == null) { - throw new RuntimeException(e); - } else { - throw rteSupplier.get(e); - } - } - }; - } - - /** - * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc1的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常 - * 如此一来,代码中就不用显示的try-catch转化成运行时异常 - * - * @param expression Lambda表达式 - * @param rteSupplier 转化运行时异常的表达式 - * @param

运行时传入的参数类型 - * @return {@link VoidFunc1Rt} - */ - public static

VoidFunc1Rt

uncheck(final VoidFunc1

expression, final Supplier1 rteSupplier) { - Objects.requireNonNull(expression, "expression can not be null"); - return t -> { - try { - expression.call(t); - } catch (final Exception e) { - if (rteSupplier == null) { - throw new RuntimeException(e); - } else { - throw rteSupplier.get(e); - } - } - }; - } - - public interface FuncRt extends Func { - @SuppressWarnings("unchecked") - @Override - R call(P... parameters) throws RuntimeException; - } - - public interface Func0Rt extends Func0 { - @Override - R call() throws RuntimeException; - } - - public interface Func1Rt extends Func1 { - @Override - R call(P parameter) throws RuntimeException; - } - - public interface VoidFuncRt

extends VoidFunc

{ - @SuppressWarnings("unchecked") - @Override - void call(P... parameters) throws RuntimeException; - } - - public interface VoidFunc0Rt extends VoidFunc0 { - @Override - void call() throws RuntimeException; - } - - public interface VoidFunc1Rt

extends VoidFunc1

{ - @Override - void call(P parameter) throws RuntimeException; - } - - -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Opt.java b/hutool-core/src/main/java/cn/hutool/core/lang/Opt.java index 6a349256e..6344601c9 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Opt.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Opt.java @@ -25,7 +25,7 @@ package cn.hutool.core.lang; import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import cn.hutool.core.text.StrUtil; import java.util.Objects; @@ -40,7 +40,7 @@ import java.util.stream.Stream; /** * 复制jdk16中的Optional,以及自己进行了一点调整和新增,比jdk8中的Optional多了几个实用的函数
- * 详细见:https://gitee.com/dromara/hutool/pulls/426 + * 详细见: * * @param 包裹里元素的类型 * @author VampireAchao @@ -116,9 +116,9 @@ public class Opt { * @param 类型 * @return 操作执行后的值 */ - public static Opt ofTry(final Func0 supplier) { + public static Opt ofTry(final SerSupplier supplier) { try { - return Opt.ofNullable(supplier.call()); + return Opt.ofNullable(supplier.getting()); } catch (final Exception e) { final Opt empty = new Opt<>(null); empty.exception = e; @@ -181,7 +181,7 @@ public class Opt { /** * 获取异常
- * 当调用 {@link #ofTry(Func0)}时,异常信息不会抛出,而是保存,调用此方法获取抛出的异常 + * 当调用 {@link #ofTry(SerSupplier)}时,异常信息不会抛出,而是保存,调用此方法获取抛出的异常 * * @return 异常 * @since 5.7.17 @@ -192,7 +192,7 @@ public class Opt { /** * 是否失败
- * 当调用 {@link #ofTry(Func0)}时,抛出异常则表示失败 + * 当调用 {@link #ofTry(SerSupplier)}时,抛出异常则表示失败 * * @return 是否失败 * @since 5.7.17 diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Singleton.java b/hutool-core/src/main/java/cn/hutool/core/lang/Singleton.java index 7bb68d7ac..14f668039 100755 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Singleton.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Singleton.java @@ -1,7 +1,7 @@ package cn.hutool.core.lang; import cn.hutool.core.classloader.ClassLoaderUtil; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import cn.hutool.core.reflect.ConstructorUtil; import cn.hutool.core.text.StrUtil; import cn.hutool.core.util.ArrayUtil; @@ -51,14 +51,14 @@ public final class Singleton { * @since 5.3.3 */ @SuppressWarnings("unchecked") - public static T get(final String key, final Func0 supplier) { + public static T get(final String key, final SerSupplier supplier) { //return (T) POOL.computeIfAbsent(key, (k)-> supplier.callWithRuntimeException()); // issues#2349 // ConcurrentHashMap.computeIfAbsent在某些情况下会导致死循环问题,此处采用Dubbo的解决方案 Object value = POOL.get(key); - if(null == value){ - POOL.putIfAbsent(key, supplier.callWithRuntimeException()); + if (null == value) { + POOL.putIfAbsent(key, supplier.get()); value = POOL.get(key); } return (T) value; @@ -104,12 +104,12 @@ public final class Singleton { /** * 判断某个类的对象是否存在 * - * @param clazz 类 + * @param clazz 类 * @param params 构造参数 * @return 是否存在 */ - public static boolean exists(final Class clazz, final Object... params){ - if (null != clazz){ + public static boolean exists(final Class clazz, final Object... params) { + if (null != clazz) { final String key = buildKey(clazz.getName(), params); return POOL.containsKey(key); } @@ -121,7 +121,7 @@ public final class Singleton { * * @return 非重复的类集合 */ - public static Set> getExistClass(){ + public static Set> getExistClass() { return POOL.values().stream().map(Object::getClass).collect(Collectors.toSet()); } diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/Consumer3.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/Consumer3.java deleted file mode 100644 index b40a2c387..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/Consumer3.java +++ /dev/null @@ -1,23 +0,0 @@ -package cn.hutool.core.lang.func; - -/** - * 3参数Consumer - * - * @param 参数一类型 - * @param 参数二类型 - * @param 参数三类型 - * @author TomXin - * @since 5.7.22 - */ -@FunctionalInterface -public interface Consumer3 { - - /** - * 接收参数方法 - * - * @param p1 参数一 - * @param p2 参数二 - * @param p3 参数三 - */ - void accept(P1 p1, P2 p2, P3 p3); -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/Func.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/Func.java deleted file mode 100644 index 4608059b4..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/Func.java +++ /dev/null @@ -1,43 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.io.Serializable; - -/** - * 函数对象
- * 接口灵感来自于ActFramework
- * 一个函数接口代表一个一个函数,用于包装一个函数为对象
- * 在JDK8之前,Java的函数并不能作为参数传递,也不能作为返回值存在,此接口用于将一个函数包装成为一个对象,从而传递对象 - * - * @author Looly - * - * @param

参数类型 - * @param 返回值类型 - * @since 3.1.0 - */ -@FunctionalInterface -public interface Func extends Serializable { - /** - * 执行函数 - * - * @param parameters 参数列表 - * @return 函数执行结果 - * @throws Exception 自定义异常 - */ - @SuppressWarnings("unchecked") - R call(P... parameters) throws Exception; - - /** - * 执行函数,异常包装为RuntimeException - * - * @param parameters 参数列表 - * @return 函数执行结果 - */ - @SuppressWarnings("unchecked") - default R callWithRuntimeException(final P... parameters){ - try { - return call(parameters); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/Func0.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/Func0.java deleted file mode 100644 index 88ba36bd8..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/Func0.java +++ /dev/null @@ -1,39 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.io.Serializable; - -/** - * 无参数的函数对象
- * 接口灵感来自于ActFramework
- * 一个函数接口代表一个一个函数,用于包装一个函数为对象
- * 在JDK8之前,Java的函数并不能作为参数传递,也不能作为返回值存在,此接口用于将一个函数包装成为一个对象,从而传递对象 - * - * @author Looly - * - * @param 返回值类型 - * @since 4.5.2 - */ -@FunctionalInterface -public interface Func0 extends Serializable { - /** - * 执行函数 - * - * @return 函数执行结果 - * @throws Exception 自定义异常 - */ - R call() throws Exception; - - /** - * 执行函数,异常包装为RuntimeException - * - * @return 函数执行结果 - * @since 5.3.6 - */ - default R callWithRuntimeException(){ - try { - return call(); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/Func1.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/Func1.java deleted file mode 100644 index cbe23e6be..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/Func1.java +++ /dev/null @@ -1,43 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.io.Serializable; - -/** - * 只有一个参数的函数对象
- * 接口灵感来自于ActFramework
- * 一个函数接口代表一个一个函数,用于包装一个函数为对象
- * 在JDK8之前,Java的函数并不能作为参数传递,也不能作为返回值存在,此接口用于将一个函数包装成为一个对象,从而传递对象 - * - * @author Looly - * - * @param

参数类型 - * @param 返回值类型 - * @since 4.2.2 - */ -@FunctionalInterface -public interface Func1 extends Serializable { - - /** - * 执行函数 - * - * @param parameter 参数 - * @return 函数执行结果 - * @throws Exception 自定义异常 - */ - R call(P parameter) throws Exception; - - /** - * 执行函数,异常包装为RuntimeException - * - * @param parameter 参数 - * @return 函数执行结果 - * @since 5.3.6 - */ - default R callWithRuntimeException(final P parameter){ - try { - return call(parameter); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiConsumer.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiConsumer.java new file mode 100644 index 000000000..a12958a7d --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiConsumer.java @@ -0,0 +1,85 @@ +package cn.hutool.core.lang.func; + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.Objects; +import java.util.function.BiConsumer; +import java.util.stream.Stream; + +/** + * SerBiConsumer + * + * @author VampireAchao + */ +@FunctionalInterface +public interface SerBiConsumer extends BiConsumer, Serializable { + /** + * multi + * + * @param consumers lambda + * @param type + * @param return type + * @return lambda + */ + @SafeVarargs + static SerBiConsumer multi(SerBiConsumer... consumers) { + return Stream.of(consumers).reduce(SerBiConsumer::andThen).orElseGet(() -> (o, q) -> {}); + } + + /** + * Performs this operation on the given arguments. + * + * @param t the first input argument + * @param u the second input argument + * @throws Exception wrappered checked exceptions for easy using + */ + @SuppressWarnings("all") + void accepting(T t, U u) throws Exception; + + /** + * Performs this operation on the given arguments. + * + * @param t the first input argument + * @param u the second input argument + */ + @Override + default void accept(T t, U u) { + try { + accepting(t, u); + } catch (Exception e) { + throw new UtilException(e); + } + } + + /** + * Returns a composed {@code SerBiCons} that performs, in sequence, this + * operation followed by the {@code after} operation. If performing either + * operation throws an exception, it is relayed to the caller of the + * composed operation. If performing this operation throws an exception, + * the {@code after} operation will not be performed. + * + * @param after the operation to perform after this operation + * @return a composed {@code SerBiCons} that performs in sequence this + * operation followed by the {@code after} operation + * @throws NullPointerException if {@code after} is null + */ + default SerBiConsumer andThen(SerBiConsumer after) { + Objects.requireNonNull(after); + return (l, r) -> { + accepting(l, r); + after.accepting(l, r); + }; + } + + /** + * 什么也不做,用于一些需要传入lambda的方法占位使用 + * + * @param 参数1类型 + * @param 参数2类型 + * @return 什么也不做 + */ + static SerBiConsumer nothing() { + return (l, r) -> {}; + } +} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiFunction.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiFunction.java new file mode 100644 index 000000000..fc903d90e --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiFunction.java @@ -0,0 +1,63 @@ +package cn.hutool.core.lang.func; + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.Objects; +import java.util.function.BiFunction; + +/** + * SerBiFunction + * + * @author VampireAchao + * @since 2022/6/8 + */ +@FunctionalInterface +public interface SerBiFunction extends BiFunction, Serializable { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @return the function result + * @throws Exception wrappered checked exceptions + */ + @SuppressWarnings("all") + R applying(T t, U u) throws Exception; + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @return the function result + */ + @Override + default R apply(T t, U u) { + try { + return this.applying(t, u); + } catch (Exception e) { + throw new UtilException(e); + } + } + + /** + * Returns a composed function that first applies this function to + * its input, and then applies the {@code after} function to the result. + * If evaluation of either function throws an exception, it is relayed to + * the caller of the composed function. + * + * @param the type of output of the {@code after} function, and of the + * composed function + * @param after the function to apply after this function is applied + * @return a composed function that first applies this function and then + * applies the {@code after} function + * @throws NullPointerException if after is null + */ + default SerBiFunction andThen(SerFunction after) { + Objects.requireNonNull(after); + return (T t, U u) -> after.apply(this.apply(t, u)); + } +} + diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiPredicate.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiPredicate.java new file mode 100644 index 000000000..74f50a0e1 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBiPredicate.java @@ -0,0 +1,103 @@ +package cn.hutool.core.lang.func; + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.Objects; +import java.util.function.BiPredicate; + +/** + * SerBiPred + * + * @author VampireAchao + * @since 2022/6/8 + */ +@FunctionalInterface +public interface SerBiPredicate extends BiPredicate, Serializable { + + + /** + * Evaluates this predicate on the given arguments. + * + * @param t the first input argument + * @param u the second input argument + * @return {@code true} if the input arguments match the predicate, + * otherwise {@code false} + * @throws Exception wrappered checked exceptions + */ + @SuppressWarnings("all") + boolean testing(T t, U u) throws Exception; + + /** + * Evaluates this predicate on the given arguments. + * + * @param t the first input argument + * @param u the second input argument + * @return {@code true} if the input arguments match the predicate, + * otherwise {@code false} + */ + @Override + default boolean test(T t, U u) { + try { + return testing(t, u); + } catch (Exception e) { + throw new UtilException(e); + } + } + + + /** + * Returns a composed predicate that represents a short-circuiting logical + * AND of this predicate and another. When evaluating the composed + * predicate, if this predicate is {@code false}, then the {@code other} + * predicate is not evaluated. + * + *

Any exceptions thrown during evaluation of either predicate are relayed + * to the caller; if evaluation of this predicate throws an exception, the + * {@code other} predicate will not be evaluated. + * + * @param other a predicate that will be logically-ANDed with this + * predicate + * @return a composed predicate that represents the short-circuiting logical + * AND of this predicate and the {@code other} predicate + * @throws NullPointerException if other is null + */ + default SerBiPredicate and(SerBiPredicate other) { + Objects.requireNonNull(other); + return (T t, U u) -> test(t, u) && other.test(t, u); + } + + /** + * Returns a predicate that represents the logical negation of this + * predicate. + * + * @return a predicate that represents the logical negation of this + * predicate + */ + @Override + default SerBiPredicate negate() { + return (T t, U u) -> !test(t, u); + } + + /** + * Returns a composed predicate that represents a short-circuiting logical + * OR of this predicate and another. When evaluating the composed + * predicate, if this predicate is {@code true}, then the {@code other} + * predicate is not evaluated. + * + *

Any exceptions thrown during evaluation of either predicate are relayed + * to the caller; if evaluation of this predicate throws an exception, the + * {@code other} predicate will not be evaluated. + * + * @param other a predicate that will be logically-ORed with this + * predicate + * @return a composed predicate that represents the short-circuiting logical + * OR of this predicate and the {@code other} predicate + * @throws NullPointerException if other is null + */ + default SerBiPredicate or(SerBiPredicate other) { + Objects.requireNonNull(other); + return (T t, U u) -> test(t, u) || other.test(t, u); + } +} + diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBinaryOperator.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBinaryOperator.java new file mode 100644 index 000000000..c8d9e1aa0 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerBinaryOperator.java @@ -0,0 +1,96 @@ +package cn.hutool.core.lang.func; + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.Comparator; +import java.util.Objects; +import java.util.function.BinaryOperator; + +/** + * SerBinaryOperator + * + * @author VampireAchao + * @since 2022/6/8 + */ +@FunctionalInterface +public interface SerBinaryOperator extends BinaryOperator, Serializable { + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @return the function result + * @throws Exception wrappered checked exceptions + */ + @SuppressWarnings("all") + T applying(T t, T u) throws Exception; + + /** + * Applies this function to the given arguments. + * + * @param t the first function argument + * @param u the second function argument + * @return the function result + */ + @Override + default T apply(T t, T u) { + try { + return this.applying(t, u); + } catch (Exception e) { + throw new UtilException(e); + } + } + + /** + * Returns a {@link SerBinaryOperator} which returns the lesser of two elements + * according to the specified {@code Comparator}. + * + * @param the type of the input arguments of the comparator + * @param comparator a {@code Comparator} for comparing the two values + * @return a {@code SerBiUnOp} which returns the lesser of its operands, + * according to the supplied {@code Comparator} + * @throws NullPointerException if the argument is null + */ + static SerBinaryOperator minBy(Comparator comparator) { + Objects.requireNonNull(comparator); + return (a, b) -> comparator.compare(a, b) <= 0 ? a : b; + } + + /** + * Returns a {@link SerBinaryOperator} which returns the greater of two elements + * according to the specified {@code Comparator}. + * + * @param the type of the input arguments of the comparator + * @param comparator a {@code Comparator} for comparing the two values + * @return a {@code SerBiUnOp} which returns the greater of its operands, + * according to the supplied {@code Comparator} + * @throws NullPointerException if the argument is null + */ + static SerBinaryOperator maxBy(Comparator comparator) { + Objects.requireNonNull(comparator); + return (a, b) -> comparator.compare(a, b) >= 0 ? a : b; + } + + /** + * just before + * + * @param type + * @return before + */ + static SerBinaryOperator justBefore() { + return (l, r) -> l; + } + + /** + * just after + * + * @param type + * @return after + */ + static SerBinaryOperator justAfter() { + return (l, r) -> r; + } +} + diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerConsumer.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerConsumer.java new file mode 100644 index 000000000..9a7312143 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerConsumer.java @@ -0,0 +1,83 @@ +package cn.hutool.core.lang.func; + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.Objects; +import java.util.function.Consumer; +import java.util.stream.Stream; + +/** + * 可序列化的Consumer + * + * @author VampireAchao + * @see Consumer + */ +@FunctionalInterface +public interface SerConsumer extends Consumer, Serializable { + + /** + * Performs this operation on the given argument. + * + * @param t the input argument + * @throws Exception wrappered checked exceptions + */ + @SuppressWarnings("all") + void accepting(T t) throws Exception; + + /** + * Performs this operation on the given argument. + * + * @param t the input argument + */ + @Override + default void accept(T t) { + try { + accepting(t); + } catch (Exception e) { + throw new UtilException(e); + } + } + + /** + * multi + * + * @param consumers lambda + * @param type + * @return lambda + */ + @SafeVarargs + static SerConsumer multi(SerConsumer... consumers) { + return Stream.of(consumers).reduce(SerConsumer::andThen).orElseGet(() -> o -> {}); + } + + /** + * Returns a composed {@code Consumer} that performs, in sequence, this + * operation followed by the {@code after} operation. If performing either + * operation throws an exception, it is relayed to the caller of the + * composed operation. If performing this operation throws an exception, + * the {@code after} operation will not be performed. + * + * @param after the operation to perform after this operation + * @return a composed {@code Consumer} that performs in sequence this + * operation followed by the {@code after} operation + * @throws NullPointerException if {@code after} is null + */ + default SerConsumer andThen(SerConsumer after) { + Objects.requireNonNull(after); + return (T t) -> { + accept(t); + after.accept(t); + }; + } + + /** + * nothing + * + * @param type + * @return nothing + */ + static SerConsumer nothing() { + return t -> {}; + } +} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerConsumer3.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerConsumer3.java new file mode 100644 index 000000000..8997dd432 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerConsumer3.java @@ -0,0 +1,65 @@ +package cn.hutool.core.lang.func; + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.Objects; + +/** + * 3参数Consumer + * + * @param 参数一类型 + * @param 参数二类型 + * @param 参数三类型 + * @author TomXin, VampireAchao + * @since 5.7.22 + */ +@FunctionalInterface +public interface SerConsumer3 extends Serializable { + + /** + * 接收参数方法 + * + * @param p1 参数一 + * @param p2 参数二 + * @param p3 参数三 + * @throws Exception wrappered checked exceptions + */ + @SuppressWarnings("all") + void accepting(P1 p1, P2 p2, P3 p3) throws Exception; + + /** + * 接收参数方法 + * + * @param p1 参数一 + * @param p2 参数二 + * @param p3 参数三 + */ + default void accept(P1 p1, P2 p2, P3 p3) { + try { + accepting(p1, p2, p3); + } catch (Exception e) { + throw new UtilException(e); + } + } + + /** + * Returns a composed {@code SerConsumer3} that performs, in sequence, this + * operation followed by the {@code after} operation. If performing either + * operation throws an exception, it is relayed to the caller of the + * composed operation. If performing this operation throws an exception, + * the {@code after} operation will not be performed. + * + * @param after the operation to perform after this operation + * @return a composed {@code SerConsumer3} that performs in sequence this + * operation followed by the {@code after} operation + * @throws NullPointerException if {@code after} is null + */ + default SerConsumer3 andThen(SerConsumer3 after) { + Objects.requireNonNull(after); + return (P1 p1, P2 p2, P3 p3) -> { + accept(p1, p2, p3); + after.accept(p1, p2, p3); + }; + } +} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerFunction.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerFunction.java new file mode 100644 index 000000000..5a0f98f3d --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerFunction.java @@ -0,0 +1,63 @@ +package cn.hutool.core.lang.func; + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.function.Function; + +/** + * 可序列化的Function + * + * @author VampireAchao + * @see Function + */ +@FunctionalInterface +public interface SerFunction extends Function, Serializable { + + /** + * Applies this function to the given argument. + * + * @param t the function argument + * @return the function result + * @throws Exception wrappered checked exceptions + */ + @SuppressWarnings("all") + R applying(T t) throws Exception; + + /** + * Applies this function to the given argument. + * + * @param t the function argument + * @return the function result + */ + @Override + default R apply(T t) { + try { + return applying(t); + } catch (Exception e) { + throw new UtilException(e); + } + } + + /** + * Returns a function that always returns its input argument. + * + * @param the type of the input and output objects to the function + * @return a function that always returns its input argument + */ + static SerFunction identity() { + return t -> t; + } + + /** + * casting identity + * + * @param param type + * @param result type + * @return identity after casting + */ + @SuppressWarnings("unchecked") + static Function castingIdentity() { + return t -> (R) t; + } +} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerPredicate.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerPredicate.java new file mode 100644 index 000000000..11479caff --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerPredicate.java @@ -0,0 +1,139 @@ +package cn.hutool.core.lang.func; + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.Objects; +import java.util.function.Predicate; +import java.util.stream.Stream; + +/** + * 可序列化的Predicate + * + * @author VampireAchao + * @see Predicate + */ +@FunctionalInterface +public interface SerPredicate extends Predicate, Serializable { + + /** + * Evaluates this predicate on the given argument. + * + * @param t the input argument + * @return {@code true} if the input argument matches the predicate, + * otherwise {@code false} + * @throws Exception wrappered checked exceptions + */ + boolean testing(T t) throws Exception; + + /** + * Evaluates this predicate on the given argument. + * + * @param t the input argument + * @return {@code true} if the input argument matches the predicate, + * otherwise {@code false} + */ + @Override + default boolean test(T t) { + try { + return testing(t); + } catch (Exception e) { + throw new UtilException(e); + } + } + + /** + * multi + * + * @param predicates lambda + * @param 类型 + * @return lambda + */ + @SafeVarargs + static SerPredicate multiAnd(SerPredicate... predicates) { + return Stream.of(predicates).reduce(SerPredicate::and).orElseGet(() -> o -> true); + } + + /** + * multi + * + * @param predicates lambda + * @param 类型 + * @return lambda + */ + @SafeVarargs + static SerPredicate multiOr(SerPredicate... predicates) { + return Stream.of(predicates).reduce(SerPredicate::or).orElseGet(() -> o -> false); + } + + /** + * Returns a predicate that tests if two arguments are equal according + * to {@link Objects#equals(Object, Object)}. + * + * @param the type of arguments to the predicate + * @param targetRef the object reference with which to compare for equality, + * which may be {@code null} + * @return a predicate that tests if two arguments are equal according + * to {@link Objects#equals(Object, Object)} + */ + static SerPredicate isEqual(Object... targetRef) { + return (null == targetRef) + ? Objects::isNull + : object -> Stream.of(targetRef).allMatch(target -> target.equals(object)); + } + + /** + * Returns a composed predicate that represents a short-circuiting logical + * AND of this predicate and another. When evaluating the composed + * predicate, if this predicate is {@code false}, then the {@code other} + * predicate is not evaluated. + * + *

Any exceptions thrown during evaluation of either predicate are relayed + * to the caller; if evaluation of this predicate throws an exception, the + * {@code other} predicate will not be evaluated. + * + * @param other a predicate that will be logically-ANDed with this + * predicate + * @return a composed predicate that represents the short-circuiting logical + * AND of this predicate and the {@code other} predicate + * @throws NullPointerException if other is null + */ + default SerPredicate and(SerPredicate other) { + Objects.requireNonNull(other); + return t -> test(t) && other.test(t); + } + + /** + * Returns a predicate that represents the logical negation of this + * predicate. + * + * @return a predicate that represents the logical negation of this + * predicate + */ + @Override + default SerPredicate negate() { + return t -> !test(t); + } + + /** + * Returns a composed predicate that represents a short-circuiting logical + * OR of this predicate and another. When evaluating the composed + * predicate, if this predicate is {@code true}, then the {@code other} + * predicate is not evaluated. + * + *

Any exceptions thrown during evaluation of either predicate are relayed + * to the caller; if evaluation of this predicate throws an exception, the + * {@code other} predicate will not be evaluated. + * + * @param other a predicate that will be logically-ORed with this + * predicate + * @return a composed predicate that represents the short-circuiting logical + * OR of this predicate and the {@code other} predicate + * @throws NullPointerException if other is null + */ + default SerPredicate or(SerPredicate other) { + Objects.requireNonNull(other); + return t -> test(t) || other.test(t); + } + +} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerRunnable.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerRunnable.java new file mode 100644 index 000000000..17c8776c4 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerRunnable.java @@ -0,0 +1,63 @@ +package cn.hutool.core.lang.func; + + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.stream.Stream; + +/** + * 可序列化的Runnable + * + * @author VampireAchao + * @see Runnable + */ +@FunctionalInterface +public interface SerRunnable extends Runnable, Serializable { + + /** + * When an object implementing interface Runnable is used + * to create a thread, starting the thread causes the object's + * run method to be called in that separately executing + * thread. + *

+ * The general contract of the method run is that it may + * take any action whatsoever. + * + * @throws Exception wrappered checked exceptions + * @see Thread#run() + */ + @SuppressWarnings("all") + void running() throws Exception; + + /** + * When an object implementing interface Runnable is used + * to create a thread, starting the thread causes the object's + * run method to be called in that separately executing + * thread. + *

+ * The general contract of the method run is that it may + * take any action whatsoever. + * + * @see Thread#run() + */ + @Override + default void run() { + try { + running(); + } catch (Exception e) { + throw new UtilException(e); + } + } + + /** + * multi + * + * @param serRunnableArray lambda + * @return lambda + */ + static SerRunnable multi(SerRunnable... serRunnableArray) { + return () -> Stream.of(serRunnableArray).forEach(SerRunnable::run); + } + +} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerSupplier.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerSupplier.java new file mode 100644 index 000000000..09005e7a1 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerSupplier.java @@ -0,0 +1,53 @@ +package cn.hutool.core.lang.func; + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.function.Supplier; +import java.util.stream.Stream; + +/** + * 可序列化的Supplier + * + * @author VampireAchao + * @see Supplier + */ +@FunctionalInterface +public interface SerSupplier extends Supplier, Serializable { + + /** + * Gets a result. + * + * @return a result + * @throws Exception wrappered checked exceptions + */ + @SuppressWarnings("all") + T getting() throws Exception; + + /** + * Gets a result. + * + * @return a result + */ + @Override + default T get() { + try { + return getting(); + } catch (Exception e) { + throw new UtilException(e); + } + } + + /** + * last + * + * @param serSups lambda + * @param type + * @return lambda + */ + @SafeVarargs + static SerSupplier last(SerSupplier... serSups) { + return Stream.of(serSups).reduce((l, r) -> r).orElseGet(() -> () -> null); + } + +} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/SerUnaryOperator.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerUnaryOperator.java new file mode 100644 index 000000000..2b3c27ecd --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/SerUnaryOperator.java @@ -0,0 +1,68 @@ +package cn.hutool.core.lang.func; + +import cn.hutool.core.exceptions.UtilException; + +import java.io.Serializable; +import java.util.function.Function; +import java.util.function.UnaryOperator; + +/** + * 可序列化的UnaryOperator + * + * @author VampireAchao + * @see UnaryOperator + */ +@FunctionalInterface +public interface SerUnaryOperator extends UnaryOperator, Serializable { + + /** + * Applies this function to the given argument. + * + * @param t the function argument + * @return the function result + * @throws Exception wrappered checked exceptions + */ + @SuppressWarnings("all") + T applying(T t) throws Exception; + + /** + * Applies this function to the given argument. + * + * @param t the function argument + * @return the function result + */ + @Override + default T apply(T t) { + try { + return applying(t); + } catch (Exception e) { + throw new UtilException(e); + } + } + + /** + * Returns a unary operator that always returns its input argument. + * + * @param the type of the input and output of the operator + * @return a unary operator that always returns its input argument + */ + static SerUnaryOperator identity() { + return t -> t; + } + + + /** + * casting identity + * + * @param function source function + * @param param type + * @param result type + * @param lambda type + * @return identity after casting + */ + @SuppressWarnings("unchecked") + static > SerUnaryOperator casting(F function) { + return t -> (T) function.apply(t); + } + +} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier1.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier1.java deleted file mode 100755 index 90309d151..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier1.java +++ /dev/null @@ -1,32 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.util.function.Supplier; - -/** - * 1参数Supplier - * - * @param 目标 类型 - * @param 参数一 类型 - * @author TomXin - * @since 5.7.21 - */ -@FunctionalInterface -public interface Supplier1 { - /** - * 生成实例的方法 - * - * @param p1 参数一 - * @return 目标对象 - */ - T get(P1 p1); - - /** - * 将带有参数的Supplier转换为无参{@link Supplier} - * - * @param p1 参数1 - * @return {@link Supplier} - */ - default Supplier toSupplier(final P1 p1) { - return () -> get(p1); - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier2.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier2.java deleted file mode 100755 index 077c489f0..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier2.java +++ /dev/null @@ -1,36 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.util.function.Supplier; - -/** - * 两个参数的Supplier - * - * @param 目标 类型 - * @param 参数一 类型 - * @param 参数二 类型 - * @author TomXin - * @since 5.7.21 - */ -@FunctionalInterface -public interface Supplier2 { - - /** - * 生成实例的方法 - * - * @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 toSupplier(final P1 p1, final P2 p2) { - return () -> get(p1, p2); - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier3.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier3.java deleted file mode 100755 index 94f2af923..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier3.java +++ /dev/null @@ -1,39 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.util.function.Supplier; - -/** - * 3参数Supplier - * - * @param 目标类型 - * @param 参数一类型 - * @param 参数二类型 - * @param 参数三类型 - * @author TomXin - * @since 5.7.21 - */ -@FunctionalInterface -public interface Supplier3 { - - /** - * 生成实例的方法 - * - * @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 toSupplier(final P1 p1, final P2 p2, final P3 p3) { - return () -> get(p1, p2, p3); - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier4.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier4.java deleted file mode 100755 index 06a793c27..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier4.java +++ /dev/null @@ -1,42 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.util.function.Supplier; - -/** - * 4参数Supplier - * - * @param 目标 类型 - * @param 参数一 类型 - * @param 参数二 类型 - * @param 参数三 类型 - * @param 参数四 类型 - * @author TomXin - * @since 5.7.21 - */ -@FunctionalInterface -public interface Supplier4 { - - /** - * 生成实例的方法 - * - * @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 toSupplier(final P1 p1, final P2 p2, final P3 p3, final P4 p4) { - return () -> get(p1, p2, p3, p4); - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier5.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier5.java deleted file mode 100755 index 9eed33622..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/Supplier5.java +++ /dev/null @@ -1,45 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.util.function.Supplier; - -/** - * 5参数Supplier - * - * @param 目标 类型 - * @param 参数一 类型 - * @param 参数二 类型 - * @param 参数三 类型 - * @param 参数四 类型 - * @param 参数五 类型 - * @author TomXin - * @since 5.7.21 - */ -@FunctionalInterface -public interface Supplier5 { - - /** - * 生成实例的方法 - * - * @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 toSupplier(final P1 p1, final P2 p2, final P3 p3, final P4 p4, final P5 p5) { - return () -> get(p1, p2, p3, p4, p5); - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc.java deleted file mode 100644 index 57b3e3fdd..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc.java +++ /dev/null @@ -1,41 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.io.Serializable; - -/** - * 函数对象
- * 接口灵感来自于ActFramework
- * 一个函数接口代表一个一个函数,用于包装一个函数为对象
- * 在JDK8之前,Java的函数并不能作为参数传递,也不能作为返回值存在,此接口用于将一个函数包装成为一个对象,从而传递对象 - * - * @author Looly - * - * @param

参数类型 - * @since 3.1.0 - */ -@FunctionalInterface -public interface VoidFunc

extends Serializable { - - /** - * 执行函数 - * - * @param parameters 参数列表 - * @throws Exception 自定义异常 - */ - @SuppressWarnings("unchecked") - void call(P... parameters) throws Exception; - - /** - * 执行函数,异常包装为RuntimeException - * - * @param parameters 参数列表 - */ - @SuppressWarnings("unchecked") - default void callWithRuntimeException(final P... parameters){ - try { - call(parameters); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc0.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc0.java deleted file mode 100644 index f0577f644..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc0.java +++ /dev/null @@ -1,37 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.io.Serializable; - -/** - * 函数对象
- * 接口灵感来自于ActFramework
- * 一个函数接口代表一个一个函数,用于包装一个函数为对象
- * 在JDK8之前,Java的函数并不能作为参数传递,也不能作为返回值存在,此接口用于将一个函数包装成为一个对象,从而传递对象 - * - * @author Looly - * - * @since 3.2.3 - */ -@FunctionalInterface -public interface VoidFunc0 extends Serializable { - - /** - * 执行函数 - * - * @throws Exception 自定义异常 - */ - void call() throws Exception; - - /** - * 执行函数,异常包装为RuntimeException - * - * @since 5.3.6 - */ - default void callWithRuntimeException(){ - try { - call(); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc1.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc1.java deleted file mode 100644 index 8c66b167f..000000000 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/VoidFunc1.java +++ /dev/null @@ -1,39 +0,0 @@ -package cn.hutool.core.lang.func; - -import java.io.Serializable; - -/** - * 函数对象
- * 接口灵感来自于ActFramework
- * 一个函数接口代表一个一个函数,用于包装一个函数为对象
- * 在JDK8之前,Java的函数并不能作为参数传递,也不能作为返回值存在,此接口用于将一个函数包装成为一个对象,从而传递对象 - * - * @author Looly - * - * @since 3.2.3 - */ -@FunctionalInterface -public interface VoidFunc1

extends Serializable { - - /** - * 执行函数 - * - * @param parameter 参数 - * @throws Exception 自定义异常 - */ - void call(P parameter) throws Exception; - - /** - * 执行函数,异常包装为RuntimeException - * - * @param parameter 参数 - * @since 5.3.6 - */ - default void callWithRuntimeException(final P parameter){ - try { - call(parameter); - } catch (final Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/hutool-core/src/main/java/cn/hutool/core/map/Dict.java b/hutool-core/src/main/java/cn/hutool/core/map/Dict.java index 6ecd496dd..3df4a2153 100755 --- a/hutool-core/src/main/java/cn/hutool/core/map/Dict.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/Dict.java @@ -7,7 +7,7 @@ import cn.hutool.core.collection.SetUtil; import cn.hutool.core.convert.Convert; import cn.hutool.core.exceptions.CloneRuntimeException; import cn.hutool.core.lang.Assert; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import cn.hutool.core.lang.func.LambdaUtil; import cn.hutool.core.lang.getter.BasicTypeGetter; @@ -581,7 +581,7 @@ public class Dict extends CustomKeyMap implements BasicTypeGette @Override protected String customKey(Object key) { if (this.caseInsensitive && null != key) { - key = ((String)key).toLowerCase(); + key = ((String) key).toLowerCase(); } return (String) key; } @@ -598,8 +598,8 @@ public class Dict extends CustomKeyMap implements BasicTypeGette * @return this * @since 5.7.23 */ - public Dict setFields(final Func0... fields) { - Arrays.stream(fields).forEach(f -> set(LambdaUtil.getFieldName(f), f.callWithRuntimeException())); + public Dict setFields(final SerSupplier... fields) { + Arrays.stream(fields).forEach(f -> set(LambdaUtil.getFieldName(f), f.get())); return this; } } diff --git a/hutool-core/src/main/java/cn/hutool/core/map/multi/CollectionValueMap.java b/hutool-core/src/main/java/cn/hutool/core/map/multi/CollectionValueMap.java index e798c7602..050d7b436 100644 --- a/hutool-core/src/main/java/cn/hutool/core/map/multi/CollectionValueMap.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/multi/CollectionValueMap.java @@ -1,6 +1,6 @@ package cn.hutool.core.map.multi; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import java.util.ArrayList; import java.util.Collection; @@ -22,17 +22,17 @@ public class CollectionValueMap extends AbsCollValueMap { private static final long serialVersionUID = 9012989578038102983L; - private final Func0> collFactory; + private final SerSupplier> collFactory; // ------------------------------------------------------------------------- Constructor start /** * 创建一个多值映射集合,基于{@code mapFactory}与{@code collFactory}实现 * - * @param mapFactory 生成集合的工厂方法 + * @param mapFactory 生成集合的工厂方法 * @param collFactory 生成值集合的工厂方法 */ - public CollectionValueMap(Supplier>> mapFactory, Func0> collFactory) { + public CollectionValueMap(Supplier>> mapFactory, SerSupplier> collFactory) { super(mapFactory); this.collFactory = collFactory; } @@ -42,7 +42,7 @@ public class CollectionValueMap extends AbsCollValueMap { * * @param collFactory 生成值集合的工厂方法 */ - public CollectionValueMap(Func0> collFactory) { + public CollectionValueMap(SerSupplier> collFactory) { this.collFactory = collFactory; } @@ -67,6 +67,6 @@ public class CollectionValueMap extends AbsCollValueMap { @Override protected Collection createCollection() { - return collFactory.callWithRuntimeException(); + return collFactory.get(); } } diff --git a/hutool-core/src/main/java/cn/hutool/core/map/multi/Table.java b/hutool-core/src/main/java/cn/hutool/core/map/multi/Table.java index 384e4db2c..6cdf535a3 100755 --- a/hutool-core/src/main/java/cn/hutool/core/map/multi/Table.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/multi/Table.java @@ -2,7 +2,7 @@ package cn.hutool.core.map.multi; import cn.hutool.core.collection.ListUtil; import cn.hutool.core.lang.Opt; -import cn.hutool.core.lang.func.Consumer3; +import cn.hutool.core.lang.func.SerConsumer3; import cn.hutool.core.map.MapUtil; import java.util.ArrayList; @@ -243,7 +243,7 @@ public interface Table extends Iterable> { * * @param consumer 单元格值处理器 */ - default void forEach(final Consumer3 consumer) { + default void forEach(final SerConsumer3 consumer) { for (final Cell cell : this) { consumer.accept(cell.getRowKey(), cell.getColumnKey(), cell.getValue()); } diff --git a/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java b/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java index 912e34fb5..e7c242c7c 100755 --- a/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/regex/ReUtil.java @@ -2,10 +2,9 @@ package cn.hutool.core.regex; import cn.hutool.core.collection.SetUtil; import cn.hutool.core.convert.Convert; -import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Validator; -import cn.hutool.core.lang.func.Func1; +import cn.hutool.core.lang.func.SerFunction; import cn.hutool.core.lang.mutable.Mutable; import cn.hutool.core.lang.mutable.MutableObj; import cn.hutool.core.map.MapUtil; @@ -216,7 +215,7 @@ public class ReUtil { * @param pattern 编译后的正则模式 * @param content 被匹配的内容 * @param withGroup0 是否包括分组0,此分组表示全匹配的信息 - * @param findAll 是否查找所有匹配到的内容,{@code false}表示只读取第一个匹配到的内容 + * @param findAll 是否查找所有匹配到的内容,{@code false}表示只读取第一个匹配到的内容 * @return 匹配后得到的字符串数组,按照分组顺序依次列出,未匹配到返回空列表,任何一个参数为null返回null * @since 4.0.13 */ @@ -234,7 +233,7 @@ public class ReUtil { result.add(matcher.group(i)); } - if(false == findAll){ + if (false == findAll) { break; } } @@ -911,7 +910,7 @@ public class ReUtil { * @return 替换后的文本 * @since 4.2.2 */ - public static String replaceAll(final CharSequence str, final String regex, final Func1 replaceFun) { + public static String replaceAll(final CharSequence str, final String regex, final SerFunction replaceFun) { return replaceAll(str, Pattern.compile(regex), replaceFun); } @@ -930,7 +929,7 @@ public class ReUtil { * @return 替换后的字符串 * @since 4.2.2 */ - public static String replaceAll(final CharSequence str, final Pattern pattern, final Func1 replaceFun) { + public static String replaceAll(final CharSequence str, final Pattern pattern, final SerFunction replaceFun) { if (StrUtil.isEmpty(str)) { return StrUtil.str(str); } @@ -938,11 +937,7 @@ public class ReUtil { final Matcher matcher = pattern.matcher(str); final StringBuffer buffer = new StringBuffer(); while (matcher.find()) { - try { - matcher.appendReplacement(buffer, replaceFun.call(matcher)); - } catch (final Exception e) { - throw new UtilException(e); - } + matcher.appendReplacement(buffer, replaceFun.apply(matcher)); } matcher.appendTail(buffer); return buffer.toString(); diff --git a/hutool-core/src/main/java/cn/hutool/core/stream/EntryStream.java b/hutool-core/src/main/java/cn/hutool/core/stream/EntryStream.java index d97e74a3f..59ff4cb97 100644 --- a/hutool-core/src/main/java/cn/hutool/core/stream/EntryStream.java +++ b/hutool-core/src/main/java/cn/hutool/core/stream/EntryStream.java @@ -27,11 +27,13 @@ public class EntryStream extends StreamWrapper, EntryStrea * 根据键与值的集合创建键值对流,若两集合在相同下标的位置找不到对应的键或值,则使用{@code null}填充。
* 比如: {@code [1, 2, 3]}与{@code [1, 2]}合并,则得到{@code [{1=1}, {2=2}, {3=null}]}。 * + * @param 键类型 + * @param 值类型 * @param keys 键集合 * @param values 值集合 * @return {@link EntryStream}实例 */ - public static EntryStream merge(Iterable keys, Iterable values) { + public static EntryStream merge(Iterable keys, Iterable values) { final boolean hasKeys = ObjUtil.isNotNull(keys); final boolean hasValues = ObjUtil.isNotNull(values); // 皆为空 @@ -47,13 +49,13 @@ public class EntryStream extends StreamWrapper, EntryStrea return of(values, v -> null, Function.identity()); } // 皆不为空 - final List> entries = new ArrayList<>(); - final Iterator keyItr = keys.iterator(); - final Iterator valueItr = values.iterator(); + final List> entries = new ArrayList<>(); + final Iterator keyItr = keys.iterator(); + final Iterator valueItr = values.iterator(); while (keyItr.hasNext() || valueItr.hasNext()) { entries.add(new Entry<>( - keyItr.hasNext() ? keyItr.next() : null, - valueItr.hasNext() ? valueItr.next() : null + keyItr.hasNext() ? keyItr.next() : null, + valueItr.hasNext() ? valueItr.next() : null )); } return of(entries); @@ -64,13 +66,13 @@ public class EntryStream extends StreamWrapper, EntryStrea * 对流的操作不会影响到入参的{@code map}实例本身 * * @param map 集合 - * @param 键类型 - * @param 值类型 + * @param 键类型 + * @param 值类型 * @return {@link EntryStream}实例 */ - public static EntryStream of(Map map) { + public static EntryStream of(Map map) { return ObjUtil.isNull(map) ? - empty() : of(map.entrySet()); + empty() : of(map.entrySet()); } /** @@ -79,13 +81,13 @@ public class EntryStream extends StreamWrapper, EntryStrea * 若输入流中存在元素为{@code null},则会映射为一个键值皆为{@code null}的键值对。 * * @param entries {@link Iterable}实例 - * @param 键类型 - * @param 值类型 + * @param 键类型 + * @param 值类型 * @return {@link EntryStream}实例 */ - public static EntryStream of(Iterable> entries) { + public static EntryStream of(Iterable> entries) { return ObjUtil.isNull(entries) ? - empty() : of(StreamSupport.stream(entries.spliterator(), false)); + empty() : of(StreamSupport.stream(entries.spliterator(), false)); } /** @@ -94,19 +96,20 @@ public class EntryStream extends StreamWrapper, EntryStrea * @param source 原始集合 * @param keyMapper 键的映射方法 * @param valueMapper 值的映射方法 - * @param 键类型 - * @param 值类型 + * @param 元素类型 + * @param 键类型 + * @param 值类型 * @return {@link EntryStream}实例 */ - public static EntryStream of( - Iterable source, Function keyMapper, Function valueMapper) { + public static EntryStream of( + Iterable source, Function keyMapper, Function valueMapper) { Objects.requireNonNull(keyMapper); Objects.requireNonNull(valueMapper); if (ObjUtil.isNull(source)) { return empty(); } - final Stream> stream = StreamSupport.stream(source.spliterator(), false) - .map(t -> new Entry<>(keyMapper.apply(t), valueMapper.apply(t))); + final Stream> stream = StreamSupport.stream(source.spliterator(), false) + .map(t -> new Entry<>(keyMapper.apply(t), valueMapper.apply(t))); return new EntryStream<>(stream); } @@ -115,23 +118,23 @@ public class EntryStream extends StreamWrapper, EntryStrea * 若输入流中存在元素为{@code null},则会映射为一个键值皆为{@code null}的键值对。 * * @param stream 流 - * @param 键类型 - * @param 值类型 + * @param 键类型 + * @param 值类型 * @return {@link EntryStream}实例 */ - public static EntryStream of(Stream> stream) { + public static EntryStream of(Stream> stream) { return ObjUtil.isNull(stream) ? - empty() : new EntryStream<>(stream.map(Entry::new)); + empty() : new EntryStream<>(stream.map(Entry::new)); } /** * 创建一个空的串行流 * - * @param 键类型 - * @param 值类型 + * @param 键类型 + * @param 值类型 * @return {@link EntryStream}实例 */ - public static EntryStream empty() { + public static EntryStream empty() { return new EntryStream<>(Stream.empty()); } @@ -336,7 +339,7 @@ public class EntryStream extends StreamWrapper, EntryStrea public EntryStream mapKeys(Function mapper) { Objects.requireNonNull(mapper); return new EntryStream<>( - stream.map(e -> new Entry<>(mapper.apply(e.getKey()), e.getValue())) + stream.map(e -> new Entry<>(mapper.apply(e.getKey()), e.getValue())) ); } @@ -350,7 +353,7 @@ public class EntryStream extends StreamWrapper, EntryStrea public EntryStream mapValues(Function mapper) { Objects.requireNonNull(mapper); return new EntryStream<>( - stream.map(e -> new Entry<>(e.getKey(), mapper.apply(e.getValue()))) + stream.map(e -> new Entry<>(e.getKey(), mapper.apply(e.getValue()))) ); } @@ -415,10 +418,10 @@ public class EntryStream extends StreamWrapper, EntryStrea public EntryStream flatMapKey(Function> keyMapper) { Objects.requireNonNull(keyMapper); return new EntryStream<>( - stream.flatMap(e -> keyMapper - .apply(e.getKey()) - .map(newKey -> new Entry<>(newKey, e.getValue())) - ) + stream.flatMap(e -> keyMapper + .apply(e.getKey()) + .map(newKey -> new Entry<>(newKey, e.getValue())) + ) ); } @@ -439,10 +442,10 @@ public class EntryStream extends StreamWrapper, EntryStrea public EntryStream flatMapValue(Function> valueMapper) { Objects.requireNonNull(valueMapper); return new EntryStream<>( - stream.flatMap(e -> valueMapper - .apply(e.getValue()) - .map(newVal -> new Entry<>(e.getKey(), newVal)) - ) + stream.flatMap(e -> valueMapper + .apply(e.getValue()) + .map(newVal -> new Entry<>(e.getKey(), newVal)) + ) ); } @@ -478,8 +481,8 @@ public class EntryStream extends StreamWrapper, EntryStrea * 转为{@link HashMap}集合 * * @return 集合 - * @see Collectors#toMap(Function, Function) * @throws IllegalArgumentException 当键重复时抛出 + * @see Collectors#toMap(Function, Function) */ public Map toMap() { return super.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); @@ -496,14 +499,14 @@ public class EntryStream extends StreamWrapper, EntryStrea * @see Collectors#groupingBy(Function, Supplier, Collector) */ public Table toTable( - BiFunction rowKeyMapper, Supplier> colMapFactory, BinaryOperator operator) { + BiFunction rowKeyMapper, Supplier> colMapFactory, BinaryOperator operator) { Objects.requireNonNull(rowKeyMapper); Objects.requireNonNull(colMapFactory); Objects.requireNonNull(operator); final Map> rawMap = collect(Collectors.groupingBy( - e -> rowKeyMapper.apply(e.getKey(), e.getValue()), - HashMap::new, - Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, operator, colMapFactory) + e -> rowKeyMapper.apply(e.getKey(), e.getValue()), + HashMap::new, + Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, operator, colMapFactory) )); return new RowKeyTable<>(rawMap, colMapFactory::get); } @@ -530,7 +533,7 @@ public class EntryStream extends StreamWrapper, EntryStrea * @return 集合 */ public Table toTableByKey( - Function rowKeyMapper, Supplier> colMapFactory, BinaryOperator operator) { + Function rowKeyMapper, Supplier> colMapFactory, BinaryOperator operator) { return toTable((k, v) -> rowKeyMapper.apply(k), colMapFactory, operator); } @@ -556,7 +559,7 @@ public class EntryStream extends StreamWrapper, EntryStrea * @return 集合 */ public Table toTableByValue( - Function rowKeyMapper, Supplier> colMapFactory, BinaryOperator operator) { + Function rowKeyMapper, Supplier> colMapFactory, BinaryOperator operator) { return toTable((k, v) -> rowKeyMapper.apply(v), colMapFactory, operator); } @@ -589,7 +592,7 @@ public class EntryStream extends StreamWrapper, EntryStrea * @return 集合 */ public > Map groupByKey(Collector collector) { - return groupByKey((Supplier>)HashMap::new, collector); + return groupByKey((Supplier>) HashMap::new, collector); } /** @@ -597,14 +600,14 @@ public class EntryStream extends StreamWrapper, EntryStrea * * @param mapFactory 创建map集合的工厂方法 * @param collector 对具有相同键的值的收集器 - * @param 值集合的类型 - * @param 返回的map集合类型 + * @param 值集合的类型 + * @param 返回的map集合类型 * @return 集合 */ public , M extends Map> M groupByKey(Supplier mapFactory, Collector collector) { return super.collect(Collectors.groupingBy( - Map.Entry::getKey, mapFactory, - CollectorUtil.transform(ArrayList::new, s -> s.stream().map(Map.Entry::getValue).collect(collector)) + Map.Entry::getKey, mapFactory, + CollectorUtil.transform(ArrayList::new, s -> s.stream().map(Map.Entry::getValue).collect(collector)) )); } @@ -625,7 +628,7 @@ public class EntryStream extends StreamWrapper, EntryStrea */ public EntryStream inverse() { return new EntryStream<>( - stream.map(e -> new Entry<>(e.getValue(), e.getKey())) + stream.map(e -> new Entry<>(e.getValue(), e.getKey())) ); } @@ -768,7 +771,7 @@ public class EntryStream extends StreamWrapper, EntryStrea * key重复时直接抛出异常 */ private static BinaryOperator throwingMerger() { - return (u,v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); }; + return (u, v) -> {throw new IllegalStateException(String.format("Duplicate key %s", u));}; } } diff --git a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java index e745820f5..373adc478 100755 --- a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java @@ -4,7 +4,7 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.comparator.VersionComparator; import cn.hutool.core.convert.Convert; import cn.hutool.core.lang.Assert; -import cn.hutool.core.lang.func.Func1; +import cn.hutool.core.lang.func.SerFunction; import cn.hutool.core.math.NumberUtil; import cn.hutool.core.regex.ReUtil; import cn.hutool.core.text.finder.CharFinder; @@ -3430,10 +3430,10 @@ public class CharSequenceUtil extends StrChecker { * @param pattern 用于匹配的正则式 * @param replaceFun 决定如何替换的函数 * @return 替换后的字符串 - * @see ReUtil#replaceAll(CharSequence, java.util.regex.Pattern, Func1) + * @see ReUtil#replaceAll(CharSequence, java.util.regex.Pattern, SerFunction) * @since 4.2.2 */ - public static String replace(final CharSequence str, final java.util.regex.Pattern pattern, final Func1 replaceFun) { + public static String replace(final CharSequence str, final java.util.regex.Pattern pattern, final SerFunction replaceFun) { return ReUtil.replaceAll(str, pattern, replaceFun); } @@ -3444,10 +3444,10 @@ public class CharSequenceUtil extends StrChecker { * @param regex 用于匹配的正则式 * @param replaceFun 决定如何替换的函数 * @return 替换后的字符串 - * @see ReUtil#replaceAll(CharSequence, String, Func1) + * @see ReUtil#replaceAll(CharSequence, String, SerFunction) * @since 4.2.2 */ - public static String replace(final CharSequence str, final String regex, final Func1 replaceFun) { + public static String replace(final CharSequence str, final String regex, final SerFunction replaceFun) { return ReUtil.replaceAll(str, regex, replaceFun); } diff --git a/hutool-core/src/main/java/cn/hutool/core/util/EnumUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/EnumUtil.java index faa86d349..220fd4762 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/EnumUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/EnumUtil.java @@ -1,8 +1,8 @@ package cn.hutool.core.util; import cn.hutool.core.lang.Assert; -import cn.hutool.core.lang.func.Func1; import cn.hutool.core.lang.func.LambdaUtil; +import cn.hutool.core.lang.func.SerFunction; import cn.hutool.core.map.MapUtil; import cn.hutool.core.reflect.FieldUtil; import cn.hutool.core.text.StrUtil; @@ -240,12 +240,12 @@ public class EnumUtil { * @param 字段类型 * @return 对应枚举 ,获取不到时为 {@code null} */ - public static , C> E getBy(final Func1 condition, final C value) { + public static , C> E getBy(final SerFunction condition, final C value) { Class implClass = LambdaUtil.getRealClass(condition); if (Enum.class.equals(implClass)) { implClass = LambdaUtil.getRealClass(condition); } - return Arrays.stream(implClass.getEnumConstants()).filter(e -> condition.callWithRuntimeException(e).equals(value)).findAny().orElse(null); + return Arrays.stream(implClass.getEnumConstants()).filter(e -> condition.apply(e).equals(value)).findAny().orElse(null); } /** @@ -260,7 +260,7 @@ public class EnumUtil { * @return 对应枚举中另一字段值 ,获取不到时为 {@code null} * @since 5.8.0 */ - public static , F, C> F getFieldBy(final Func1 field, + public static , F, C> F getFieldBy(final SerFunction field, final Function condition, final C value) { Class implClass = LambdaUtil.getRealClass(field); if (Enum.class.equals(implClass)) { @@ -270,7 +270,7 @@ public class EnumUtil { // 过滤 .filter(e -> condition.apply(e).equals(value)) // 获取第一个并转换为结果 - .findFirst().map(field::callWithRuntimeException).orElse(null); + .findFirst().map(field).orElse(null); } /** diff --git a/hutool-core/src/test/java/cn/hutool/core/builder/GenericBuilderTest.java b/hutool-core/src/test/java/cn/hutool/core/builder/GenericBuilderTest.java index a05c34a71..f4240f095 100644 --- a/hutool-core/src/test/java/cn/hutool/core/builder/GenericBuilderTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/builder/GenericBuilderTest.java @@ -52,7 +52,7 @@ public class GenericBuilderTest { // 多参数构造 final Box box1 = GenericBuilder - .of(Box::new, 2048L, "Hello Partner!", 222, 333, 444) + .of(() -> new Box(2048L, "Hello Partner!", 222, 333, 444)) .with(Box::alis) .build(); @@ -65,10 +65,10 @@ public class GenericBuilderTest { } @Test - public void buildMapTest(){ + public void buildMapTest() { //Map创建 final HashMap colorMap = GenericBuilder - .of(HashMap::new) + .of(HashMap::new) .with(Map::put, "red", "#FF0000") .with(Map::put, "yellow", "#FFFF00") .with(Map::put, "blue", "#0000FF") diff --git a/hutool-core/src/test/java/cn/hutool/core/exceptions/CheckedUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/exceptions/CheckedUtilTest.java deleted file mode 100644 index 760e4b154..000000000 --- a/hutool-core/src/test/java/cn/hutool/core/exceptions/CheckedUtilTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package cn.hutool.core.exceptions; - -import cn.hutool.core.lang.func.Func1; -import cn.hutool.core.lang.func.VoidFunc0; -import org.junit.Assert; -import org.junit.Test; - -import java.io.File; -import java.io.FileInputStream; - -/** - * 方便的执行会抛出受检查类型异常的方法调用或者代码段 - *

- * 该工具通过函数式的方式将那些需要抛出受检查异常的表达式或者代码段转化成一个标准的java8 functional 对象 - *

- * - * @author conder - */ -public class CheckedUtilTest { - - - @Test - public void sleepTest() { - final VoidFunc0 func = () -> Thread.sleep(1000L); - func.callWithRuntimeException(); - } - - - @SuppressWarnings("ConstantConditions") - @Test - public void supplierTest() { - final File noFile = new File("./no-file"); - try { - //本行代码原本需要抛出受检查异常,现在只抛出运行时异常 - CheckedUtil.uncheck(() -> new FileInputStream(noFile)).call(); - } catch (final Exception re) { - Assert.assertTrue(re instanceof RuntimeException); - } - } - - @Test - public void functionTest() { - final Func1 afunc = (funcParam) -> { - if (funcParam.length() > 5) { - throw new Exception("这是受检查异常需要屌用处显示处理"); - } - return funcParam.toUpperCase(); - }; - - //afunc.apply("hello world"); 直接调用需要处理异常 - - - try { - //本行代码原本需要抛出受检查异常,现在只抛出运行时异常 - CheckedUtil.uncheck(afunc).call("hello world"); - } catch (final Exception re) { - Assert.assertTrue(re instanceof RuntimeException); - } - - } - -} diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/func/LambdaUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/func/LambdaUtilTest.java index e0ddc9428..a02a714aa 100644 --- a/hutool-core/src/test/java/cn/hutool/core/lang/func/LambdaUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/func/LambdaUtilTest.java @@ -14,14 +14,14 @@ public class LambdaUtilTest { @Test public void getMethodNameTest() { - final Func1 lambda = MyTeacher::getAge; + final SerFunction lambda = MyTeacher::getAge; final String methodName = LambdaUtil.getMethodName(lambda); Assert.assertEquals("getAge", methodName); } @Test public void getFieldNameTest() { - final Func1 lambda = MyTeacher::getAge; + final SerFunction lambda = MyTeacher::getAge; final String fieldName = LambdaUtil.getFieldName(lambda); Assert.assertEquals("age", fieldName); } @@ -30,31 +30,31 @@ public class LambdaUtilTest { public void resolveTest() { Stream.of(() -> { // 引用构造函数 - final Func0 lambda = MyTeacher::new; + final SerSupplier lambda = MyTeacher::new; final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda); Assert.assertEquals(0, lambdaInfo.getParameterTypes().length); Assert.assertEquals(MyTeacher.class, lambdaInfo.getReturnType()); }, () -> { // 数组构造函数引用(此处数组构造参数) - final Func1 lambda = MyTeacher[]::new; + final SerFunction lambda = MyTeacher[]::new; final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda); Assert.assertEquals(int.class, lambdaInfo.getParameterTypes()[0]); Assert.assertEquals(MyTeacher[].class, lambdaInfo.getReturnType()); }, () -> { // 引用静态方法 - final Func0 lambda = MyTeacher::takeAge; + final SerSupplier lambda = MyTeacher::takeAge; final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda); Assert.assertEquals(0, lambdaInfo.getParameterTypes().length); Assert.assertEquals(String.class, lambdaInfo.getReturnType()); }, () -> { // 引用特定对象的实例方法 - final Func0 lambda = new MyTeacher()::getAge; + final SerSupplier lambda = new MyTeacher()::getAge; final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda); Assert.assertEquals(0, lambdaInfo.getParameterTypes().length); Assert.assertEquals(String.class, lambdaInfo.getReturnType()); }, () -> { // 引用特定类型的任意对象的实例方法 - final Func1 lambda = MyTeacher::getAge; + final SerFunction lambda = MyTeacher::getAge; final LambdaInfo lambdaInfo = LambdaUtil.resolve(lambda); Assert.assertEquals(0, lambdaInfo.getParameterTypes().length); Assert.assertEquals(String.class, lambdaInfo.getReturnType()); @@ -80,15 +80,15 @@ public class LambdaUtilTest { Assert.assertEquals(void.class, lambdaInfo.getReturnType()); }, () -> { // 一些特殊的lambda - Assert.assertEquals("T[]", LambdaUtil.>>resolve(Stream::of).getParameterTypes()[0].getTypeName()); - Assert.assertEquals(MyTeacher[][].class, LambdaUtil.>resolve(MyTeacher[][]::new).getReturnType()); - Assert.assertEquals(Integer[][][].class, LambdaUtil.>resolve(a -> {}).getParameterTypes()[0]); - Assert.assertEquals(Integer[][][].class, LambdaUtil.resolve((Serializable & Consumer3) (a, b, c) -> {}).getParameterTypes()[0]); + Assert.assertEquals("T", LambdaUtil.>>resolve(Stream::of).getParameterTypes()[0].getTypeName()); + Assert.assertEquals(MyTeacher[][].class, LambdaUtil.>resolve(MyTeacher[][]::new).getReturnType()); + Assert.assertEquals(Integer[][][].class, LambdaUtil.>resolve(a -> {}).getParameterTypes()[0]); + Assert.assertEquals(Integer[][][].class, LambdaUtil.resolve((Serializable & SerConsumer3) (a, b, c) -> {}).getParameterTypes()[0]); }).forEach(Runnable::run); } - interface SerThiCons extends Consumer3, Serializable { + interface SerThiCons extends SerConsumer3, Serializable { } @Test @@ -96,48 +96,48 @@ public class LambdaUtilTest { final MyTeacher myTeacher = new MyTeacher(); Stream.of(() -> { // 引用特定类型的任意对象的实例方法 - final Func1 lambda = MyTeacher::getAge; + final SerFunction lambda = MyTeacher::getAge; Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda)); }, () -> { // 枚举测试,不会导致类型擦除 - final Func1 lambda = LambdaKindEnum::ordinal; + final SerFunction lambda = LambdaKindEnum::ordinal; Assert.assertEquals(LambdaKindEnum.class, LambdaUtil.getRealClass(lambda)); }, () -> { // 调用父类方法,能获取到正确的子类类型 - final Func1 lambda = MyTeacher::getId; + final SerFunction lambda = MyTeacher::getId; Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda)); }, () -> { // 引用特定对象的实例方法 - final Func0 lambda = myTeacher::getAge; + final SerSupplier lambda = myTeacher::getAge; Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda)); }, () -> { // 枚举测试,只能获取到枚举类型 - final Func0 lambda = LambdaKindEnum.REF_NONE::ordinal; + final SerSupplier lambda = LambdaKindEnum.REF_NONE::ordinal; Assert.assertEquals(Enum.class, LambdaUtil.getRealClass(lambda)); }, () -> { // 调用父类方法,只能获取到父类类型 //noinspection ResultOfMethodCallIgnored - final VoidFunc0 lambda = myTeacher::getId; + final SerSupplier lambda = myTeacher::getId; Assert.assertEquals(Entity.class, LambdaUtil.getRealClass(lambda)); }, () -> { // 引用静态带参方法,能够获取到正确的参数类型 - final Func1 lambda = MyTeacher::takeAgeBy; + final SerFunction lambda = MyTeacher::takeAgeBy; Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda)); }, () -> { // 引用父类静态带参方法,只能获取到父类类型 - final Func0 lambda = MyTeacher::takeId; + final SerSupplier lambda = MyTeacher::takeId; Assert.assertEquals(Entity.class, LambdaUtil.getRealClass(lambda)); }, () -> { // 引用静态无参方法,能够获取到正确的类型 - final Func0 lambda = MyTeacher::takeAge; + final SerSupplier lambda = MyTeacher::takeAge; Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda)); }, () -> { // 引用父类静态无参方法,能够获取到正确的参数类型 - final Func1 lambda = MyTeacher::takeIdBy; + final SerFunction lambda = MyTeacher::takeIdBy; Assert.assertEquals(MyTeacher.class, LambdaUtil.getRealClass(lambda)); }, () -> { // 数组测试 - final VoidFunc1 lambda = (String[] stringList) -> {}; + final SerConsumer lambda = (String[] stringList) -> {}; Assert.assertEquals(String[].class, LambdaUtil.getRealClass(lambda)); }).forEach(Runnable::run); } diff --git a/hutool-core/src/test/java/cn/hutool/core/map/FuncMapTest.java b/hutool-core/src/test/java/cn/hutool/core/map/SerFunctionMapTest.java old mode 100755 new mode 100644 similarity index 92% rename from hutool-core/src/test/java/cn/hutool/core/map/FuncMapTest.java rename to hutool-core/src/test/java/cn/hutool/core/map/SerFunctionMapTest.java index 1a26d127a..01654139e --- a/hutool-core/src/test/java/cn/hutool/core/map/FuncMapTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/map/SerFunctionMapTest.java @@ -5,7 +5,7 @@ import org.junit.Test; import java.util.HashMap; -public class FuncMapTest { +public class SerFunctionMapTest { @Test public void putGetTest(){ diff --git a/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java b/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java index e8a637fcd..04f99ed2c 100755 --- a/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java +++ b/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java @@ -1,7 +1,7 @@ package cn.hutool.db; import cn.hutool.core.collection.ListUtil; -import cn.hutool.core.lang.func.Func1; +import cn.hutool.core.lang.func.SerFunction; import cn.hutool.db.dialect.Dialect; import cn.hutool.db.handler.BeanListHandler; import cn.hutool.db.handler.EntityHandler; @@ -191,7 +191,7 @@ public abstract class AbstractDb> extends DefaultConnect * @throws DbRuntimeException SQL执行异常 * @since 5.7.17 */ - public T query(final Func1 statementFunc, final RsHandler rsh) throws DbRuntimeException { + public T query(final SerFunction statementFunc, final RsHandler rsh) throws DbRuntimeException { Connection conn = null; try { conn = this.getConnection(); diff --git a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java index 35a3798a7..36de96896 100755 --- a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java +++ b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java @@ -1,6 +1,6 @@ package cn.hutool.db; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import cn.hutool.core.map.MapUtil; import java.util.Collection; @@ -144,7 +144,7 @@ public class ActiveEntity extends Entity { * @return this */ @Override - public ActiveEntity setFields(final Func0... fields) { + public ActiveEntity setFields(final SerSupplier... fields) { return (ActiveEntity) super.setFields(fields); } diff --git a/hutool-db/src/main/java/cn/hutool/db/Db.java b/hutool-db/src/main/java/cn/hutool/db/Db.java index 7c1da14d4..42eede2ca 100644 --- a/hutool-db/src/main/java/cn/hutool/db/Db.java +++ b/hutool-db/src/main/java/cn/hutool/db/Db.java @@ -1,6 +1,6 @@ package cn.hutool.db; -import cn.hutool.core.lang.func.VoidFunc1; +import cn.hutool.core.lang.func.SerConsumer; import cn.hutool.db.dialect.Dialect; import cn.hutool.db.dialect.DialectFactory; import cn.hutool.db.ds.DSFactory; @@ -114,7 +114,7 @@ public class Db extends AbstractDb { * @return this * @throws SQLException SQL异常 */ - public Db tx(final VoidFunc1 func) throws SQLException { + public Db tx(final SerConsumer func) throws SQLException { return tx(null, func); } @@ -127,7 +127,7 @@ public class Db extends AbstractDb { * @return this * @throws SQLException SQL异常 */ - public Db tx(final TransactionLevel transactionLevel, final VoidFunc1 func) throws SQLException { + public Db tx(final TransactionLevel transactionLevel, final SerConsumer func) throws SQLException { final Connection conn = getConnection(); // 检查是否支持事务 diff --git a/hutool-db/src/main/java/cn/hutool/db/Entity.java b/hutool-db/src/main/java/cn/hutool/db/Entity.java index 32fe7a3e3..176eb3eea 100755 --- a/hutool-db/src/main/java/cn/hutool/db/Entity.java +++ b/hutool-db/src/main/java/cn/hutool/db/Entity.java @@ -2,7 +2,7 @@ package cn.hutool.db; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.SetUtil; -import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.SerSupplier; import cn.hutool.core.map.Dict; import cn.hutool.core.reflect.MethodUtil; import cn.hutool.core.text.StrUtil; @@ -180,7 +180,7 @@ public class Entity extends Dict { * @return this */ @Override - public Entity setFields(final Func0... fields) { + public Entity setFields(final SerSupplier... fields) { return (Entity) super.setFields(fields); } diff --git a/hutool-db/src/main/java/cn/hutool/db/Session.java b/hutool-db/src/main/java/cn/hutool/db/Session.java index a4b43141e..8d809b338 100644 --- a/hutool-db/src/main/java/cn/hutool/db/Session.java +++ b/hutool-db/src/main/java/cn/hutool/db/Session.java @@ -1,6 +1,6 @@ package cn.hutool.db; -import cn.hutool.core.lang.func.VoidFunc1; +import cn.hutool.core.lang.func.SerConsumer; import cn.hutool.core.text.StrUtil; import cn.hutool.db.dialect.Dialect; import cn.hutool.db.dialect.DialectFactory; @@ -249,16 +249,16 @@ public class Session extends AbstractDb implements Closeable { } /** - * 在事务中执行操作,通过实现{@link VoidFunc1}接口的call方法执行多条SQL语句从而完成事务 + * 在事务中执行操作,通过实现{@link SerConsumer}接口的call方法执行多条SQL语句从而完成事务 * * @param func 函数抽象,在函数中执行多个SQL操作,多个操作会被合并为同一事务 * @throws DbRuntimeException SQL异常 * @since 3.2.3 */ - public void tx(final VoidFunc1 func) throws DbRuntimeException { + public void tx(final SerConsumer func) throws DbRuntimeException { try { beginTransaction(); - func.call(this); + func.accept(this); commit(); } catch (final Throwable e) { quietRollback(); diff --git a/hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java b/hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java index 59c0ab2e8..d9fd99262 100755 --- a/hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java +++ b/hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java @@ -1,7 +1,7 @@ package cn.hutool.db.sql; import cn.hutool.core.collection.iter.ArrayIter; -import cn.hutool.core.lang.func.Func1; +import cn.hutool.core.lang.func.SerFunction; import cn.hutool.db.DbRuntimeException; import cn.hutool.db.DbUtil; import cn.hutool.db.StatementUtil; @@ -289,10 +289,10 @@ public class SqlExecutor { * @throws DbRuntimeException SQL执行异常 * @since 5.7.17 */ - public static T query(final Connection conn, final Func1 statementFunc, final RsHandler rsh) throws DbRuntimeException { + public static T query(final Connection conn, final SerFunction statementFunc, final RsHandler rsh) throws DbRuntimeException { PreparedStatement ps = null; try { - ps = statementFunc.callWithRuntimeException(conn); + ps = statementFunc.apply(conn); return executeQuery(ps, rsh); } finally { DbUtil.close(ps); From f70ff3186c2a35c3f62bf13465d878b35855209c Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Wed, 7 Sep 2022 15:12:11 +0800 Subject: [PATCH 2/3] =?UTF-8?q?:trollface:=20=E6=94=B9=E6=BC=8F=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hutool-db/src/main/java/cn/hutool/db/Db.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hutool-db/src/main/java/cn/hutool/db/Db.java b/hutool-db/src/main/java/cn/hutool/db/Db.java index 42eede2ca..9155001da 100644 --- a/hutool-db/src/main/java/cn/hutool/db/Db.java +++ b/hutool-db/src/main/java/cn/hutool/db/Db.java @@ -151,7 +151,7 @@ public class Db extends AbstractDb { // 执行事务 try { - func.call(this); + func.accept(this); // 提交 conn.commit(); } catch (final Throwable e) { From b7d924b34df459c7fb4811dc51c045c5bd46d36f Mon Sep 17 00:00:00 2001 From: achao Date: Wed, 7 Sep 2022 15:57:29 +0800 Subject: [PATCH 3/3] =?UTF-8?q?:trollface:=20=E8=A7=A3=E5=86=B3=E5=86=B2?= =?UTF-8?q?=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/stream/EntryStream.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/stream/EntryStream.java b/hutool-core/src/main/java/cn/hutool/core/stream/EntryStream.java index 3ee4d022b..dff2c8801 100644 --- a/hutool-core/src/main/java/cn/hutool/core/stream/EntryStream.java +++ b/hutool-core/src/main/java/cn/hutool/core/stream/EntryStream.java @@ -61,8 +61,8 @@ public class EntryStream extends AbstractEnhancedWrappedStream valueItr = values.iterator(); while (keyItr.hasNext() || valueItr.hasNext()) { entries.add(ofEntry( - keyItr.hasNext() ? keyItr.next() : null, - valueItr.hasNext() ? valueItr.next() : null + keyItr.hasNext() ? keyItr.next() : null, + valueItr.hasNext() ? valueItr.next() : null )); } return of(entries); @@ -116,7 +116,7 @@ public class EntryStream extends AbstractEnhancedWrappedStream> stream = StreamSupport.stream(source.spliterator(), false) - .map(t -> new Entry<>(keyMapper.apply(t), valueMapper.apply(t))); + .map(t -> ofEntry(keyMapper.apply(t), valueMapper.apply(t))); return new EntryStream<>(stream); } @@ -131,7 +131,7 @@ public class EntryStream extends AbstractEnhancedWrappedStream EntryStream of(Stream> stream) { return ObjUtil.isNull(stream) ? - empty() : new EntryStream<>(stream.map(Entry::new)); + empty() : new EntryStream<>(stream.map(EntryStream::ofEntry)); } /** @@ -330,7 +330,7 @@ public class EntryStream extends AbstractEnhancedWrappedStream> contacted = StreamSupport.stream(entries.spliterator(), isParallel()) - .map(EntryStream::ofEntry); + .map(EntryStream::ofEntry); return wrap(Stream.concat(stream, contacted)); } @@ -346,7 +346,7 @@ public class EntryStream extends AbstractEnhancedWrappedStream> contacted = StreamSupport.stream(entries.spliterator(), isParallel()) - .map(EntryStream::ofEntry); + .map(EntryStream::ofEntry); return wrap(Stream.concat(contacted, stream)); } @@ -378,7 +378,7 @@ public class EntryStream extends AbstractEnhancedWrappedStream EntryStream mapKeys(final Function mapper) { Objects.requireNonNull(mapper); return new EntryStream<>( - stream.map(e -> ofEntry(mapper.apply(e.getKey()), e.getValue())) + stream.map(e -> ofEntry(mapper.apply(e.getKey()), e.getValue())) ); } @@ -392,7 +392,7 @@ public class EntryStream extends AbstractEnhancedWrappedStream EntryStream mapValues(final Function mapper) { Objects.requireNonNull(mapper); return new EntryStream<>( - stream.map(e -> ofEntry(e.getKey(), mapper.apply(e.getValue()))) + stream.map(e -> ofEntry(e.getKey(), mapper.apply(e.getValue()))) ); } @@ -457,10 +457,10 @@ public class EntryStream extends AbstractEnhancedWrappedStream EntryStream flatMapKey(final Function> keyMapper) { Objects.requireNonNull(keyMapper); return new EntryStream<>( - stream.flatMap(e -> keyMapper - .apply(e.getKey()) - .map(newKey -> ofEntry(newKey, e.getValue())) - ) + stream.flatMap(e -> keyMapper + .apply(e.getKey()) + .map(newKey -> ofEntry(newKey, e.getValue())) + ) ); } @@ -481,10 +481,10 @@ public class EntryStream extends AbstractEnhancedWrappedStream EntryStream flatMapValue(final Function> valueMapper) { Objects.requireNonNull(valueMapper); return new EntryStream<>( - stream.flatMap(e -> valueMapper - .apply(e.getValue()) - .map(newVal -> ofEntry(e.getKey(), newVal)) - ) + stream.flatMap(e -> valueMapper + .apply(e.getValue()) + .map(newVal -> ofEntry(e.getKey(), newVal)) + ) ); } @@ -644,7 +644,7 @@ public class EntryStream extends AbstractEnhancedWrappedStream, M extends Map> M groupByKey( - final Supplier mapFactory, final Collector collector) { + final Supplier mapFactory, final Collector collector) { return super.collect(Collectors.groupingBy( Map.Entry::getKey, mapFactory, CollectorUtil.transform(ArrayList::new, s -> s.stream().map(Map.Entry::getValue).collect(collector)) @@ -668,7 +668,7 @@ public class EntryStream extends AbstractEnhancedWrappedStream inverse() { return new EntryStream<>( - stream.map(e -> ofEntry(e.getValue(), e.getKey())) + stream.map(e -> ofEntry(e.getValue(), e.getKey())) ); } @@ -734,7 +734,7 @@ public class EntryStream extends AbstractEnhancedWrappedStream Map.Entry ofEntry(final Map.Entry entry) { return ObjUtil.defaultIfNull( - entry, e -> ofEntry(e.getKey(), e.getValue()), (Map.Entry)EMPTY_ENTRY + entry, e -> ofEntry(e.getKey(), e.getValue()), (Map.Entry) EMPTY_ENTRY ); }