From f846094a5b0fd5930ff78f4bff6777257528a800 Mon Sep 17 00:00:00 2001 From: fxbin Date: Tue, 19 Sep 2023 03:06:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20:art:=20=E9=94=99=E8=AF=AF=E7=A0=81?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E9=AA=8C=E8=AF=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fxbin/bubble/core/constant/CharPool.java | 10 ++++ .../bubble/core/constant/StringPool.java | 5 ++ .../bubble/core/dataobject/ErrorCode.java | 50 ++++++++++++++++++- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/CharPool.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/CharPool.java index 264552c6..e3ff4adb 100644 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/CharPool.java +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/CharPool.java @@ -49,4 +49,14 @@ public interface CharPool { */ char UNDERLINE = '_'; + /** + * 字符常量:冒号 {@code ':'} + */ + char COLON = ':'; + + /** + * 字符常量:艾特 {@code '@'} + */ + char AT = '@'; + } diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/StringPool.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/StringPool.java index 93d613da..585c9077 100644 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/StringPool.java +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/constant/StringPool.java @@ -64,6 +64,11 @@ public interface StringPool { */ String COMMA = ","; + /** + * 冒号 + */ + String COLON = ":"; + /** * 点 */ diff --git a/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/ErrorCode.java b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/ErrorCode.java index 58a8732e..51bfe109 100644 --- a/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/ErrorCode.java +++ b/bubble-core/src/main/java/cn/fxbin/bubble/core/dataobject/ErrorCode.java @@ -2,6 +2,7 @@ import java.io.Serializable; +import java.util.ServiceLoader; /** * 错误代码 @@ -80,19 +81,64 @@ default boolean is5xxServerError() { * @see #is4xxClientError() * @see #is5xxServerError() */ - boolean isError(); + default boolean isError() { + return true; + }; default boolean isSameCodeAs(ErrorCode other) { return value() == other.value(); } + /** + * 值 + * + * 这是一种示例,关于BizErrorCodee 实现的 + * + * @AutoService(BizErrorCode.class) + * public class AppErrorCode implements BizErrorCode { + * public static final AppErrorCode USERNAME_FORMAT_ERROR = new AppErrorCode(1000000, "用户名格式有误"); + * private int value; + * private String reasonPhrase; + * public AppErrorCode() {} + * public AppErrorCode(int value, String reasonPhrase) { + * this.value = value; + * this.reasonPhrase = reasonPhrase; + * } + * @Override + * public int value() { + * return this.value; + * } + * @Override + * public String reasonPhrase() { + * return this.reasonPhrase; + * } + * public BizErrorCode resolve(int errorCode) { + * return Arrays.stream(ReflectUtil.getFields(AppErrorCode.class)) + * .filter(field -> !"value".equals(field.getName()) && !"reasonPhrase".equals(field.getName())) + * .filter(field -> ((AppErrorCode) ReflectUtil.getStaticFieldValue(field)).value == errorCode) + * .map(field -> ((AppErrorCode) ReflectUtil.getStaticFieldValue(field))) + * .findAny().orElse(null); + * } + * } + * + * @param code code + * @return {@link ErrorCode} + */ static ErrorCode valueOf(int code) { GlobalErrorCode errorCode = GlobalErrorCode.resolve(code); if (errorCode != null) { return errorCode; } - // TODO 实现对业务错误码的判断 + // BizErrorCode 实现类的一种可行的示例: + // + ServiceLoader errCodeLoader = ServiceLoader.load(BizErrorCode.class); + for (BizErrorCode codeLoader : errCodeLoader) { + BizErrorCode bizErrorCode = codeLoader.resolve(code); + if (bizErrorCode.value() == code) { + return bizErrorCode; + } + } return null; }