From abf697d60a2fe85e46b02b107efd1e87d0ae1342 Mon Sep 17 00:00:00 2001 From: TomXin <766781886@qq.com> Date: Fri, 18 Feb 2022 17:15:25 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BD=BFMapBuilder=E5=AE=9E=E7=8E=B0Builde?= =?UTF-8?q?r=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/core/map/MapBuilder.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/map/MapBuilder.java b/hutool-core/src/main/java/cn/hutool/core/map/MapBuilder.java index a8409e6d0..5840d9204 100644 --- a/hutool-core/src/main/java/cn/hutool/core/map/MapBuilder.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/MapBuilder.java @@ -1,6 +1,8 @@ package cn.hutool.core.map; -import java.io.Serializable; + +import cn.hutool.core.builder.Builder; + import java.util.Map; import java.util.function.Supplier; @@ -11,7 +13,7 @@ import java.util.function.Supplier; * @param Value类型 * @since 3.1.1 */ -public class MapBuilder implements Serializable { +public class MapBuilder implements Builder> { private static final long serialVersionUID = 1L; private final Map map; @@ -133,6 +135,7 @@ public class MapBuilder implements Serializable { * @return 创建后的map * @since 3.3.0 */ + @Override public Map build() { return map(); } From 1b2234f6a22a5fe042f5baba3740ff79ed936b24 Mon Sep 17 00:00:00 2001 From: TomXin <766781886@qq.com> Date: Fri, 18 Feb 2022 18:18:42 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=89=A9=E5=B1=95CheckedUtil=E7=9A=84unche?= =?UTF-8?q?ck=E6=96=B9=E6=B3=95,=E4=BD=BF=E5=85=B6=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=BC=82=E5=B8=B8=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hutool/core/exceptions/CheckedUtil.java | 155 +++++++++++++++++- 1 file changed, 152 insertions(+), 3 deletions(-) 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 index f1380a2d0..3b76d8a78 100644 --- a/hutool-core/src/main/java/cn/hutool/core/exceptions/CheckedUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/exceptions/CheckedUtil.java @@ -145,6 +145,31 @@ public class CheckedUtil { }; } + /** + * 接收一个可以转化成 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(Func expression, Supplier1 rteSupplier) { + Objects.requireNonNull(expression, "expression can not be null"); + return t -> { + try { + return expression.call(t); + } catch (Exception e) { + if (rteSupplier == null) { + throw new RuntimeException(e); + } else { + throw rteSupplier.get(e); + } + } + }; + } + /** * 接收一个可以转化成 cn.hutool.core.lang.func.Func0的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常 * 如此一来,代码中就不用显示的try-catch转化成运行时异常 @@ -170,6 +195,30 @@ public class CheckedUtil { }; } + /** + * 接收一个可以转化成 cn.hutool.core.lang.func.Func0的Lambda表达式,和一个可以把Exception转化成RuntimeExceptionde的表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 + * 如此一来,代码中就不用显示的try-catch转化成运行时异常 + * + * @param expression Lambda表达式 + * @param rteSupplier 转化运行时异常的表达式 + * @param 最终返回的数据类型 + * @return {@link Func0Rt} + */ + public static Func0Rt uncheck(Func0 expression, Supplier1 rteSupplier) { + Objects.requireNonNull(expression, "expression can not be null"); + return () -> { + try { + return expression.call(); + } catch (Exception e) { + if (rteSupplier == null) { + throw new RuntimeException(e); + } else { + throw rteSupplier.get(e); + } + } + }; + } + /** * 接收一个可以转化成 cn.hutool.core.lang.func.Func1的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常 * 如此一来,代码中就不用显示的try-catch转化成运行时异常 @@ -197,7 +246,32 @@ public class CheckedUtil { } /** - * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常 + * 接收一个可以转化成 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(Func1 expression, Supplier1 rteSupplier) { + Objects.requireNonNull(expression, "expression can not be null"); + return t -> { + try { + return expression.call(t); + } catch (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表达式 @@ -221,6 +295,30 @@ public class CheckedUtil { }; } + /** + * 接收一个可以转化成 cn.hutool.core.lang.func.VoidFunc的Lambda表达式,和一个可以把Exception转化成RuntimeExceptionde的表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常 + * 如此一来,代码中就不用显示的try-catch转化成运行时异常 + * + * @param expression Lambda表达式 + * @param rteSupplier 转化运行时异常的表达式 + * @param

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

VoidFuncRt

uncheck(VoidFunc

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

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

VoidFunc1Rt

uncheck(VoidFunc1

expression, Supplier1 rteSupplier) { + Objects.requireNonNull(expression, "expression can not be null"); + return t -> { + try { + expression.call(t); + } catch (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; } From 9636725f83b4c14605ec4c6053d4574bc595a07c Mon Sep 17 00:00:00 2001 From: TomXin <766781886@qq.com> Date: Fri, 18 Feb 2022 18:41:36 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BD=BFSSLContextBuilder=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0Builder=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/net/SSLContextBuilder.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/net/SSLContextBuilder.java b/hutool-core/src/main/java/cn/hutool/core/net/SSLContextBuilder.java index 8a0efced2..d08c5fcdb 100644 --- a/hutool-core/src/main/java/cn/hutool/core/net/SSLContextBuilder.java +++ b/hutool-core/src/main/java/cn/hutool/core/net/SSLContextBuilder.java @@ -1,5 +1,7 @@ package cn.hutool.core.net; +import cn.hutool.core.builder.Builder; +import cn.hutool.core.exceptions.CheckedUtil; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.StrUtil; @@ -20,13 +22,13 @@ import java.security.SecureRandom; *

  • {@link TrustManager},默认{@link DefaultTrustManager},即信任全部
  • *
  • {@link SecureRandom}
  • * - * + *

    * 构建后可获得{@link SSLContext},通过调用{@link SSLContext#getSocketFactory()}获取{@link javax.net.ssl.SSLSocketFactory} * * @author Looly * @since 5.5.2 */ -public class SSLContextBuilder implements SSLProtocols { +public class SSLContextBuilder implements SSLProtocols, Builder { private String protocol = TLS; private KeyManager[] keyManagers; @@ -99,10 +101,21 @@ public class SSLContextBuilder implements SSLProtocols { * 构建{@link SSLContext} * * @return {@link SSLContext} - * @throws NoSuchAlgorithmException 无此算法 - * @throws KeyManagementException Key管理异常 */ - public SSLContext build() throws NoSuchAlgorithmException, KeyManagementException { + @Override + public SSLContext build() { + return CheckedUtil.uncheck(this::buildChecked, IORuntimeException::new).call(); + } + + /** + * 构建{@link SSLContext}需要处理异常 + * + * @return {@link SSLContext} + * @throws NoSuchAlgorithmException 无此算法异常 + * @throws KeyManagementException 密钥管理异常 + * @since 5.7.22 + */ + public SSLContext buildChecked() throws NoSuchAlgorithmException, KeyManagementException { SSLContext sslContext = SSLContext.getInstance(protocol); sslContext.init(this.keyManagers, this.trustManagers, this.secureRandom); return sslContext; @@ -116,7 +129,7 @@ public class SSLContextBuilder implements SSLProtocols { */ public SSLContext buildQuietly() throws IORuntimeException { try { - return build(); + return buildChecked(); } catch (GeneralSecurityException e) { throw new IORuntimeException(e); } From 9aaea4026d7f06fd3a0a2ea91c73fa626ddbed0e Mon Sep 17 00:00:00 2001 From: TomXin <766781886@qq.com> Date: Fri, 18 Feb 2022 18:50:01 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BD=BFUrlBuilder=E5=AE=9E=E7=8E=B0Builde?= =?UTF-8?q?r=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/core/net/url/UrlBuilder.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/net/url/UrlBuilder.java b/hutool-core/src/main/java/cn/hutool/core/net/url/UrlBuilder.java index 708e9721a..0a9adc1a8 100644 --- a/hutool-core/src/main/java/cn/hutool/core/net/url/UrlBuilder.java +++ b/hutool-core/src/main/java/cn/hutool/core/net/url/UrlBuilder.java @@ -1,12 +1,12 @@ package cn.hutool.core.net.url; +import cn.hutool.core.builder.Builder; import cn.hutool.core.lang.Assert; import cn.hutool.core.net.RFC3986; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.URLUtil; -import java.io.Serializable; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -26,7 +26,7 @@ import java.nio.charset.Charset; * @see Uniform Resource Identifier * @since 5.3.1 */ -public final class UrlBuilder implements Serializable { +public final class UrlBuilder implements Builder { private static final long serialVersionUID = 1L; private static final String DEFAULT_SCHEME = "http"; @@ -468,6 +468,7 @@ public final class UrlBuilder implements Serializable { * * @return URL字符串 */ + @Override public String build() { return toURL().toString(); }