forked from plusone/plusone-commons
重构代码,调整 API。
parent
3f4343c996
commit
27183d8e0e
13
pom.xml
13
pom.xml
|
@ -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>
|
|
@ -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;
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue