mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-09 15:17:59 +08:00
!545 使多个xxxBuilder实现Builder接口,扩展CheckedUtil
Merge pull request !545 from meiMingle/v5-dev
This commit is contained in:
commit
5610516142
@ -145,6 +145,31 @@ public class CheckedUtil {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接收一个可以转化成 cn.hutool.core.lang.func.Func的Lambda表达式,和一个可以把Exception转化成RuntimeExceptionde的表达式,当执行表达式抛出任何异常的时候,都会转化成运行时异常
|
||||||
|
* 如此一来,代码中就不用显示的try-catch转化成运行时异常
|
||||||
|
*
|
||||||
|
* @param expression Lambda表达式
|
||||||
|
* @param rteSupplier 转化运行时异常的表达式
|
||||||
|
* @param <P> 运行时传入的参数类型
|
||||||
|
* @param <R> 最终返回的数据类型
|
||||||
|
* @return {@link FuncRt}
|
||||||
|
*/
|
||||||
|
public static <P, R> FuncRt<P, R> uncheck(Func<P, R> expression, Supplier1<RuntimeException, Exception> 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,当执行表达式抛出任何异常的时候,都会转化成运行时异常
|
* 接收一个可以转化成 cn.hutool.core.lang.func.Func0的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常
|
||||||
* 如此一来,代码中就不用显示的try-catch转化成运行时异常
|
* 如此一来,代码中就不用显示的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 <R> 最终返回的数据类型
|
||||||
|
* @return {@link Func0Rt}
|
||||||
|
*/
|
||||||
|
public static <R> Func0Rt<R> uncheck(Func0<R> expression, Supplier1<RuntimeException, Exception> 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,当执行表达式抛出任何异常的时候,都会转化成运行时异常
|
* 接收一个可以转化成 cn.hutool.core.lang.func.Func1的Lambda表达式,和一个RuntimeException,当执行表达式抛出任何异常的时候,都会转化成运行时异常
|
||||||
* 如此一来,代码中就不用显示的try-catch转化成运行时异常
|
* 如此一来,代码中就不用显示的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 <P> 运行时传入的参数类型
|
||||||
|
* @param <R> 最终返回的数据类型
|
||||||
|
* @return {@link Func1Rt}
|
||||||
|
*/
|
||||||
|
public static <P, R> Func1Rt<P, R> uncheck(Func1<P, R> expression, Supplier1<RuntimeException, Exception> 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转化成运行时异常
|
* 如此一来,代码中就不用显示的try-catch转化成运行时异常
|
||||||
*
|
*
|
||||||
* @param expression Lambda表达式
|
* @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 <P> 运行时传入的参数类型
|
||||||
|
* @return {@link VoidFuncRt}
|
||||||
|
*/
|
||||||
|
public static <P> VoidFuncRt<P> uncheck(VoidFunc<P> expression, Supplier1<RuntimeException, Exception> 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,当执行表达式抛出任何异常的时候,都会转化成运行时异常
|
* 接收一个可以转化成 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<RuntimeException, Exception> 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,当执行表达式抛出任何异常的时候,都会转化成运行时异常
|
* 接收一个可以转化成 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 <P> 运行时传入的参数类型
|
||||||
|
* @return {@link VoidFunc1Rt}
|
||||||
|
*/
|
||||||
|
public static <P> VoidFunc1Rt<P> uncheck(VoidFunc1<P> expression, Supplier1<RuntimeException, Exception> 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<P, R> extends Func<P, R> {
|
public interface FuncRt<P, R> extends Func<P, R> {
|
||||||
@SuppressWarnings("unchecked")
|
@Override
|
||||||
R call(P... parameters) throws RuntimeException;
|
R call(P... parameters) throws RuntimeException;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Func0Rt<R> extends Func0<R> {
|
public interface Func0Rt<R> extends Func0<R> {
|
||||||
|
@Override
|
||||||
R call() throws RuntimeException;
|
R call() throws RuntimeException;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Func1Rt<P, R> extends Func1<P, R> {
|
public interface Func1Rt<P, R> extends Func1<P, R> {
|
||||||
|
@Override
|
||||||
R call(P parameter) throws RuntimeException;
|
R call(P parameter) throws RuntimeException;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface VoidFuncRt<P> extends VoidFunc<P> {
|
public interface VoidFuncRt<P> extends VoidFunc<P> {
|
||||||
@SuppressWarnings("unchecked")
|
@Override
|
||||||
void call(P... parameters) throws RuntimeException;
|
void call(P... parameters) throws RuntimeException;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface VoidFunc0Rt extends VoidFunc0 {
|
public interface VoidFunc0Rt extends VoidFunc0 {
|
||||||
|
@Override
|
||||||
void call() throws RuntimeException;
|
void call() throws RuntimeException;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface VoidFunc1Rt<P> extends VoidFunc1<P> {
|
public interface VoidFunc1Rt<P> extends VoidFunc1<P> {
|
||||||
|
@Override
|
||||||
void call(P parameter) throws RuntimeException;
|
void call(P parameter) throws RuntimeException;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package cn.hutool.core.map;
|
package cn.hutool.core.map;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
import cn.hutool.core.builder.Builder;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@ -11,7 +13,7 @@ import java.util.function.Supplier;
|
|||||||
* @param <V> Value类型
|
* @param <V> Value类型
|
||||||
* @since 3.1.1
|
* @since 3.1.1
|
||||||
*/
|
*/
|
||||||
public class MapBuilder<K, V> implements Serializable {
|
public class MapBuilder<K, V> implements Builder<Map<K,V>> {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final Map<K, V> map;
|
private final Map<K, V> map;
|
||||||
@ -133,6 +135,7 @@ public class MapBuilder<K, V> implements Serializable {
|
|||||||
* @return 创建后的map
|
* @return 创建后的map
|
||||||
* @since 3.3.0
|
* @since 3.3.0
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public Map<K, V> build() {
|
public Map<K, V> build() {
|
||||||
return map();
|
return map();
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package cn.hutool.core.net;
|
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.io.IORuntimeException;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
@ -20,13 +22,13 @@ import java.security.SecureRandom;
|
|||||||
* <li>{@link TrustManager},默认{@link DefaultTrustManager},即信任全部</li>
|
* <li>{@link TrustManager},默认{@link DefaultTrustManager},即信任全部</li>
|
||||||
* <li>{@link SecureRandom}</li>
|
* <li>{@link SecureRandom}</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
* <p>
|
||||||
* 构建后可获得{@link SSLContext},通过调用{@link SSLContext#getSocketFactory()}获取{@link javax.net.ssl.SSLSocketFactory}
|
* 构建后可获得{@link SSLContext},通过调用{@link SSLContext#getSocketFactory()}获取{@link javax.net.ssl.SSLSocketFactory}
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
* @since 5.5.2
|
* @since 5.5.2
|
||||||
*/
|
*/
|
||||||
public class SSLContextBuilder implements SSLProtocols {
|
public class SSLContextBuilder implements SSLProtocols, Builder<SSLContext> {
|
||||||
|
|
||||||
private String protocol = TLS;
|
private String protocol = TLS;
|
||||||
private KeyManager[] keyManagers;
|
private KeyManager[] keyManagers;
|
||||||
@ -99,10 +101,21 @@ public class SSLContextBuilder implements SSLProtocols {
|
|||||||
* 构建{@link SSLContext}
|
* 构建{@link SSLContext}
|
||||||
*
|
*
|
||||||
* @return {@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 sslContext = SSLContext.getInstance(protocol);
|
||||||
sslContext.init(this.keyManagers, this.trustManagers, this.secureRandom);
|
sslContext.init(this.keyManagers, this.trustManagers, this.secureRandom);
|
||||||
return sslContext;
|
return sslContext;
|
||||||
@ -116,7 +129,7 @@ public class SSLContextBuilder implements SSLProtocols {
|
|||||||
*/
|
*/
|
||||||
public SSLContext buildQuietly() throws IORuntimeException {
|
public SSLContext buildQuietly() throws IORuntimeException {
|
||||||
try {
|
try {
|
||||||
return build();
|
return buildChecked();
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (GeneralSecurityException e) {
|
||||||
throw new IORuntimeException(e);
|
throw new IORuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package cn.hutool.core.net.url;
|
package cn.hutool.core.net.url;
|
||||||
|
|
||||||
|
import cn.hutool.core.builder.Builder;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.net.RFC3986;
|
import cn.hutool.core.net.RFC3986;
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
@ -26,7 +26,7 @@ import java.nio.charset.Charset;
|
|||||||
* @see <a href="https://en.wikipedia.org/wiki/Uniform_Resource_Identifier">Uniform Resource Identifier</a>
|
* @see <a href="https://en.wikipedia.org/wiki/Uniform_Resource_Identifier">Uniform Resource Identifier</a>
|
||||||
* @since 5.3.1
|
* @since 5.3.1
|
||||||
*/
|
*/
|
||||||
public final class UrlBuilder implements Serializable {
|
public final class UrlBuilder implements Builder<String> {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private static final String DEFAULT_SCHEME = "http";
|
private static final String DEFAULT_SCHEME = "http";
|
||||||
|
|
||||||
@ -468,6 +468,7 @@ public final class UrlBuilder implements Serializable {
|
|||||||
*
|
*
|
||||||
* @return URL字符串
|
* @return URL字符串
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public String build() {
|
public String build() {
|
||||||
return toURL().toString();
|
return toURL().toString();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user