重构代码,调整 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>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<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>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.4</version>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.8.9</version>
<version>${hutool.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -1,27 +1,27 @@
package xyz.zhouxy.plusone.exception;
/**
* 9999999
*
*
* @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 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;
}

View File

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

View File

@ -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<T extends Enumeration<T>> {
protected final int value;
@ -42,4 +44,23 @@ public abstract class Enumeration<T extends Enumeration<T>> {
builder.append("[").append(value).append(": ").append(name).append("]");
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 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");
}
}