From ff19f997bd39b57146f25a2865aff72cf89e811d Mon Sep 17 00:00:00 2001 From: yishui Date: Fri, 18 Oct 2024 22:09:54 +0800 Subject: [PATCH 1/6] Optimize dependencies and remove useless utility classes --- pom.xml | 544 +++++++++--------- .../common/tool/bean/BeanUtil.java | 27 +- .../common/tool/encoder/DES.java | 3 - .../common/tool/entity/Response.java | 51 +- .../exception/IllegalParameterException.java | 179 ------ .../common/tool/http/CookieUtil.java | 222 ------- .../common/tool/http/HttpUtil.java | 13 +- .../common/tool/io/ImageUtil.java | 7 +- .../yishuifengxiao/common/tool/io/IoUtil.java | 3 +- .../common/tool/log/LogInfo.java | 78 ++- .../common/tool/log/RedisAppender.java | 435 -------------- .../common/tool/utils/ExceptionUtil.java | 93 +++ 12 files changed, 452 insertions(+), 1203 deletions(-) delete mode 100644 src/main/java/com/yishuifengxiao/common/tool/exception/IllegalParameterException.java delete mode 100644 src/main/java/com/yishuifengxiao/common/tool/http/CookieUtil.java delete mode 100644 src/main/java/com/yishuifengxiao/common/tool/log/RedisAppender.java diff --git a/pom.xml b/pom.xml index ef26460..d1af6bd 100644 --- a/pom.xml +++ b/pom.xml @@ -1,280 +1,278 @@ - 4.0.0 - com.yishuifengxiao.common - common-tool - BUILD-SNAPSHOT - jar - 本工具包主要集成了目前在项目开发过程中个人经常会使用到的一些工具类,对工具类进行了一下简单的封装 - common-tool - https://gitee.com/zhiyubujian/tool - - - The Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - - - - - yishuifengxiao - zhiyubujian@163.com - yishuifengxiao - http://www.yishuifengxiao.com - - - - - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.yishuifengxiao.common + common-tool + BUILD-SNAPSHOT + jar + 本工具包主要集成了目前在项目开发过程中个人经常会使用到的一些工具类,对工具类进行了一下简单的封装 + + common-tool + https://gitee.com/zhiyubujian/tool + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + yishuifengxiao + zhiyubujian@163.com + yishuifengxiao + http://www.yishuifengxiao.com + + + + + scm:git:https://gitee.com/zhiyubujian/tool.git - - - + + + scm:git:https://gitee.com/zhiyubujian/tool.git - - https://gitee.com/zhiyubujian/tool/tree/master - v1.0.0 - - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - - - org.springframework.boot - spring-boot-starter-parent - 3.2.1 - - - 2.2.20 - 4.4 - 1.17.2 - 2.1.4 - 1.18.30 - 3.12.1 - 3.3.0 - 3.0.1 - 1.6.13 - 3.1.0 - - - - org.projectlombok - lombok - provided - ${lombok.version} - - - org.apache.commons - commons-lang3 - - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - - - commons-codec - commons-codec - + + https://gitee.com/zhiyubujian/tool/tree/master + v1.0.0 + + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + ossrh + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + org.springframework.boot + spring-boot-starter-parent + 3.2.1 + + + 2.2.20 + 4.4 + 1.17.2 + 2.1.4 + 1.18.30 + 1.5.11 + 2.9.1 + 3.12.1 + 3.3.0 + 3.0.1 + 1.6.13 + 3.1.0 + + + + org.projectlombok + lombok + provided + ${lombok.version} + + + org.apache.commons + commons-lang3 + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + commons-codec + commons-codec + + + ch.qos.logback + logback-classic + ${logback.version} + - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.boot - spring-boot-starter-web - true - - - org.hibernate.validator - hibernate-validator - true - - - io.lettuce - lettuce-core - true - - - io.swagger.core.v3 - swagger-annotations - ${swagger.v3.version} - - - org.jsoup - jsoup - ${jsoup.version} - - - org.dom4j - dom4j - ${dom4j.version} - - - jaxen - jaxen - - - com.jayway.jsonpath - json-path - - - - - disable-javadoc-doclint - - [1.8,) - - - -Xdoclint:none - - - - - dev - - dev - - - - true - - - - doc - - -Xdoclint:none - - - - - org.apache.maven.plugins - maven-javadoc-plugin - - - attach-javadocs - - jar - - - UTF-8 - -Xdoclint:none - false - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 17 - 17 - - - - - - - - release - - release - - - - false - - - - - org.sonatype.plugins - nexus-staging-maven-plugin - ${nexus-staging-maven-plugin.version} - true - - ossrh - https://oss.sonatype.org/ - true - - - - - org.apache.maven.plugins - maven-release-plugin - ${maven-release-plugin.version>} - - true - false - release - deploy - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 17 - 17 - - - - org.apache.maven.plugins - maven-gpg-plugin - ${maven-gpg-plugin.version} - - - sign-artifacts - verify - - sign - - - - - - org.apache.maven.plugins - maven-source-plugin - ${maven-source-plugin.version} - - - attach-sources - - jar-no-fork - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - - - attach-javadocs - - jar - - - -Xdoclint:none - - - - - - - - - + + org.hibernate.validator + hibernate-validator + true + + + io.swagger.core.v3 + swagger-annotations + ${swagger.v3.version} + + + org.jsoup + jsoup + ${jsoup.version} + + + org.dom4j + dom4j + ${dom4j.version} + + + jaxen + jaxen + + + com.jayway.jsonpath + json-path + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + + disable-javadoc-doclint + + [1.8,) + + + -Xdoclint:none + + + + + dev + + dev + + + + true + + + + doc + + -Xdoclint:none + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + + + UTF-8 + -Xdoclint:none + false + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 17 + 17 + + + + + + + + release + + release + + + + false + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + ${nexus-staging-maven-plugin.version} + true + + ossrh + https://oss.sonatype.org/ + true + + + + + org.apache.maven.plugins + maven-release-plugin + ${maven-release-plugin.version>} + + true + false + release + deploy + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 17 + 17 + + + + org.apache.maven.plugins + maven-gpg-plugin + ${maven-gpg-plugin.version} + + + sign-artifacts + verify + + sign + + + + + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + + + -Xdoclint:none + + + + + + + + + diff --git a/src/main/java/com/yishuifengxiao/common/tool/bean/BeanUtil.java b/src/main/java/com/yishuifengxiao/common/tool/bean/BeanUtil.java index 38f62df..9518fd8 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/bean/BeanUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/bean/BeanUtil.java @@ -8,14 +8,16 @@ import com.yishuifengxiao.common.tool.exception.UncheckedException; import com.yishuifengxiao.common.tool.io.CloseUtil; -import org.springframework.beans.BeanUtils; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.Collections; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /** *

