重构代码,调整 API。

feature/net-util
ZhouXY108 2023-02-18 11:58:06 +08:00
parent 3f4343c996
commit 27183d8e0e
6 changed files with 69 additions and 39 deletions

13
pom.xml
View File

@ -10,26 +10,29 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<lombok.version>1.18.24</lombok.version>
<jackson.version>2.13.4</jackson.version>
<hutool.version>5.8.11</hutool.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.24</version> <version>${lombok.version}</version>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId> <artifactId>jackson-annotations</artifactId>
<version>2.13.4</version> <version>${jackson.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId> <artifactId>hutool-core</artifactId>
<version>5.8.9</version> <version>${hutool.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,27 +1,27 @@
package xyz.zhouxy.plusone.exception; package xyz.zhouxy.plusone.exception;
/** /**
* 9999999 *
* *
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a> * @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/ */
public class PlusoneException extends RuntimeException implements IWithCode { public abstract class BaseException extends RuntimeException implements IWithCode {
private static final long serialVersionUID = -2546365325001947203L; private static final long serialVersionUID = -2546365325001947203L;
private final int code; private final int code;
public PlusoneException(int code, String msg) { public BaseException(int code, String msg) {
super(msg); super(msg);
this.code = code; this.code = code;
} }
public PlusoneException(int code, Throwable cause) { public BaseException(int code, Throwable cause) {
super(cause); super(cause);
this.code = code; this.code = code;
} }
public PlusoneException(int code, String msg, Throwable cause) { public BaseException(int code, String msg, Throwable cause) {
super(msg, cause); super(msg, cause);
this.code = code; this.code = code;
} }

View File

@ -6,7 +6,7 @@ package xyz.zhouxy.plusone.exception;
* 便 {@code code} * 便 {@code code}
* *
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a> * @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @see PlusoneException * @see BaseException
*/ */
public interface IWithCode { public interface IWithCode {
int getCode(); int getCode();

View File

@ -1,6 +1,8 @@
package xyz.zhouxy.plusone.util; package xyz.zhouxy.plusone.util;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
public abstract class Enumeration<T extends Enumeration<T>> { public abstract class Enumeration<T extends Enumeration<T>> {
protected final int value; protected final int value;
@ -42,4 +44,23 @@ public abstract class Enumeration<T extends Enumeration<T>> {
builder.append("[").append(value).append(": ").append(name).append("]"); builder.append("[").append(value).append(": ").append(name).append("]");
return builder.toString(); return builder.toString();
} }
protected static final class EnumerationValuesHolder<T extends Enumeration<T>> {
private final Map<Integer, T> 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);
}
}
} }

View File

@ -1,22 +0,0 @@
package xyz.zhouxy.plusone.util;
import java.util.HashMap;
import java.util.Map;
public final class EnumerationValuesHolder<T extends Enumeration<T>> {
private final Map<Integer, T> 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);
}
}

View File

@ -2,20 +2,33 @@ package xyz.zhouxy.plusone.util;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import cn.hutool.core.util.ReUtil;
public class RegexUtil { public class RegexUtil {
private RegexUtil() { public static boolean matches(CharSequence input, String regex) {
throw new IllegalStateException("Utility class"); return ReUtil.isMatch(regex, input);
} }
public static boolean matches(CharSequence input, String regex) { public static boolean matches(CharSequence input, Pattern regex) {
return Pattern.matches(regex, input); return ReUtil.isMatch(regex, input);
} }
public static boolean matchesOr(CharSequence input, String... regexs) { public static boolean matchesOr(CharSequence input, String... regexs) {
boolean isMatched; boolean isMatched;
for (String regex : regexs) { 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) { if (isMatched) {
return true; return true;
} }
@ -26,11 +39,26 @@ public class RegexUtil {
public static boolean matchesAnd(CharSequence input, String... regexs) { public static boolean matchesAnd(CharSequence input, String... regexs) {
boolean isMatched; boolean isMatched;
for (String regex : regexs) { for (String regex : regexs) {
isMatched = Pattern.matches(regex, input); isMatched = matches(input, regex);
if (!isMatched) { if (!isMatched) {
return false; return false;
} }
} }
return true; 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");
}
} }