From 0d5eef24e3cf5a4c10c6519701fc113527494b07 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 7 Nov 2022 17:49:27 +0800 Subject: [PATCH] first commit. --- .gitignore | 35 +++++++ pom.xml | 30 ++++++ .../zhouxy/plusone/exception/IWithCode.java | 13 +++ .../plusone/exception/PlusoneException.java | 33 +++++++ .../xyz/zhouxy/plusone/util/EnumUtil.java | 91 +++++++++++++++++++ .../xyz/zhouxy/plusone/util/Enumeration.java | 38 ++++++++ .../plusone/util/EnumerationValuesHolder.java | 22 +++++ .../xyz/zhouxy/plusone/util/NumberUtil.java | 29 ++++++ .../zhouxy/plusone/util/RestfulResult.java | 58 ++++++++++++ 9 files changed, 349 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/xyz/zhouxy/plusone/exception/IWithCode.java create mode 100644 src/main/java/xyz/zhouxy/plusone/exception/PlusoneException.java create mode 100644 src/main/java/xyz/zhouxy/plusone/util/EnumUtil.java create mode 100644 src/main/java/xyz/zhouxy/plusone/util/Enumeration.java create mode 100644 src/main/java/xyz/zhouxy/plusone/util/EnumerationValuesHolder.java create mode 100644 src/main/java/xyz/zhouxy/plusone/util/NumberUtil.java create mode 100644 src/main/java/xyz/zhouxy/plusone/util/RestfulResult.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e403e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..5c25ec3 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + xyz.zhouxy.plusone + plusone-commons + 0.0.1-SNAPSHOT + + + UTF-8 + 8 + 8 + + + + + org.projectlombok + lombok + 1.18.24 + true + + + com.fasterxml.jackson.core + jackson-annotations + 2.13.4 + + + \ No newline at end of file diff --git a/src/main/java/xyz/zhouxy/plusone/exception/IWithCode.java b/src/main/java/xyz/zhouxy/plusone/exception/IWithCode.java new file mode 100644 index 0000000..fb378f4 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/exception/IWithCode.java @@ -0,0 +1,13 @@ +package xyz.zhouxy.plusone.exception; + +/** + * 规定实现类带有 {@code getCode} 方法。 + * 用于像自定义异常等需要带有 {@code code} 字段的类, + * 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。 + * + * @author ZhouXY + * @see PlusoneException + */ +public interface IWithCode { + int getCode(); +} diff --git a/src/main/java/xyz/zhouxy/plusone/exception/PlusoneException.java b/src/main/java/xyz/zhouxy/plusone/exception/PlusoneException.java new file mode 100644 index 0000000..c874ff3 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/exception/PlusoneException.java @@ -0,0 +1,33 @@ +package xyz.zhouxy.plusone.exception; + +/** + * 项目的基础异常,默认错误码为 9999999 + * + * @author ZhouXY + */ +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; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/util/EnumUtil.java b/src/main/java/xyz/zhouxy/plusone/util/EnumUtil.java new file mode 100644 index 0000000..522ed87 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/util/EnumUtil.java @@ -0,0 +1,91 @@ +package xyz.zhouxy.plusone.util; + +import java.util.Objects; + +/** + * 枚举工具类 + * + * @author ZhouXY + */ +public final class EnumUtil { + + private EnumUtil() { + throw new IllegalStateException("Utility class"); + } + + /** + * 通过 ordinal 获取枚举实例 + * + * @param 枚举的类型 + * @param clazz 枚举的类型信息 + * @param ordinal 数据库中对应的数值 + * @return 枚举对象 + */ + public static > E valueOf(Class clazz, int ordinal) { + E[] values = clazz.getEnumConstants(); + try { + return values[ordinal]; + } catch (IndexOutOfBoundsException e) { + throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)); + } + } + + /** + * 通过 ordinal 获取枚举实例 + * + * @param 枚举的类型 + * @param clazz 枚举的类型信息 + * @param ordinal 数据库中对应的数值 + * @return 枚举对象 + */ + public static > E getValueOrDefault(Class 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 枚举的类型 + * @param clazz 枚举的类型信息 + * @param ordinal 数据库中对应的数值 + * @return 枚举对象 + */ + public static > E getValueNullable(Class 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 > Integer checkOrdinal(Class 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 > Integer checkOrdinalNullable(Class clazz, Integer ordinal) { + if (Objects.isNull(ordinal)) { + return null; + } + return checkOrdinal(clazz, ordinal); + } + + public static > Integer checkOrdinalOrDefault(Class clazz, Integer ordinal) { + if (Objects.isNull(ordinal)) { + return 0; + } + return checkOrdinal(clazz, ordinal); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/util/Enumeration.java b/src/main/java/xyz/zhouxy/plusone/util/Enumeration.java new file mode 100644 index 0000000..632f548 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/util/Enumeration.java @@ -0,0 +1,38 @@ +package xyz.zhouxy.plusone.util; + +import java.util.Objects; + +public abstract class Enumeration> { + 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; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/util/EnumerationValuesHolder.java b/src/main/java/xyz/zhouxy/plusone/util/EnumerationValuesHolder.java new file mode 100644 index 0000000..25a37be --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/util/EnumerationValuesHolder.java @@ -0,0 +1,22 @@ +package xyz.zhouxy.plusone.util; + +import java.util.HashMap; +import java.util.Map; + +public final class EnumerationValuesHolder> { + private final Map 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); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/util/NumberUtil.java b/src/main/java/xyz/zhouxy/plusone/util/NumberUtil.java new file mode 100644 index 0000000..b5bac8b --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/util/NumberUtil.java @@ -0,0 +1,29 @@ +package xyz.zhouxy.plusone.util; + +/** + * NumberUtil + * + * @author ZhouXY + */ +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; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/util/RestfulResult.java b/src/main/java/xyz/zhouxy/plusone/util/RestfulResult.java new file mode 100644 index 0000000..7a596c5 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/util/RestfulResult.java @@ -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 ZhouXY + */ +@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()); + } +}