From 27183d8e0e9282e574d76ddebff64335078a5abc Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 18 Feb 2023 11:58:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E8=B0=83=E6=95=B4=20API=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 13 +++--- ...usoneException.java => BaseException.java} | 10 ++--- .../zhouxy/plusone/exception/IWithCode.java | 2 +- .../xyz/zhouxy/plusone/util/Enumeration.java | 21 ++++++++++ .../plusone/util/EnumerationValuesHolder.java | 22 ---------- .../xyz/zhouxy/plusone/util/RegexUtil.java | 40 ++++++++++++++++--- 6 files changed, 69 insertions(+), 39 deletions(-) rename src/main/java/xyz/zhouxy/plusone/exception/{PlusoneException.java => BaseException.java} (59%) delete mode 100644 src/main/java/xyz/zhouxy/plusone/util/EnumerationValuesHolder.java diff --git a/pom.xml b/pom.xml index 4f88683..982b2c7 100644 --- a/pom.xml +++ b/pom.xml @@ -10,26 +10,29 @@ UTF-8 - 8 - 8 + 1.8 + 1.8 + 1.18.24 + 2.13.4 + 5.8.11 org.projectlombok lombok - 1.18.24 + ${lombok.version} true com.fasterxml.jackson.core jackson-annotations - 2.13.4 + ${jackson.version} cn.hutool hutool-core - 5.8.9 + ${hutool.version} \ No newline at end of file diff --git a/src/main/java/xyz/zhouxy/plusone/exception/PlusoneException.java b/src/main/java/xyz/zhouxy/plusone/exception/BaseException.java similarity index 59% rename from src/main/java/xyz/zhouxy/plusone/exception/PlusoneException.java rename to src/main/java/xyz/zhouxy/plusone/exception/BaseException.java index dbcaf2c..ac6cb7e 100644 --- a/src/main/java/xyz/zhouxy/plusone/exception/PlusoneException.java +++ b/src/main/java/xyz/zhouxy/plusone/exception/BaseException.java @@ -1,27 +1,27 @@ package xyz.zhouxy.plusone.exception; /** - * 项目的基础异常,默认错误码为 9999999 + * 带错误码的异常。 * * @author ZhouXY */ -public class PlusoneException extends RuntimeException implements IWithCode { +public abstract class BaseException extends RuntimeException implements IWithCode { private static final long serialVersionUID = -2546365325001947203L; private final int code; - public PlusoneException(int code, String msg) { + public BaseException(int code, String msg) { super(msg); this.code = code; } - public PlusoneException(int code, Throwable cause) { + public BaseException(int code, Throwable cause) { super(cause); this.code = code; } - public PlusoneException(int code, String msg, Throwable cause) { + public BaseException(int code, String msg, Throwable cause) { super(msg, cause); this.code = code; } diff --git a/src/main/java/xyz/zhouxy/plusone/exception/IWithCode.java b/src/main/java/xyz/zhouxy/plusone/exception/IWithCode.java index fb378f4..c559573 100644 --- a/src/main/java/xyz/zhouxy/plusone/exception/IWithCode.java +++ b/src/main/java/xyz/zhouxy/plusone/exception/IWithCode.java @@ -6,7 +6,7 @@ package xyz.zhouxy.plusone.exception; * 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。 * * @author ZhouXY - * @see PlusoneException + * @see BaseException */ public interface IWithCode { int getCode(); diff --git a/src/main/java/xyz/zhouxy/plusone/util/Enumeration.java b/src/main/java/xyz/zhouxy/plusone/util/Enumeration.java index 28927d1..4ad6e6e 100644 --- a/src/main/java/xyz/zhouxy/plusone/util/Enumeration.java +++ b/src/main/java/xyz/zhouxy/plusone/util/Enumeration.java @@ -1,6 +1,8 @@ package xyz.zhouxy.plusone.util; +import java.util.Map; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; public abstract class Enumeration> { protected final int value; @@ -42,4 +44,23 @@ public abstract class Enumeration> { builder.append("[").append(value).append(": ").append(name).append("]"); return builder.toString(); } + + protected static final class EnumerationValuesHolder> { + private final Map constants = new ConcurrentHashMap<>(); + + @SafeVarargs + public EnumerationValuesHolder(T... values) { + for (T value : values) { + put(value); + } + } + + private void put(T constant) { + this.constants.put(constant.getValue(), constant); + } + + public T get(int value) { + return this.constants.get(value); + } + } } diff --git a/src/main/java/xyz/zhouxy/plusone/util/EnumerationValuesHolder.java b/src/main/java/xyz/zhouxy/plusone/util/EnumerationValuesHolder.java deleted file mode 100644 index 25a37be..0000000 --- a/src/main/java/xyz/zhouxy/plusone/util/EnumerationValuesHolder.java +++ /dev/null @@ -1,22 +0,0 @@ -package xyz.zhouxy.plusone.util; - -import java.util.HashMap; -import java.util.Map; - -public final class EnumerationValuesHolder> { - private final Map constants = new HashMap<>(); - - public EnumerationValuesHolder(T[] values) { - for (T value : values) { - put(value); - } - } - - private void put(T constant) { - this.constants.put(constant.getValue(), constant); - } - - public T get(int value) { - return this.constants.get(value); - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/util/RegexUtil.java b/src/main/java/xyz/zhouxy/plusone/util/RegexUtil.java index 023160c..9f88a04 100644 --- a/src/main/java/xyz/zhouxy/plusone/util/RegexUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/util/RegexUtil.java @@ -2,20 +2,33 @@ package xyz.zhouxy.plusone.util; import java.util.regex.Pattern; +import cn.hutool.core.util.ReUtil; + public class RegexUtil { - private RegexUtil() { - throw new IllegalStateException("Utility class"); + public static boolean matches(CharSequence input, String regex) { + return ReUtil.isMatch(regex, input); } - public static boolean matches(CharSequence input, String regex) { - return Pattern.matches(regex, input); + public static boolean matches(CharSequence input, Pattern regex) { + return ReUtil.isMatch(regex, input); } public static boolean matchesOr(CharSequence input, String... regexs) { boolean isMatched; for (String regex : regexs) { - isMatched = Pattern.matches(regex, input); + isMatched = matches(input, regex); + if (isMatched) { + return true; + } + } + return false; + } + + public static boolean matchesOr(CharSequence input, Pattern... regexs) { + boolean isMatched; + for (Pattern regex : regexs) { + isMatched = matches(input, regex); if (isMatched) { return true; } @@ -26,11 +39,26 @@ public class RegexUtil { public static boolean matchesAnd(CharSequence input, String... regexs) { boolean isMatched; for (String regex : regexs) { - isMatched = Pattern.matches(regex, input); + isMatched = matches(input, regex); if (!isMatched) { return false; } } return true; } + + public static boolean matchesAnd(CharSequence input, Pattern... regexs) { + boolean isMatched; + for (Pattern regex : regexs) { + isMatched = matches(input, regex); + if (!isMatched) { + return false; + } + } + return true; + } + + private RegexUtil() { + throw new IllegalStateException("Utility class"); + } }