@@ -47,15 +49,26 @@ public static T copy(S source, T target) { if (null == source || null == target) { return null; } - try { - BeanUtils.copyProperties(source, target); - return target; - } catch (Exception e) { - throw new UncheckedException(e.getMessage()); + List targetFields = ClassUtil.fields(target.getClass()); + List sourceFields = ClassUtil.fields(source.getClass()); + targetFields = targetFields.stream().filter(v -> !(Modifier.isAbstract(v.getModifiers()) || Modifier.isNative(v.getModifiers()) || Modifier.isStatic(v.getModifiers()) || Modifier.isFinal(v.getModifiers()))).filter(v -> sourceFields.stream().anyMatch(s -> s.getName().equals(v.getName()))).collect(Collectors.toList()); + for (Field field : targetFields) { + field.setAccessible(true); + try { + Object val = field.get(source); + if (null == val) { + continue; + } + field.set(target, val); + } catch (IllegalAccessException e) { + throw new UncheckedException(e.getMessage()); + } } + return target; } + /** * 将Java对象序列化为二进制数据 * diff --git a/src/main/java/com/yishuifengxiao/common/tool/encoder/DES.java b/src/main/java/com/yishuifengxiao/common/tool/encoder/DES.java index e62c3c9..89b349e 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/encoder/DES.java +++ b/src/main/java/com/yishuifengxiao/common/tool/encoder/DES.java @@ -2,7 +2,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.springframework.util.Assert; import javax.crypto.Cipher; import javax.crypto.SecretKey; @@ -51,7 +50,6 @@ public class DES { * @return 加密后的数据, null表示加密失败 */ public static final String encrypt(String key, String data) { - Assert.notNull(data, "需要加密的数据不能为空"); try { return byte2hex(encrypt(data.getBytes("utf-8"), keyValidate(key).getBytes("utf-8"))); } catch (Exception e) { @@ -78,7 +76,6 @@ public static final String encrypt(String data) { * @return 解密后的数据, null表示解密失败 */ public static final String decrypt(String key, String data) { - Assert.notNull(data, "需要解密的数据不能为空"); try { return new String(decrypt(hex2byte(data.getBytes("utf-8")), keyValidate(key).getBytes("utf-8"))); } catch (Exception e) { diff --git a/src/main/java/com/yishuifengxiao/common/tool/entity/Response.java b/src/main/java/com/yishuifengxiao/common/tool/entity/Response.java index 51207ac..de5d4dd 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/entity/Response.java +++ b/src/main/java/com/yishuifengxiao/common/tool/entity/Response.java @@ -7,7 +7,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.yishuifengxiao.common.tool.random.IdWorker; import io.swagger.v3.oas.annotations.media.Schema; -import org.springframework.http.HttpStatus; import java.io.Serializable; import java.util.Date; @@ -39,6 +38,7 @@ @Schema(name = "通用响应", description = "用于所有接口的通用返回数据") public class Response implements Serializable { + /** * */ @@ -46,7 +46,7 @@ public class Response implements Serializable { /** * 请求ID,用于请求追踪 .无论调用接口成功与否,都会返回请求 ID,该序列号全局唯一且随机 */ - @Schema(title ="请求ID,用于请求追踪 .无论调用接口成功与否,都会返回请求 ID,该序列号全局唯一且随机") + @Schema(title = "请求ID,用于请求追踪 .无论调用接口成功与否,都会返回请求 ID,该序列号全局唯一且随机") @JsonProperty("request-id") protected String id; @@ -57,27 +57,26 @@ public class Response implements Serializable { * "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status">https://developer.mozilla * .org/en-US/docs/Web/HTTP/Status */ - @Schema(title ="请求的响应码,这里借用HttpStatus作为状态标识,具体代码的含义请参见 HttpStatus( https://developer.mozilla" + - ".org/en-US/docs/Web/HTTP/Status)") + @Schema(title = "请求的响应码,这里借用HttpStatus作为状态标识,具体代码的含义请参见 HttpStatus( https://developer.mozilla" + ".org/en-US/docs/Web/HTTP/Status)") protected int code; /** * 响应提示信息,一般与响应码的状态对应,对响应结果进行简单地描述 */ - @Schema(title =" 响应提示信息,一般与响应码的状态对应,对响应结果进行简单地描述") + @Schema(title = " 响应提示信息,一般与响应码的状态对应,对响应结果进行简单地描述") protected String msg; /** * 响应数据,在基本基本信息无法满足时会出现此信息,一般情况下无此信息 */ - @Schema(title =" 响应数据,在基本基本信息无法满足时会出现此信息,一般情况下无此信息") + @Schema(title = " 响应数据,在基本基本信息无法满足时会出现此信息,一般情况下无此信息") @JsonProperty("data") protected T data; /** * 响应时间 */ - @Schema(title ="响应时间") + @Schema(title = "响应时间") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") @JsonProperty("response-time") protected Date date; @@ -103,7 +102,7 @@ public static Response of(int code, String msg, T data) { * @return 表示请求成功的响应对象 */ public static Response suc(T data) { - return new Response<>(HttpStatus.OK.value(), Const.MSG_OK, data); + return new Response<>(Const.CODE_OK, Const.MSG_OK, data); } /** @@ -112,7 +111,7 @@ public static Response suc(T data) { * @return 表示成功的响应对象(响应码200) */ public static Response suc() { - return new Response(HttpStatus.OK.value(), Const.MSG_OK); + return new Response(Const.CODE_OK, Const.MSG_OK); } /** @@ -122,7 +121,7 @@ public static Response suc() { * @return 表示成功的响应对象(响应码200) */ public static Response suc(String msg) { - return new Response(HttpStatus.OK.value(), msg); + return new Response(Const.CODE_OK, msg); } /** @@ -133,7 +132,7 @@ public static Response suc(String msg) { * @return 表示成功的响应对象(响应码200) */ public static Response sucData(T data) { - return new Response(HttpStatus.OK.value(), Const.MSG_OK, data); + return new Response(Const.CODE_OK, Const.MSG_OK, data); } /** @@ -145,7 +144,7 @@ public static Response sucData(T data) { * @return 表示成功的响应对象(响应码200) */ public static Response suc(String msg, T data) { - return new Response<>(HttpStatus.OK.value(), msg, data); + return new Response<>(Const.CODE_OK, msg, data); } /** @@ -154,7 +153,7 @@ public static Response suc(String msg, T data) { * @return 表示参数有误的响应对象(响应码400) */ public static Response badParam() { - return new Response(HttpStatus.BAD_REQUEST.value(), Const.MSG_BAD_REQUEST); + return new Response(Const.CODE_BAD_REQUEST, Const.MSG_BAD_REQUEST); } /** @@ -164,7 +163,7 @@ public static Response badParam() { * @return 表示参数有误的响应对象(响应码400) */ public static Response badParam(String msg) { - return new Response(HttpStatus.BAD_REQUEST.value(), msg); + return new Response(Const.CODE_BAD_REQUEST, msg); } /** @@ -176,7 +175,7 @@ public static Response badParam(String msg) { * @return 表示参数有误的响应对象(响应码400) */ public static Response badParam(String msg, T data) { - return new Response<>(HttpStatus.BAD_REQUEST.value(), msg, data); + return new Response<>(Const.CODE_BAD_REQUEST, msg, data); } /** @@ -185,7 +184,7 @@ public static Response badParam(String msg, T data) { * @return 表示资源未授权的响应对象(401响应码) */ public static Response unAuth() { - return new Response(HttpStatus.UNAUTHORIZED.value(), Const.MSG_UNAUTHORIZED); + return new Response(Const.CODE_UNAUTHORIZED, Const.MSG_UNAUTHORIZED); } /** @@ -195,7 +194,7 @@ public static Response unAuth() { * @return 表示资源未授权的响应对象(401响应码) */ public static Response unAuth(String msg) { - return new Response(HttpStatus.UNAUTHORIZED.value(), msg); + return new Response(Const.CODE_UNAUTHORIZED, msg); } /** @@ -207,7 +206,7 @@ public static Response unAuth(String msg) { * @return 表示资源未授权的响应对象(401响应码) */ public static Response unAuth(String msg, T data) { - return new Response<>(HttpStatus.UNAUTHORIZED.value(), msg, data); + return new Response<>(Const.CODE_UNAUTHORIZED, msg, data); } /** @@ -216,7 +215,7 @@ public static Response unAuth(String msg, T data) { * @return 表示资源不可用的响应对象(403响应码) */ public static Response notAllow() { - return new Response(HttpStatus.FORBIDDEN.value(), Const.MSG_FORBIDDEN); + return new Response(Const.CODE_FORBIDDEN, Const.MSG_FORBIDDEN); } /** @@ -226,7 +225,7 @@ public static Response notAllow() { * @return 表示资源不可用的响应对象(403响应码) */ public static Response notAllow(String msg) { - return new Response(HttpStatus.FORBIDDEN.value(), msg); + return new Response(Const.CODE_FORBIDDEN, msg); } /** @@ -235,7 +234,7 @@ public static Response notAllow(String msg) { * @return 表示资源不存在的响应对象(404响应码) */ public static Response notFoundt() { - return new Response(HttpStatus.NOT_FOUND.value(), Const.MSG_NOT_FOUND); + return new Response(Const.CODE_NOT_FOUND, Const.MSG_NOT_FOUND); } /** @@ -244,7 +243,7 @@ public static Response notFoundt() { * @return 表示请求业务未完成的响应对象(500响应码) */ public static Response error() { - return new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), Const.MSG_INTERNAL_SERVER_ERROR); + return new Response(Const.CODE_INTERNAL_SERVER_ERROR, Const.MSG_INTERNAL_SERVER_ERROR); } /** @@ -255,7 +254,7 @@ public static Response error() { * @return 表示请求业务未完成的响应对象(500响应码) */ public static Response errorData(T data) { - return new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), Const.MSG_INTERNAL_SERVER_ERROR, data); + return new Response(Const.CODE_INTERNAL_SERVER_ERROR, Const.MSG_INTERNAL_SERVER_ERROR, data); } /** @@ -265,7 +264,7 @@ public static Response errorData(T data) { * @return 表示服务器内部异常500时的返回信息 */ public static Response error(String msg) { - return new Response(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg); + return new Response(Const.CODE_INTERNAL_SERVER_ERROR, msg); } /** @@ -276,7 +275,7 @@ public static Response error(String msg) { * @return 表示服务器内部异常500时的返回信息 */ public static Response error(T data) { - return new Response<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), Const.MSG_INTERNAL_SERVER_ERROR, data); + return new Response<>(Const.CODE_INTERNAL_SERVER_ERROR, Const.MSG_INTERNAL_SERVER_ERROR, data); } /** @@ -288,7 +287,7 @@ public static Response error(T data) { * @return 表示服务器内部异常500时的返回信息 */ public static Response error(String msg, T data) { - return new Response<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, data); + return new Response<>(Const.CODE_INTERNAL_SERVER_ERROR, msg, data); } /** diff --git a/src/main/java/com/yishuifengxiao/common/tool/exception/IllegalParameterException.java b/src/main/java/com/yishuifengxiao/common/tool/exception/IllegalParameterException.java deleted file mode 100644 index 9303462..0000000 --- a/src/main/java/com/yishuifengxiao/common/tool/exception/IllegalParameterException.java +++ /dev/null @@ -1,179 +0,0 @@ -package com.yishuifengxiao.common.tool.exception; - -/** - * 非法参数异常 - * - * @author qingteng - * @version 1.0.0 - * @date 2023/12/23 15:51 - * @since 1.0.0 - */ -public class IllegalParameterException extends UncheckedException { - /** - * - */ - private static final long serialVersionUID = 9071897872977285359L; - - /** - * 错误码 - */ - protected int code; - - /** - * 携带的附加信息 - */ - protected transient Object context; - - /** - * Constructs a new runtime exception with {@code null} as its detail message. - * The cause is not initialized, and may subsequently be initialized by a call - * to {@link #initCause}. - */ - public IllegalParameterException() { - - } - - /** - * Constructs a new runtime exception with the specified detail message. The - * cause is not initialized, and may subsequently be initialized by a call to - * {@link #initCause}. - * - * @param code 异常码 - * @param message the detail message. The detail message is saved for later - * retrieval by the {@link #getMessage()} method. - */ - - public IllegalParameterException(int code, String message) { - super(message); - this.code = code; - } - - /** - * Constructs a new runtime exception with the specified detail message, cause, - * suppression enabled or disabled, and writable stack trace enabled or - * disabled. - * - * @param message the detail message. - * @param cause the cause. (A {@code null} value is permitted, and - * indicates that the cause is nonexistent or - * unknown.) - * @param enableSuppression whether or not suppression is enabled or disabled - * @param writableStackTrace whether or not the stack trace should be writable - * @since 1.7 - */ - public IllegalParameterException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - - } - - /** - * Constructs a new runtime exception with the specified detail message and - * cause. - *

- * Note that the detail message associated with {@code cause} is not - * automatically incorporated in this runtime exception's detail message. - * - * @param message the detail message (which is saved for later retrieval by the - * {@link #getMessage()} method). - * @param cause the cause (which is saved for later retrieval by the - * {@link #getCause()} method). (A {@code null} value is - * permitted, and indicates that the cause is nonexistent or - * unknown.) - * @since 1.4 - */ - public IllegalParameterException(String message, Throwable cause) { - super(message, cause); - } - - /** - * Constructs a new runtime exception with the specified detail message. The - * cause is not initialized, and may subsequently be initialized by a call to - * {@link #initCause}. - * - * @param message the detail message. The detail message is saved for later - * retrieval by the {@link #getMessage()} method. - */ - public IllegalParameterException(String message) { - super(message); - } - - /** - * Constructs a new runtime exception with the specified cause and a detail - * message of {@code (cause==null ? null : cause.toString())} (which typically - * contains the class and detail message of {@code cause}). This constructor is - * useful for runtime exceptions that are little more than wrappers for other - * throwables. - * - * @param cause the cause (which is saved for later retrieval by the - * {@link #getCause()} method). (A {@code null} value is permitted, - * and indicates that the cause is nonexistent or unknown.) - * @since 1.4 - */ - public IllegalParameterException(Throwable cause) { - super(cause); - } - - /** - * 设置错误码 - * - * @param code 错误码 - * @return 当前对象 - */ - public IllegalParameterException setCode(int code) { - this.code = code; - return this; - } - - /** - * 获取错误码 - * - * @return 错误码 - */ - public int getCode() { - - return this.code; - } - - /** - * 获取携带的附加信息 - * - * @return 携带的附加信息 - */ - public Object getContext() { - return context; - } - - /** - * 设置携带的附加信息 - * - * @param context 附加信息 - * @return 当前对象 - */ - public IllegalParameterException setContext(Object context) { - this.context = context; - return this; - } - - /** - * 构建一个IllegalParameterException实例对象 - * - * @param msg 异常信息 - * @return CustomException实例对象 - */ - public static IllegalParameterException of(String msg) { - return new IllegalParameterException(msg); - } - - /** - * 构建一个CustomException实例对象 - * - * @param msg 异常信息 - * @param context 附带的信息 - * @return CustomException实例对象 - */ - public static IllegalParameterException of(String msg, Object context) { - IllegalParameterException exception = new IllegalParameterException(msg); - exception.setContext(context); - return exception; - } -} diff --git a/src/main/java/com/yishuifengxiao/common/tool/http/CookieUtil.java b/src/main/java/com/yishuifengxiao/common/tool/http/CookieUtil.java deleted file mode 100644 index 2175893..0000000 --- a/src/main/java/com/yishuifengxiao/common/tool/http/CookieUtil.java +++ /dev/null @@ -1,222 +0,0 @@ -package com.yishuifengxiao.common.tool.http; - -import com.yishuifengxiao.common.tool.collections.CollUtil; -import jakarta.servlet.http.Cookie; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -/** - *

- * cookie 操作工具 - *

- * 该工具的主要作用是对cookie进行操作,其主要的功能如下: - *
    - *
  1. 根据cookie里键的名字获取对应的值
  2. - *
  3. 根据cookie里键的名字移除对应的值
  4. - *
  5. 在cookie里增加一个键值对数据
  6. - *
- * - * @author yishui - * @version 1.0.0 - * @since 1.0.0 - */ -@Slf4j -public class CookieUtil { - - /** - * cookie的默认有效时间 - */ - public static final int COOKIE_HALF_HOUR = 30 * 60; - - /** - * HttpServletRequest - */ - private HttpServletRequest request; - - /** - * HttpServletResponse - */ - private HttpServletResponse response; - - /** - * 获得一个cookie操作实例 - * - * @param request HttpServletRequest - * @param response HttpServletResponse - * @return cookie操作实例 - */ - public static CookieUtil getInstance(HttpServletRequest request, HttpServletResponse response) { - return new CookieUtil(request, response); - } - - /** - * 根据Cookie名称得到Cookie对象,不存在该对象则返回Null - * - * @param name cookie的名字 - * @return Cookie对象 - */ - public Cookie getCookie(String name) { - Cookie[] cookies = request.getCookies(); - if (cookies == null || cookies.length < 1) { - return null; - } - Cookie cookie = null; - for (Cookie c : cookies) { - if (name.equals(c.getName())) { - cookie = c; - break; - } - } - return cookie; - } - - /** - * 根据Cookie名称直接得到Cookie值 - * - * @param name cookie的名字 - * @return Cookie值 - */ - public String getCookieValue(String name) { - Cookie cookie = getCookie(name); - if (cookie != null) { - return cookie.getValue(); - } - return null; - } - - /** - * 移除cookie - * - * @param name 这个是名称,不是值 - */ - public void removeCookie(String name) { - if (null == name) { - return; - } - Cookie cookie = getCookie(name); - if (null != cookie) { - cookie.setPath("/"); - cookie.setValue(""); - cookie.setMaxAge(0); - response.addCookie(cookie); - } - } - - /** - * 添加一条新的Cookie,可以指定过期时间(单位:秒) - * - * @param name cookie的名字 - * @param value cookie的值 - * @param maxValue 过期时间(单位:秒) - */ - public void setCookie(String name, String value, int maxValue) { - if (StringUtils.isBlank(name)) { - return; - } - if (null == value) { - value = ""; - } - Cookie cookie = new Cookie(name, value); - cookie.setPath("/"); - cookie.setSecure(true); - if (maxValue != 0) { - cookie.setMaxAge(maxValue); - } else { - cookie.setMaxAge(COOKIE_HALF_HOUR); - } - response.addCookie(cookie); - try { - response.flushBuffer(); - } catch (IOException e) { - log.debug("【易水工具】设置cookie时出现问题,出现问题的原因为 {} ", e.getMessage()); - } - } - - /** - * 添加一条新的Cookie,默认30分钟过期时间 - * - * @param name cookie的名字 - * @param value cookie的值 - */ - public void setCookie(String name, String value) { - setCookie(name, value, COOKIE_HALF_HOUR); - } - - /** - * 将cookie封装到Map里面 - * - * @return cookie数据 - */ - public Map getCookieMap() { - - Cookie[] cookies = request.getCookies(); - if (CollUtil.isNotEmpty(cookies)) { - Map cookieMap = new HashMap<>(cookies.length); - for (Cookie cookie : cookies) { - cookieMap.put(cookie.getName(), cookie); - } - } - return null; - } - - /** - * 获取HttpServletRequest - * - * @return HttpServletRequest - */ - public HttpServletRequest getRequest() { - return request; - } - - /** - * 设置 HttpServletRequest - * - * @param request HttpServletRequest - */ - public void setRequest(HttpServletRequest request) { - this.request = request; - } - - /** - * 获取HttpServletResponse - * - * @return HttpServletResponse - */ - public HttpServletResponse getResponse() { - return response; - } - - /** - * 设置HttpServletResponse - * - * @param response HttpServletResponse - */ - public void setResponse(HttpServletResponse response) { - this.response = response; - } - - /** - * 全参构造函数 - * - * @param request HttpServletRequest - * @param response HttpServletResponse - */ - public CookieUtil(HttpServletRequest request, HttpServletResponse response) { - this.request = request; - this.response = response; - } - - /** - * 无参构造函数 - */ - public CookieUtil() { - - } - -} \ No newline at end of file diff --git a/src/main/java/com/yishuifengxiao/common/tool/http/HttpUtil.java b/src/main/java/com/yishuifengxiao/common/tool/http/HttpUtil.java index 74ddca1..79e686d 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/http/HttpUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/http/HttpUtil.java @@ -2,7 +2,6 @@ import com.yishuifengxiao.common.tool.text.RegexUtil; import com.yishuifengxiao.common.tool.utils.OsUtils; -import jakarta.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import java.util.HashMap; @@ -47,7 +46,7 @@ public final class HttpUtil { * HTTP_CLIENT_IP:一些代理服务器 * X-Real-IP:nginx服务代理 */ - private final static List IP_HEAD_LIST = Stream.of("X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED", "HTTP_X_CLUSTER_CLIENT_IP", "HTTP_CLIENT_IP", "HTTP_FORWARDED_FOR", "HTTP_FORWARDED", "HTTP_VIA", "REMOTE_ADDR", "X-Real-IP").collect(Collectors.toList()); + public final static List IP_HEAD_LIST = Stream.of("X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP", "HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED", "HTTP_X_CLUSTER_CLIENT_IP", "HTTP_CLIENT_IP", "HTTP_FORWARDED_FOR", "HTTP_FORWARDED", "HTTP_VIA", "REMOTE_ADDR", "X-Real-IP").collect(Collectors.toList()); /** @@ -207,14 +206,4 @@ public static Map queryString2Map(String queryString) { } - /** - * 获取访问者的远程IP,多个代理的情况,第一个IP为客户端真实IP - * - * @param request HttpServletRequest - * @return 访问者的远程IP - */ - public static String getVisitorIp(HttpServletRequest request) { - return IP_HEAD_LIST.stream().map(request::getHeader).filter(StringUtils::isNotBlank).filter("unknown"::equalsIgnoreCase).map(ip -> ip.split(",")[0]).findFirst().orElseGet(() -> OsUtils.LOCAL_IPV6.equals(request.getRemoteAddr()) || StringUtils.isBlank(request.getRemoteAddr()) ? OsUtils.LOCAL_IPV4 : request.getRemoteAddr()); - } - } diff --git a/src/main/java/com/yishuifengxiao/common/tool/io/ImageUtil.java b/src/main/java/com/yishuifengxiao/common/tool/io/ImageUtil.java index 903bef7..a6c847d 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/io/ImageUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/io/ImageUtil.java @@ -2,7 +2,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.springframework.util.Base64Utils; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; @@ -78,7 +77,7 @@ public static String imageToBase64ByLocal(File image) throws IOException { CloseUtil.close(inputStream); } // 返回Base64编码过的字节数组字符串 - return Base64Utils.encodeToString(data); + return Base64.getEncoder().encodeToString(data); } /** @@ -117,7 +116,7 @@ public static File base64ToImage(String imgBase64Str, File img) throws IOExcepti try { imgBase64Str = imgBase64Str.replaceAll(FORMAT_TEMPLATE, ""); // Base64解码 - byte[] b = Base64Utils.decodeFromString(imgBase64Str); + byte[] b = Base64.getDecoder().decode(imgBase64Str); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) { // 调整异常数据 @@ -195,7 +194,7 @@ public static BufferedImage base64ToBufferedImage(String base64) { } ByteArrayInputStream byteArrayInputStream = null; try { - byte[] b = Base64Utils.decodeFromString(base64); + byte[] b = Base64.getDecoder().decode(base64); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) { // 调整异常数据 diff --git a/src/main/java/com/yishuifengxiao/common/tool/io/IoUtil.java b/src/main/java/com/yishuifengxiao/common/tool/io/IoUtil.java index 5956841..299f850 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/io/IoUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/io/IoUtil.java @@ -7,7 +7,6 @@ import com.yishuifengxiao.common.tool.random.IdWorker; import jakarta.validation.constraints.NotNull; import org.apache.commons.lang3.StringUtils; -import org.springframework.util.StreamUtils; import java.io.*; import java.nio.charset.Charset; @@ -82,7 +81,7 @@ public static byte[] inputStream2ByteArray(InputStream inputStream) throws IOExc if (inputStream == null) { return new byte[0]; } - ByteArrayOutputStream out = new ByteArrayOutputStream(StreamUtils.BUFFER_SIZE); + ByteArrayOutputStream out = new ByteArrayOutputStream(1024); copy(inputStream, out); return out.toByteArray(); } diff --git a/src/main/java/com/yishuifengxiao/common/tool/log/LogInfo.java b/src/main/java/com/yishuifengxiao/common/tool/log/LogInfo.java index 21de0a0..5e11ca8 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/log/LogInfo.java +++ b/src/main/java/com/yishuifengxiao/common/tool/log/LogInfo.java @@ -6,7 +6,6 @@ import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; -import org.springframework.format.annotation.DateTimeFormat; import java.io.Serializable; import java.util.Date; @@ -16,7 +15,7 @@ * 自定义日志的内容 *

* 利用redis的订阅发布功能,将信息通过redis发布时的内容的格式 - * + * * @author yishui * @version 1.0.0 * @since 1.0.0 @@ -28,51 +27,50 @@ @Builder public class LogInfo implements Serializable { - /** - * - */ - private static final long serialVersionUID = 5380449689877786879L; + /** + * + */ + private static final long serialVersionUID = 5380449689877786879L; - /** - * 日志的时间 - */ - @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8") - private Date date; + /** + * 日志的时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS", timezone = "GMT+8") + private Date date; - /** - * 线程名字 - */ - private String threadName; + /** + * 线程名字 + */ + private String threadName; - /** - * logger 的名字 - */ - private String loggerName; + /** + * logger 的名字 + */ + private String loggerName; - /** - * 日志级别 - */ - private String level; + /** + * 日志级别 + */ + private String level; - /** - * 原始消息【格式化之后的消息】 - */ - private String message; + /** + * 原始消息【格式化之后的消息】 + */ + private String message; - /** - * 应用名称 - */ - private String application; + /** + * 应用名称 + */ + private String application; - /** - * 附加信息 - */ - private String extra; + /** + * 附加信息 + */ + private String extra; - /** - * 发送时间 - */ - private Long timeStamp; + /** + * 发送时间 + */ + private Long timeStamp; } diff --git a/src/main/java/com/yishuifengxiao/common/tool/log/RedisAppender.java b/src/main/java/com/yishuifengxiao/common/tool/log/RedisAppender.java deleted file mode 100644 index 37dc77d..0000000 --- a/src/main/java/com/yishuifengxiao/common/tool/log/RedisAppender.java +++ /dev/null @@ -1,435 +0,0 @@ -package com.yishuifengxiao.common.tool.log; - -import ch.qos.logback.classic.spi.LoggingEvent; -import ch.qos.logback.core.UnsynchronizedAppenderBase; -import ch.qos.logback.core.spi.DeferredProcessingAware; -import ch.qos.logback.core.status.ErrorStatus; -import com.yishuifengxiao.common.tool.collections.JsonUtil; -import io.lettuce.core.RedisClient; -import io.lettuce.core.RedisURI; -import io.lettuce.core.RedisURI.Builder; -import io.lettuce.core.api.StatefulRedisConnection; -import io.lettuce.core.api.async.RedisAsyncCommands; -import org.apache.commons.lang3.StringUtils; - -import java.io.IOException; -import java.net.InetAddress; -import java.time.Duration; -import java.time.temporal.ChronoUnit; -import java.util.Date; -import java.util.concurrent.locks.ReentrantLock; - -/** - *

- * 自定义日志输出器 - *

- *

- * 该工具是logback日志里的一个自定义日志输出器,主要是利用redis的订阅发布功能, 将日志信息通过redis发布出来,以便其他终端能够接收到日志信息 - *

- * 使用方法如下: - * - *
- * {@code
- *  
- * 	Redis服务器地址
- * 	Redis服务器端口号 
- * 	redis连接密码
- * 	项目名字,可选,如 测试项目 
- * 	发送的订阅通道的名字,选填,默认为application的值 
- * 	附加数据,会被加载在日志中 
- * 
- * 
- * }
- * 
- * - * - * - * - * @author yishui - * @version 1.0.0 - * @since 1.0.0 - * @param 数据类型 - */ -public class RedisAppender extends UnsynchronizedAppenderBase { - - /** - * 默认的端口 6379 - */ - private static final int DEFAULT_PORT = 6379; - - /** - * 默认的超时时间,180秒 - */ - private static final int DEFAULT_TIMEOUT_SECOND = 180; - - /** - * All synchronization in this class is done via the lock object. - */ - protected final ReentrantLock lock = new ReentrantLock(false); - - private RedisClient redisClient; - - private StatefulRedisConnection connection; - - private RedisAsyncCommands async; - - /** - * Redis 数据库地址 - */ - private String host; - - /** - * Redis 数据库密码 - */ - private String password; - - /** - * Redis 数据库端口,默认为6379 - */ - private Integer port; - - /** - * Redis 订阅通道名字 - */ - private String channel; - - /** - * Redis 连接过期时间 - */ - private Integer timeoutInSecond; - - /** - * 应用名称 - */ - private String application; - - /** - * 附加信息 - */ - private String extra; - - @Override - protected void append(E eventObject) { - if (!isStarted()) { - return; - } - subAppend(eventObject); - } - - /** - * 获取连接的端口 - * - * @return 连接的端口 - */ - protected int port() { - if (null == this.port) { - return DEFAULT_PORT; - } - return this.port; - } - - /** - * 获取连接的超时时间,单位为秒 - * - * @return 连接的超时时间,单位为秒 - */ - protected int timeout() { - if (null == this.timeoutInSecond) { - return DEFAULT_TIMEOUT_SECOND; - } - return this.timeoutInSecond; - } - - /** - * Redis 订阅通道名字 - * - * @return Redis 订阅通道名字 - */ - protected String channel() { - if (null == this.channel || "".equals(this.channel.trim())) { - return this.application(); - } - return this.channel.trim(); - } - - /** - * 获取应用名字,如果应用名字为空就使用当前的主机名 - * - * @return 应用名字 - */ - protected String application() { - if (StringUtils.isBlank(this.application)) { - try { - return InetAddress.getLocalHost().getHostName(); - } catch (Exception e) { - } - } - - return this.application.trim(); - } - - /** - * Actual writing occurs here. - *

- * Most subclasses of WriterAppender will need to override this - * method. - * - * @param event 日志事件 - */ - protected void subAppend(E event) { - if (!isStarted()) { - return; - } - try { - // this step avoids LBCLASSIC-139 - if (event instanceof DeferredProcessingAware) { - ((DeferredProcessingAware) event).prepareForDeferredProcessing(); - } - // the synchronization prevents the OutputStream from being closed while we - // are writing. It also prevents multiple threads from entering the same - // converter. Converters assume that they are in a synchronized block. - // lock.lock(); - - if (event instanceof LoggingEvent) { - LoggingEvent e = (LoggingEvent) event; - // 提取出需要输出的信息 - LogInfo logInfo = LogInfo.builder() - // 应用名字 - .application(this.application()) - // 附加信息 - .extra(this.extra) - // 线程名字 - .threadName(e.getThreadName()) - // 时间戳 - .timeStamp(e.getTimeStamp()) - // 解析后的时间 - .date(new Date(e.getTimeStamp())) - // logger 名字 - .loggerName(e.getLoggerName()) - // 日志级别 - .level(e.getLevel().toString()) - // 日志消息 - .message(e.getFormattedMessage()) - // 构建 - .build(); - writeBytes(logInfo); - } - - } catch (Exception ioe) { - // as soon as an exception occurs, move to non-started state - // and add a single ErrorStatus to the SM. - this.started = false; - addStatus(new ErrorStatus("IO failure in appender", this, ioe)); - } - } - - private void writeBytes(LogInfo logInfo) throws IOException { - if (null == logInfo) { - return; - } - - lock.lock(); - try { - this.async.publish(this.channel(), JsonUtil.toJSONString(logInfo)); - } finally { - lock.unlock(); - } - } - - @Override - public void start() { - int errors = 0; - if (null == this.host || "".equals(this.host.trim())) { - addStatus(new ErrorStatus("The address of redis database cannot be empty", this)); - errors++; - } - try { - this.init(); - } catch (Exception e) { - addStatus(new ErrorStatus("Failed to initialize log output. The reason for failure is " + e.getMessage(), - this)); - errors++; - } - // only error free appenders should be activated - if (errors == 0) { - super.start(); - } - - } - - /** - * 初始化 - */ - @SuppressWarnings("deprecation") - private void init() { - // <1> 创建单机连接的连接信息 - Builder builder = RedisURI.builder().withHost(this.host.trim()).withPort(this.port()) - .withTimeout(Duration.of(this.timeout(), ChronoUnit.SECONDS)); - // 设置密码 - if (StringUtils.isNotBlank(this.password)) { - builder = builder.withPassword(this.password.trim()); - } - // <2> 创建客户端 - this.redisClient = RedisClient.create(builder.build()); - // <3> 创建线程安全的连接 - this.connection = redisClient.connect(); - // <4> 创建同步命令 - this.async = connection.async(); - } - - @Override - public void stop() { - - lock.lock(); - try { - close(); - super.stop(); - } finally { - lock.unlock(); - } - - } - - private void close() { - try { // ... 其他操作 - if (null != this.connection) { - connection.close(); // <5> 关闭连接 - } - - if (null != this.redisClient) { - redisClient.shutdown(); // <6> 关闭客户端} - } - } catch (Exception e) { - addStatus(new ErrorStatus( - "Failed to close the redis connection. The reason for the failure is " + e.getMessage(), this, e)); - } - - } - - /** - * 设置Redis 数据库地址 - * - * @return Redis 数据库地址 - */ - public String getHost() { - return host; - } - - /** - * 设置 Redis 数据库地址 - * - * @param host Redis 数据库地址 - */ - public void setHost(String host) { - this.host = host; - } - - /** - * 获取设置的 Redis 数据库密码 - * - * @return Redis 数据库密码 - */ - public String getPassword() { - return password; - } - - /** - * 设置 Redis 数据库密码,如果redis数据没有连接密码可以不用设置 - * - * @param password Redis 数据库密码 - */ - public void setPassword(String password) { - this.password = password; - } - - /** - * 获取 Redis 数据库端口 - * - * @return Redis 数据库端口 - */ - public Integer getPort() { - return port; - } - - /** - * 设置 Redis 数据库端口 - * - * @param port Redis 数据库端口,默认为6379 - */ - public void setPort(Integer port) { - this.port = port; - } - - /** - * 获取Redis 连接过期时间 - * - * @return Redis 连接过期时间 - */ - public Integer getTimeoutInSecond() { - return timeoutInSecond; - } - - /** - * 设置Redis 连接过期时间 - * - * @param timeoutInSecond Redis 连接过期时间,单位为秒 - */ - public void setTimeoutInSecond(Integer timeoutInSecond) { - this.timeoutInSecond = timeoutInSecond; - } - - /** - * 获取Redis 订阅通道名字 - * - * @return Redis 订阅通道名字 - */ - public String getChannel() { - return channel; - } - - /** - * 设置 Redis 订阅通道 - * - * @param channel Redis 订阅通道 - */ - public void setChannel(String channel) { - this.channel = channel; - } - - /** - * 获取应用名称 - * - * @return 应用名称 - */ - public String getApplication() { - return application; - } - - /** - * 设置 应用名称 - * - * @param application 应用名称 - */ - public void setApplication(String application) { - this.application = application; - } - - /** - * 获取附加信息 - * - * @return 附加信息 - */ - public String getExtra() { - return extra; - } - - /** - * 设置附加信息 - * - * @param extra 附加信息 - */ - public void setExtra(String extra) { - this.extra = extra; - } - -} diff --git a/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java b/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java index b32d1b9..d621c94 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java @@ -5,6 +5,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.function.Supplier; /** *

@@ -16,6 +17,98 @@ * @since 1.0.0 */ public class ExceptionUtil { + + /** + * 抛出一个运行时异常 + * + * @param e 运行时异常 + */ + public static final void throwThrowable(RuntimeException e) { + if (null != e) { + throw e; + } + } + + /** + * 判断给定的值是否为true,若为null或false则抛出异常 + * + * @param val 待判断的值 + * @param msg 异常提示信息 + */ + public static void isTrue(Boolean val, String msg) { + if (null == val || !val) { + throw new UncheckedException(msg); + } + } + + /** + * 判断给定的值是否为true或null,若为false则抛出异常 + * + * @param val 待判断的值 + * @param msg 异常提示信息 + */ + public static void isTrueOrNull(Boolean val, String msg) { + if (null != val && !val) { + throw new UncheckedException(msg); + } + } + + /** + * 判断给定的值是否为false,若为null或true则抛出异常 + * + * @param val 待判断的值 + * @param msg 异常提示信息 + */ + public static void isFalse(Boolean val, String msg) { + if (null == val || val) { + throw new UncheckedException(msg); + } + } + + /** + * 判断给定的值是否为false或null,若为true则抛出异常 + * + * @param val 待判断的值 + * @param msg 异常提示信息 + */ + public static void isFalseOrNull(Boolean val, String msg) { + if (null != val && val) { + throw new UncheckedException(msg); + } + } + + + /** + * 生成一个 Supplier + * + * @param message 提示信息 + * @return Supplier + */ + public static final Supplier orElseThrow(String message) { + return () -> new UncheckedException(message); + } + + /** + * 生成一个 Supplier + * + * @param code 错误码 + * @param message 提示信息 + * @return Supplier + */ + public static final Supplier orElseThrow(int code, String message) { + return () -> new UncheckedException(code, message); + } + + /** + * 生成一个Supplier + * + * @param exception 异常信息 + * @return Supplier + */ + public static final Supplier orElseThrow(UncheckedException exception) { + return () -> exception; + } + /** * 提取出异常中的所有输出信息 * From 78b0ea7ce49a1277fffb1ca9aacb8b6bf88ec70a Mon Sep 17 00:00:00 2001 From: zhiyubujian Date: Tue, 22 Oct 2024 18:12:29 +0800 Subject: [PATCH 2/6] Simplify dependencies Simplify dependencies --- README.md | 8 +- pom.xml | 15 +- .../common/tool/{encoder => codec}/AES.java | 2 +- .../common/tool/{encoder => codec}/DES.java | 2 +- .../common/tool/{encoder => codec}/Md5.java | 16 +- .../tool/{encoder => codec}/package-info.java | 2 +- .../common/tool/collections/DataUtil.java | 1 + .../common/tool/collections/MapUtil.java | 78 ----- .../common/tool/collections/StreamUtil.java | 20 +- .../tool/exception/CustomException.java | 331 +++++++++--------- .../tool/exception/UncheckedException.java | 19 +- .../tool/http/{HttpUtil.java => UrlUtil.java} | 4 +- .../common/tool/lang/NumberUtil.java | 101 +++++- .../common/tool/text/RegexUtil.java | 2 +- .../common/tool/utils/ExceptionUtil.java | 19 +- 15 files changed, 313 insertions(+), 307 deletions(-) rename src/main/java/com/yishuifengxiao/common/tool/{encoder => codec}/AES.java (99%) rename src/main/java/com/yishuifengxiao/common/tool/{encoder => codec}/DES.java (96%) rename src/main/java/com/yishuifengxiao/common/tool/{encoder => codec}/Md5.java (86%) rename src/main/java/com/yishuifengxiao/common/tool/{encoder => codec}/package-info.java (66%) delete mode 100644 src/main/java/com/yishuifengxiao/common/tool/collections/MapUtil.java rename src/main/java/com/yishuifengxiao/common/tool/http/{HttpUtil.java => UrlUtil.java} (96%) diff --git a/README.md b/README.md index 95da549..5d1b9e2 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ | [com.yishuifengxiao.common.tool.context](https://apidoc.gitee.com/zhiyubujian/tool/com/yishuifengxiao/common/tool/context/package-summary.html) | 数据存储工具 | | [com.yishuifengxiao.common.tool.converter](https://apidoc.gitee.com/zhiyubujian/tool/com/yishuifengxiao/common/tool/converter/package-summary.html) | 数据转换工具 | | [com.yishuifengxiao.common.tool.datetime](https://apidoc.gitee.com/zhiyubujian/tool/com/yishuifengxiao/common/tool/datetime/package-summary.html) | 日期时间工具 | -| [com.yishuifengxiao.common.tool.encoder](https://apidoc.gitee.com/zhiyubujian/tool/com/yishuifengxiao/common/tool/encoder/package-summary.html) | 加解密工具 | +| [com.yishuifengxiao.common.tool.codec](https://apidoc.gitee.com/zhiyubujian/tool/com/yishuifengxiao/common/tool/encoder/package-summary.html) | 加解密工具 | | [com.yishuifengxiao.common.tool.entity](https://apidoc.gitee.com/zhiyubujian/tool/com/yishuifengxiao/common/tool/entity/package-summary.html) | 基础通用对象 | | [com.yishuifengxiao.common.tool.exception](https://apidoc.gitee.com/zhiyubujian/tool/com/yishuifengxiao/common/tool/exception/package-summary.html) | 自定义异常 | | [com.yishuifengxiao.common.tool.exception.constant](https://apidoc.gitee.com/zhiyubujian/tool/com/yishuifengxiao/common/tool/exception/constant/package-summary.html) | 异常错误码常量 | @@ -310,7 +310,7 @@ LocalDateTime parse1 = DateTimeUtil.parse("2021-10-10 12:12:12", "yyyy-MM-dd HH: 工具路径: ```java -com.yishuifengxiao.common.tool.encoder.AES +com.yishuifengxiao.common.tool.codec.AES ``` 使用示例: @@ -332,7 +332,7 @@ AES.decrypt("秘钥", encrypt); 工具路径: ```java -com.yishuifengxiao.common.tool.encoder.DES +com.yishuifengxiao.common.tool.codec.DES ``` 使用示例: @@ -351,7 +351,7 @@ DES.decrypt("秘钥", encrypt); 工具路径: ```java -com.yishuifengxiao.common.tool.encoder.Md5 +com.yishuifengxiao.common.tool.codec.Md5 ``` 使用示例: diff --git a/pom.xml b/pom.xml index d1af6bd..2d5e012 100644 --- a/pom.xml +++ b/pom.xml @@ -78,15 +78,6 @@ org.apache.commons commons-lang3 - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - - - commons-codec - commons-codec - ch.qos.logback logback-classic @@ -120,6 +111,12 @@ com.jayway.jsonpath json-path + + + slf4j-api + org.slf4j + + com.fasterxml.jackson.core diff --git a/src/main/java/com/yishuifengxiao/common/tool/encoder/AES.java b/src/main/java/com/yishuifengxiao/common/tool/codec/AES.java similarity index 99% rename from src/main/java/com/yishuifengxiao/common/tool/encoder/AES.java rename to src/main/java/com/yishuifengxiao/common/tool/codec/AES.java index e44e3c9..c14ecb6 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/encoder/AES.java +++ b/src/main/java/com/yishuifengxiao/common/tool/codec/AES.java @@ -1,4 +1,4 @@ -package com.yishuifengxiao.common.tool.encoder; +package com.yishuifengxiao.common.tool.codec; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; diff --git a/src/main/java/com/yishuifengxiao/common/tool/encoder/DES.java b/src/main/java/com/yishuifengxiao/common/tool/codec/DES.java similarity index 96% rename from src/main/java/com/yishuifengxiao/common/tool/encoder/DES.java rename to src/main/java/com/yishuifengxiao/common/tool/codec/DES.java index 89b349e..48ef687 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/encoder/DES.java +++ b/src/main/java/com/yishuifengxiao/common/tool/codec/DES.java @@ -1,4 +1,4 @@ -package com.yishuifengxiao.common.tool.encoder; +package com.yishuifengxiao.common.tool.codec; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; diff --git a/src/main/java/com/yishuifengxiao/common/tool/encoder/Md5.java b/src/main/java/com/yishuifengxiao/common/tool/codec/Md5.java similarity index 86% rename from src/main/java/com/yishuifengxiao/common/tool/encoder/Md5.java rename to src/main/java/com/yishuifengxiao/common/tool/codec/Md5.java index 18832a1..f393d28 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/encoder/Md5.java +++ b/src/main/java/com/yishuifengxiao/common/tool/codec/Md5.java @@ -1,8 +1,7 @@ -package com.yishuifengxiao.common.tool.encoder; +package com.yishuifengxiao.common.tool.codec; import com.yishuifengxiao.common.tool.io.CloseUtil; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.codec.binary.Hex; import org.apache.commons.lang3.StringUtils; import java.io.File; @@ -26,8 +25,7 @@ */ @Slf4j public class Md5 { - private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", - "e", "f"}; + private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; /** * 对字符串md5加密(小写+字母) @@ -98,12 +96,18 @@ public synchronized static String md5(File file) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); inputStream = new FileInputStream(file); - byte[] buffer = new byte[8192]; + byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { md5.update(buffer, 0, length); } - return new String(Hex.encodeHex(md5.digest())); + // 将字节数组转换成十六进制格式的字符串 + StringBuilder sb = new StringBuilder(); + // 获取最终的摘要字节 + for (byte b : md5.digest()) { + sb.append(String.format("%02x", b)); + } + return sb.toString().toLowerCase(); } catch (Exception e) { log.info("计算文件{}的md5值时出现问题{}", file, e.getMessage()); return null; diff --git a/src/main/java/com/yishuifengxiao/common/tool/encoder/package-info.java b/src/main/java/com/yishuifengxiao/common/tool/codec/package-info.java similarity index 66% rename from src/main/java/com/yishuifengxiao/common/tool/encoder/package-info.java rename to src/main/java/com/yishuifengxiao/common/tool/codec/package-info.java index be7bbff..5789ece 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/encoder/package-info.java +++ b/src/main/java/com/yishuifengxiao/common/tool/codec/package-info.java @@ -8,4 +8,4 @@ * @version 1.0.0 * @since 1.0.0 */ -package com.yishuifengxiao.common.tool.encoder; \ No newline at end of file +package com.yishuifengxiao.common.tool.codec; \ No newline at end of file diff --git a/src/main/java/com/yishuifengxiao/common/tool/collections/DataUtil.java b/src/main/java/com/yishuifengxiao/common/tool/collections/DataUtil.java index 353486e..05dd15d 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/collections/DataUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/collections/DataUtil.java @@ -157,6 +157,7 @@ public static List toList(T[] objs) { return Arrays.asList(objs).stream().collect(Collectors.toList()); } + /** * 将数组转换成Set * diff --git a/src/main/java/com/yishuifengxiao/common/tool/collections/MapUtil.java b/src/main/java/com/yishuifengxiao/common/tool/collections/MapUtil.java deleted file mode 100644 index f3ef187..0000000 --- a/src/main/java/com/yishuifengxiao/common/tool/collections/MapUtil.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.yishuifengxiao.common.tool.collections; - -import java.util.Map; -import java.util.TreeMap; - -/** - *

- * map生成工具 - *

- * 该工具的主要目的是采用链式接口快速生成一个map对象 - * - * @author yishui - * @version 1.0.0 - * @since 1.0.0 - */ -public final class MapUtil { - - /** - * 数据存储对象 - */ - private final Map map = new TreeMap<>(); - - /** - * 获取一个map工具类实例 - * - * @return map工具类实例 - */ - @SuppressWarnings("rawtypes") - public static MapUtil map() { - return new MapUtil(); - } - - - /** - * 输出最终需要的map数据 - * - * @param Key的数据类型 - * @param 值得数据类型 - * @return 最终需要的map数据 - */ - @SuppressWarnings("rawtypes") - public Map build() { - return this.map; - } - - /** - *

- * 增加一个键值对数据 - *

- * 如果增加的键值对的键为空,则不会增加该条记录 - * - * @param key 数据的键 - * @param value 数据的值 - * @return 当前map工具类实例 - */ - public MapUtil put(K key, V value) { - if (null != key && null != value) { - this.map.put(key, value); - } - return this; - } - - - /** - * 批量添加一组键值对 - * - * @param map 键值对 - * @return 当前map工具类实例 - */ - public MapUtil putAll(Map map) { - if (null != map) { - map.forEach((k, v) -> put(k, v)); - } - return this; - } - - -} diff --git a/src/main/java/com/yishuifengxiao/common/tool/collections/StreamUtil.java b/src/main/java/com/yishuifengxiao/common/tool/collections/StreamUtil.java index 32ddc2a..cd742cd 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/collections/StreamUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/collections/StreamUtil.java @@ -26,7 +26,7 @@ public class StreamUtil { * @param 数据类型 * @return 任意一个满足条件的非空数据 */ - public static T findAny(Stream stream, Predicate predicate) { + public synchronized static T findAny(Stream stream, Predicate predicate) { if (null == stream) { return null; } @@ -42,7 +42,7 @@ public static T findAny(Stream stream, Predicate predicate) { * @param 数据类型 * @return 任意一个满足条件的非空数据 */ - public static T findAny(List list, Predicate predicate) { + public synchronized static T findAny(List list, Predicate predicate) { if (null == list) { return null; } @@ -58,7 +58,7 @@ public static T findAny(List list, Predicate predicate) { * @param 数据类型 * @return 任意一个满足条件的非空数据 */ - public static T findAny(Set set, Predicate predicate) { + public synchronized static T findAny(Set set, Predicate predicate) { if (null == set) { return null; } @@ -73,7 +73,7 @@ public static T findAny(Set set, Predicate predicate) { * @param 数据类型 * @return 存在返回为true,否则为false */ - public static boolean contain(List list, Predicate predicate) { + public synchronized static boolean contain(List list, Predicate predicate) { return null != findAny(list, predicate); } @@ -85,7 +85,7 @@ public static boolean contain(List list, Predicate predicate) * @param 数据类型 * @return 存在返回为true,否则为false */ - public static boolean contain(Set set, Predicate predicate) { + public synchronized static boolean contain(Set set, Predicate predicate) { return null != findAny(set, predicate); } @@ -98,7 +98,7 @@ public static boolean contain(Set set, Predicate predicate) { * @param 数据类型 * @return 删除元素之后的集合 */ - public static List remove(List list, Predicate predicate) { + public synchronized static List remove(List list, Predicate predicate) { if (null == list) { list = new ArrayList<>(); } @@ -117,7 +117,7 @@ public static List remove(List list, Predicate predicate) { * @param 数据类型 * @return 删除元素之后的集合 */ - public static Set remove(Set set, Predicate predicate) { + public synchronized static Set remove(Set set, Predicate predicate) { if (null == set) { set = new HashSet<>(); } @@ -137,7 +137,7 @@ public static Set remove(Set set, Predicate predicate) { * @return 替换元素之后的集合 */ @SuppressWarnings({"rawtypes", "unchecked"}) - public static List replace(List list, T t, Predicate predicate) { + public synchronized static List replace(List list, T t, Predicate predicate) { if (null == list) { list = new ArrayList<>(); } @@ -156,7 +156,7 @@ public static List replace(List list, T t, Predicate predic * @return 替换元素之后的集合 */ @SuppressWarnings({"rawtypes", "unchecked"}) - public static Set replace(Set set, T t, Predicate predicate) { + public synchronized static Set replace(Set set, T t, Predicate predicate) { if (null == set) { set = new HashSet<>(); } @@ -174,7 +174,7 @@ public static Set replace(Set set, T t, Predicate predicate * @param 数据类型 * @return 处理之后的元素集合 */ - public static List addIfNoPresent(List list, T t, Predicate predicate) { + public synchronized static List addIfNoPresent(List list, T t, Predicate predicate) { if (null == list) { list = new ArrayList<>(); } diff --git a/src/main/java/com/yishuifengxiao/common/tool/exception/CustomException.java b/src/main/java/com/yishuifengxiao/common/tool/exception/CustomException.java index 95f31dd..73fb1c1 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/exception/CustomException.java +++ b/src/main/java/com/yishuifengxiao/common/tool/exception/CustomException.java @@ -15,170 +15,169 @@ */ public class CustomException extends Exception { - /** - * - */ - private static final long serialVersionUID = 1825226802361294349L; - - /** - * 错误码 - */ - protected int code; - - /** - * 携带的附加信息 - */ - protected transient Object context; - - /** - * Constructs a new runtime exception with {@code null} as its detail message. - * The cause is not initialized, and may subsequently be initialized by a call - * to {@link #initCause}. - */ - public CustomException() { - - } - - /** - * Constructs a new runtime exception with the specified detail message. The - * cause is not initialized, and may subsequently be initialized by a call to - * {@link #initCause}. - * - * @param message the detail message. The detail message is saved for later - * retrieval by the {@link #getMessage()} method. - */ - public CustomException(String message) { - this(0, message); - - } - - /** - * Constructs a new runtime exception with the specified detail message. The - * cause is not initialized, and may subsequently be initialized by a call to - * {@link #initCause}. - * - * @param code 异常码 - * @param message the detail message. The detail message is saved for later - * retrieval by the {@link #getMessage()} method. - */ - public CustomException(int code, String message) { - super(message); - this.code = code; - } - - /** - * Constructs a new runtime exception with the specified cause and a detail - * message of {@code (cause==null ? null : cause.toString())} (which typically - * contains the class and detail message of {@code cause}). This constructor is - * useful for runtime exceptions that are little more than wrappers for other - * throwables. - * - * @param cause the cause (which is saved for later retrieval by the - * {@link #getCause()} method). (A {@code null} value is permitted, - * and indicates that the cause is nonexistent or unknown.) - * @since 1.4 - */ - public CustomException(Throwable cause) { - super(cause); - } - - /** - * Constructs a new runtime exception with the specified detail message and - * cause. - *

- * Note that the detail message associated with {@code cause} is not - * automatically incorporated in this runtime exception's detail message. - * - * @param message the detail message (which is saved for later retrieval by the - * {@link #getMessage()} method). - * @param cause the cause (which is saved for later retrieval by the - * {@link #getCause()} method). (A {@code null} value is - * permitted, and indicates that the cause is nonexistent or - * unknown.) - * @since 1.4 - */ - public CustomException(String message, Throwable cause) { - super(message, cause); - } - - /** - * Constructs a new runtime exception with the specified detail message, cause, - * suppression enabled or disabled, and writable stack trace enabled or - * disabled. - * - * @param message the detail message. - * @param cause the cause. (A {@code null} value is permitted, and - * indicates that the cause is nonexistent or - * unknown.) - * @param enableSuppression whether or not suppression is enabled or disabled - * @param writableStackTrace whether or not the stack trace should be writable - * - * @since 1.7 - */ - public CustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - - /** - * 获取错误码 - * - * @return 错误码 - */ - public int getCode() { - return code; - } - - /** - * 设置错误码 - * - * @param code 错误码 - * @return 当前对象 - */ - public CustomException setCode(int code) { - this.code = code; - return this; - } - - /** - * 获取携带的附加信息 - * - * @return 携带的附加信息 - */ - public Object getContext() { - return context; - } - - /** - * 设置携带的附加信息 - * - * @param context 附加信息 - * @return 当前对象 - */ - public CustomException setContext(Object context) { - this.context = context; - return this; - } - - /** - * 构建一个CustomException实例对象 - * - * @param msg 异常信息 - * @return CustomException实例对象 - */ - public static CustomException of(String msg) { - return new CustomException(msg); - } - - /** - * 构建一个CustomException实例对象 - * - * @param msg 异常信息 - * @param context 附带的信息 - * @return CustomException实例对象 - */ - public static CustomException of(String msg, Object context) { - CustomException exception = new CustomException(msg); - exception.setContext(context); - return exception; - } + /** + * + */ + private static final long serialVersionUID = 1825226802361294349L; + + /** + * 错误码 + */ + protected Integer code; + + /** + * 携带的附加信息 + */ + protected transient Object context; + + /** + * Constructs a new runtime exception with {@code null} as its detail message. + * The cause is not initialized, and may subsequently be initialized by a call + * to {@link #initCause}. + */ + public CustomException() { + + } + + /** + * Constructs a new runtime exception with the specified detail message. The + * cause is not initialized, and may subsequently be initialized by a call to + * {@link #initCause}. + * + * @param message the detail message. The detail message is saved for later + * retrieval by the {@link #getMessage()} method. + */ + public CustomException(String message) { + this(0, message); + + } + + /** + * Constructs a new runtime exception with the specified detail message. The + * cause is not initialized, and may subsequently be initialized by a call to + * {@link #initCause}. + * + * @param code 异常码 + * @param message the detail message. The detail message is saved for later + * retrieval by the {@link #getMessage()} method. + */ + public CustomException(Integer code, String message) { + super(message); + this.code = code; + } + + /** + * Constructs a new runtime exception with the specified cause and a detail + * message of {@code (cause==null ? null : cause.toString())} (which typically + * contains the class and detail message of {@code cause}). This constructor is + * useful for runtime exceptions that are little more than wrappers for other + * throwables. + * + * @param cause the cause (which is saved for later retrieval by the + * {@link #getCause()} method). (A {@code null} value is permitted, + * and indicates that the cause is nonexistent or unknown.) + * @since 1.4 + */ + public CustomException(Throwable cause) { + super(cause); + } + + /** + * Constructs a new runtime exception with the specified detail message and + * cause. + *

+ * Note that the detail message associated with {@code cause} is not + * automatically incorporated in this runtime exception's detail message. + * + * @param message the detail message (which is saved for later retrieval by the + * {@link #getMessage()} method). + * @param cause the cause (which is saved for later retrieval by the + * {@link #getCause()} method). (A {@code null} value is + * permitted, and indicates that the cause is nonexistent or + * unknown.) + * @since 1.4 + */ + public CustomException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Constructs a new runtime exception with the specified detail message, cause, + * suppression enabled or disabled, and writable stack trace enabled or + * disabled. + * + * @param message the detail message. + * @param cause the cause. (A {@code null} value is permitted, and + * indicates that the cause is nonexistent or + * unknown.) + * @param enableSuppression whether or not suppression is enabled or disabled + * @param writableStackTrace whether or not the stack trace should be writable + * @since 1.7 + */ + public CustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + /** + * 获取错误码 + * + * @return 错误码 + */ + public Integer getCode() { + return code; + } + + /** + * 设置错误码 + * + * @param code 错误码 + * @return 当前对象 + */ + public CustomException setCode(Integer code) { + this.code = code; + return this; + } + + /** + * 获取携带的附加信息 + * + * @return 携带的附加信息 + */ + public Object getContext() { + return context; + } + + /** + * 设置携带的附加信息 + * + * @param context 附加信息 + * @return 当前对象 + */ + public CustomException setContext(Object context) { + this.context = context; + return this; + } + + /** + * 构建一个CustomException实例对象 + * + * @param msg 异常信息 + * @return CustomException实例对象 + */ + public static CustomException of(String msg) { + return new CustomException(msg); + } + + /** + * 构建一个CustomException实例对象 + * + * @param msg 异常信息 + * @param context 附带的信息 + * @return CustomException实例对象 + */ + public static CustomException of(String msg, Object context) { + CustomException exception = new CustomException(msg); + exception.setContext(context); + return exception; + } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/exception/UncheckedException.java b/src/main/java/com/yishuifengxiao/common/tool/exception/UncheckedException.java index aa68fa8..a9e3149 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/exception/UncheckedException.java +++ b/src/main/java/com/yishuifengxiao/common/tool/exception/UncheckedException.java @@ -22,7 +22,7 @@ public class UncheckedException extends RuntimeException { /** * 错误码 */ - protected int code; + protected Integer code; /** * 携带的附加信息 @@ -48,7 +48,7 @@ public UncheckedException() { * retrieval by the {@link #getMessage()} method. */ - public UncheckedException(int code, String message) { + public UncheckedException(Integer code, String message) { super(message); this.code = code; } @@ -124,7 +124,7 @@ public UncheckedException(Throwable cause) { * @param code 错误码 * @return 当前对象 */ - public UncheckedException setCode(int code) { + public UncheckedException setCode(Integer code) { this.code = code; return this; } @@ -134,7 +134,7 @@ public UncheckedException setCode(int code) { * * @return 错误码 */ - public int getCode() { + public Integer getCode() { return this.code; } @@ -159,6 +159,17 @@ public UncheckedException setContext(Object context) { return this; } + /** + * 构建一个UncheckedException实例对象 + * + * @param code 错误码 + * @param msg 异常信息 + * @return CustomException实例对象 + */ + public static UncheckedException of(Integer code, String msg) { + return new UncheckedException(code, msg); + } + /** * 构建一个UncheckedException实例对象 * diff --git a/src/main/java/com/yishuifengxiao/common/tool/http/HttpUtil.java b/src/main/java/com/yishuifengxiao/common/tool/http/UrlUtil.java similarity index 96% rename from src/main/java/com/yishuifengxiao/common/tool/http/HttpUtil.java rename to src/main/java/com/yishuifengxiao/common/tool/http/UrlUtil.java index 79e686d..c4cf8d1 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/http/HttpUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/http/UrlUtil.java @@ -12,13 +12,13 @@ import java.util.stream.Stream; /** - * http数据工具 + * url数据工具 * * @author yishui * @version 1.0.0 * @since 1.0.0 */ -public final class HttpUtil { +public final class UrlUtil { /** * com.cn域名表达式 diff --git a/src/main/java/com/yishuifengxiao/common/tool/lang/NumberUtil.java b/src/main/java/com/yishuifengxiao/common/tool/lang/NumberUtil.java index 613de77..a1675c8 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/lang/NumberUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/lang/NumberUtil.java @@ -4,7 +4,10 @@ import org.apache.commons.lang3.StringUtils; import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.IntStream; /** *

@@ -167,7 +170,7 @@ public static Long parseLong(String str, long defaultValue) { public static Optional parse(Object val) { try { if (null == val || StringUtils.isBlank(val.toString())) { - return null; + return Optional.empty(); } BigDecimal number = new BigDecimal(val.toString().replaceAll(",", "").trim()); return Optional.ofNullable(number); @@ -178,21 +181,91 @@ public static Optional parse(Object val) { } /** - *

- * 将输入值转换为 BigDecimal - *

- *

- * 线程安全 - *

+ * 将16进制字符串数据转为10进制数字 * - * @param val 输入值 - * @param defaultVal 默认值 - * @return 转换后的 BigDecimal ,若转换失败则返回为 defaultVal + * @param hexString 待转换的字符串 + * @return 转换后的10进制数字 */ - public static BigDecimal parse(Object val, BigDecimal defaultVal) { - if (null == val) { - return defaultVal; + public static Optional parseHex(String hexString) { + try { + if (null == hexString || StringUtils.isBlank(hexString.toString())) { + return Optional.empty(); + } + BigDecimal number = new BigDecimal(new BigInteger(hexString.toString().replaceAll(",", "").trim(), 16)); + return Optional.ofNullable(number); + } catch (Throwable e) { + log.debug("将数据【{}】转换为数值时出现问题 {}", hexString, e); } - return parse(val).orElse(defaultVal); + return Optional.empty(); + } + + /** + *

将数字转换指定字节数为16进制字符串

+ *

注意转换后的字符串为偶数个字符,即为2*byteNum个字符数,若为奇数个字符则自动左补零

+ *

转换后的字符的长度可能大于2*byteNum个字符数

+ * + * @param number 待转换的数字 + * @param byteNum 转换后的字节数,例如byteNum为2时表示最短的字符数为 2*2 + * @return 16进制字符串 + */ + public static String toHexString(Number number, Integer byteNum) { + + String hexString = ""; + if (null != number) { + if (number instanceof Integer) { + hexString = Integer.toHexString(number.intValue()).toUpperCase(); // 转为16进制并大写 + } else if (number instanceof Long) { + hexString = Long.toHexString(number.longValue()).toUpperCase(); // 转为16进制并大写 + } else if (number instanceof Short) { + hexString = Integer.toHexString(number.shortValue()).toUpperCase(); // 转为16进制并大写 + } else if (number instanceof Byte) { + hexString = Integer.toHexString(number.byteValue()).toUpperCase(); // 转为16进制并大写 + } else if (number instanceof Double || number instanceof Float) { + // 如果是浮点数,通常我们只处理其整数部分 + hexString = Long.toHexString(number.longValue()).toUpperCase(); // 转为16进制并大写 + } else { + throw new IllegalArgumentException("Unsupported number type."); + } + } + if (hexString.length() % 2 == 1) { + hexString = "0" + hexString; + } + if (null != byteNum && byteNum > 0 && hexString.length() < byteNum * 2) { + String prefix = IntStream.range(0, byteNum * 2 - hexString.length()).mapToObj(v -> "0").collect(Collectors.joining()); + hexString = prefix + hexString; + } + return hexString; + } + + /** + *

将数字转换指定字节数为16进制字符串

+ *

注意转换后的字符串为偶数个字符,即为2*byteNum个字符数,若为奇数个字符则自动左补零

+ *

转换后的字符的长度恰好2*byteNum个字符数,故转换后的

+ * + * @param number 待转换的数字 + * @param byteNum 转换后的字节数,例如byteNum为2时表示最短的字符数为 2*2 + * @return 16进制字符串 + */ + public static String toHex(Number number, Integer byteNum) { + String hexString = toHexString(number, byteNum); + if (null == byteNum || byteNum <= 0) { + return hexString; + } + if (hexString.length() > (byteNum * 2)) { + hexString = hexString.substring(0, byteNum * 2); + } + return hexString; + } + + + /** + *

将数字转换为16进制字符串

+ *

注意转换后的字符串为偶数个字符,若为奇数个字符则自动左补零

+ * + * @param number 待转换的数字 + * @return 16进制字符串 + */ + public static String toHexString(Number number) { + return toHexString(number, null); } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/text/RegexUtil.java b/src/main/java/com/yishuifengxiao/common/tool/text/RegexUtil.java index 2d76445..258ef84 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/text/RegexUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/text/RegexUtil.java @@ -211,7 +211,7 @@ public static List extractAllNumber(String text) { return Collections.emptyList(); } text = text.replaceAll(REGEX_ILLEGAL_NUMBER, "").trim(); - return extractAll(REGEX_NUMBER, text).stream().map(v -> NumberUtil.parse(v, null)).filter(Objects::nonNull).collect(Collectors.toList()); + return extractAll(REGEX_NUMBER, text).stream().map(v -> NumberUtil.parse(v).orElse(null)).filter(Objects::nonNull).collect(Collectors.toList()); } /** diff --git a/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java b/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java index d621c94..226afd1 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java @@ -18,16 +18,6 @@ */ public class ExceptionUtil { - /** - * 抛出一个运行时异常 - * - * @param e 运行时异常 - */ - public static final void throwThrowable(RuntimeException e) { - if (null != e) { - throw e; - } - } /** * 判断给定的值是否为true,若为null或false则抛出异常 @@ -127,6 +117,15 @@ public static String extractError(Throwable throwable) { return result; } + /** + * 抛出一个自定义运行时异常 + * + * @param msg 异常信息 + */ + public static void throwException(Integer code, String msg) { + throw new UncheckedException(code, msg); + } + /** * 抛出一个自定义运行时异常 * From 78e6e5c8251f8b4b28284f21f7efd88bd2609eeb Mon Sep 17 00:00:00 2001 From: yishuifengxiao Date: Tue, 22 Oct 2024 22:43:12 +0800 Subject: [PATCH 3/6] Publish a new version after modifying the comments --- pom.xml | 4 ++-- .../common/tool/bean/CustomStringJavaCompiler.java | 11 ++++++++--- .../common/tool/collections/JsonUtil.java | 2 +- .../common/tool/context/LocalCache.java | 8 ++++++++ .../common/tool/datetime/DateTimeUtil.java | 3 +++ .../yishuifengxiao/common/tool/entity/PageQuery.java | 6 ++++-- .../com/yishuifengxiao/common/tool/entity/Slice.java | 4 ++-- .../common/tool/utils/ExceptionUtil.java | 3 ++- .../yishuifengxiao/common/tool/utils/ExecuteUtil.java | 7 +++---- .../com/yishuifengxiao/common/tool/utils/OsUtils.java | 2 -- .../yishuifengxiao/common/tool/validate/InBool.java | 5 +++++ .../yishuifengxiao/common/tool/validate/InInt.java | 5 +++++ .../yishuifengxiao/common/tool/validate/InLong.java | 5 +++++ .../yishuifengxiao/common/tool/validate/InString.java | 5 +++++ 14 files changed, 53 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index 2d5e012..4659367 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.yishuifengxiao.common common-tool - BUILD-SNAPSHOT + 5.1.0 jar 本工具包主要集成了目前在项目开发过程中个人经常会使用到的一些工具类,对工具类进行了一下简单的封装 @@ -208,7 +208,7 @@ org.apache.maven.plugins maven-release-plugin - ${maven-release-plugin.version>} + ${maven-release-plugin.version} true false diff --git a/src/main/java/com/yishuifengxiao/common/tool/bean/CustomStringJavaCompiler.java b/src/main/java/com/yishuifengxiao/common/tool/bean/CustomStringJavaCompiler.java index 9467002..b788874 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/bean/CustomStringJavaCompiler.java +++ b/src/main/java/com/yishuifengxiao/common/tool/bean/CustomStringJavaCompiler.java @@ -52,6 +52,11 @@ public class CustomStringJavaCompiler { */ private Boolean compile; + /** + * 默认构造器 + * + * @param sourceCode 源代码 + */ public CustomStringJavaCompiler(String sourceCode) { this.sourceCode = sourceCode; this.fullClassName = getFullClassName(sourceCode); @@ -110,7 +115,7 @@ public Class result(String className) throws Exception { * @return 编译信息(错误 警告) */ @SuppressWarnings("rawtypes") - public String getCompilerMessage() { + public String getCompilerMessage() { StringBuilder sb = new StringBuilder(); List> diagnostics = diagnosticsCollector.getDiagnostics(); for (Diagnostic diagnostic : diagnostics) { @@ -207,9 +212,9 @@ public byte[] getCompiledBytes() { * 自定义一个JavaFileManage来控制编译之后字节码的输出位置 */ @SuppressWarnings("rawtypes") - private class StringJavaFileManage extends ForwardingJavaFileManager { + private class StringJavaFileManage extends ForwardingJavaFileManager { @SuppressWarnings("unchecked") - StringJavaFileManage(JavaFileManager fileManager) { + StringJavaFileManage(JavaFileManager fileManager) { super(fileManager); } diff --git a/src/main/java/com/yishuifengxiao/common/tool/collections/JsonUtil.java b/src/main/java/com/yishuifengxiao/common/tool/collections/JsonUtil.java index 2e7461c..f70266a 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/collections/JsonUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/collections/JsonUtil.java @@ -636,7 +636,7 @@ public static boolean isJSON(String text) { * 将对象转换为json格式的字符串 * * @param value 待转换的数据 - * @return + * @return json格式的字符串 */ public static String toJSONString(Object value) { try { diff --git a/src/main/java/com/yishuifengxiao/common/tool/context/LocalCache.java b/src/main/java/com/yishuifengxiao/common/tool/context/LocalCache.java index 41e87eb..5153044 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/context/LocalCache.java +++ b/src/main/java/com/yishuifengxiao/common/tool/context/LocalCache.java @@ -67,6 +67,14 @@ public static synchronized Object get(String key) { return LOCAL_HOLDER.get(key.trim()); } + /** + * 根据key获取一个数据 + * + * @param key 指定的key + * @param supplier Supplier,不存在key对应的数据时触发 + * @param 数据类型 + * @return 返回的数据 + */ @SuppressWarnings("unchecked") public static synchronized T get(String key, Supplier supplier) { Object value = get(key.trim()); diff --git a/src/main/java/com/yishuifengxiao/common/tool/datetime/DateTimeUtil.java b/src/main/java/com/yishuifengxiao/common/tool/datetime/DateTimeUtil.java index 6f0d9e0..92b39c9 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/datetime/DateTimeUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/datetime/DateTimeUtil.java @@ -38,6 +38,9 @@ @Slf4j public final class DateTimeUtil { + /** + * 默认的时区-北京 utc+8 + */ public final static ZoneId ZONEID_OF_CHINA = OsUtils.ZONEID_OF_CHINA; diff --git a/src/main/java/com/yishuifengxiao/common/tool/entity/PageQuery.java b/src/main/java/com/yishuifengxiao/common/tool/entity/PageQuery.java index a537f2a..05373d4 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/entity/PageQuery.java +++ b/src/main/java/com/yishuifengxiao/common/tool/entity/PageQuery.java @@ -30,8 +30,10 @@ public class PageQuery extends Slice implements Serializable { */ private static final long serialVersionUID = 1L; - - @Schema(title ="查询参数") + /** + * 查询参数 + */ + @Schema(title = "查询参数") protected T query; diff --git a/src/main/java/com/yishuifengxiao/common/tool/entity/Slice.java b/src/main/java/com/yishuifengxiao/common/tool/entity/Slice.java index f26a424..eaec696 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/entity/Slice.java +++ b/src/main/java/com/yishuifengxiao/common/tool/entity/Slice.java @@ -55,7 +55,7 @@ public class Slice implements Serializable { /** *

获取分页大小

- *

若分页大小为null或者<=0则返回默认值 10

+ *

若分页大小为null或者<=0则返回默认值 10

* * @return 分页大小 */ @@ -70,7 +70,7 @@ public Number size() { /** *

获取当前页页码

- *

若分页大小为null或者<=0则返回默认值 1

+ *

若分页大小为null或者<=0则返回默认值 1

* * @return 当前页页码 */ diff --git a/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java b/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java index 226afd1..1dd7ab8 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java @@ -120,7 +120,8 @@ public static String extractError(Throwable throwable) { /** * 抛出一个自定义运行时异常 * - * @param msg 异常信息 + * @param code 异常码 + * @param msg 异常信息 */ public static void throwException(Integer code, String msg) { throw new UncheckedException(code, msg); diff --git a/src/main/java/com/yishuifengxiao/common/tool/utils/ExecuteUtil.java b/src/main/java/com/yishuifengxiao/common/tool/utils/ExecuteUtil.java index 3ecd762..ad2dae3 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/utils/ExecuteUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/utils/ExecuteUtil.java @@ -25,9 +25,7 @@ public class ExecuteUtil { /** * 线程池初始化 */ - private final static ExecutorService POOL = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), - Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), SIMPLE_THREAD_FACTORY, - new ThreadPoolExecutor.AbortPolicy()); + private final static ExecutorService POOL = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), SIMPLE_THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy()); /** * 获取线程池 @@ -71,6 +69,7 @@ public static void execute(Runnable runnable, ExecuteError error) { * 执行任务 * * @param runnable 待执行的任务 + * @param complete 执行完成后触发 * @param error 执行失败后触发的动作 */ public static void execute(Runnable runnable, ExecuteComplete complete, ExecuteError error) { @@ -188,7 +187,7 @@ public static T execute(Predicate match, Supplier... suppliers) { *
  • 以Both/Combine结尾的方法,必须所有都完成。
  • * * - * @param futures + * @param futures 待执行的任务 */ public static void execute(CompletableFuture... futures) { if (null == futures || futures.length == 0) { diff --git a/src/main/java/com/yishuifengxiao/common/tool/utils/OsUtils.java b/src/main/java/com/yishuifengxiao/common/tool/utils/OsUtils.java index 20c774b..1fe80fc 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/utils/OsUtils.java +++ b/src/main/java/com/yishuifengxiao/common/tool/utils/OsUtils.java @@ -18,8 +18,6 @@ public final class OsUtils { /** * 获取北京时间的ZoneId - * - * @return 北京时间的ZoneId */ public final static ZoneId ZONEID_OF_CHINA = ZoneId.of(DEFAULT_ZONE); /** diff --git a/src/main/java/com/yishuifengxiao/common/tool/validate/InBool.java b/src/main/java/com/yishuifengxiao/common/tool/validate/InBool.java index 076f773..1ab248b 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/validate/InBool.java +++ b/src/main/java/com/yishuifengxiao/common/tool/validate/InBool.java @@ -66,6 +66,11 @@ @Retention(RUNTIME) @Documented @interface List { + /** + * 待校验的值 + * + * @return 待校验的值 + */ InBool[] value(); } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/validate/InInt.java b/src/main/java/com/yishuifengxiao/common/tool/validate/InInt.java index ecca20f..fb6539b 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/validate/InInt.java +++ b/src/main/java/com/yishuifengxiao/common/tool/validate/InInt.java @@ -69,6 +69,11 @@ @Retention(RUNTIME) @Documented @interface List { + /** + * 待校验的值 + * + * @return 待校验的值 + */ InInt[] value(); } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/validate/InLong.java b/src/main/java/com/yishuifengxiao/common/tool/validate/InLong.java index 564f794..c8094a3 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/validate/InLong.java +++ b/src/main/java/com/yishuifengxiao/common/tool/validate/InLong.java @@ -71,6 +71,11 @@ @Retention(RUNTIME) @Documented @interface List { + /** + * 待校验的值 + * + * @return 待校验的值 + */ InLong[] value(); } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/validate/InString.java b/src/main/java/com/yishuifengxiao/common/tool/validate/InString.java index 15804a1..6eb9daf 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/validate/InString.java +++ b/src/main/java/com/yishuifengxiao/common/tool/validate/InString.java @@ -76,6 +76,11 @@ @Retention(RUNTIME) @Documented @interface List { + /** + * 待校验的值 + * + * @return 待校验的值 + */ InString[] value(); } } From c75e2d2424ec50e2beec30a67b7ab25c7aaac5d2 Mon Sep 17 00:00:00 2001 From: yishuifengxiao Date: Tue, 22 Oct 2024 23:10:12 +0800 Subject: [PATCH 4/6] Publish a new version after modifying the comments --- pom.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/pom.xml b/pom.xml index 4659367..658ef82 100644 --- a/pom.xml +++ b/pom.xml @@ -261,9 +261,6 @@ jar - - -Xdoclint:none -
    From 0dfe39d312aff2c54973ba1baf0c59e6ece8c837 Mon Sep 17 00:00:00 2001 From: yishuifengxiao Date: Tue, 22 Oct 2024 23:12:02 +0800 Subject: [PATCH 5/6] Publish a new version after modifying the comments --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 658ef82..5ba22e4 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.yishuifengxiao.common common-tool - 5.1.0 + BUILD-SNAPSHOT jar 本工具包主要集成了目前在项目开发过程中个人经常会使用到的一些工具类,对工具类进行了一下简单的封装 From a7066c66d5522e80068ea73faba4361ca8164653 Mon Sep 17 00:00:00 2001 From: yishuifengxiao Date: Sat, 16 Nov 2024 17:41:07 +0800 Subject: [PATCH 6/6] Refactoring a large number of tools and upgrading basic versions --- README.md | 27 +- pom.xml | 37 +- .../common/tool/bean/BeanUtil.java | 91 +- .../common/tool/bean/ClassUtil.java | 44 +- .../tool/bean/CustomStringJavaCompiler.java | 17 +- .../tool/{collections => bean}/JsonUtil.java | 252 +++- .../yishuifengxiao/common/tool/codec/AES.java | 20 +- .../yishuifengxiao/common/tool/codec/DES.java | 17 +- .../yishuifengxiao/common/tool/codec/Md5.java | 26 +- .../common/tool/collections/CollUtil.java | 258 +++- .../common/tool/collections/DataUtil.java | 451 ------- .../common/tool/collections/StreamUtil.java | 165 +-- .../common/tool/context/LocalCache.java | 106 +- .../common/tool/datetime/DateOffsetUtil.java | 5 +- .../common/tool/datetime/DateTimeUtil.java | 25 +- .../tool/datetime/LocalDateTimeUtil.java | 6 +- .../common/tool/entity/BoolStat.java | 20 +- .../common/tool/entity/KeyValue.java | 6 + .../common/tool/entity/Page.java | 46 +- .../common/tool/entity/PageQuery.java | 1 - .../common/tool/entity/Response.java | 97 +- .../common/tool/entity/RootEnum.java | 62 + .../common/tool/entity/StringKeyValue.java | 4 + .../tool/exception/CustomException.java | 21 +- .../tool/exception/UncheckedException.java | 20 +- .../common/tool/http/HttpClient.java | 1185 +++++++++-------- .../common/tool/http/UserAgent.java | 187 ++- .../common/tool/io/CloseUtil.java | 10 +- .../common/tool/io/ImageUtil.java | 9 +- .../yishuifengxiao/common/tool/io/IoUtil.java | 13 +- .../common/tool/lang/NumberUtil.java | 16 +- .../common/tool/log/LogLevelUtil.java | 13 +- .../common/tool/random/IdWorker.java | 9 +- .../common/tool/random/RandomUtil.java | 3 - .../common/tool/text/HtmlExtract.java | 19 +- .../common/tool/text/RegexUtil.java | 13 +- .../common/tool/utils/CertNoUtil.java | 19 +- .../common/tool/utils/ExecuteUtil.java | 16 +- .../common/tool/utils/GpsUtil.java | 3 - ...{ExceptionUtil.java => ValidateUtils.java} | 90 +- .../common/tool/validate/InBool.java | 4 +- .../common/tool/validate/InInt.java | 4 +- .../common/tool/validate/InLong.java | 4 +- .../common/tool/validate/InString.java | 4 +- 44 files changed, 1793 insertions(+), 1652 deletions(-) rename src/main/java/com/yishuifengxiao/common/tool/{collections => bean}/JsonUtil.java (74%) delete mode 100644 src/main/java/com/yishuifengxiao/common/tool/collections/DataUtil.java create mode 100644 src/main/java/com/yishuifengxiao/common/tool/entity/RootEnum.java rename src/main/java/com/yishuifengxiao/common/tool/utils/{ExceptionUtil.java => ValidateUtils.java} (62%) diff --git a/README.md b/README.md index 5d1b9e2..1dec52b 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,6 @@ CustomException e = new CustomException(); DataException ex = new DataException(); //将CustomException复制为DataException -// 在使用此方法时,属性名一致的将会被复制,该方法是一个线程安全类的 // 第一个参数为源对象,第二个参数为目标对象 DataException copy = BeanUtil.copy(e, ex); @@ -83,7 +82,7 @@ CustomException exception = BeanUtil.byteToObject(bytes, CustomException.class); - 将数据转换成集合 - 安全地创建集合 -> 该工具是线程安全类的 + 工具路径: @@ -176,7 +175,7 @@ Map map = MapUtil.instance().put("k1", "v1"). - 获取过去指定月份的那个月份的1号的开始时间点(00:00:00) - 获取过去指定年份的那个时间的1月1号的那个时间的开始时间点(00:00:00) -> 该工具是线程安全类的 + 工具路径: @@ -221,7 +220,7 @@ Date monthStart1 = DateOffsetUtil.monthStart(1); - 获取过去指定月份的那个月份的1号的开始时间点(00:00:00) - 获取过去指定年份的那个时间的1月1号的那个时间的开始时间点(00:00:00) -> 该工具是线程安全类的 + 工具路径: @@ -260,7 +259,7 @@ LocalDateTime monthStart1 = TemporalUtil.monthStart(1); - 将字符串解析为时间 - 将时间格式化为字符串 -> 该工具是线程安全类的 + 工具路径: @@ -351,7 +350,7 @@ DES.decrypt("秘钥", encrypt); 工具路径: ```java -com.yishuifengxiao.common.tool.codec.Md5 +com.yishuifengxiao.common.tool.codec.MD5 ``` 使用示例: @@ -483,7 +482,7 @@ Page convert = page.convert(原始数据 -> { 该工具的主要目的是在安全地关闭各种Closeable实例。 -> 该工具是一个线程安全类的工具 + 工具路径: @@ -505,7 +504,7 @@ CloseUtil.close(inputStream, outputStream); 该工具的主要目的是进行文件和base64字符串之间进行互相转换和获取文件的MD5值。 -> 该工具是一个线程安全类的工具 + 工具路径: @@ -530,7 +529,7 @@ String md5 = FileUtil.getMd5(file); 该工具的主要目的是进行图片和base64字符串之间进行互相转换 -> 该工具是一个线程安全类的工具 + 工具路径: @@ -561,7 +560,7 @@ ImageUtil.base64ToImage(image2Base64, "待保存的图片的地址"); 该工具的主要目的是构建一个基于内存的全局字典缓存工具。在使用该工具时注意防止内存泄露。 -> 该工具是一个线程安全类的工具 + 工具路径: @@ -596,9 +595,9 @@ com.yishuifengxiao.common.tool.context.LocalCache ## 7.2 上下文存储工具 -该工具与全局存储工具基本类似,但不同的是,该工具是基于当前线程实现的,在当前线程中存储的数据不能再其他线程中访问。在使用该工具时注意防止内存泄露。 +该工具与全局存储工具基本类似,但不同的是,在当前线程中存储的数据不能再其他线程中访问。在使用该工具时注意防止内存泄露。 + -> 该工具是一个线程安全类的工具 工具路径: @@ -724,7 +723,7 @@ ExecuteUtil.execute(() -> { - 判断该字符串是否为一个合法的身份证号 - 从字符串格式的身份证号里提取出出生日期 -> 该工具是一个线程安全类的工具 + 工具路径: @@ -747,7 +746,7 @@ System.out.println(birthday); 该工具的主要目的是计算两个经纬度之间距离。 -> 该工具是一个线程安全类的工具 + 工具路径: diff --git a/pom.xml b/pom.xml index 5ba22e4..66394f8 100644 --- a/pom.xml +++ b/pom.xml @@ -51,7 +51,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.1 + 3.3.5 2.2.20 @@ -119,21 +119,12 @@ - com.fasterxml.jackson.core - jackson-databind + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 ${jackson.version} - - disable-javadoc-doclint - - [1.8,) - - - -Xdoclint:none - - dev @@ -144,12 +135,6 @@ true - - - doc - - -Xdoclint:none - @@ -163,12 +148,24 @@ UTF-8 - -Xdoclint:none false + + org.apache.maven.plugins + maven-source-plugin + ${maven-source-plugin.version} + + + attach-sources + + jar-no-fork + + + + org.apache.maven.plugins maven-compiler-plugin @@ -208,7 +205,7 @@ org.apache.maven.plugins maven-release-plugin - ${maven-release-plugin.version} + ${maven-release-plugin.version>} true false diff --git a/src/main/java/com/yishuifengxiao/common/tool/bean/BeanUtil.java b/src/main/java/com/yishuifengxiao/common/tool/bean/BeanUtil.java index 9518fd8..c20ace9 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/bean/BeanUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/bean/BeanUtil.java @@ -4,9 +4,10 @@ package com.yishuifengxiao.common.tool.bean; -import com.yishuifengxiao.common.tool.collections.JsonUtil; import com.yishuifengxiao.common.tool.exception.UncheckedException; import com.yishuifengxiao.common.tool.io.CloseUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -17,7 +18,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; /** *

    @@ -34,10 +34,12 @@ * @version 1.0.0 * @since 1.0.0 */ +@Slf4j public final class BeanUtil { + /** - * 将源对象里属性值复制给目标对象(当前方法是一个线程安全类的方法) + * 将源对象里属性值复制给目标对象 * * @param 源对象的类型 * @param 目标对象的类型 @@ -47,24 +49,42 @@ public final class BeanUtil { */ public static T copy(S source, T target) { if (null == source || null == target) { - return null; + return target; } + //@formatter:off List targetFields = ClassUtil.fields(target.getClass()); List sourceFields = ClassUtil.fields(source.getClass()); - targetFields = targetFields.stream().filter(v -> !(Modifier.isAbstract(v.getModifiers()) || Modifier.isNative(v.getModifiers()) || Modifier.isStatic(v.getModifiers()) || Modifier.isFinal(v.getModifiers()))).filter(v -> sourceFields.stream().anyMatch(s -> s.getName().equals(v.getName()))).collect(Collectors.toList()); - for (Field field : targetFields) { - field.setAccessible(true); + targetFields.parallelStream().filter(v -> !( + Modifier.isAbstract(v.getModifiers()) + || Modifier.isNative(v.getModifiers()) + || Modifier.isStatic(v.getModifiers()) + || Modifier.isFinal(v.getModifiers()) + )) + .filter(v -> sourceFields.stream().anyMatch(s -> s.getName().equals(v.getName()))) + .map(Field::getName) + .forEach(fieldName -> { try { - Object val = field.get(source); - if (null == val) { - continue; + Field targetField = + targetFields.stream().filter(s -> StringUtils.equals(s.getName(), + fieldName)).findFirst().orElse(null); + Field sourceField = + sourceFields.stream().filter(s -> StringUtils.equals(s.getName(), + fieldName)).findFirst().orElse(null); + targetField.setAccessible(true); + sourceField.setAccessible(true); + Object val = sourceField.get(source); + if (null != val) { + targetField.set(target, val); + } + } catch (Exception e) { + if(log.isInfoEnabled()){ + log.info("There was a problem copying the attribute {} from {} to " + "{}, the " + + "problem is {}", fieldName, source, target, e); } - field.set(target, val); - } catch (IllegalAccessException e) { - throw new UncheckedException(e.getMessage()); - } - } + } + }); + //@formatter:on return target; } @@ -77,17 +97,16 @@ public static T copy(S source, T target) { */ public static byte[] objectToByte(Object obj) { byte[] bytes = null; - try { + //@formatter:off + try (ByteArrayOutputStream bo = new ByteArrayOutputStream(); + ObjectOutputStream oo = new ObjectOutputStream(bo);) { // object to bytearray - ByteArrayOutputStream bo = new ByteArrayOutputStream(); - ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(obj); bytes = bo.toByteArray(); - bo.close(); - oo.close(); } catch (Exception e) { throw new UncheckedException(e.getMessage()); } + //@formatter:on return bytes; } @@ -99,16 +118,15 @@ public static byte[] objectToByte(Object obj) { */ public static Object byteToObject(byte[] bytes) { Object obj = null; - try { + //@formatter:off + try (ByteArrayInputStream bi = new ByteArrayInputStream(bytes); + ObjectInputStream oi = new ObjectInputStream(bi);) { // bytearray to object - ByteArrayInputStream bi = new ByteArrayInputStream(bytes); - ObjectInputStream oi = new ObjectInputStream(bi); obj = oi.readObject(); - bi.close(); - oi.close(); } catch (Exception e) { throw new UncheckedException(e.getMessage()); } + //@formatter:on return obj; } @@ -123,16 +141,15 @@ public static Object byteToObject(byte[] bytes) { @SuppressWarnings("unchecked") public static T byteToObject(byte[] bytes, Class clazz) { T t = null; - try { + //@formatter:off + try ( ByteArrayInputStream bi = new ByteArrayInputStream(bytes); + ObjectInputStream oi = new ObjectInputStream(bi);){ // bytearray to object - ByteArrayInputStream bi = new ByteArrayInputStream(bytes); - ObjectInputStream oi = new ObjectInputStream(bi); t = (T) oi.readObject(); - bi.close(); - oi.close(); } catch (Exception e) { throw new UncheckedException(e.getMessage()); } + //@formatter:on return t; } @@ -176,6 +193,7 @@ public static Object cloneVal(Object val) { if (null == val) { return null; } + //@formatter:off ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; ByteArrayInputStream bis = null; @@ -192,10 +210,21 @@ public static Object cloneVal(Object val) { // 从字节数组中读取对象 return ois.readObject(); } catch (Exception e) { - throw UncheckedException.of(e.getMessage()); + throw new UncheckedException(e); } finally { CloseUtil.close(ois, bis, oos, bos); } + //@formatter:on + } + + /** + * 使用jackson的方式实现深克隆 + * + * @param val 待克隆的的对象 + * @return 克隆后的对象 + */ + public static Object deepClone(Object val) { + return JsonUtil.deepClone(val); } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/bean/ClassUtil.java b/src/main/java/com/yishuifengxiao/common/tool/bean/ClassUtil.java index 746bfff..157f2ae 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/bean/ClassUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/bean/ClassUtil.java @@ -3,7 +3,6 @@ */ package com.yishuifengxiao.common.tool.bean; -import com.yishuifengxiao.common.tool.collections.DataUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -34,9 +33,6 @@ *

  • 根据属性的名字获取对象的属性的值
  • * * - *

    - * 当前工具是一个线程安全类工具 - *

    * * @author yishui * @version 1.0.0 @@ -67,7 +63,8 @@ public static List fields(Class clazz) { public static List fields(Class clazz, boolean noSpecialModifier) { List fields = fields(null, clazz); - return noSpecialModifier ? fields.stream().filter(v -> !isSpecialModifier(v)).collect(Collectors.toList()) : + return noSpecialModifier ? + fields.stream().filter(v -> !isSpecialModifier(v)).collect(Collectors.toList()) : fields; } @@ -81,7 +78,7 @@ public static List fields(Class clazz, boolean noSpecialModifier) */ private static List fields(List list, Class clazz) { list = null == list ? new ArrayList<>() : list; - DataUtil.stream(clazz.getDeclaredFields()).filter(Objects::nonNull).forEach(list::add); + Arrays.asList(clazz.getDeclaredFields()).stream().filter(Objects::nonNull).forEach(list::add); Class superclass = clazz.getSuperclass(); if (null == superclass || superclass.isAssignableFrom(Object.class)) { return list.stream().filter(Objects::nonNull).collect(Collectors.toList()); @@ -95,7 +92,8 @@ private static List fields(List list, Class clazz) { *

    * *

    - * 特殊修饰的关键字如:@Transientfinalstaticnativeabstract + * 特殊修饰的关键字如:@Transientfinalstaticnative + * abstract * *

    * @@ -157,7 +155,12 @@ public static Object extractValue(Object data, String fieldName) { field.setAccessible(true); return field.get(data); } catch (Exception e) { - log.warn("根据属性名获取属性值时出现问题,出现问题的原因为 {}", e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There was a problem obtaining attribute values based on attribute " + + "names, and the reason for" + + " the problem is {}", e.getMessage()); + } + } return null; } @@ -191,12 +194,18 @@ public static String pojoFieldName(SerFunction function) { String implMethodName = serializedLambda.getImplMethodName(); // 判断是否为boolean属性字段对应的后的is开头的getter方法 String instantiatedMethodType = serializedLambda.getInstantiatedMethodType(); - boolean bool = instantiatedMethodType.endsWith("Ljava/lang/Boolean;") && implMethodName.startsWith("is"); + boolean bool = + instantiatedMethodType.endsWith("Ljava/lang/Boolean;") && implMethodName.startsWith("is"); String fieldName = bool ? Introspector.decapitalize(implMethodName.substring(2)) : Introspector.decapitalize(implMethodName.substring(3)); return fieldName; } catch (Exception e) { - log.warn("根据pojo类的属性的Function函数获取原始属性的名字,出现问题的原因为 {}", e.getMessage()); + if (log.isInfoEnabled()) { + log.info("The reason for the problem in obtaining the name of the original " + + "attribute based on the " + + "Function function of the pojo class attribute is {}", e.getMessage()); + } + } return null; @@ -209,18 +218,23 @@ public static String pojoFieldName(SerFunction function) { * *

    The {@link SerFunction} class provides common functions and related utilities. * - *

    As this interface extends {@code java.util.function.Functiona}, an instance of this type can be + *

    As this interface extends {@code java.util.function.Functiona}, an instance of this + * type can be * used as a {@code java.util.function.Functiona} directly. To use a {@code - * java.util.function.Functiona} in a context where a {@code com.google.common.base.Functiona} is + * java.util.function.Functiona} in a context where a {@code com.google.common.base + * .Functiona} is * needed, use {@code function::apply}. * *

    This interface is now a legacy type. Use {@code java.util.function.Functiona} (or the - * appropriate primitive specialization such as {@code ToIntFunction}) instead whenever possible. - * Otherwise, at least reduce explicit dependencies on this type by using lambda expressions + * appropriate primitive specialization such as {@code ToIntFunction}) instead whenever + * possible. + * Otherwise, at least reduce explicit dependencies on this type by using lambda + * expressions * or method references instead of classes, leaving your code easier to migrate in the future. * *

    See the Guava User Guide article on the use of {@code Functiona}. + * href="https://github.com/google/guava/wiki/FunctionalExplained">the use of {@code + * Functiona}. * * @author Kevin Bourrillion * @since 2.0 diff --git a/src/main/java/com/yishuifengxiao/common/tool/bean/CustomStringJavaCompiler.java b/src/main/java/com/yishuifengxiao/common/tool/bean/CustomStringJavaCompiler.java index b788874..9c1366d 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/bean/CustomStringJavaCompiler.java +++ b/src/main/java/com/yishuifengxiao/common/tool/bean/CustomStringJavaCompiler.java @@ -44,7 +44,8 @@ public class CustomStringJavaCompiler { /** * 存放编译过程中输出的信息 */ - private final DiagnosticCollector diagnosticsCollector = new DiagnosticCollector<>(); + private final DiagnosticCollector diagnosticsCollector = + new DiagnosticCollector<>(); /** @@ -53,7 +54,7 @@ public class CustomStringJavaCompiler { private Boolean compile; /** - * 默认构造器 + * 构造函数 * * @param sourceCode 源代码 */ @@ -72,12 +73,14 @@ public boolean compiler() { Assert.isNotBlank("不是java代码", this.fullClassName); if (null == this.compile) { //标准的内容管理器,更换成自己的实现,覆盖部分方法 - StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(diagnosticsCollector, null, null); + StandardJavaFileManager standardFileManager = + compiler.getStandardFileManager(diagnosticsCollector, null, null); JavaFileManager javaFileManager = new StringJavaFileManage(standardFileManager); //构造源代码对象 JavaFileObject javaFileObject = new StringJavaFileObject(fullClassName, sourceCode); //获取一个编译任务 - JavaCompiler.CompilationTask task = compiler.getTask(null, javaFileManager, diagnosticsCollector, null, null, Arrays.asList(javaFileObject)); + JavaCompiler.CompilationTask task = compiler.getTask(null, javaFileManager, + diagnosticsCollector, null, null, Arrays.asList(javaFileObject)); this.compile = task.call(); } return this.compile; @@ -117,7 +120,8 @@ public Class result(String className) throws Exception { @SuppressWarnings("rawtypes") public String getCompilerMessage() { StringBuilder sb = new StringBuilder(); - List> diagnostics = diagnosticsCollector.getDiagnostics(); + List> diagnostics = + diagnosticsCollector.getDiagnostics(); for (Diagnostic diagnostic : diagnostics) { sb.append(diagnostic.toString()).append("\r\n"); } @@ -232,7 +236,8 @@ private class StringJavaFileManage extends ForwardingJavaFileManager { * @throws IOException */ @Override - public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException { + public JavaFileObject getJavaFileForOutput(Location location, String className, + JavaFileObject.Kind kind, FileObject sibling) throws IOException { ByteJavaFileObject javaFileObject = new ByteJavaFileObject(className, kind); javaFileObjectMap.put(className, javaFileObject); return javaFileObject; diff --git a/src/main/java/com/yishuifengxiao/common/tool/collections/JsonUtil.java b/src/main/java/com/yishuifengxiao/common/tool/bean/JsonUtil.java similarity index 74% rename from src/main/java/com/yishuifengxiao/common/tool/collections/JsonUtil.java rename to src/main/java/com/yishuifengxiao/common/tool/bean/JsonUtil.java index f70266a..cf21287 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/collections/JsonUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/bean/JsonUtil.java @@ -1,19 +1,29 @@ /** * */ -package com.yishuifengxiao.common.tool.collections; +package com.yishuifengxiao.common.tool.bean; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.jayway.jsonpath.JsonPath; +import com.yishuifengxiao.common.tool.exception.UncheckedException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import java.util.List; import java.util.Map; +import java.util.TimeZone; /** *

    json转换提取工具

    @@ -273,7 +283,8 @@ * 22px">/store/book/author
    * $.store.book[*] + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">$.store.book[*] * .author *
    //author + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">//author * $..author + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">$..author *
    所有的作者
    @@ -295,10 +308,12 @@ * * /store/* + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">/store/* * $.store.* + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">$.store.* *
    store的所有元素。所有的bookst和bicycle
    @@ -312,7 +327,8 @@ * $.store. + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">$.store. * .price * //book[3] + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">//book[3] * $..book[2] + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">$..book[2] *
    第三个书
    @@ -333,12 +351,14 @@ * //book[last() + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">//book[last() * ] * $..book[(@ + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">$..book[(@ * .length-1)] * //book[position() + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">//book[position() * <3] * $..book[0, + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">$..book[0, * 1]
    $. @@ -368,7 +390,8 @@ * $..book[?(@ + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">$..book[?(@ * .isbn)] * //book[price< + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">//book[price< * 10] * $..book[?(@ + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">$..book[?(@ * .price<10)] * 过滤出价格低于10的书。 @@ -390,11 +415,13 @@ * //* + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">//* * $..* + * "font-family: "Courier New", Courier, monospace; font-size: 14px; line-height: + * 22px">$..* *
    T str2Bean(String json, Class clazz) { return null; } try { - return MAPPER.readValue(json.trim(), clazz); + return with_class_mapper.readValue(json.trim(), clazz); } catch (Exception e) { - if (log.isInfoEnabled()) { - log.info("将字符串 {} 转换成 java对象 {} 时出现问题 {}", json, clazz, e.getMessage()); + if (log.isDebugEnabled()) { + log.debug("There was a problem converting string {} to Java object {}, problem " + + "{}", json, clazz, e.getMessage()); } } return null; @@ -445,11 +553,12 @@ public static T str2Bean(String json, Class clazz) { public static List str2List(String json, Class clazz) { List t = null; try { - t = MAPPER.readValue(json.trim(), new TypeReference>() { + t = with_class_mapper.readValue(json.trim(), new TypeReference>() { }); } catch (Exception e) { - if (log.isInfoEnabled()) { - log.info("将字符串 {} 转换成 {} 集合 时出现问题 {}", json, clazz, e.getMessage()); + if (log.isDebugEnabled()) { + log.debug("There was a problem converting the string {} to the {} List, the " + + "problem is" + " {}", json, clazz, e.getMessage()); } } return t; @@ -511,8 +620,11 @@ public static T extract(String json, String jsonPath) { try { return JsonPath.read(json.trim(), jsonPath); } catch (Exception e) { - if (log.isInfoEnabled()) { - log.info("根据表达式 {} 从字符串 {} 提取数据 时出现问题 {}", jsonPath, json, e.getMessage()); + + if (log.isDebugEnabled()) { + log.debug("There was a problem extracting data from string {} based on " + + "expression" + " {}. " + "The problem is " + "{}", jsonPath, json, + e.getMessage()); } } return null; @@ -567,11 +679,12 @@ public static Map json2Map(String text) { return null; } try { - return MAPPER.readValue(text.trim(), new TypeReference>() { + return with_class_mapper.readValue(text.trim(), new TypeReference<>() { }); } catch (Exception e) { - if (log.isInfoEnabled()) { - log.info("将字符串 {} 转换成 map 时出现问题 {} ", text, e.getMessage()); + if (log.isDebugEnabled()) { + log.debug("There was a problem converting the string {} to a map, the problem is " + + "{} ", text, e.getMessage()); } } return null; @@ -588,7 +701,7 @@ public static boolean isJSONObject(String text) { return false; } try { - JsonNode tree = MAPPER.readTree(text.trim()); + JsonNode tree = with_class_mapper.readTree(text.trim()); return tree.isObject(); } catch (Throwable e) { return false; @@ -606,7 +719,7 @@ public static boolean isJSONArray(String text) { return false; } try { - JsonNode tree = MAPPER.readTree(text.trim()); + JsonNode tree = with_class_mapper.readTree(text.trim()); return tree.isArray(); } catch (Throwable e) { return false; @@ -625,7 +738,7 @@ public static boolean isJSON(String text) { return false; } try { - MAPPER.readTree(text.trim()); + with_class_mapper.readTree(text.trim()); return true; } catch (Exception e) { return false; @@ -639,15 +752,82 @@ public static boolean isJSON(String text) { * @return json格式的字符串 */ public static String toJSONString(Object value) { + + return toJSONString(true, value); + } + + /** + * 将对象转换为json格式的字符串 + * + * @param includeNull 是否包含空值 + * @param value 待转换的数据 + * @return json格式的字符串 + */ + public static String toJSONString(boolean includeNull, Object value) { try { - return MAPPER.writeValueAsString(value); + ObjectMapper mapper = includeNull ? default_mapper : none_null_mapper; + return mapper.writeValueAsString(value); } catch (JsonProcessingException e) { - if (log.isInfoEnabled()) { - log.info("将数据 {} 转换成 json格式的字符串 时出现问题 {} ", value, e); + if (log.isDebugEnabled()) { + log.debug("There was a problem converting data {} to a JSON format string, the " + "problem is {} ", value, e); } } return null; } + /** + * Factory method for constructing ObjectWriter that will serialize objects using the default + * pretty printer for + * indentation + * + * @param value 待转换的数据 + * @return json格式的字符串 + */ + public static String prettyPrinter(Object value) { + return prettyPrinter(true, value); + } + + /** + * Factory method for constructing ObjectWriter that will serialize objects using the default + * pretty printer for + * indentation + * + * @param includeNull 是否包含空值 + * @param value 待转换的数据 + * @return json格式的字符串 + */ + public static String prettyPrinter(boolean includeNull, Object value) { + if (null == value) { + return null; + } + try { + ObjectMapper mapper = includeNull ? default_mapper : none_null_mapper; + return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value); + } catch (Exception e) { + if (log.isDebugEnabled()) { + log.debug("There was a problem converting data {} to a JSON format string, the " + "problem is {} ", value, e); + } + throw new UncheckedException(e); + } + + } + + /** + * 使用jackson的方式实现深克隆 + * + * @param val 待克隆的的对象 + * @return 克隆后的对象 + */ + public static Object deepClone(Object val) { + if (null == val) { + return null; + } + try { + return with_class_mapper.readValue(with_class_mapper.writeValueAsString(val), + val.getClass()); + } catch (Exception e) { + throw new UncheckedException(e); + } + } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/codec/AES.java b/src/main/java/com/yishuifengxiao/common/tool/codec/AES.java index c14ecb6..d89a29f 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/codec/AES.java +++ b/src/main/java/com/yishuifengxiao/common/tool/codec/AES.java @@ -15,8 +15,7 @@ *

    * AES加密工具 *

    - * 基于DES加解密实现的加密工具,该工具可以进行可逆加密,加密时的秘钥很重要,一定要自己改秘钥,打死也不要告诉其他人。 - * 该工具是一个线程安全类的工具。 + *

    基于DES加解密实现的加密工具,该工具可以进行可逆加密,加密时的秘钥很重要,一定要自己改秘钥,打死也不要告诉其他人。

    * * @author yishui * @version 1.0.0 @@ -69,7 +68,13 @@ public static final String encrypt(String key, String data) { return new String(Base64.getEncoder().encode(cipher.doFinal(data.getBytes(StandardCharsets.UTF_8))), StandardCharsets.UTF_8); } catch (Exception e) { - log.info("【易水工具】使用密钥加密数据 {} 时出现问题,出现问题的原因为 {}", data, e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There is a problem encrypting data {} with a key, and the reason for " + + "the problem is {}", + data, + e); + } + } // 如果有错就返加nulll return null; @@ -111,9 +116,14 @@ public static final String decrypt(String key, String data) { // 5.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY cipher.init(Cipher.DECRYPT_MODE, aesKey); // 6.将加密并编码后的内容解码成字节数组 - return new String(cipher.doFinal(Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8))), StandardCharsets.UTF_8); + return new String(cipher.doFinal(Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8))), + StandardCharsets.UTF_8); } catch (Exception e) { - log.info("【易水工具】使用密钥解密数据 {} 时出现问题,出现问题的原因为 {}", data, e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There was a problem decrypting data {} using the key, and the reason for the problem is {}", + data, e); + } + } // 如果有错就返加nulll diff --git a/src/main/java/com/yishuifengxiao/common/tool/codec/DES.java b/src/main/java/com/yishuifengxiao/common/tool/codec/DES.java index 48ef687..2f1b53e 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/codec/DES.java +++ b/src/main/java/com/yishuifengxiao/common/tool/codec/DES.java @@ -1,5 +1,6 @@ package com.yishuifengxiao.common.tool.codec; +import com.yishuifengxiao.common.tool.exception.UncheckedException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -13,8 +14,7 @@ *

    * DES加密工具 *

    - * 基于DES加解密实现的加密工具,该工具可以进行可逆加密,加密时的秘钥很重要,一定要自己改秘钥,打死也不要告诉其他人。 - * 该工具是一个线程安全类的工具。 + *

    基于DES加解密实现的加密工具,该工具可以进行可逆加密,加密时的秘钥很重要,一定要自己改秘钥,打死也不要告诉其他人。

    * * @author yishui * @version 1.0.0 @@ -53,7 +53,10 @@ public static final String encrypt(String key, String data) { try { return byte2hex(encrypt(data.getBytes("utf-8"), keyValidate(key).getBytes("utf-8"))); } catch (Exception e) { - log.info("【易水工具】使用密钥加密数据 {} 时出现问题,出现问题的原因为 {}", data, e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There is a problem encrypting data {} with a key, and the reason for the problem is {}", + data, e); + } } return null; } @@ -79,7 +82,11 @@ public static final String decrypt(String key, String data) { try { return new String(decrypt(hex2byte(data.getBytes("utf-8")), keyValidate(key).getBytes("utf-8"))); } catch (Exception e) { - log.info("【易水工具】使用密钥解密数据 {} 时出现问题,出现问题的原因为 {}", data, e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There was a problem decrypting data {} using the key, and the reason " + + "for the problem is {}" + , data, e); + } } return null; } @@ -206,7 +213,7 @@ private static String byte2hex(byte[] b) { private static byte[] hex2byte(byte[] b) { if ((b.length % EVEN_FLAG) != 0) { - throw new IllegalArgumentException("长度不是偶数"); + throw new UncheckedException("The length is not even"); } byte[] b2 = new byte[b.length / 2]; for (int n = 0; n < b.length; n += EVEN_FLAG) { diff --git a/src/main/java/com/yishuifengxiao/common/tool/codec/Md5.java b/src/main/java/com/yishuifengxiao/common/tool/codec/Md5.java index f393d28..73ba39c 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/codec/Md5.java +++ b/src/main/java/com/yishuifengxiao/common/tool/codec/Md5.java @@ -15,17 +15,17 @@ *

    * 基于MD5算法实现的加密工具 * - *

    - * 该工具是一个线程安全类的工具 - *

    - * * @author yishui * @version 1.0.0 * @since 1.0.0 */ @Slf4j -public class Md5 { - private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; +public class MD5 { + /** + * HEX_DIGITS + */ + private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", + "e", "f"}; /** * 对字符串md5加密(小写+字母) @@ -47,7 +47,11 @@ public static String md5(String str) { // 将字节数组转换为16进制位,然后统一返回大写形式的字符串摘要 return sb.toString().toLowerCase(); } catch (Exception e) { - log.warn("【易水工具】使用md5加密字符串{} 时出现问题,出现问题的原因为 {}", str, e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There was a problem encrypting the string {} using md5, and the reason for the problem is " + + "{}", str, e); + } + return null; } } @@ -86,7 +90,6 @@ public static String md5Short(String str) { *

    * 计算一个文件的MD5值 *

    - * 线程安全 * * @param file 待计算的文件 * @return 文件的MD5值(32位小写) @@ -109,7 +112,11 @@ public synchronized static String md5(File file) { } return sb.toString().toLowerCase(); } catch (Exception e) { - log.info("计算文件{}的md5值时出现问题{}", file, e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There was a problem calculating the md5 value of file {}, the problem is {}", file, + e.getMessage()); + } + return null; } finally { CloseUtil.close(inputStream); @@ -120,7 +127,6 @@ public synchronized static String md5(File file) { *

    * 计算一个文件的MD5值(16位) *

    - * 线程安全 * * @param file 待计算的文件 * @return MD5加密后的字符串(16位) diff --git a/src/main/java/com/yishuifengxiao/common/tool/collections/CollUtil.java b/src/main/java/com/yishuifengxiao/common/tool/collections/CollUtil.java index 40c8e7f..ebcc662 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/collections/CollUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/collections/CollUtil.java @@ -5,18 +5,25 @@ import com.yishuifengxiao.common.tool.entity.Page; -import java.util.Arrays; -import java.util.Collection; +import java.util.*; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.BiConsumer; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** *

    - * 元素长度判断工具 + * 集合增强工具类 *

    - * - * 该工具的主要目标是快速地判断集合是否为空,其具备以下的几项功能 + *

    + * 该工具的主要目标是快速地判断集合是否为空,其具备以下的几项功能

    *
      *
    1. 判断集合是否为空或者空元素的集合
    2. *
    3. 判断集合是否仅有一个元素
    4. + *
    5. 将集合转换成java8中的串行流或并行流
    6. + *
    7. 将数组转换成List或Set,而从避免Arrays.asList()转换后存在的问题
    8. + *
    9. 安全地获取集合或数组里的第一个元素
    10. + *
    11. 安全地获取集合或数组里的元素的数量
    12. *
    * * @author yishui @@ -24,6 +31,227 @@ * @since 1.0.0 */ public final class CollUtil { + /** + * 将数据安全地转换成串行流Stream + * + * @param 集合里数据的类型 + * @param data 需要转换的集合数据 + * @return 串行流Stream + */ + public static Stream stream(Collection data) { + if (null == data) { + data = new ArrayList<>(); + } + return data.stream(); + } + + /** + * 将数据安全地转换成串行流Stream + * + * @param 集合里数据的类型 + * @param data 需要转换的集合数据 + * @return 串行流Stream + */ + public static Stream stream(T[] data) { + if (null == data) { + return new ArrayList().stream(); + } + return Arrays.stream(data); + } + + + /** + * 将数组转换成List + * + * @param 数组里元素的类型 + * @param objs 需要转换的数据 + * @return 转换后的List数据 + */ + public static List toList(T[] objs) { + if (null == objs) { + return new ArrayList<>(); + } + return Arrays.asList(objs).stream().collect(Collectors.toList()); + } + + + /** + * 将数组转换成Set + * + * @param 数组里元素的类型 + * @param objs 需要转换的数据 + * @return 转换后的Set数据 + */ + public static Set toSet(T[] objs) { + if (null == objs) { + return new HashSet<>(); + } + return Arrays.asList(objs).stream().collect(Collectors.toSet()); + } + + /** + * 取出数组的第一个元素, 若数组为空则返回null + * + * @param 数组里数据的类型 + * @param data 数据源 + * @return 数组的第一个元素 + */ + public static Optional first(T[] data) { + if (data == null || data.length == 0) { + return Optional.empty(); + } + return Optional.ofNullable(data[0]); + } + + + /** + * 取出集合里的第一个元素,若集合为空则返回为null + * + * @param 集合里元素的类型 + * @param data 数据源 + * @return 集合里的第一个元素 + */ + public static Optional first(Collection data) { + if (data == null || data.size() == 0) { + return Optional.empty(); + } + Iterator it = data.iterator(); + return Optional.ofNullable(it.next()); + } + + + /** + * 取出List中的最后一个非空元素,如果Stream为空则返回null + * + * @param List里的数据的类型 + * @param data 链表 + * @return List中的第一个非空元素 + */ + public static Optional last(List data) { + if (data == null || data.size() == 0) { + return null; + } + return Optional.ofNullable(data.get(data.size() - 1)); + } + + + /** + * 将指定的数据转换成list + * + * @param 数据类型 + * @param a 需要转换的数据 + * @return 转换后的list + */ + @SafeVarargs + public static List asList(T... a) { + if (null == a || a.length == 0) { + return new ArrayList<>(); + } + List list = new ArrayList<>(a.length); + for (int i = 0; i < a.length; i++) { + list.add(a[i]); + } + return list; + } + + /** + * 将不定参数转为数组 + * + * @param vals 不定参数 + * @param 参数类型 + * @return 数组 + */ + public static T[] asArray(T... vals) { + return vals; + } + + /** + * 将多个链表合并成一个链表 + * + * @param list 待合并的链表 + * @param 数据类型 + * @return 合并后的链表 + */ + @SuppressWarnings("unchecked") + public static List merge(Collection... list) { + if (null == list) { + return Collections.emptyList(); + } + List result = new ArrayList<>(); + for (Collection data : list) { + if (null == data) { + continue; + } + result.addAll(data); + } + return result; + } + + + /** + * 将指定的数据转换成Set + * + * @param 数据类型 + * @param a 需要转换的数据 + * @return 转换后的Set + */ + @SafeVarargs + public static Set asSet(T... a) { + if (null == a || a.length == 0) { + return new HashSet<>(); + } + Set list = new HashSet<>(a.length); + for (int i = 0; i < a.length; i++) { + list.add(a[i]); + } + return list; + } + + + /** + * 获取链表里的第index个元素 + * + * @param data 链表 + * @param index 序号 + * @param 数据类型 + * @return 链表为空或index超过容量时返回为null + */ + public static Optional get(List data, int index) { + if (null == data || data.size() <= index) { + return Optional.empty(); + } + return Optional.ofNullable(data.get(index)); + } + + /** + * 获取数组里的第index个元素 + * + * @param data 链表 + * @param index 序号 + * @param 数据类型 + * @return 数组为空或index超过容量时返回为null + */ + public static Optional get(T[] data, int index) { + if (null == data || data.length <= index) { + return Optional.empty(); + } + return Optional.ofNullable(data[index]); + } + + /** + * 遍历一个集合 + * + * @param collection 待遍历的集合 + * @param consumer 消费者,第一个参数为元素的序号,第二个参数为被遍历到的元素 + * @param 数据类型 + */ + public static void forEach(Collection collection, BiConsumer consumer) { + if (null == collection || collection.isEmpty()) { + return; + } + AtomicLong atomic = new AtomicLong(0L); + collection.stream().forEach(v -> consumer.accept(atomic.getAndIncrement(), v)); + } /** * 判断分页对象是否为空 @@ -78,6 +306,26 @@ public static boolean isNotEmpty(T[] data) { return !isEmpty(data); } + /** + * 判断Map是否为空 + * + * @param map 数据源 + * @return 如果为空则返回true,否则为false + */ + public static boolean isEmpty(Map map) { + return map == null || map.isEmpty(); + } + + /** + * 判断Map是否不为空 + * + * @param map 数据源 + * @return 如果不为空则返回true,否则为false + */ + public static boolean isNotEmpty(Map map) { + return null != map && !map.isEmpty(); + } + /** * 判断集合是否为空 * diff --git a/src/main/java/com/yishuifengxiao/common/tool/collections/DataUtil.java b/src/main/java/com/yishuifengxiao/common/tool/collections/DataUtil.java deleted file mode 100644 index 05dd15d..0000000 --- a/src/main/java/com/yishuifengxiao/common/tool/collections/DataUtil.java +++ /dev/null @@ -1,451 +0,0 @@ -/** - * - */ -package com.yishuifengxiao.common.tool.collections; - -import com.yishuifengxiao.common.tool.exception.UncheckedException; - -import java.util.*; -import java.util.concurrent.atomic.AtomicLong; -import java.util.function.BiConsumer; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -/** - *

    - * 集合元素处理工具 - *

    - *

    - * 该工具的主要目标是在不发生NPE的前提下对集合以及集合里的元素进行操作,其具备以下的几项功能 - *

      - *
    1. 将集合转换成java8中的串行流或并行流
    2. - *
    3. 将数组转换成List或Set,而从避免Arrays.asList()转换后存在的问题
    4. - *
    5. 安全地获取集合里的第一个元素
    6. - *
    - * - *

    - * 该工具是一个非线程安全类的工具 - *

    - * - * @author yishui - * @version 1.0.0 - * @since 1.0.0 - */ -public final class DataUtil { - - /** - * 将数据安全地转换成串行流Stream - * - * @param 集合里数据的类型 - * @param data 需要转换的集合数据 - * @return 串行流Stream - */ - public static Stream stream(Collection data) { - if (null == data) { - data = new ArrayList<>(); - } - return data.stream(); - } - - /** - * 将数据安全地转换成串行流Stream,并检查输入的数据,若输入的数据源为空就抛出异常 - * - * @param 集合里数据的类型 - * @param data 需要转换的集合数据 - * @param msg 异常提示信息 - * @return 串行流Stream - */ - public static Stream stream(Collection data, String msg) { - if (null == data) { - throw new UncheckedException(msg); - } - return data.stream(); - } - - /** - * 将数据安全地转换成串行流Stream - * - * @param 集合里数据的类型 - * @param data 需要转换的集合数据 - * @return 串行流Stream - */ - public static Stream stream(T[] data) { - return stream(toList(data)); - } - - /** - * 将数据安全地转换成串行流Stream,并检查输入的数据,若输入的数据源为空就抛出异常 - * - * @param 集合里数据的类型 - * @param data 需要转换的集合数据 - * @param msg 异常提示信息 - * @return 串行流Stream - */ - public static Stream stream(T[] data, String msg) { - if (null == data) { - throw new UncheckedException(msg); - } - return stream(toList(data)); - } - - /** - * 将数据安全地转换成并行流Stream - * - * @param 集合里数据的类型 - * @param data 需要转换的集合数据 - * @return 并行流Stream - */ - public static Stream parallelStream(Collection data) { - if (null == data) { - data = new ArrayList<>(); - } - return data.parallelStream(); - } - - /** - * 将数据安全地转换成并行流Stream,并检查输入的数据,若输入的数据源为空就抛出异常 - * - * @param 集合里数据的类型 - * @param data 需要转换的集合数据 - * @param msg 异常提示信息 - * @return 并行流Stream - */ - public static Stream parallelStream(Collection data, String msg) { - if (null == data) { - throw new UncheckedException(msg); - } - return data.parallelStream(); - } - - /** - * 将数据安全地转换成并行流Stream - * - * @param 集合里数据的类型 - * @param data 需要转换的集合数据 - * @return 并行流Stream - */ - public static Stream parallelStream(T[] data) { - return parallelStream(toList(data)); - } - - /** - * 将数据安全地转换成并行流Stream,并检查输入的数据,若输入的数据源为空就抛出异常 - * - * @param 集合里数据的类型 - * @param data 需要转换的集合数据 - * @param msg 异常提示信息 - * @return 并行流Stream - */ - public static Stream parallelStream(T[] data, String msg) { - if (null == data) { - throw new UncheckedException(msg); - } - return parallelStream(toList(data)); - } - - /** - * 将数组转换成List - * - * @param 数组里元素的类型 - * @param objs 需要转换的数据 - * @return 转换后的List数据 - */ - public static List toList(T[] objs) { - if (null == objs) { - return new ArrayList<>(); - } - return Arrays.asList(objs).stream().collect(Collectors.toList()); - } - - - /** - * 将数组转换成Set - * - * @param 数组里元素的类型 - * @param objs 需要转换的数据 - * @return 转换后的Set数据 - */ - public static Set toSet(T[] objs) { - if (null == objs) { - return new HashSet<>(); - } - return Arrays.asList(objs).stream().collect(Collectors.toSet()); - } - - /** - * 取出Stream中的第一个非空元素,如果Stream为空则返回null - * - * @param 数据流里的数据的类型 - * @param data 数据流 - * @return Stream中的第一个非空元素 - */ - public static T first(Stream data) { - if (data == null) { - return null; - } - - return data.filter(Objects::nonNull).findFirst().orElse(null); - } - - /** - * 取出Stream中的第一个非空元素,如果Stream为空则返回 缺省值 - * - * @param 数据流里的数据的类型 - * @param data 数据流 - * @param defaultValue 缺省值 - * @return Stream中的第一个非空元素 - */ - public static T first(Stream data, T defaultValue) { - if (data == null) { - return null; - } - - return data.filter(Objects::nonNull).findFirst().orElse(defaultValue); - } - - /** - * 取出数组的第一个元素, 若数组为空则返回null - * - * @param 数组里数据的类型 - * @param data 数据源 - * @return 数组的第一个元素 - */ - public static T first(T[] data) { - if (data == null || data.length == 0) { - return null; - } - return data[0]; - } - - /** - * 取出数组的第一个元素, 若数组为空则返回给定的缺省值 - * - * @param 数组里的数据的类型 - * @param data 数据源 - * @param defaultValue 缺省值 - * @return 数组里的第一个元素 - */ - public static T first(T[] data, T defaultValue) { - T t = first(data); - return null == t ? defaultValue : t; - } - - /** - * 取出数组的第一个元素,若数组为空则抛出异常 - * - * @param 数组里的数据的类型 - * @param data 数据源 - * @param errorMsg 异常提示信息 - * @return 数组的第一个元素 - */ - public static T first(T[] data, String errorMsg) { - T t = first(data); - if (null == t) { - throw new UncheckedException(errorMsg); - } - return t; - } - - /** - * 取出集合里的第一个元素,若集合为空则返回为null - * - * @param 集合里元素的类型 - * @param data 数据源 - * @return 集合里的第一个元素 - */ - public static T first(Collection data) { - if (data == null || data.size() == 0) { - return null; - } - Iterator it = data.iterator(); - return it.next(); - } - - /** - * 取出集合的第一个元素, 若集合为空则返回给定的缺省值 - * - * @param 集合里的数据的类型 - * @param data 数据源 - * @param defaultValue 缺省值 - * @return 集合里的第一个元素 - */ - public static T first(Collection data, T defaultValue) { - T t = first(data); - return null == t ? defaultValue : t; - } - - /** - * 取出集合的第一个元素,若集合为空则抛出异常 - * - * @param 集合里的数据的类型 - * @param data 数据源 - * @param errorMsg 异常提示信息 - * @return 集合里的第一个元素 - */ - public static T first(Collection data, String errorMsg) { - T t = first(data); - if (null == t) { - throw new UncheckedException(errorMsg); - } - return t; - } - - /** - * 取出List中的最后一个非空元素,如果Stream为空则返回null - * - * @param List里的数据的类型 - * @param data 链表 - * @return List中的第一个非空元素 - */ - public static T last(List data) { - if (data == null || data.size() == 0) { - return null; - } - return data.get(data.size() - 1); - } - - /** - * 取出Stream中的最后一个非空元素,如果Stream为空则返回null - * - * @param 数据流里的数据的类型 - * @param data 数据流 - * @return Stream中的第一个非空元素 - */ - public static T last(Stream data) { - if (data == null) { - return null; - } - - return last(data.filter(Objects::nonNull).collect(Collectors.toList())); - } - - /** - * 将指定的数据转换成list - * - * @param 数据类型 - * @param a 需要转换的数据 - * @return 转换后的list - */ - @SafeVarargs - public static List asList(T... a) { - if (null == a || a.length == 0) { - return new ArrayList<>(); - } - List list = new ArrayList<>(a.length); - for (int i = 0; i < a.length; i++) { - list.add(a[i]); - } - return list; - } - - /** - * 将多个链表合并成一个链表 - * - * @param list 待合并的链表 - * @param 数据类型 - * @return 合并后的链表 - */ - @SuppressWarnings("unchecked") - public static List merge(List... list) { - if (null == list) { - return Collections.emptyList(); - } - List result = new ArrayList<>(); - for (List data : list) { - if (null == data) { - continue; - } - result.addAll(data); - } - return result; - } - - /** - * 将多个Set合并成一个Set - * - * @param sets 待合并的Set - * @param 数据类型 - * @return 合并后的Set - */ - @SuppressWarnings({"unchecked"}) - public static Set merge(Set... sets) { - if (null == sets) { - return Collections.emptySet(); - } - Set result = new HashSet<>(); - for (Set data : sets) { - if (null == data) { - continue; - } - result.addAll(data); - } - return result; - } - - /** - * 将指定的数据转换成Set - * - * @param 数据类型 - * @param a 需要转换的数据 - * @return 转换后的Set - */ - @SafeVarargs - public static Set asSet(T... a) { - if (null == a || a.length == 0) { - return new HashSet<>(); - } - Set list = new HashSet<>(a.length); - for (int i = 0; i < a.length; i++) { - list.add(a[i]); - } - return list; - } - - - /** - * 获取链表里的第index个元素 - * - * @param data 链表 - * @param index 序号 - * @param 数据类型 - * @return 链表为空或index超过容量时返回为null - */ - public static T get(List data, int index) { - if (null == data || data.size() <= index) { - return null; - } - return data.get(index); - } - - /** - * 获取数组里的第index个元素 - * - * @param data 链表 - * @param index 序号 - * @param 数据类型 - * @return 数组为空或index超过容量时返回为null - */ - public static T get(T[] data, int index) { - if (null == data || data.length <= index) { - return null; - } - return data[index]; - } - - /** - * 遍历一个集合 - * - * @param collection 待遍历的集合 - * @param consumer 消费者,第一个参数为元素的序号,第二个参数为被遍历到的元素 - * @param 数据类型 - */ - public static void forEach(Collection collection, BiConsumer consumer) { - if (null == collection || collection.isEmpty()) { - return; - } - AtomicLong atomic = new AtomicLong(0L); - collection.stream().forEach(v -> consumer.accept(atomic.getAndIncrement(), v)); - } - - -} diff --git a/src/main/java/com/yishuifengxiao/common/tool/collections/StreamUtil.java b/src/main/java/com/yishuifengxiao/common/tool/collections/StreamUtil.java index cd742cd..db372ec 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/collections/StreamUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/collections/StreamUtil.java @@ -3,13 +3,9 @@ import java.util.*; import java.util.function.Predicate; import java.util.stream.Collectors; -import java.util.stream.Stream; /** *

    数据流工具

    - *

    - * 该工具是一个线程安全类的工具 - *

    * * @author yishui * @version 1.0.0 @@ -17,172 +13,77 @@ */ public class StreamUtil { - /** - *

    从数据流获取一个符合条件的非空数据

    - *

    注意:会排除掉null数据

    - * - * @param stream 输入的数据流 - * @param predicate 判断条件,不能为空null - * @param 数据类型 - * @return 任意一个满足条件的非空数据 - */ - public synchronized static T findAny(Stream stream, Predicate predicate) { - if (null == stream) { - return null; - } - return stream.filter(Objects::nonNull).filter(predicate).findAny().orElse(null); - } /** *

    从List获取一个符合条件的非空数据

    *

    注意:会排除掉null数据

    * - * @param list 输入的数据集合 - * @param predicate 判断条件,不能为空null - * @param 数据类型 + * @param collection 输入的数据集合 + * @param predicate 判断条件,不能为空null + * @param 数据类型 * @return 任意一个满足条件的非空数据 */ - public synchronized static T findAny(List list, Predicate predicate) { - if (null == list) { - return null; + public static Optional findAny(Collection collection, Predicate predicate) { + if (null == collection || null == predicate) { + return Optional.empty(); } - return list.parallelStream().filter(Objects::nonNull).filter(predicate).findAny().orElse(null); + return collection.parallelStream().filter(Objects::nonNull).filter(predicate).findFirst(); } - /** - *

    从Set获取一个符合条件的非空数据

    - *

    注意:会排除掉null数据

    - * - * @param set 输入的数据集合 - * @param predicate 判断条件,不能为空null - * @param 数据类型 - * @return 任意一个满足条件的非空数据 - */ - public synchronized static T findAny(Set set, Predicate predicate) { - if (null == set) { - return null; - } - return set.parallelStream().filter(Objects::nonNull).filter(predicate).findAny().orElse(null); - } /** * 判断集合中是否存在符合指定条件的元素 * - * @param list 待匹配的集合 - * @param predicate 集合条件 - * @param 数据类型 + * @param collection 待匹配的集合 + * @param predicate 集合条件 + * @param 数据类型 * @return 存在返回为true,否则为false */ - public synchronized static boolean contain(List list, Predicate predicate) { - return null != findAny(list, predicate); + public static boolean contain(Collection collection, + Predicate predicate) { + return collection.parallelStream().filter(Objects::nonNull).allMatch(predicate); } - /** - * 判断集合中是否存在符合指定条件的元素 - * - * @param set 待匹配的集合 - * @param predicate 集合条件 - * @param 数据类型 - * @return 存在返回为true,否则为false - */ - public synchronized static boolean contain(Set set, Predicate predicate) { - return null != findAny(set, predicate); - } /** *

    在集合中根据条件删除一个元素

    *

    注意:会排除掉null数据

    * - * @param list 原始集合 - * @param predicate 删除条件,不能为空null - * @param 数据类型 + * @param collection 原始集合 + * @param predicate 删除条件,不能为空null + * @param 数据类型 * @return 删除元素之后的集合 */ - public synchronized static List remove(List list, Predicate predicate) { - if (null == list) { - list = new ArrayList<>(); + public static List remove(Collection collection, + Predicate predicate) { + if (null == collection) { + collection = new ArrayList<>(); } List collect = - list.parallelStream().filter(Objects::nonNull).filter(predicate.negate()).collect(Collectors.toList()); - return collect; - } - - - /** - *

    在集合中根据条件删除一个元素

    - *

    注意:会排除掉null数据

    - * - * @param set 原始集合 - * @param predicate 删除条件,不能为空null - * @param 数据类型 - * @return 删除元素之后的集合 - */ - public synchronized static Set remove(Set set, Predicate predicate) { - if (null == set) { - set = new HashSet<>(); - } - Set collect = - set.parallelStream().filter(Objects::nonNull).filter(predicate.negate()).collect(Collectors.toSet()); - return collect; - } - - - /** - *

    在集合中替换并添加一个元素

    - * - * @param list 原始集合 - * @param t 待添加的元素 - * @param predicate 替换条件,不能为空null - * @param 数据类型 - * @return 替换元素之后的集合 - */ - @SuppressWarnings({"rawtypes", "unchecked"}) - public synchronized static List replace(List list, T t, Predicate predicate) { - if (null == list) { - list = new ArrayList<>(); - } - List collect = remove(list, predicate); - collect.add(t); + collection.parallelStream().filter(Objects::nonNull).filter(predicate.negate()).collect(Collectors.toList()); return collect; } - /** - *

    在集合中替换并添加一个元素

    - * - * @param set 原始集合 - * @param t 待添加的元素 - * @param predicate 替换条件,不能为空null - * @param 数据类型 - * @return 替换元素之后的集合 - */ - @SuppressWarnings({"rawtypes", "unchecked"}) - public synchronized static Set replace(Set set, T t, Predicate predicate) { - if (null == set) { - set = new HashSet<>(); - } - Set collect = remove(set, predicate); - collect.add(t); - return collect; - } /** *

    先判断集合中是否存在此元素,若不存在则添加,存在该元素则跳过

    * - * @param list 原始集合 - * @param t 待添加的元素 - * @param predicate 存在条件判断,不能为空null - * @param 数据类型 + * @param collection 原始集合 + * @param t 待添加的元素 + * @param predicate 存在条件判断,不能为空null + * @param 数据类型 * @return 处理之后的元素集合 */ - public synchronized static List addIfNoPresent(List list, T t, Predicate predicate) { - if (null == list) { - list = new ArrayList<>(); + public static List addIfNoPresent(Collection collection, T t, + Predicate predicate) { + if (null == collection) { + return new ArrayList<>(); } - Optional optional = list.parallelStream().filter(Objects::nonNull).filter(predicate).findAny(); - if (!optional.isPresent()) { - list.add(t); + if (!contain(collection, predicate)) { + collection.add(t); } - return list; + return collection.stream().collect(Collectors.toList()); } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/context/LocalCache.java b/src/main/java/com/yishuifengxiao/common/tool/context/LocalCache.java index 5153044..00b61a5 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/context/LocalCache.java +++ b/src/main/java/com/yishuifengxiao/common/tool/context/LocalCache.java @@ -2,9 +2,8 @@ import org.apache.commons.lang3.StringUtils; -import java.util.HashMap; -import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Supplier; /** @@ -13,10 +12,6 @@ *

    * 该工具主要是一个基于内存的KV键值对存储工具。 * - *

    - * 该工具是一个线程安全类的工具 - *

    - * * @author yishui * @version 1.0.0 * @since 1.0.0 @@ -25,7 +20,7 @@ public final class LocalCache { /** * 本地线程存储 */ - private static final Map LOCAL_HOLDER = new HashMap<>(); + private static final ConcurrentHashMap LOCAL_HOLDER = new ConcurrentHashMap<>(); /** @@ -47,7 +42,7 @@ public static void put(Object value) { * @param key 待存储的数据的key * @param value 待存储的数据 */ - public static synchronized void put(String key, Object value) { + public synchronized static void put(String key, Object value) { if (StringUtils.isBlank(key) || null == value) { return; } @@ -60,7 +55,7 @@ public static synchronized void put(String key, Object value) { * @param key 待存储的数据的key * @return 获取到的存储数据 */ - public static synchronized Object get(String key) { + public synchronized static Object get(String key) { if (StringUtils.isBlank(key)) { return null; } @@ -68,15 +63,15 @@ public static synchronized Object get(String key) { } /** - * 根据key获取一个数据 + * 根据指定的key获取对应的值,若key不存在时则调用supplier * * @param key 指定的key - * @param supplier Supplier,不存在key对应的数据时触发 + * @param supplier Supplier * @param 数据类型 - * @return 返回的数据 + * @return 获取的值 */ @SuppressWarnings("unchecked") - public static synchronized T get(String key, Supplier supplier) { + public synchronized static T get(String key, Supplier supplier) { Object value = get(key.trim()); if (null != value) { try { @@ -93,24 +88,6 @@ public static synchronized T get(String key, Supplier supplier) { } - /** - * 根据数据的key获取数据 - * - * @param key 待存储的数据的key - * @param clazz 数据的类型Class - * @param 数据的类型 - * @return 获取到的存储数据 - */ - @SuppressWarnings("unchecked") - public static T get(String key, Class clazz) { - try { - return (T) get(key.trim()); - } catch (Exception e) { - } - return null; - - } - /** *

    根据数据的key获取数据

    *

    此方式下默认key为clazz.getName()

    @@ -119,76 +96,29 @@ public static T get(String key, Class clazz) { * @param 数据的类型 * @return 获取到的存储数据 */ - public static synchronized T get(Class clazz) { + public static T get(Class clazz) { if (null == clazz) { return null; } - return get(clazz.getName(), clazz); - } - - /** - * 根据数据的key获取数据,若成功获取到此数据则从缓存中删除此数据 - * - * @param key 待存储的数据的key - * @param clazz 数据的类型Class - * @param 数据的类型 - * @return 获取到的存储数据 - */ - public synchronized static T getAndRemove(String key, Class clazz) { - if (StringUtils.isBlank(key)) { - return null; - } - T value = get(key.trim(), clazz); - if (null != value) { - remove(key.trim()); - } - return value; - - } - - /** - *

    根据数据的key获取数据,若成功获取到此数据则从缓存中删除此数据

    - *

    此方式下默认key为clazz.getName()

    - * - * @param clazz 数据的类型Class - * @param 数据的类型 - * @return 获取到的存储数据 - */ - public static T getAndRemove(Class clazz) { - if (null == clazz) { - return null; + try { + return (T) get(clazz.getName()); + } catch (Exception e) { } + return null; - return getAndRemove(clazz.getName(), clazz); } - /** - * 根据数据的key获取数据,若成功获取到此数据则从缓存中删除此数据 - * - * @param key 待存储的数据的key - * @return 获取到的存储数据 - */ - public static synchronized Object getAndRemove(String key) { - if (StringUtils.isBlank(key)) { - return null; - } - Object value = get(key.trim()); - if (null != value) { - remove(key.trim()); - } - return value; - } /** * 移除存储的数据 * * @param key 待移除的数据的key */ - public static synchronized void remove(String key) { + public synchronized static void remove(String key) { if (StringUtils.isBlank(key)) { return; } - LOCAL_HOLDER.remove(key.trim()); + LOCAL_HOLDER.remove(key); } /** @@ -210,7 +140,7 @@ public static void remove(Class clazz) { * * @return 所有存储的数据的key */ - public static synchronized Set keys() { + public static Set keys() { return LOCAL_HOLDER.keySet(); } @@ -220,7 +150,7 @@ public static synchronized Set keys() { * @param key 指定的key * @return 包含返回为true, 否则为false */ - public static synchronized boolean keys(String key) { + public static boolean containsKey(String key) { if (StringUtils.isBlank(key)) { return false; } @@ -230,7 +160,7 @@ public static synchronized boolean keys(String key) { /** * 清空所有存储的数据 */ - public static synchronized void clear() { + public synchronized static void clear() { LOCAL_HOLDER.clear(); } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/datetime/DateOffsetUtil.java b/src/main/java/com/yishuifengxiao/common/tool/datetime/DateOffsetUtil.java index a8dbb19..ec9b403 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/datetime/DateOffsetUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/datetime/DateOffsetUtil.java @@ -11,7 +11,7 @@ *

    * 基于Date数据类型的日期时间偏移工具 *

    - * 该工具的主要目的是计算距离当前日期、时间指定偏移量的时间点,该工具是一个线程安全类的工具,主要功能如下: + * 该工具的主要目的是计算距离当前日期、时间指定偏移量的时间点,主要功能如下: *
      *
    1. 获取今天的开始时间点(00:00:00)
    2. *
    3. 获取昨天的开始时间点(00:00:00)和结束时间点(23:59:59)
    4. @@ -26,9 +26,6 @@ *
    5. 获取过去指定年份的那个时间的1月1号的那个时间的开始时间点(00:00:00)
    6. *
    * - *

    - * 该工具是一个线程安全类的工具 - *

    * * @author yishui * @version 1.0.0 diff --git a/src/main/java/com/yishuifengxiao/common/tool/datetime/DateTimeUtil.java b/src/main/java/com/yishuifengxiao/common/tool/datetime/DateTimeUtil.java index 92b39c9..ebb93f8 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/datetime/DateTimeUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/datetime/DateTimeUtil.java @@ -20,26 +20,21 @@ *

    * 时间转换解析工具 *

    - * 该工具的主要作用是将LocalDateTime和Date形式的时间互相转换以及将字符串格式的时间和日期时间互相转换。该工具是一个线程安全类的工具,其主要的功能如下: + * 该工具的主要作用是将LocalDateTime和Date形式的时间互相转换以及将字符串格式的时间和日期时间互相转换,其主要的功能如下: *
      *
    1. 将LocalDateTime和Date形式的时间互相转换
    2. *
    3. 将指定格式的字符串按解析为日期时间
    4. *
    5. 将日期时间按照指定的格式转换成字符串
    6. *
    * - *

    - * 该工具是一个线程安全类的工具 - *

    - * * @author yishui * @version 1.0.0 * @since 1.0.0 */ @Slf4j public final class DateTimeUtil { - /** - * 默认的时区-北京 utc+8 + * 默认的中国时区uct+8 */ public final static ZoneId ZONEID_OF_CHINA = OsUtils.ZONEID_OF_CHINA; @@ -168,11 +163,21 @@ public synchronized static Date parseDate(String timeStr, String... patterns) { return null; } try { - patterns = (null != patterns && patterns.length > 0) ? patterns : new String[]{DEFAULT_DATETIME_FORMAT, DEFAULT_DATE_FORMAT, SIMPLE_DATETIME_FORMAT, DEFAULT_FULL_DATE_FORMAT, DEFAULT_SLASH_DATE_FORMAT}; + patterns = (null != patterns && patterns.length > 0) ? patterns : + new String[]{DEFAULT_DATETIME_FORMAT, DEFAULT_DATE_FORMAT, + SIMPLE_DATETIME_FORMAT, DEFAULT_FULL_DATE_FORMAT, + DEFAULT_SLASH_DATE_FORMAT}; return DateUtils.parseDate(timeStr.trim(), patterns); } catch (Exception e) { - log.info("【易水工具】按照解析规则 {} 从字符串 {} 中解析出时间时出现问题,出现问题的原因为{}", patterns, timeStr, e.getMessage()); - throw new UncheckedException("从字符串中解析时间失败").setContext(e); + if (log.isInfoEnabled()) { + log.info("There was a problem parsing the time from the string {} according to " + + "the parsing rule {}, " + + "and " + + "the reason for the problem is {}", patterns, timeStr, + e); + } + + throw new UncheckedException(e); } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/datetime/LocalDateTimeUtil.java b/src/main/java/com/yishuifengxiao/common/tool/datetime/LocalDateTimeUtil.java index 83ce7ca..0de177e 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/datetime/LocalDateTimeUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/datetime/LocalDateTimeUtil.java @@ -12,7 +12,7 @@ *

    * 基于LocalDateTime数据类型的日期时间偏移工具 *

    - * 该工具的主要目的是计算距离当前日期、时间指定偏移量的时间点,该工具是一个线程安全类的工具,主要功能如下: + * 该工具的主要目的是计算距离当前日期、时间指定偏移量的时间点,主要功能如下: *
      *
    1. 获取今天的开始时间点(00:00:00)
    2. *
    3. 获取昨天的开始时间点(00:00:00)和结束时间点(23:59:59)
    4. @@ -27,10 +27,6 @@ *
    5. 获取过去指定年份的那个时间的1月1号的那个时间的开始时间点(00:00:00)
    6. *
    * - *

    - * 该工具是一个线程安全类的工具 - *

    - * * @author yishui * @version 1.0.0 * @since 1.0.0 diff --git a/src/main/java/com/yishuifengxiao/common/tool/entity/BoolStat.java b/src/main/java/com/yishuifengxiao/common/tool/entity/BoolStat.java index 35bd1aa..9277778 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/entity/BoolStat.java +++ b/src/main/java/com/yishuifengxiao/common/tool/entity/BoolStat.java @@ -22,7 +22,7 @@ * @since 1.0.0 */ @Schema(name = " 布尔类型替代枚举", description = "用于替换Boolean的使用,方便在灵活业务场景下进行功能扩展") -public enum BoolStat { +public enum BoolStat implements RootEnum { /** @@ -33,17 +33,17 @@ public enum BoolStat { /** * 类似Boolean中的true值,对应编码1 */ - @Schema(name ="类似Boolean中的true值,对应编码1") True(1, true), + @Schema(name = "类似Boolean中的true值,对应编码1") True(1, true), /** * 类似Boolean中的null值,对应编码-1 */ - @Schema(name ="类似Boolean中的null值,对应编码-1") Null(-1, null); + @Schema(name = "类似Boolean中的null值,对应编码-1") Null(-1, null); /** * 编码 */ - private int code; + private Integer code; /** * 代表的值 */ @@ -54,10 +54,20 @@ public enum BoolStat { * * @return 枚举值对应的编码 */ - public int code() { + public Integer code() { return this.code; } + /** + * 获取枚举值的描述 + * + * @return 枚举值的描述 + */ + @Override + public String description() { + return null; + } + /** * 枚举状态值是否对应的布尔值true diff --git a/src/main/java/com/yishuifengxiao/common/tool/entity/KeyValue.java b/src/main/java/com/yishuifengxiao/common/tool/entity/KeyValue.java index 0136429..77855fd 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/entity/KeyValue.java +++ b/src/main/java/com/yishuifengxiao/common/tool/entity/KeyValue.java @@ -1,5 +1,7 @@ package com.yishuifengxiao.common.tool.entity; +import com.fasterxml.jackson.annotation.JsonIgnore; + import java.io.Serializable; /** @@ -86,6 +88,7 @@ public KeyValue setVal(V val) { * * @return 若该实例的键和值均不为null即返回为true, 否则为false */ + @JsonIgnore public boolean notNull() { return null != this.key && null != this.val; } @@ -95,6 +98,7 @@ public boolean notNull() { * * @return 若该该实例的键和值是否均为null即返回为true, 否则为false */ + @JsonIgnore public boolean isNull() { return null == this.key && null == this.val; } @@ -104,6 +108,7 @@ public boolean isNull() { * * @return 若该实例的键为null即返回为true, 否则为false */ + @JsonIgnore public boolean nullKey() { return null == this.key; } @@ -113,6 +118,7 @@ public boolean nullKey() { * * @return 若该实例的值为null即返回为true, 否则为false */ + @JsonIgnore public boolean nullVal() { return null == this.val; } diff --git a/src/main/java/com/yishuifengxiao/common/tool/entity/Page.java b/src/main/java/com/yishuifengxiao/common/tool/entity/Page.java index cdaaf8e..036f5b6 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/entity/Page.java +++ b/src/main/java/com/yishuifengxiao/common/tool/entity/Page.java @@ -218,7 +218,8 @@ public static Number page(Number total, Number size) { if (null == size) { size = DEFAULT_PAGE_SIZE; } - return total.longValue() % size.longValue() == 0 ? total.longValue() / size.longValue() : (total.longValue() / size.longValue() + 1); + return total.longValue() % size.longValue() == 0 ? total.longValue() / size.longValue() : + (total.longValue() / size.longValue() + 1); } @@ -233,21 +234,8 @@ public Page map(DataConverter converter) { if (null == this.data) { return Page.of(Collections.emptyList(), this.total, this.size, this.num); } - return Page.of(this.data.stream().map(converter::map).collect(Collectors.toList()), this.total, this.size, this.num); - } - - /** - *

    将一种类型数据的分页对象转换成另一种数据类型的分页对象

    - * - * @param 分页元素转换工具的类型 - * @param converter 分页元素转换工具 - * @return 另一种数据类型的分页对象 - */ - public Page maps(ListConverter converter) { - if (null == this.data) { - return Page.of(Collections.emptyList(), this.total, this.size, this.num); - } - return Page.of(converter.map(this.data), this.total, this.size, this.num); + return Page.of(this.data.stream().map(converter::map).collect(Collectors.toList()), this.total, this.size, + this.num); } @@ -346,7 +334,8 @@ public boolean isEmpty() { @Override public String toString() { - String builder = "Page [size=" + size + ", num=" + num + ", data=" + data + ", pages=" + pages + ", " + "total=" + total + "]"; + String builder = "Page [size=" + size + ", num=" + num + ", data=" + data + ", pages=" + pages + ", " + + "total=" + total + "]"; return builder; } @@ -374,28 +363,5 @@ public interface DataConverter { T map(S s); } - /** - *

    - * 分页数据转换器 - *

    - * 将分页对象里的源数据转换成目标数据 - * - * @param 源数据 - * @param 目标数据 - * @author yishui - * @version 1.0.0 - * @since 1.0.0 - */ - @FunctionalInterface - public interface ListConverter { - - /** - * 将源数据转换成目标数据 - * - * @param s 源数据 - * @return 目标数据 - */ - List map(List s); - } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/entity/PageQuery.java b/src/main/java/com/yishuifengxiao/common/tool/entity/PageQuery.java index 05373d4..8e5ee26 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/entity/PageQuery.java +++ b/src/main/java/com/yishuifengxiao/common/tool/entity/PageQuery.java @@ -29,7 +29,6 @@ public class PageQuery extends Slice implements Serializable { * */ private static final long serialVersionUID = 1L; - /** * 查询参数 */ diff --git a/src/main/java/com/yishuifengxiao/common/tool/entity/Response.java b/src/main/java/com/yishuifengxiao/common/tool/entity/Response.java index de5d4dd..2db8f3e 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/entity/Response.java +++ b/src/main/java/com/yishuifengxiao/common/tool/entity/Response.java @@ -57,8 +57,9 @@ public class Response implements Serializable { * "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status">https://developer.mozilla * .org/en-US/docs/Web/HTTP/Status */ - @Schema(title = "请求的响应码,这里借用HttpStatus作为状态标识,具体代码的含义请参见 HttpStatus( https://developer.mozilla" + ".org/en-US/docs/Web/HTTP/Status)") - protected int code; + @Schema(title = "请求的响应码,这里借用HttpStatus作为状态标识,具体代码的含义请参见 HttpStatus( https://developer" + + ".mozilla" + ".org/en-US/docs/Web/HTTP/Status)") + protected Object code; /** * 响应提示信息,一般与响应码的状态对应,对响应结果进行简单地描述 @@ -90,7 +91,7 @@ public class Response implements Serializable { * @param data 响应数据 * @return 响应对象 */ - public static Response of(int code, String msg, T data) { + public static Response of(Object code, String msg, T data) { return new Response<>(code, msg, data); } @@ -111,29 +112,9 @@ public static Response suc(T data) { * @return 表示成功的响应对象(响应码200) */ public static Response suc() { - return new Response(Const.CODE_OK, Const.MSG_OK); + return new Response<>(Const.CODE_OK, Const.MSG_OK); } - /** - * 根据响应提示信息生成一个表示成功的响应对象 - * - * @param msg 响应提示信息 - * @return 表示成功的响应对象(响应码200) - */ - public static Response suc(String msg) { - return new Response(Const.CODE_OK, msg); - } - - /** - * 根据响应提示信息生成一个表示成功的响应对象 - * - * @param 响应数据的类型 - * @param data 响应数据 - * @return 表示成功的响应对象(响应码200) - */ - public static Response sucData(T data) { - return new Response(Const.CODE_OK, Const.MSG_OK, data); - } /** * 根据响应提示信息和响应数据生成一个表示成功的响应对象 @@ -153,7 +134,7 @@ public static Response suc(String msg, T data) { * @return 表示参数有误的响应对象(响应码400) */ public static Response badParam() { - return new Response(Const.CODE_BAD_REQUEST, Const.MSG_BAD_REQUEST); + return new Response<>(Const.CODE_BAD_REQUEST, Const.MSG_BAD_REQUEST); } /** @@ -163,7 +144,7 @@ public static Response badParam() { * @return 表示参数有误的响应对象(响应码400) */ public static Response badParam(String msg) { - return new Response(Const.CODE_BAD_REQUEST, msg); + return new Response<>(Const.CODE_BAD_REQUEST, msg); } /** @@ -184,7 +165,7 @@ public static Response badParam(String msg, T data) { * @return 表示资源未授权的响应对象(401响应码) */ public static Response unAuth() { - return new Response(Const.CODE_UNAUTHORIZED, Const.MSG_UNAUTHORIZED); + return new Response<>(Const.CODE_UNAUTHORIZED, Const.MSG_UNAUTHORIZED); } /** @@ -194,7 +175,7 @@ public static Response unAuth() { * @return 表示资源未授权的响应对象(401响应码) */ public static Response unAuth(String msg) { - return new Response(Const.CODE_UNAUTHORIZED, msg); + return new Response<>(Const.CODE_UNAUTHORIZED, msg); } /** @@ -215,7 +196,7 @@ public static Response unAuth(String msg, T data) { * @return 表示资源不可用的响应对象(403响应码) */ public static Response notAllow() { - return new Response(Const.CODE_FORBIDDEN, Const.MSG_FORBIDDEN); + return new Response<>(Const.CODE_FORBIDDEN, Const.MSG_FORBIDDEN); } /** @@ -225,7 +206,7 @@ public static Response notAllow() { * @return 表示资源不可用的响应对象(403响应码) */ public static Response notAllow(String msg) { - return new Response(Const.CODE_FORBIDDEN, msg); + return new Response<>(Const.CODE_FORBIDDEN, msg); } /** @@ -233,8 +214,8 @@ public static Response notAllow(String msg) { * * @return 表示资源不存在的响应对象(404响应码) */ - public static Response notFoundt() { - return new Response(Const.CODE_NOT_FOUND, Const.MSG_NOT_FOUND); + public static Response notFound() { + return new Response<>(Const.CODE_NOT_FOUND, Const.MSG_NOT_FOUND); } /** @@ -243,18 +224,8 @@ public static Response notFoundt() { * @return 表示请求业务未完成的响应对象(500响应码) */ public static Response error() { - return new Response(Const.CODE_INTERNAL_SERVER_ERROR, Const.MSG_INTERNAL_SERVER_ERROR); - } - - /** - * 生成一个默认表示请求业务未完成的响应对象(500响应码) - * - * @param 响应数据的类型 - * @param data 响应数据 - * @return 表示请求业务未完成的响应对象(500响应码) - */ - public static Response errorData(T data) { - return new Response(Const.CODE_INTERNAL_SERVER_ERROR, Const.MSG_INTERNAL_SERVER_ERROR, data); + return new Response<>(Const.CODE_INTERNAL_SERVER_ERROR, + Const.MSG_INTERNAL_SERVER_ERROR); } /** @@ -264,18 +235,7 @@ public static Response errorData(T data) { * @return 表示服务器内部异常500时的返回信息 */ public static Response error(String msg) { - return new Response(Const.CODE_INTERNAL_SERVER_ERROR, msg); - } - - /** - * 根据响应数据生成表示服务器内部异常500时的返回信息 - * - * @param 响应数据的数据类型 - * @param data 响应数据 - * @return 表示服务器内部异常500时的返回信息 - */ - public static Response error(T data) { - return new Response<>(Const.CODE_INTERNAL_SERVER_ERROR, Const.MSG_INTERNAL_SERVER_ERROR, data); + return new Response<>(Const.CODE_INTERNAL_SERVER_ERROR, msg); } /** @@ -306,7 +266,7 @@ public Response() { * @param data 响应数据 * @param date 响应时间 */ - public Response(String id, int code, String msg, T data, Date date) { + public Response(String id, Object code, String msg, T data, Date date) { this.id = id; this.code = code; this.msg = msg; @@ -321,7 +281,7 @@ public Response(String id, int code, String msg, T data, Date date) { * @param msg 响应提示信息 * @param data 响应数据 */ - public Response(int code, String msg, T data) { + public Response(Object code, String msg, T data) { this(IdWorker.uuid(), code, msg, data, new Date()); } @@ -331,7 +291,7 @@ public Response(int code, String msg, T data) { * @param code 响应码 * @param msg 响应提示信息 */ - public Response(int code, String msg) { + public Response(Object code, String msg) { this(IdWorker.uuid(), code, msg, null, new Date()); } @@ -360,8 +320,8 @@ public Response setId(String id) { * * @return 当前响应的响应码 */ - public int getCode() { - return code; + public Object getCode() { + return this.code; } /** @@ -370,7 +330,7 @@ public int getCode() { * @param code 响应码 * @return 当前通用响应对象 */ - public Response setCode(int code) { + public Response setCode(Object code) { this.code = code; return this; } @@ -381,7 +341,7 @@ public Response setCode(int code) { * @return 响应提示信息 */ public String getMsg() { - return msg; + return this.msg; } /** @@ -412,7 +372,7 @@ public Response msg(String msg) { * @return 响应数据 */ public T getData() { - return data; + return this.data; } /** @@ -443,7 +403,7 @@ public Response data(T data) { * @return 响应的时间 */ public Date getDate() { - return date; + return this.date; } /** @@ -508,7 +468,7 @@ public static class Const { /** * 500响应码对应的默认信息 */ - public final static String MSG_INTERNAL_SERVER_ERROR = "请求失败"; + public final static String MSG_INTERNAL_SERVER_ERROR = "请求处理失败"; /** * 500响应码 */ @@ -520,7 +480,7 @@ public static class Const { public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + code; + result = prime * result + ((code == null) ? 0 : code.hashCode()); result = prime * result + ((data == null) ? 0 : data.hashCode()); result = prime * result + ((date == null) ? 0 : date.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); @@ -577,7 +537,8 @@ public boolean equals(Object obj) { @Override public String toString() { - return "Response [id=" + id + ", code=" + code + ", msg=" + msg + ", data=" + data + ", date=" + date + "]"; + return "Response [id=" + id + ", code=" + code + ", msg=" + msg + ", data=" + data + ", " + + "date=" + date + "]"; } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/entity/RootEnum.java b/src/main/java/com/yishuifengxiao/common/tool/entity/RootEnum.java new file mode 100644 index 0000000..b0f15a4 --- /dev/null +++ b/src/main/java/com/yishuifengxiao/common/tool/entity/RootEnum.java @@ -0,0 +1,62 @@ +package com.yishuifengxiao.common.tool.entity; + +import java.io.Serializable; +import java.util.Objects; + +/** + * 自定义枚举类接口 + * + * @author qingteng + * @version 1.0.0 + * @since 1.0.0 + */ +public interface RootEnum extends Serializable { + + /** + * 枚举值的编码 + * + * @return 枚举值的编码 + */ + default Object code() { + return null; + } + + + /** + * 枚举值的名称 + * + * @return 枚举值的名称 + */ + default String enumName() { + return null; + } + + + /** + * 枚举值的描述 + * + * @return 枚举值的描述 + */ + default String description() { + return null; + } + + /** + * 判断给定的编码与枚举值的编码是否一致 + * + * @param code 待判断的给定的编码,若为null则直接返回为false + * @return 给定的编码与枚举值的编码一致返回为true, 否则为false + */ + default boolean equalCode(Object code) { + if (null == code) { + return false; + } + Object val = this.code(); + if (null == val) { + return false; + } + return Objects.equals(val, code); + } + + +} diff --git a/src/main/java/com/yishuifengxiao/common/tool/entity/StringKeyValue.java b/src/main/java/com/yishuifengxiao/common/tool/entity/StringKeyValue.java index ffd63f8..a8eb32d 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/entity/StringKeyValue.java +++ b/src/main/java/com/yishuifengxiao/common/tool/entity/StringKeyValue.java @@ -1,5 +1,6 @@ package com.yishuifengxiao.common.tool.entity; +import com.fasterxml.jackson.annotation.JsonIgnore; import org.apache.commons.lang3.StringUtils; import java.io.Serializable; @@ -58,6 +59,7 @@ public StringKeyValue setKey(String key) { * * @return 若该实例的键为null或空字符串即返回为true, 否则为false */ + @JsonIgnore public boolean blankKey() { return StringUtils.isBlank(getKey()); } @@ -67,6 +69,7 @@ public boolean blankKey() { * * @return 该实例的键是否不为null或空字符串且值不为null即返回为true, 否则为false */ + @JsonIgnore public boolean notBlank() { return !blankKey() && !nullVal(); } @@ -76,6 +79,7 @@ public boolean notBlank() { * * @return 该实例的键是否为null或空字符串且值为null即返回为true, 否则为false */ + @JsonIgnore public boolean isBlank() { return blankKey() && nullVal(); } diff --git a/src/main/java/com/yishuifengxiao/common/tool/exception/CustomException.java b/src/main/java/com/yishuifengxiao/common/tool/exception/CustomException.java index 73fb1c1..39b847a 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/exception/CustomException.java +++ b/src/main/java/com/yishuifengxiao/common/tool/exception/CustomException.java @@ -3,6 +3,8 @@ */ package com.yishuifengxiao.common.tool.exception; +import com.yishuifengxiao.common.tool.entity.RootEnum; + /** *

    * 自定义异常基类 @@ -23,7 +25,7 @@ public class CustomException extends Exception { /** * 错误码 */ - protected Integer code; + protected Object code; /** * 携带的附加信息 @@ -52,6 +54,19 @@ public CustomException(String message) { } + /** + * Constructs a new runtime exception with the specified detail message. The + * cause is not initialized, and may subsequently be initialized by a call to + * {@link #initCause}. + * + * @param rootEnum 异常信息 + */ + public CustomException(RootEnum rootEnum) { + super(null == rootEnum ? null : rootEnum.description()); + this.code = null == rootEnum ? null : rootEnum.code(); + } + + /** * Constructs a new runtime exception with the specified detail message. The * cause is not initialized, and may subsequently be initialized by a call to @@ -123,7 +138,7 @@ public CustomException(String message, Throwable cause, boolean enableSuppressio * * @return 错误码 */ - public Integer getCode() { + public Object getCode() { return code; } @@ -133,7 +148,7 @@ public Integer getCode() { * @param code 错误码 * @return 当前对象 */ - public CustomException setCode(Integer code) { + public CustomException setCode(Object code) { this.code = code; return this; } diff --git a/src/main/java/com/yishuifengxiao/common/tool/exception/UncheckedException.java b/src/main/java/com/yishuifengxiao/common/tool/exception/UncheckedException.java index a9e3149..333dc8a 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/exception/UncheckedException.java +++ b/src/main/java/com/yishuifengxiao/common/tool/exception/UncheckedException.java @@ -3,6 +3,8 @@ */ package com.yishuifengxiao.common.tool.exception; +import com.yishuifengxiao.common.tool.entity.RootEnum; + /** *

    * 自定义运行时异常基类 @@ -22,7 +24,7 @@ public class UncheckedException extends RuntimeException { /** * 错误码 */ - protected Integer code; + protected Object code; /** * 携带的附加信息 @@ -48,11 +50,23 @@ public UncheckedException() { * retrieval by the {@link #getMessage()} method. */ - public UncheckedException(Integer code, String message) { + public UncheckedException(Object code, String message) { super(message); this.code = code; } + /** + * Constructs a new runtime exception with the specified detail message. The + * cause is not initialized, and may subsequently be initialized by a call to + * {@link #initCause}. + * + * @param rootEnum 异常信息 + */ + public UncheckedException(RootEnum rootEnum) { + super(null == rootEnum ? null : rootEnum.description()); + this.code = null == rootEnum ? null : rootEnum.code(); + } + /** * Constructs a new runtime exception with the specified detail message, cause, * suppression enabled or disabled, and writable stack trace enabled or @@ -134,7 +148,7 @@ public UncheckedException setCode(Integer code) { * * @return 错误码 */ - public Integer getCode() { + public Object getCode() { return this.code; } diff --git a/src/main/java/com/yishuifengxiao/common/tool/http/HttpClient.java b/src/main/java/com/yishuifengxiao/common/tool/http/HttpClient.java index f7fbc8c..3abb74f 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/http/HttpClient.java +++ b/src/main/java/com/yishuifengxiao/common/tool/http/HttpClient.java @@ -26,592 +26,601 @@ @Slf4j public class HttpClient { - /** - * Content-Type:application/x-www-form-urlencoded; charset=UTF-8 - */ - public final static String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded; charset=UTF-8"; - - /** - * Content-Type:application/json;charset=UTF-8 - */ - public final static String CONTENT_TYPE_JSON = "application/json;charset=UTF-8"; - - /** - * User-Agent - */ - private String userAgent; - - /** - * 是否使用自动User-Agent - */ - private boolean autoUserAgent = true; - - /** - * Content-Type - */ - private String contentType; - - /** - * Referer - */ - private String referrer; - - /** - * 请求头 - */ - private Map headers = new HashMap<>(); - - /** - * 超时连接或读取之前的毫秒数(千分之一秒) - */ - private Integer timeout; - - /** - * 请求的url - */ - private String url; - - /** - * 请求的类型 - */ - private String method; - /** - * cookies - */ - private Map cookies; - /** - * 请求体键值对与 requestBody 互斥 - */ - private Map data; - - /** - * 请求体,与data 互斥 - */ - private String requestBody; - - /** - * 获取http客户端(单例模式) - * - * @return http客户端 - */ - public final static HttpClient instance() { - return new HttpClient(); - } - - /** - * 设置 userAgent - * - * @param userAgent 设置 userAgent - * @return 当前对象实例 - */ - public HttpClient userAgent(String userAgent) { - this.userAgent = userAgent; - return this; - } - - /** - * 设置 contentType - * - * @param contentType contentType - * @return 当前对象实例 - */ - public HttpClient contentType(String contentType) { - this.contentType = contentType; - return this; - } - - /** - * 设置 referrer - * - * @param referrer referrer - * @return 当前对象实例 - */ - public HttpClient referrer(String referrer) { - this.referrer = referrer; - return this; - } - - /** - * 增加一个请求头 - * - * @param name 请求头名字 - * @param value 请求头的值 - * @return 当前对象实例 - */ - public HttpClient addHeader(String name, Object value) { - if (StringUtils.isNoneBlank(name) && null != value) { - this.headers.put(name, value.toString()); - } - return this; - } - - /** - * 批量增加请求头 - * - * @param headers 请求头 - * @return 当前对象实例 - */ - public HttpClient addHeaders(Map headers) { - if (null == headers) { - return this; - } - headers.forEach((name, value) -> this.addHeader(name, value)); - return this; - } - - /** - * 设置请求头 - * - * @param headers 请求头 - * @return 当前对象实例 - */ - public HttpClient setHeaders(Map headers) { - if (null == headers) { - this.headers = new HashMap<>(0); - return this; - } - this.headers.clear(); - this.addHeaders(headers); - return this; - } - - /** - *

    - * 设置超时时间 - *

    - *

    - * 设置总请求超时持续时间。如果发生超时,将抛出java.net.SocketTimeoutException。 - * 默认超时为30秒(30000毫秒)。超时为零被视为无限超时。 请注意,此超时指定了连接时间和读取完整响应时间的组合最大持续时间。 - *

    - * - * @param timeout 连接或读取超时前的毫秒数(千分之一秒)。 - * @return 当前对象实例 - */ - public HttpClient timeout(Integer timeout) { - this.timeout = timeout; - return this; - } - - /** - * 设置请求的url - * - * @param url 请求的url - * @return 当前对象实例 - */ - public HttpClient url(String url) { - this.url = url; - return this; - } - - /** - *

    - * 设置请求的类型 - *

    - *

    - * 默认为get请求 - *

    - * - * @param method 请求的类型 - * @return 当前对象实例 - */ - public HttpClient method(String method) { - this.method = method; - return this; - } - - /** - * 设置是否使用是否使用自动User-Agent - * - * @param autoUserAgent 是否使用是否使用自动User-Agent,默认值为true - * @return 当前对象实例 - */ - public HttpClient autoUserAgent(boolean autoUserAgent) { - this.autoUserAgent = autoUserAgent; - return this; - } - - /** - * 设置请求体 - * - * @param requestBody 请求体(与data 互斥) - * @return 当前对象实例 - */ - public HttpClient data(String requestBody) { - this.requestBody = requestBody; - return this.post(); - } - - /** - * 设置请求体键值对 - * - * @param data 请求体键值对与 requestBody 互斥 - * @return 当前对象实例 - */ - public HttpClient data(Map data) { - this.data = data; - return this.post(); - } - - /** - * 使用 application/x-www-form-urlencoded; charset=UTF-8 方式发送请求 - * - * @return 当前对象实例 - */ - public HttpClient form() { - this.contentType = CONTENT_TYPE_FORM; - return this; - } - - /** - * 使用 application/json;charset=UTF-8 方式发送请求 - * - * @return 当前对象实例 - */ - public HttpClient json() { - this.contentType = CONTENT_TYPE_JSON; - return this; - } - - /** - * 设置 cookies - * - * @param cookies cookies - * @return 当前对象实例 - */ - public HttpClient cookies(Map cookies) { - this.cookies = cookies; - return this; - } - - /** - * 设置为post请求 - * - * @return 当前对象实例 - */ - public HttpClient post() { - this.method = "post"; - return this; - } - - /** - * 设置为get请求 - * - * @return 当前对象实例 - */ - public HttpClient get() { - this.method = "get"; - return this; - } - - /** - * 设置为put请求 - * - * @return 当前对象实例 - */ - public HttpClient put() { - this.method = "put"; - return this; - } - - /** - * 设置为delete请求 - * - * @return 当前对象实例 - */ - public HttpClient delete() { - this.method = "delete"; - return this; - } - - /** - * 根据url发送get请求,并把响应体转换成文本 - * - * @param url url - * @return 请求的响应体文本 - */ - public static String get(String url) { - HttpClient client = new HttpClient(); - client.url = url; - client.method = "get"; - return client.executeAsString(); - } - - /** - * 使用 application/x-www-form-urlencoded; charset=UTF-8 方式发送POST请求,并把响应体转换成文本 - * - * @param url url - * @param headers 请求头 - * @param data 请求数据 - * @return 请求的响应体的响应文本 - */ - public static String postForm(String url, Map headers, Map data) { - return executeAsString(url, "post", CONTENT_TYPE_FORM, headers, data); - } - - /** - * 使用 application/x-www-form-urlencoded; charset=UTF-8 方式发送POST请求,并把响应体转换成文本 - * - * @param url url - * @param data 请求数据 - * @return 请求的响应体的响应文本 - */ - public static String postForm(String url, Map data) { - - return executeAsString(url, "post", CONTENT_TYPE_FORM, null, data); - } - - /** - * 使用 application/json;charset=UTF-8 方式发送POST请求,并把响应体转换成文本 - * - * @param url url - * @param headers 请求头 - * @param data 请求数据 - * @return 请求的响应体的响应文本 - */ - public static String postJson(String url, Map headers, Map data) { - return executeAsString(url, "post", CONTENT_TYPE_JSON, headers, data); - } - - /** - * 使用 application/json;charset=UTF-8 方式发送POST请求,并把响应体转换成文本 - * - * @param url url - * @param data 请求数据 - * @return 请求的响应体的响应文本 - */ - public static String postJson(String url, Map data) { - return executeAsString(url, "post", CONTENT_TYPE_JSON, null, data); - } - - /** - * 使用 application/json;charset=UTF-8 方式发送POST请求,并把响应体转换成文本 - * - * @param url url - * @param requestBody 请求体 - * @return 请求的响应体的响应文本 - */ - public static String postJson(String url, String requestBody) { - return executeAsString(url, "post", CONTENT_TYPE_JSON, null, requestBody); - } - - /** - * 发送POST请求并将响应体转换为文本 - * - * @param url url - * @param method 请求方法 - * @param contentType Content-Type - * @param headers 请求头 - * @param data 请求体 - * @return 响应 - */ - public static Connection.Response execute(String url, String method, String contentType, - Map headers, Map data) { - HttpClient client = new HttpClient(); - client.url(url); - client.method(method); - client.contentType(contentType); - client.setHeaders(headers); - client.data(data); - return client.execute(); - } - - /** - * 发送POST请求并将响应体转换为文本 - * - * @param url url - * @param method 请求方法 - * @param contentType Content-Type - * @param headers 请求头 - * @param data 请求体 - * @return 响应 - */ - public static String executeAsString(String url, String method, String contentType, Map headers, - Map data) { - Connection.Response response = execute(url, method, contentType, headers, data); - return null == response ? null : response.body(); - } - - /** - * 发送POST请求并将响应体转换为文本 - * - * @param url url - * @param method 请求方法 - * @param contentType Content-Type - * @param headers 请求头 - * @param requestBody 请求体 - * @return 响应 - */ - public static Connection.Response execute(String url, String method, String contentType, - Map headers, String requestBody) { - HttpClient client = new HttpClient(); - client.url(url); - client.method(method); - client.contentType(contentType); - client.setHeaders(headers); - client.data(requestBody); - return client.execute(); - } - - /** - * 发送POST请求并将响应体转换为文本 - * - * @param url url - * @param method 请求方法 - * @param contentType Content-Type - * @param headers 请求头 - * @param requestBody 请求体 - * @return 响应 - */ - public static String executeAsString(String url, String method, String contentType, Map headers, - String requestBody) { - Connection.Response response = execute(url, method, contentType, headers, requestBody); - return null == response ? null : response.body(); - } - - /** - * 执行请求 - * - * @return 响应 - */ - public Connection.Response execute() { - Connection.Response response = null; - try { - response = this.connection().execute(); - } catch (Throwable e) { - log.warn("======> 请求 {} 时出现问题 {}", this.url, e); - } - return response; - } - - /** - * 执行请求,并将响应体转换为文本 - * - * @return 文本响应体 - */ - public String executeAsString() { - Connection.Response response = this.execute(); - return null == response ? null : response.body(); - } - - /** - * 建立一个连接 - * - * @return 连接 - */ - private Connection connection() { - Assert.isNotBlank(this.url, "请求的url不能为空"); - String userAgent = this.userAgent; - if (this.autoUserAgent && StringUtils.isBlank(this.userAgent)) { - userAgent = UserAgent.USER_AGENT_EDGE_VERSION_110_0; - } - String url = this.url.trim(); - // 如果是Https请求 - if (url.startsWith("https")) { - getTrust(); - } - Connection connection = Jsoup.connect(url); - connection.method(getMethod(this.method)); - if (null != this.timeout) { - if (this.timeout < 0) { - connection.timeout(0); - } else { - connection.timeout(this.timeout); - } - - } - connection.ignoreHttpErrors(true); - connection.ignoreContentType(true); - connection.followRedirects(true); - connection.maxBodySize(0); - // 浏览器标识 - - if (StringUtils.isNotBlank(userAgent)) { - connection.userAgent(userAgent); - } - // 用于指明当前流量的来源参考页面 - if (StringUtils.isNotBlank(this.referrer)) { - connection.referrer(this.referrer); - } - Map headers = null == this.headers ? new HashMap<>(0) : this.headers; - if (StringUtils.isNotBlank(this.contentType)) { - headers.put("Content-Type", this.contentType); - } - headers.forEach((k, v) -> connection.header(k, null == v ? null : v)); - if (null != this.cookies && !this.cookies.isEmpty()) { - this.cookies.forEach((k, v) -> { - if (null != k && null != v) { - connection.cookie(k, v); - } - }); - - } - if (StringUtils.isNotBlank(this.requestBody)) { - connection.requestBody(this.requestBody.trim()); - } else { - if (null != this.data) { - this.data.forEach((k, v) -> { - if (null != k && null != v) { - connection.data(k, v); - } - }); - } - } - - return connection; - } - - /** - * 获取请求类型 - * - * @param method 请求方式 - * @return 请求方式 - */ - private static Connection.Method getMethod(String method) { - if (StringUtils.isBlank(method)) { - return Connection.Method.GET; - } - Connection.Method connMethod = Connection.Method.GET; - switch (method.trim().toLowerCase()) { - case "post": - connMethod = Connection.Method.POST; - break; - case "delete": - connMethod = Connection.Method.DELETE; - break; - case "put": - connMethod = Connection.Method.PUT; - break; - default: - break; - } - return connMethod; - } - - /** - * 获取服务器信任 - */ - private static void getTrust() { - try { - HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true); - SSLContext context = SSLContext.getInstance("TLS"); - context.init(null, new X509TrustManager[] { new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, String authType) { - } - - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) { - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - } }, new SecureRandom()); - HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory()); - - } catch (Exception e) { - e.printStackTrace(); - } - } + /** + * Content-Type:application/x-www-form-urlencoded; charset=UTF-8 + */ + public final static String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded; " + + "charset=UTF-8"; + + /** + * Content-Type:application/json;charset=UTF-8 + */ + public final static String CONTENT_TYPE_JSON = "application/json;charset=UTF-8"; + + /** + * User-Agent + */ + private String userAgent; + + /** + * 是否使用自动User-Agent + */ + private boolean autoUserAgent = true; + + /** + * Content-Type + */ + private String contentType; + + /** + * Referer + */ + private String referrer; + + /** + * 请求头 + */ + private Map headers = new HashMap<>(); + + /** + * 超时连接或读取之前的毫秒数(千分之一秒) + */ + private Integer timeout; + + /** + * 请求的url + */ + private String url; + + /** + * 请求的类型 + */ + private String method; + /** + * cookies + */ + private Map cookies; + /** + * 请求体键值对与 requestBody 互斥 + */ + private Map data; + + /** + * 请求体,与data 互斥 + */ + private String requestBody; + + /** + * 获取http客户端(单例模式) + * + * @return http客户端 + */ + public final static HttpClient instance() { + return new HttpClient(); + } + + /** + * 设置 userAgent + * + * @param userAgent 设置 userAgent + * @return 当前对象实例 + */ + public HttpClient userAgent(String userAgent) { + this.userAgent = userAgent; + return this; + } + + /** + * 设置 contentType + * + * @param contentType contentType + * @return 当前对象实例 + */ + public HttpClient contentType(String contentType) { + this.contentType = contentType; + return this; + } + + /** + * 设置 referrer + * + * @param referrer referrer + * @return 当前对象实例 + */ + public HttpClient referrer(String referrer) { + this.referrer = referrer; + return this; + } + + /** + * 增加一个请求头 + * + * @param name 请求头名字 + * @param value 请求头的值 + * @return 当前对象实例 + */ + public HttpClient addHeader(String name, Object value) { + if (StringUtils.isNoneBlank(name) && null != value) { + this.headers.put(name, value.toString()); + } + return this; + } + + /** + * 批量增加请求头 + * + * @param headers 请求头 + * @return 当前对象实例 + */ + public HttpClient addHeaders(Map headers) { + if (null == headers) { + return this; + } + headers.forEach((name, value) -> this.addHeader(name, value)); + return this; + } + + /** + * 设置请求头 + * + * @param headers 请求头 + * @return 当前对象实例 + */ + public HttpClient setHeaders(Map headers) { + if (null == headers) { + this.headers = new HashMap<>(0); + return this; + } + this.headers.clear(); + this.addHeaders(headers); + return this; + } + + /** + *

    + * 设置超时时间 + *

    + *

    + * 设置总请求超时持续时间。如果发生超时,将抛出java.net.SocketTimeoutException。 + * 默认超时为30秒(30000毫秒)。超时为零被视为无限超时。 请注意,此超时指定了连接时间和读取完整响应时间的组合最大持续时间。 + *

    + * + * @param timeout 连接或读取超时前的毫秒数(千分之一秒)。 + * @return 当前对象实例 + */ + public HttpClient timeout(Integer timeout) { + this.timeout = timeout; + return this; + } + + /** + * 设置请求的url + * + * @param url 请求的url + * @return 当前对象实例 + */ + public HttpClient url(String url) { + this.url = url; + return this; + } + + /** + *

    + * 设置请求的类型 + *

    + *

    + * 默认为get请求 + *

    + * + * @param method 请求的类型 + * @return 当前对象实例 + */ + public HttpClient method(String method) { + this.method = method; + return this; + } + + /** + * 设置是否使用是否使用自动User-Agent + * + * @param autoUserAgent 是否使用是否使用自动User-Agent,默认值为true + * @return 当前对象实例 + */ + public HttpClient autoUserAgent(boolean autoUserAgent) { + this.autoUserAgent = autoUserAgent; + return this; + } + + /** + * 设置请求体 + * + * @param requestBody 请求体(与data 互斥) + * @return 当前对象实例 + */ + public HttpClient data(String requestBody) { + this.requestBody = requestBody; + return this.post(); + } + + /** + * 设置请求体键值对 + * + * @param data 请求体键值对与 requestBody 互斥 + * @return 当前对象实例 + */ + public HttpClient data(Map data) { + this.data = data; + return this.post(); + } + + /** + * 使用 application/x-www-form-urlencoded; charset=UTF-8 方式发送请求 + * + * @return 当前对象实例 + */ + public HttpClient form() { + this.contentType = CONTENT_TYPE_FORM; + return this; + } + + /** + * 使用 application/json;charset=UTF-8 方式发送请求 + * + * @return 当前对象实例 + */ + public HttpClient json() { + this.contentType = CONTENT_TYPE_JSON; + return this; + } + + /** + * 设置 cookies + * + * @param cookies cookies + * @return 当前对象实例 + */ + public HttpClient cookies(Map cookies) { + this.cookies = cookies; + return this; + } + + /** + * 设置为post请求 + * + * @return 当前对象实例 + */ + public HttpClient post() { + this.method = "post"; + return this; + } + + /** + * 设置为get请求 + * + * @return 当前对象实例 + */ + public HttpClient get() { + this.method = "get"; + return this; + } + + /** + * 设置为put请求 + * + * @return 当前对象实例 + */ + public HttpClient put() { + this.method = "put"; + return this; + } + + /** + * 设置为delete请求 + * + * @return 当前对象实例 + */ + public HttpClient delete() { + this.method = "delete"; + return this; + } + + /** + * 根据url发送get请求,并把响应体转换成文本 + * + * @param url url + * @return 请求的响应体文本 + */ + public static String get(String url) { + HttpClient client = new HttpClient(); + client.url = url; + client.method = "get"; + return client.executeAsString(); + } + + /** + * 使用 application/x-www-form-urlencoded; charset=UTF-8 方式发送POST请求,并把响应体转换成文本 + * + * @param url url + * @param headers 请求头 + * @param data 请求数据 + * @return 请求的响应体的响应文本 + */ + public static String postForm(String url, Map headers, + Map data) { + return executeAsString(url, "post", CONTENT_TYPE_FORM, headers, data); + } + + /** + * 使用 application/x-www-form-urlencoded; charset=UTF-8 方式发送POST请求,并把响应体转换成文本 + * + * @param url url + * @param data 请求数据 + * @return 请求的响应体的响应文本 + */ + public static String postForm(String url, Map data) { + + return executeAsString(url, "post", CONTENT_TYPE_FORM, null, data); + } + + /** + * 使用 application/json;charset=UTF-8 方式发送POST请求,并把响应体转换成文本 + * + * @param url url + * @param headers 请求头 + * @param data 请求数据 + * @return 请求的响应体的响应文本 + */ + public static String postJson(String url, Map headers, + Map data) { + return executeAsString(url, "post", CONTENT_TYPE_JSON, headers, data); + } + + /** + * 使用 application/json;charset=UTF-8 方式发送POST请求,并把响应体转换成文本 + * + * @param url url + * @param data 请求数据 + * @return 请求的响应体的响应文本 + */ + public static String postJson(String url, Map data) { + return executeAsString(url, "post", CONTENT_TYPE_JSON, null, data); + } + + /** + * 使用 application/json;charset=UTF-8 方式发送POST请求,并把响应体转换成文本 + * + * @param url url + * @param requestBody 请求体 + * @return 请求的响应体的响应文本 + */ + public static String postJson(String url, String requestBody) { + return executeAsString(url, "post", CONTENT_TYPE_JSON, null, requestBody); + } + + /** + * 发送POST请求并将响应体转换为文本 + * + * @param url url + * @param method 请求方法 + * @param contentType Content-Type + * @param headers 请求头 + * @param data 请求体 + * @return 响应 + */ + public static Connection.Response execute(String url, String method, String contentType, + Map headers, + Map data) { + HttpClient client = new HttpClient(); + client.url(url); + client.method(method); + client.contentType(contentType); + client.setHeaders(headers); + client.data(data); + return client.execute(); + } + + /** + * 发送POST请求并将响应体转换为文本 + * + * @param url url + * @param method 请求方法 + * @param contentType Content-Type + * @param headers 请求头 + * @param data 请求体 + * @return 响应 + */ + public static String executeAsString(String url, String method, String contentType, + Map headers, + Map data) { + Connection.Response response = execute(url, method, contentType, headers, data); + return null == response ? null : response.body(); + } + + /** + * 发送POST请求并将响应体转换为文本 + * + * @param url url + * @param method 请求方法 + * @param contentType Content-Type + * @param headers 请求头 + * @param requestBody 请求体 + * @return 响应 + */ + public static Connection.Response execute(String url, String method, String contentType, + Map headers, String requestBody) { + HttpClient client = new HttpClient(); + client.url(url); + client.method(method); + client.contentType(contentType); + client.setHeaders(headers); + client.data(requestBody); + return client.execute(); + } + + /** + * 发送POST请求并将响应体转换为文本 + * + * @param url url + * @param method 请求方法 + * @param contentType Content-Type + * @param headers 请求头 + * @param requestBody 请求体 + * @return 响应 + */ + public static String executeAsString(String url, String method, String contentType, + Map headers, + String requestBody) { + Connection.Response response = execute(url, method, contentType, headers, requestBody); + return null == response ? null : response.body(); + } + + /** + * 执行请求 + * + * @return 响应 + */ + public Connection.Response execute() { + Connection.Response response = null; + try { + response = this.connection().execute(); + } catch (Throwable e) { + if (log.isInfoEnabled()) { + log.info("There was a problem requesting {}, the problem is {}", this.url, e); + } + + } + return response; + } + + /** + * 执行请求,并将响应体转换为文本 + * + * @return 文本响应体 + */ + public String executeAsString() { + Connection.Response response = this.execute(); + return null == response ? null : response.body(); + } + + /** + * 建立一个连接 + * + * @return 连接 + */ + private Connection connection() { + Assert.isNotBlank(this.url, "The requested URL cannot be empty"); + String userAgent = this.userAgent; + if (this.autoUserAgent && StringUtils.isBlank(this.userAgent)) { + userAgent = UserAgent.USER_AGENT_EDGE_VERSION_110_0.description(); + } + String url = this.url.trim(); + // 如果是Https请求 + if (url.startsWith("https")) { + getTrust(); + } + Connection connection = Jsoup.connect(url); + connection.method(getMethod(this.method)); + if (null != this.timeout) { + if (this.timeout < 0) { + connection.timeout(0); + } else { + connection.timeout(this.timeout); + } + + } + connection.ignoreHttpErrors(true); + connection.ignoreContentType(true); + connection.followRedirects(true); + connection.maxBodySize(0); + // 浏览器标识 + + if (StringUtils.isNotBlank(userAgent)) { + connection.userAgent(userAgent); + } + // 用于指明当前流量的来源参考页面 + if (StringUtils.isNotBlank(this.referrer)) { + connection.referrer(this.referrer); + } + Map headers = null == this.headers ? new HashMap<>(0) : this.headers; + if (StringUtils.isNotBlank(this.contentType)) { + headers.put("Content-Type", this.contentType); + } + headers.forEach((k, v) -> connection.header(k, null == v ? null : v)); + if (null != this.cookies && !this.cookies.isEmpty()) { + this.cookies.forEach((k, v) -> { + if (null != k && null != v) { + connection.cookie(k, v); + } + }); + + } + if (StringUtils.isNotBlank(this.requestBody)) { + connection.requestBody(this.requestBody.trim()); + } else { + if (null != this.data) { + this.data.forEach((k, v) -> { + if (null != k && null != v) { + connection.data(k, v); + } + }); + } + } + + return connection; + } + + /** + * 获取请求类型 + * + * @param method 请求方式 + * @return 请求方式 + */ + private static Connection.Method getMethod(String method) { + if (StringUtils.isBlank(method)) { + return Connection.Method.GET; + } + Connection.Method connMethod = Connection.Method.GET; + switch (method.trim().toLowerCase()) { + case "post": + connMethod = Connection.Method.POST; + break; + case "delete": + connMethod = Connection.Method.DELETE; + break; + case "put": + connMethod = Connection.Method.PUT; + break; + default: + break; + } + return connMethod; + } + + /** + * 获取服务器信任 + */ + private static void getTrust() { + try { + HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true); + SSLContext context = SSLContext.getInstance("TLS"); + context.init(null, new X509TrustManager[]{new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }}, new SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory()); + + } catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/http/UserAgent.java b/src/main/java/com/yishuifengxiao/common/tool/http/UserAgent.java index f308167..a6d4377 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/http/UserAgent.java +++ b/src/main/java/com/yishuifengxiao/common/tool/http/UserAgent.java @@ -3,6 +3,7 @@ */ package com.yishuifengxiao.common.tool.http; +import com.yishuifengxiao.common.tool.entity.RootEnum; import org.apache.commons.lang3.RandomUtils; /** @@ -12,132 +13,172 @@ * @version 1.0.0 * @since 1.0.0 */ -public final class UserAgent { +public enum UserAgent implements RootEnum { /** * EDGE浏览器标识,默认为 110.0 */ - public final static String USER_AGENT_EDGE_VERSION_110_0 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.41"; + USER_AGENT_EDGE_VERSION_110_0(1, "EDGE110.0", "Mozilla/5.0 (Windows NT 10.0; " + "Win64; x64)" + + " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 " + "Edg" + + "/110.0.1587.41"), /** * 谷歌浏览器标识,默认为 谷歌78.0 */ - public final static String USER_AGENT_GOOGLE_VERSION_78_0 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"; + USER_AGENT_GOOGLE_VERSION_78_0(2, "谷歌78.0", "Mozilla/5.0 (Windows NT 10.0; " + "Win64; x64) " + + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537" + ".36"), /** * 谷歌浏览器标识,默认为 谷歌75.0 */ - public final static String USER_AGENT_GOOGLE_VERSION_75_0 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"; + USER_AGENT_GOOGLE_VERSION_75_0(3, "谷歌75.0", "Mozilla/5.0 (Windows NT 10.0; " + "Win64; x64) " + + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537" + ".36"), /** * 火狐浏览器标识,windows 火狐70.0.1 */ - public final static String USER_AGENT_FIREFOX_VERSION_70_0 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0"; + USER_AGENT_FIREFOX_VERSION_70_0(4, "火狐70.0.1", "Mozilla/5.0 (Windows NT 10.0; " + "Win64; " + + "x64; rv:70.0) Gecko/20100101 Firefox/70.0"), /** * 火狐浏览器标识,windows 火狐97.0 */ - public final static String USER_AGENT_FIREFOX_VERSION_97_0 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0"; + USER_AGENT_FIREFOX_VERSION_97_0(5, "windows 火狐97.0", "Mozilla/5.0 (Windows NT 10.0; " + + "Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0"), /** * 火狐浏览器标识,Mac版 火狐 */ - public final static String USER_AGENT_FIREFOX_VERSION_MAC = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"; + USER_AGENT_FIREFOX_VERSION_MAC(6, "Mac版 火狐", "Mozilla/5.0 (Macintosh; Intel " + "Mac OS X 10" + + ".6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"), /** * IE浏览器标识, IE 11.476 */ - public final static String USER_AGENT_IE_VERSION_11_476 = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"; + USER_AGENT_IE_VERSION_11_476(7, "IE 11.476", "Mozilla/5.0 (Windows NT 10.0; " + "WOW64; " + + "Trident/7.0; rv:11.0) like Gecko"), /** * IE浏览器标识, IE 9.0 */ - public final static String USER_AGENT_IE_VERSION_9_0 = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0"; + USER_AGENT_IE_VERSION_9_0(8, "IE 9.0", "Mozilla/5.0 (compatible; MSIE 9.0; " + "Windows NT 6" + + ".1; Trident/5.0"), /** * EDAG浏览器 */ - public final static String USER_AGENT_EDAG_VERSION_11_476 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362"; + USER_AGENT_EDAG_VERSION_11_476(9, "EDAG 70.0", "Mozilla/5.0 (Windows NT 10.0; " + "Win64; " + + "x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537" + + ".36 Edge/18.18362"), /** * EDAG浏览器 99.0.1150.25 */ - public final static String USER_AGENT_EDAG_VERSION_99 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.45 Safari/537.36 Edg/99.0.1150.25"; + USER_AGENT_EDAG_VERSION_99(10, "EDAG 99.0.1150.25", "Mozilla/5.0 (Windows NT 10.0; Win64;" + + " x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.45 Safari/537.36 " + + "Edg/99.0.1150.25"), /** * safari 浏览器 Mac 版 */ - public final static String USER_AGENT_SAFARI_VERSION_MAC = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50"; + USER_AGENT_SAFARI_VERSION_MAC(11, "safari 浏览器 Mac 版", "Mozilla/5.0 (Macintosh; U; Intel " + + "Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 " + + "Safari/534.50"), /** * safari 浏览器 Windows 版 */ - public final static String USER_AGENT_SAFARI_VERSION_WINDOWS = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50"; + USER_AGENT_SAFARI_VERSION_WINDOWS(12, "safari 浏览器 Windows 版", "Mozilla/5.0 (Windows; U; " + + "Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 " + + "Safari/534.50"), /** * Opera浏览器 Windows 版 */ - public final static String USER_AGENT_OPERA_VERSION_WINDOWS = "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11"; + USER_AGENT_OPERA_VERSION_WINDOWS(13, "Opera浏览器 Windows 版", + "Opera/9.80 (Windows NT 6.1; U;" + " en) Presto/2.8.131 Version/11.11"), /** * Opera浏览器 mac 版 */ - public final static String USER_AGENT_OPERA_VERSION_MAC = "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11"; + USER_AGENT_OPERA_VERSION_MAC(14, "Opera浏览器 mac 版", "Opera/9.80 (Macintosh; Intel Mac " + "OS " + + "X 10.6.8; U; en) Presto/2.8.131 Version/11.11"), /** * 世界之窗浏览器 windows 版 */ - public final static String USER_AGENT_WORLD_VERSION_WINDOWS = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; + USER_AGENT_WORLD_VERSION_WINDOWS(15, "世界之窗浏览器 windows 版", "Mozilla/4.0 (compatible; MSIE " + + "7.0; Windows NT 5.1)"), /** * 360浏览器 windows 版 */ - public final static String USER_AGENT_360_VERSION_WINDOWS = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)"; + USER_AGENT_360_VERSION_WINDOWS(16, "360浏览器 windows 版", "Mozilla/4.0 (compatible; MSIE 7" + + ".0; Windows NT 5.1; 360SE)"), /** * 猎豹浏览器 windows 版 */ - public final static String USER_AGENT_LBBROWSER_VERSION_WINDOWS = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER"; + USER_AGENT_LBBROWSER_VERSION_WINDOWS(17, "猎豹浏览器 windows 版", "Mozilla/5.0 (Windows NT 6" + + ".1; WOW64) AppleWebKit/537.1 (KHTML, like " + "Gecko) Chrome/21.0.1180.71 Safari/537" + + ".1 " + "LBBROWSER"), /** * Avant浏览器 windows 版 */ - public final static String USER_AGENT_AVANT_VERSION_WINDOWS = "User-Agent, Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Avant Browser)"; + USER_AGENT_AVANT_VERSION_WINDOWS(18, "Avant浏览器 windows 版", "User-Agent, Mozilla/4.0 " + + "(compatible; MSIE 7.0; Windows NT 5.1; Avant " + "Browser)"), /** * Green Browser浏览器 windows 版 */ - public final static String USER_AGENT_GREEN_VERSION_WINDOWS = "User-Agent, Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; + USER_AGENT_GREEN_VERSION_WINDOWS(19, "Green Browser浏览器 windows 版", + "User-Agent, Mozilla/4.0 " + "(compatible; MSIE 7.0; Windows NT 5.1)"), + /** * 腾讯TT浏览器 windows 版 */ - public final static String USER_AGENT_QQTT_VERSION_WINDOWS = "User-Agent, Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; TencentTraveler 4.0)"; + USER_AGENT_QQTT_VERSION_WINDOWS(20, "腾讯TT浏览器 windows 版", "User-Agent, Mozilla/4.0 " + + "(compatible; MSIE 7.0; Windows NT 5.1; " + "TencentTraveler 4.0)"), /** * QQ浏览器 windows 版 */ - public final static String USER_AGENT_QQ_VERSION_WINDOWS = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)"; + USER_AGENT_QQ_VERSION_WINDOWS(21, "QQ浏览器 windows 版", "Mozilla/5.0 (compatible; MSIE 9" + ".0;" + + " Windows NT 6.1; WOW64; Trident/5.0; SLCC2;" + " .NET CLR 2.0.50727; .NET CLR 3.5" + ".30729; .NET CLR" + + " 3.0.30729; Media Center PC 6" + ".0; .NET4.0C; .NET4.0E; QQBrowser/7.0" + ".3698.400)"), /** * sogou浏览器 windows 版 */ - public final static String USER_AGENT_SOUGOU_VERSION_WINDOWS = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0"; + USER_AGENT_SOUGOU_VERSION_WINDOWS(22, "sogou浏览器 windows 版", + "Mozilla/5.0 (Windows NT 5.1) " + "AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17" + ".0.963.84 " + + "Safari/535.11 SE 2.X " + "MetaSr 1.0"), /** * 傲游(maxthon)浏览器 */ - public final static String USER_AGENT_AOYOU_VERSION_WINDOWS = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101 Safari/537.36"; + USER_AGENT_AOYOU_VERSION_WINDOWS(23, "傲游(maxthon)浏览器", "Mozilla/5.0 (Windows NT 6.1; " + + "WOW64) AppleWebKit/537.36 (KHTML, like " + "Gecko) Maxthon/4.4.3.4000 Chrome/30.0" + + ".1599" + ".101 Safari/537.36"), /** * UC浏览器 windows 版 */ - public final static String USER_AGENT_UC_VERSION_WINDOWS = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 UBrowser/4.0.3214.0 Safari/537.36"; + USER_AGENT_UC_VERSION_WINDOWS(24, "UC浏览器 windows 版", "Mozilla/5.0 (Windows NT 6.1; " + + "WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/38.0.2125.122 UBrowser/4" + + ".0" + ".3214.0 Safari/537.36"), /** - * 浏览器标识符集合 + * Microsoft Edge 127 + */ + USER_AGENT_MICROSOFT_EDGE_127(25, "Microsoft Edge 127", "Mozilla/5.0 (Windows NT 10.0; " + + "Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 " + "Edg/127.0.0.0"), + /** + * QQ浏览器12.3 */ - public static final String[] USER_AGENT_ARRAY = new String[]{USER_AGENT_EDGE_VERSION_110_0, USER_AGENT_GOOGLE_VERSION_78_0, - USER_AGENT_GOOGLE_VERSION_75_0, USER_AGENT_FIREFOX_VERSION_70_0, USER_AGENT_FIREFOX_VERSION_MAC, - USER_AGENT_IE_VERSION_11_476, USER_AGENT_IE_VERSION_9_0, USER_AGENT_EDAG_VERSION_11_476, - USER_AGENT_SAFARI_VERSION_MAC, USER_AGENT_SAFARI_VERSION_WINDOWS, USER_AGENT_OPERA_VERSION_WINDOWS, - USER_AGENT_OPERA_VERSION_MAC, USER_AGENT_WORLD_VERSION_WINDOWS, USER_AGENT_360_VERSION_WINDOWS, - USER_AGENT_LBBROWSER_VERSION_WINDOWS, USER_AGENT_AVANT_VERSION_WINDOWS, USER_AGENT_GREEN_VERSION_WINDOWS, - USER_AGENT_QQTT_VERSION_WINDOWS, USER_AGENT_QQ_VERSION_WINDOWS, USER_AGENT_SOUGOU_VERSION_WINDOWS, - USER_AGENT_AOYOU_VERSION_WINDOWS, USER_AGENT_UC_VERSION_WINDOWS, USER_AGENT_EDAG_VERSION_99, - USER_AGENT_FIREFOX_VERSION_97_0}; + USER_AGENT_QQ_VERSION_12_3(26, "QQ浏览器12." + "3", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + + "AppleWebKit/537.36 (KHTML, like " + "Gecko) Chrome/94.0.4606.71 Safari/537.36 " + + "Core/1.94.232.400 QQBrowser/12.3.5573.400"), + /** + * deepin 浏览器6.4.6 + */ + USER_AGENT_DEEPIN_BROWSER_6(27, "deepin 浏览器6.4.6", "Mozilla/5.0 (X11; Linux x86_64) " + + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 UOS " + + "Community"); /** * 随机获取一个浏览器UserAgent @@ -145,7 +186,79 @@ public final class UserAgent { * @return 浏览器UserAgent */ public static String autoUserAgent() { - int randomVal = RandomUtils.nextInt(0, USER_AGENT_ARRAY.length); - return USER_AGENT_ARRAY[randomVal]; + int randomVal = RandomUtils.nextInt(0, values().length); + return values()[randomVal].description; + } + + UserAgent(Integer code, String name, String description) { + this.code = code; + this.name = name; + this.description = description; + } + + /** + * 编码 + */ + private Integer code; + + /** + * 名称 + */ + private String name; + /** + * 描述 + */ + private String description; + + /** + * 获取编码 + * + * @return 编码 + */ + public Integer getCode() { + return code; + } + + /** + * 获取描述 + * + * @return 描述 + */ + public String getDescription() { + return description; + } + + /** + * 获取名称 + * + * @return 名称 + */ + public String getName() { + return this.name; + } + + /** + * 获取编码 + * + * @return 编码 + */ + @Override + public Integer code() { + return this.code; + } + + @Override + public String enumName() { + return this.name; + } + + /** + * 获取描述 + * + * @return 描述 + */ + @Override + public java.lang.String description() { + return this.description; } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/io/CloseUtil.java b/src/main/java/com/yishuifengxiao/common/tool/io/CloseUtil.java index e03e6da..a61121c 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/io/CloseUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/io/CloseUtil.java @@ -11,10 +11,6 @@ *

    * 该工具主要目的是优雅地关闭掉各种IO流,从而屏蔽掉因为关闭IO时强制异常捕获代码造成代码优雅性的降低 * - *

    - * 该工具是一个线程安全类的工具 - *

    - * * @author yishui * @version 1.0.0 * @since 1.0.0 @@ -54,7 +50,11 @@ public static void close(boolean flush, Closeable... closeable) { close.close(); close = null; } catch (Exception e) { - log.debug("【易水工具】关闭流时出现问题,出现问题的原因为 {}", e.getMessage()); + if (log.isDebugEnabled()) { + log.debug("There was a problem closing the stream, and the reason for the problem is {}", + e.getMessage()); + } + } } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/io/ImageUtil.java b/src/main/java/com/yishuifengxiao/common/tool/io/ImageUtil.java index a6c847d..4500aca 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/io/ImageUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/io/ImageUtil.java @@ -15,9 +15,6 @@ *

    * 该工具的主要作用是实现图片与base64字符串之间的互相转换. *

    - *

    - * 该工具是一个线程安全类的工具 - *

    * * @author yishui * @version 1.0.0 @@ -156,7 +153,11 @@ public static String image2Base64(BufferedImage image) { stream.flush(); } catch (Exception e) { - log.info("将图片转换成base64时出现问题,出现问题的原因为 {}", e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There was a problem converting the image to base64, and the reason for " + + "the problem is {}", e); + } + } return base64; diff --git a/src/main/java/com/yishuifengxiao/common/tool/io/IoUtil.java b/src/main/java/com/yishuifengxiao/common/tool/io/IoUtil.java index 299f850..0174697 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/io/IoUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/io/IoUtil.java @@ -17,10 +17,6 @@ /** * 文件处理工具 * - *

    - * 该工具是一个线程安全类的工具 - *

    - * * @author yishui * @version 1.0.0 * @since 1.0.0 @@ -55,7 +51,6 @@ public static String suffix(String fileName) { *

    * 将文件转换为字节数组 *

    - * 线程安全 * * @param file 待转换的文件 * @return 转换后的字节数组 @@ -71,7 +66,6 @@ public static byte[] file2ByteArray(File file) throws IOException { *

    * 将输入流转换为字节数组 *

    - * 线程安全 * * @param inputStream 输入流 * @return 转换后的字节数组 @@ -131,7 +125,6 @@ public static String inputStream2String(InputStream in) throws IOException { *

    * 将文件输入流保存为文件 *

    - * 线程安全 * * @param inputStream 文件输入流 * @param file 待保存的目标文件 @@ -209,7 +202,6 @@ public static int copy(File file, OutputStream out) { * Copy the contents of the given InputStream to the given OutputStream. Closes * both streams when done. *

    - * 线程安全 * * @param in the stream to copy from * @param out the stream to copy to @@ -236,7 +228,6 @@ public static int copy(InputStream in, OutputStream out) throws IOException { *

    * 将字符串复制到指定的文件中 *

    - * 线程安全 * * @param text 待输入的字符串 * @param file 目标文件 @@ -281,7 +272,6 @@ public static String file2String(@NotNull File file, String charsetName) throws *

    * 将base64字符串转换成文件 *

    - * 线程安全 * * @param base64File base64格式的文件 * @return 转换后的文件 @@ -298,7 +288,7 @@ public static File base64ToFile(String base64File) { bos.write(bytes); bos.flush(); } catch (Exception e) { - throw new UncheckedException("文件转换失败,失败的原因为 " + e); + throw new UncheckedException(e); } finally { CloseUtil.close(bos, fos); } @@ -309,7 +299,6 @@ public static File base64ToFile(String base64File) { *

    * 将文件转换成base64字符串 *

    - * 线程安全 * * @param file 待转换的文件 * @return 转换后的base64字符串 diff --git a/src/main/java/com/yishuifengxiao/common/tool/lang/NumberUtil.java b/src/main/java/com/yishuifengxiao/common/tool/lang/NumberUtil.java index a1675c8..e999fe3 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/lang/NumberUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/lang/NumberUtil.java @@ -175,8 +175,13 @@ public static Optional parse(Object val) { BigDecimal number = new BigDecimal(val.toString().replaceAll(",", "").trim()); return Optional.ofNullable(number); } catch (Throwable e) { - log.debug("将数据【{}】转换为数值时出现问题 {}", val, e); + if (log.isInfoEnabled()) { + log.info("There was a problem converting data [{}] to numerical values, and the reason for the " + + "problem is" + + " {}", val, e); + } } + return Optional.empty(); } @@ -194,7 +199,11 @@ public static Optional parseHex(String hexString) { BigDecimal number = new BigDecimal(new BigInteger(hexString.toString().replaceAll(",", "").trim(), 16)); return Optional.ofNullable(number); } catch (Throwable e) { - log.debug("将数据【{}】转换为数值时出现问题 {}", hexString, e); + if (log.isInfoEnabled()) { + log.info("There was a problem converting data [{}] to numerical values, and the reason for the " + + "problem is" + + " {}", hexString, e); + } } return Optional.empty(); } @@ -231,7 +240,8 @@ public static String toHexString(Number number, Integer byteNum) { hexString = "0" + hexString; } if (null != byteNum && byteNum > 0 && hexString.length() < byteNum * 2) { - String prefix = IntStream.range(0, byteNum * 2 - hexString.length()).mapToObj(v -> "0").collect(Collectors.joining()); + String prefix = + IntStream.range(0, byteNum * 2 - hexString.length()).mapToObj(v -> "0").collect(Collectors.joining()); hexString = prefix + hexString; } return hexString; diff --git a/src/main/java/com/yishuifengxiao/common/tool/log/LogLevelUtil.java b/src/main/java/com/yishuifengxiao/common/tool/log/LogLevelUtil.java index ee46f9f..187bc70 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/log/LogLevelUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/log/LogLevelUtil.java @@ -29,14 +29,16 @@ public class LogLevelUtil { * @param logLevel 日志级别 ,例如 info * @return 是否修改成功 */ - public static boolean setLevel(String loggerName, String logLevel) { + public static boolean setLevel(String loggerName, String logLevel) { if (StringUtils.isAnyBlank(loggerName, logLevel)) { return false; } try { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); final List loggers = loggerContext.getLoggerList(); - final Set sets = loggers.parallelStream().filter(v -> StringUtils.equalsIgnoreCase(v.getName(), loggerName)).collect(Collectors.toSet()); + final Set sets = + loggers.parallelStream().filter(v -> StringUtils.equalsIgnoreCase(v.getName() + , loggerName)).collect(Collectors.toSet()); if (CollUtil.isEmpty(sets)) { return false; } @@ -45,7 +47,12 @@ public static boolean setLevel(String loggerName, String logLevel) { }); } catch (Throwable e) { - log.warn("动态修改日志级别 loggerName ={} , logLevel ={} 时出现问题 {} ", loggerName, logLevel, e); + if (log.isWarnEnabled()) { + log.warn("There is a problem when dynamically modifying the log level loggerName={}, logLevel={}, and" + + " the" + + " reason for the problem is {} ", loggerName, logLevel, e); + } + return false; } return true; diff --git a/src/main/java/com/yishuifengxiao/common/tool/random/IdWorker.java b/src/main/java/com/yishuifengxiao/common/tool/random/IdWorker.java index 948ef99..23cd041 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/random/IdWorker.java +++ b/src/main/java/com/yishuifengxiao/common/tool/random/IdWorker.java @@ -119,11 +119,13 @@ public class IdWorker { public IdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException( - String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); + String.format("worker Id can't be greater than %d or less than 0", + maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException( - String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); + String.format("datacenter Id can't be greater than %d or less than 0", + maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; @@ -147,7 +149,8 @@ public synchronized long nextId() { // 如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException(String.format( - "Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); + "Clock moved backwards. Refusing to generate id for %d milliseconds", + lastTimestamp - timestamp)); } // 如果是同一时间生成的,则进行毫秒内序列 diff --git a/src/main/java/com/yishuifengxiao/common/tool/random/RandomUtil.java b/src/main/java/com/yishuifengxiao/common/tool/random/RandomUtil.java index fff83ed..ea5862b 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/random/RandomUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/random/RandomUtil.java @@ -22,9 +22,6 @@ *
  • 根据当前时间生成yyyyMMddhhmmss的字符串
  • *
  • 根据当前时间生成yyyyMMddhhmmss+随机数 形式的字符串
  • * - *

    - * 该工具是一个线程安全类的工具。 - *

    * * @author yishui * @version 1.0.0 diff --git a/src/main/java/com/yishuifengxiao/common/tool/text/HtmlExtract.java b/src/main/java/com/yishuifengxiao/common/tool/text/HtmlExtract.java index 3ffbeaf..de9fe7d 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/text/HtmlExtract.java +++ b/src/main/java/com/yishuifengxiao/common/tool/text/HtmlExtract.java @@ -56,8 +56,12 @@ public static List extractByCss(String html, String cssSelector, String list.add(StringUtils.isBlank(attrName) ? e.outerHtml() : e.attr(attrName.trim())); }); } catch (Exception e) { - log.info("使用【css规则】 提取 {} 时出现问题,提取参数为 cssSelector= {} ,attrName = {},问题为 {}", html, cssSelector, attrName, - e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There was a problem extracting {} using the CSS rules. The extraction parameters were " + + "cssLlector={}, attrName={}, and the problem was {}", html, cssSelector, attrName, + e.getMessage()); + } + } return list; } @@ -130,7 +134,11 @@ public static List extractTextByCss(String html, String cssSelector) { list.add(e.ownText()); }); } catch (Exception e) { - log.info("使用【css 文本规则】 提取 {} 时出现问题,提取参数为 cssSelector= {} ,问题为 {}", html, cssSelector, e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There was a problem extracting {} using the CSS Text Rules. The extraction parameter is " + + "cssLlector={}, and the problem is {}", html, cssSelector, e.getMessage()); + } + } return list; } @@ -200,7 +208,10 @@ public static org.dom4j.Element element(String xml) { org.dom4j.Element root = doc.getRootElement(); return root; } catch (Exception e) { - log.info("解析xml [ {} ] 时出现问题 {}", xml, e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There was a problem parsing XML [{}], the problem is {}", xml, e.getMessage()); + } + } return null; } diff --git a/src/main/java/com/yishuifengxiao/common/tool/text/RegexUtil.java b/src/main/java/com/yishuifengxiao/common/tool/text/RegexUtil.java index 258ef84..dd19035 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/text/RegexUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/text/RegexUtil.java @@ -19,10 +19,6 @@ *
  • 判断给定的字符串是否符合给定的正则表达式
  • * * - *

    - * 该工具是一个线程安全类的工具 - *

    - * * @author yishui * @version 1.0.0 * @since 1.0.0 @@ -32,7 +28,8 @@ public final class RegexUtil { /** * 协议和域名的正则表达式 */ - private final static String REGEX_PROTOCOL_AND_HOST = "http[s]?://[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\" + ".[a-zA-Z0" + "-9][-a-zA-Z0-9]{0,62})+\\.?"; + private final static String REGEX_PROTOCOL_AND_HOST = + "http[s]?://[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\" + ".[a-zA-Z0" + "-9][-a-zA-Z0-9]{0,62})+\\.?"; /** * 域名的正则表达式 @@ -70,12 +67,14 @@ public final class RegexUtil { /** * URL正则表达式 */ - private final static String REGEX_URL = "((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\" + ".[0-9]{1,3}\\.[0-9]{1,3}\\" + ".[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?"; + private final static String REGEX_URL = + "((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\" + ".[0-9]{1,3}\\.[0-9]{1,3}\\" + ".[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?"; /** * IPv4地址正则表达式 */ - private final static String REGEX_IPV4 = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))" + "|[0" + "-1]?\\d{1,2})){3}"; + private final static String REGEX_IPV4 = + "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))" + "|[0" + "-1]?\\d{1,2})){3}"; /** * 判断是否符合形如 http://www.yishuifengxiao.com 的正则表达式 diff --git a/src/main/java/com/yishuifengxiao/common/tool/utils/CertNoUtil.java b/src/main/java/com/yishuifengxiao/common/tool/utils/CertNoUtil.java index 98e9905..f72120f 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/utils/CertNoUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/utils/CertNoUtil.java @@ -21,7 +21,6 @@ *
  • 判断给定的字符串是否为一个合法的18位身份证号
  • *
  • 从合法的身份证号中提取出当前身份证里的出生日期
  • * - * 该工具是一个线程安全类的工具。 * * @author yishui * @version 1.0.0 @@ -45,10 +44,6 @@ public final class CertNoUtil { * 校验18位身份证号的合法性 *

    * - *

    - * 线程安全 - *

    - * * @param idcard 身份证号 * @return true表示合法,false不合法 */ @@ -93,23 +88,25 @@ public static boolean isValid(String idcard) { // 非18位为假 *

    * 从身份证号里提取出出生日期 *

    - *

    - * 线程安全 - *

    * * @param idcard 身份证号 * @return 出生日期 */ public static LocalDate extractBirthday(String idcard) { if (!isValid(idcard)) { - throw new UncheckedException("身份证号格式不正确"); + throw new UncheckedException("身份证号格式不正确").setContext(idcard); } try { String dateStr = StringUtils.substring(idcard.trim(), 6, 14); return LocalDate.parse(dateStr, DateTimeFormatter.BASIC_ISO_DATE); } catch (Exception e) { - log.info("【易水工具】从身份证号{}中提取出生日期时出现异常,出现异常的原因为 {}", idcard.trim(), e.getMessage()); - throw new UncheckedException("身份证号出生日期格式不正确"); + if (log.isInfoEnabled()) { + log.info("An exception occurred while extracting the birth date from ID number {}. The reason for the" + + " " + + "exception is {}", idcard.trim(), e.getMessage()); + } + + throw new UncheckedException("身份证号出生日期格式不正确").setContext(idcard); } } diff --git a/src/main/java/com/yishuifengxiao/common/tool/utils/ExecuteUtil.java b/src/main/java/com/yishuifengxiao/common/tool/utils/ExecuteUtil.java index ad2dae3..9bfd658 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/utils/ExecuteUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/utils/ExecuteUtil.java @@ -25,7 +25,11 @@ public class ExecuteUtil { /** * 线程池初始化 */ - private final static ExecutorService POOL = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), SIMPLE_THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy()); + private final static ExecutorService POOL = + new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), + Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), + SIMPLE_THREAD_FACTORY, + new ThreadPoolExecutor.AbortPolicy()); /** * 获取线程池 @@ -69,7 +73,7 @@ public static void execute(Runnable runnable, ExecuteError error) { * 执行任务 * * @param runnable 待执行的任务 - * @param complete 执行完成后触发 + * @param complete 执行完成后触发的动作 * @param error 执行失败后触发的动作 */ public static void execute(Runnable runnable, ExecuteComplete complete, ExecuteError error) { @@ -77,7 +81,13 @@ public static void execute(Runnable runnable, ExecuteComplete complete, ExecuteE try { runnable.run(); } catch (Throwable e) { - log.warn("执行回调任务 {} 时出现问题,出现的问题为{}", runnable, e.getMessage()); + if (log.isInfoEnabled()) { + log.info("There was a problem while executing callback task {}, and the " + + "problem that occurred was" + + " {}" + , runnable, e.getMessage()); + } + if (null != error) { error.onError(e); } diff --git a/src/main/java/com/yishuifengxiao/common/tool/utils/GpsUtil.java b/src/main/java/com/yishuifengxiao/common/tool/utils/GpsUtil.java index 8360b8a..809fc5d 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/utils/GpsUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/utils/GpsUtil.java @@ -13,9 +13,6 @@ *

    * 算法来源 https://www.cnblogs.com/zhoug2020/p/8993750.html *

    - *

    - * 该工具是一个线程安全类的工具。 - *

    * * @author yishui * @version 1.0.0 diff --git a/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java b/src/main/java/com/yishuifengxiao/common/tool/utils/ValidateUtils.java similarity index 62% rename from src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java rename to src/main/java/com/yishuifengxiao/common/tool/utils/ValidateUtils.java index 1dd7ab8..ff3cb93 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/utils/ExceptionUtil.java +++ b/src/main/java/com/yishuifengxiao/common/tool/utils/ValidateUtils.java @@ -1,5 +1,6 @@ package com.yishuifengxiao.common.tool.utils; +import com.yishuifengxiao.common.tool.entity.RootEnum; import com.yishuifengxiao.common.tool.exception.UncheckedException; import com.yishuifengxiao.common.tool.io.CloseUtil; @@ -16,7 +17,7 @@ * @version 1.0.0 * @since 1.0.0 */ -public class ExceptionUtil { +public class ValidateUtils { /** @@ -31,6 +32,18 @@ public static void isTrue(Boolean val, String msg) { } } + /** + * 判断给定的值是否为true,若为null或false则抛出异常 + * + * @param val 待判断的值 + * @param rootEnum 异常提示信息 + */ + public static void isTrue(Boolean val, RootEnum rootEnum) { + if (null == val || !val) { + throw new UncheckedException(rootEnum); + } + } + /** * 判断给定的值是否为true或null,若为false则抛出异常 * @@ -43,6 +56,18 @@ public static void isTrueOrNull(Boolean val, String msg) { } } + /** + * 判断给定的值是否为true或null,若为false则抛出异常 + * + * @param val 待判断的值 + * @param rootEnum 异常提示信息 + */ + public static void isTrueOrNull(Boolean val, RootEnum rootEnum) { + if (null != val && !val) { + throw new UncheckedException(rootEnum); + } + } + /** * 判断给定的值是否为false,若为null或true则抛出异常 * @@ -55,6 +80,18 @@ public static void isFalse(Boolean val, String msg) { } } + /** + * 判断给定的值是否为false,若为null或true则抛出异常 + * + * @param val 待判断的值 + * @param rootEnum 异常提示信息 + */ + public static void isFalse(Boolean val, RootEnum rootEnum) { + if (null == val || val) { + throw new UncheckedException(rootEnum); + } + } + /** * 判断给定的值是否为false或null,若为true则抛出异常 * @@ -67,6 +104,18 @@ public static void isFalseOrNull(Boolean val, String msg) { } } + /** + * 判断给定的值是否为false或null,若为true则抛出异常 + * + * @param val 待判断的值 + * @param rootEnum 异常提示信息 + */ + public static void isFalseOrNull(Boolean val, RootEnum rootEnum) { + if (null != val && val) { + throw new UncheckedException(rootEnum); + } + } + /** * 生成一个 Supplier @@ -78,6 +127,16 @@ public static final Supplier orElseThrow(String message) { return () -> new UncheckedException(message); } + /** + * 生成一个 Supplier + * + * @param rootEnum 参数信息 + * @return Supplier + */ + public static final Supplier orElseThrow(RootEnum rootEnum) { + return () -> new UncheckedException(rootEnum); + } + /** * 生成一个 Supplier * @@ -117,6 +176,35 @@ public static String extractError(Throwable throwable) { return result; } + /** + * 抛出一个自定义运行时异常 + * + * @param rootEnum 异常信息 + */ + public static void throwException(RootEnum rootEnum) { + throw new UncheckedException(rootEnum); + } + + /** + * 抛出一个自定义运行时异常 + * + * @param exception 异常信息 + */ + public static void throwException(RuntimeException exception) { + throw exception; + } + + + /** + * 抛出一个自定义运行时异常 + * + * @param rootEnum 异常信息 + * @param context 异常原因 + */ + public static void throwException(RootEnum rootEnum, Object context) { + throw new UncheckedException(rootEnum).setContext(context); + } + /** * 抛出一个自定义运行时异常 * diff --git a/src/main/java/com/yishuifengxiao/common/tool/validate/InBool.java b/src/main/java/com/yishuifengxiao/common/tool/validate/InBool.java index 1ab248b..4d533ab 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/validate/InBool.java +++ b/src/main/java/com/yishuifengxiao/common/tool/validate/InBool.java @@ -67,9 +67,9 @@ @Documented @interface List { /** - * 待校验的值 + * 校验值 * - * @return 待校验的值 + * @return 校验值 */ InBool[] value(); } diff --git a/src/main/java/com/yishuifengxiao/common/tool/validate/InInt.java b/src/main/java/com/yishuifengxiao/common/tool/validate/InInt.java index fb6539b..7c67863 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/validate/InInt.java +++ b/src/main/java/com/yishuifengxiao/common/tool/validate/InInt.java @@ -70,9 +70,9 @@ @Documented @interface List { /** - * 待校验的值 + * 校验值 * - * @return 待校验的值 + * @return 校验值 */ InInt[] value(); } diff --git a/src/main/java/com/yishuifengxiao/common/tool/validate/InLong.java b/src/main/java/com/yishuifengxiao/common/tool/validate/InLong.java index c8094a3..f291947 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/validate/InLong.java +++ b/src/main/java/com/yishuifengxiao/common/tool/validate/InLong.java @@ -72,9 +72,9 @@ @Documented @interface List { /** - * 待校验的值 + * 校验值 * - * @return 待校验的值 + * @return 校验值 */ InLong[] value(); } diff --git a/src/main/java/com/yishuifengxiao/common/tool/validate/InString.java b/src/main/java/com/yishuifengxiao/common/tool/validate/InString.java index 6eb9daf..0619b01 100644 --- a/src/main/java/com/yishuifengxiao/common/tool/validate/InString.java +++ b/src/main/java/com/yishuifengxiao/common/tool/validate/InString.java @@ -77,9 +77,9 @@ @Documented @interface List { /** - * 待校验的值 + * 校验值 * - * @return 待校验的值 + * @return 校验值 */ InString[] value(); }