forked from plusone/plusone-commons
重构。
This commit is contained in:
91
src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java
Normal file
91
src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 枚举工具类
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
public final class EnumUtil {
|
||||
|
||||
private EnumUtil() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 ordinal 获取枚举实例
|
||||
*
|
||||
* @param <E> 枚举的类型
|
||||
* @param clazz 枚举的类型信息
|
||||
* @param ordinal 数据库中对应的数值
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public static <E extends Enum<?>> E valueOf(Class<E> clazz, int ordinal) {
|
||||
E[] values = clazz.getEnumConstants();
|
||||
try {
|
||||
return values[ordinal];
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 ordinal 获取枚举实例
|
||||
*
|
||||
* @param <E> 枚举的类型
|
||||
* @param clazz 枚举的类型信息
|
||||
* @param ordinal 数据库中对应的数值
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public static <E extends Enum<?>> E getValueOrDefault(Class<E> clazz, Integer ordinal) {
|
||||
E[] values = clazz.getEnumConstants();
|
||||
try {
|
||||
return Objects.nonNull(ordinal) ? values[ordinal] : values[0];
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过 ordinal 获取枚举实例
|
||||
*
|
||||
* @param <E> 枚举的类型
|
||||
* @param clazz 枚举的类型信息
|
||||
* @param ordinal 数据库中对应的数值
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public static <E extends Enum<?>> E getValueNullable(Class<E> clazz, Integer ordinal) {
|
||||
E[] values = clazz.getEnumConstants();
|
||||
try {
|
||||
return Objects.nonNull(ordinal) ? values[ordinal] : null;
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
|
||||
}
|
||||
}
|
||||
|
||||
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> clazz, Integer ordinal) {
|
||||
if (ordinal == null) {
|
||||
throw new IllegalArgumentException("ordinal 不能为空");
|
||||
}
|
||||
E[] values = clazz.getEnumConstants();
|
||||
if (ordinal >= 0 && ordinal < values.length) {
|
||||
return ordinal;
|
||||
}
|
||||
throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
|
||||
}
|
||||
|
||||
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> clazz, Integer ordinal) {
|
||||
if (Objects.isNull(ordinal)) {
|
||||
return null;
|
||||
}
|
||||
return checkOrdinal(clazz, ordinal);
|
||||
}
|
||||
|
||||
public static <E extends Enum<?>> Integer checkOrdinalOrDefault(Class<E> clazz, Integer ordinal) {
|
||||
if (Objects.isNull(ordinal)) {
|
||||
return 0;
|
||||
}
|
||||
return checkOrdinal(clazz, ordinal);
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
package xyz.zhouxy.plusone.commons.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;
|
||||
protected final String name;
|
||||
|
||||
protected Enumeration(int value, String name) {
|
||||
this.value = value;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Enumeration<?> other = (Enumeration<?>) obj;
|
||||
return value == other.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
/**
|
||||
* NumberUtil
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
public class NumberUtil {
|
||||
|
||||
private NumberUtil() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
public static int sum(int... numbers) {
|
||||
int result = 0;
|
||||
for (int number : numbers) {
|
||||
result += number;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static long sum(long... numbers) {
|
||||
long result = 0;
|
||||
for (long number : numbers) {
|
||||
result += number;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
40
src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java
Normal file
40
src/main/java/xyz/zhouxy/plusone/commons/util/PageDTO.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* 返回分页查询的结果
|
||||
*
|
||||
* @param <T> 内容列表的元素类型
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
* @see PagingAndSortingQueryParams
|
||||
*/
|
||||
@ToString
|
||||
@Setter
|
||||
public class PageDTO<T> {
|
||||
|
||||
private Long total;
|
||||
private List<T> content;
|
||||
|
||||
private PageDTO(List<T> content, Long total) {
|
||||
this.content = content;
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public static <T> PageDTO<T> of(List<T> content, Long total) {
|
||||
return new PageDTO<>(content, total);
|
||||
}
|
||||
|
||||
public Long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public List<T> getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 分页排序查询参数
|
||||
*
|
||||
* <p>
|
||||
* 根据传入的 {@code size} 和 {@code pageNum},
|
||||
* 提供 {@code getOffset} 方法计算 SQL 语句中 {@code offset} 的值。
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
* @see PageDTO
|
||||
*/
|
||||
@Setter
|
||||
public class PagingAndSortingQueryParams {
|
||||
|
||||
protected String orderBy;
|
||||
protected Integer size;
|
||||
protected Long pageNum;
|
||||
|
||||
private final List<String> sortableColNames;
|
||||
|
||||
public PagingAndSortingQueryParams() {
|
||||
sortableColNames = Collections.emptyList();
|
||||
}
|
||||
|
||||
public PagingAndSortingQueryParams(String... sortableColNames) {
|
||||
this.sortableColNames = Arrays.asList(sortableColNames);
|
||||
}
|
||||
|
||||
public String getOrderBy() {
|
||||
return orderBy != null && sortableColNames.contains(orderBy) ? orderBy : null;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return this.size != null ? this.size : 15;
|
||||
}
|
||||
|
||||
public long getPageNum() {
|
||||
return this.pageNum != null ? this.pageNum : 1;
|
||||
}
|
||||
|
||||
public long getOffset() {
|
||||
return (getPageNum() - 1) * getSize();
|
||||
}
|
||||
|
||||
}
|
38
src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java
Normal file
38
src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class RegexUtil {
|
||||
|
||||
public static boolean matches(CharSequence input, Pattern regex) {
|
||||
Matcher m = regex.matcher(input);
|
||||
return m.matches();
|
||||
}
|
||||
|
||||
public static boolean matchesOr(CharSequence input, Pattern... regexs) {
|
||||
boolean isMatched;
|
||||
for (Pattern regex : regexs) {
|
||||
isMatched = matches(input, regex);
|
||||
if (isMatched) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* 对返回给前端的数据进行封装
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@ToString
|
||||
@Getter
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class RestfulResult {
|
||||
|
||||
public static final int SUCCESS_STATUS = 2000000;
|
||||
|
||||
private final Object status;
|
||||
private final String message;
|
||||
private final Object data;
|
||||
|
||||
private RestfulResult(Object status, String message) {
|
||||
this(status, message, null);
|
||||
}
|
||||
|
||||
public static RestfulResult success() {
|
||||
return new RestfulResult(SUCCESS_STATUS, "操作成功");
|
||||
}
|
||||
|
||||
public static RestfulResult success(String message) {
|
||||
return new RestfulResult(SUCCESS_STATUS, message);
|
||||
}
|
||||
|
||||
public static RestfulResult success(String message, Object data) {
|
||||
return new RestfulResult(SUCCESS_STATUS, message, data);
|
||||
}
|
||||
|
||||
public static RestfulResult error() {
|
||||
return new RestfulResult(500000, "未知错误");
|
||||
}
|
||||
|
||||
public static RestfulResult error(Object status, String message) {
|
||||
return new RestfulResult(status, message);
|
||||
}
|
||||
|
||||
public static RestfulResult error(Object status, String message, Object data) {
|
||||
return new RestfulResult(status, message, data);
|
||||
}
|
||||
|
||||
public static RestfulResult error(Object status, Throwable e) {
|
||||
return new RestfulResult(status, e.getMessage());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user