first commit.
This commit is contained in:
13
src/main/java/xyz/zhouxy/plusone/exception/IWithCode.java
Normal file
13
src/main/java/xyz/zhouxy/plusone/exception/IWithCode.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package xyz.zhouxy.plusone.exception;
|
||||
|
||||
/**
|
||||
* 规定实现类带有 {@code getCode} 方法。
|
||||
* 用于像自定义异常等需要带有 {@code code} 字段的类,
|
||||
* 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
* @see PlusoneException
|
||||
*/
|
||||
public interface IWithCode {
|
||||
int getCode();
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package xyz.zhouxy.plusone.exception;
|
||||
|
||||
/**
|
||||
* 项目的基础异常,默认错误码为 9999999
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
public class PlusoneException extends RuntimeException implements IWithCode {
|
||||
|
||||
private static final long serialVersionUID = -2546365325001947203L;
|
||||
|
||||
private final int code;
|
||||
|
||||
protected PlusoneException(int code, String msg) {
|
||||
super(msg);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
protected PlusoneException(int code, Throwable cause) {
|
||||
super(cause);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
protected PlusoneException(int code, String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCode() {
|
||||
return this.code;
|
||||
}
|
||||
}
|
91
src/main/java/xyz/zhouxy/plusone/util/EnumUtil.java
Normal file
91
src/main/java/xyz/zhouxy/plusone/util/EnumUtil.java
Normal file
@@ -0,0 +1,91 @@
|
||||
package xyz.zhouxy.plusone.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);
|
||||
}
|
||||
}
|
38
src/main/java/xyz/zhouxy/plusone/util/Enumeration.java
Normal file
38
src/main/java/xyz/zhouxy/plusone/util/Enumeration.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package xyz.zhouxy.plusone.util;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
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);
|
||||
}
|
||||
}
|
29
src/main/java/xyz/zhouxy/plusone/util/NumberUtil.java
Normal file
29
src/main/java/xyz/zhouxy/plusone/util/NumberUtil.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package xyz.zhouxy.plusone.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;
|
||||
}
|
||||
}
|
58
src/main/java/xyz/zhouxy/plusone/util/RestfulResult.java
Normal file
58
src/main/java/xyz/zhouxy/plusone/util/RestfulResult.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package xyz.zhouxy.plusone.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