From 15a501b2cd614505eebc0c0c54af2a3a7665cbdb Mon Sep 17 00:00:00 2001 From: redkale Date: Wed, 12 Jun 2024 16:18:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0RetCodes=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=A0=81=E5=8A=A0=E8=BD=BD=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/redkale/service/RetCodes.java | 154 ++++++++++++++++++ .../java/org/redkale/service/RetLabel.java | 13 +- 2 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 src/main/java/org/redkale/service/RetCodes.java diff --git a/src/main/java/org/redkale/service/RetCodes.java b/src/main/java/org/redkale/service/RetCodes.java new file mode 100644 index 000000000..a4e2d7726 --- /dev/null +++ b/src/main/java/org/redkale/service/RetCodes.java @@ -0,0 +1,154 @@ +/* + +*/ + +package org.redkale.service; + +import java.text.MessageFormat; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.locks.ReentrantLock; + +/** + * 错误码加载器 + * + *

详情见: https://redkale.org + * + * @author zhangjx + * @since 2.8.0 + */ +public abstract class RetCodes { + + private static final ReentrantLock loadLock = new ReentrantLock(); + + private static Map> rets = new LinkedHashMap<>(); + + private static Map defret = new LinkedHashMap<>(); + + protected RetCodes() { + throw new IllegalStateException(); + } + + public static void load(Class codeClass) { + load(RetLabel.RetLoader.loadMap(codeClass)); + } + + public static void load(Map> map) { + if (map.isEmpty()) { + return; + } + loadLock.lock(); + try { + Map> newMap = new LinkedHashMap<>(); + rets.forEach((k, v) -> newMap.put(k, new LinkedHashMap<>(v))); + map.forEach((k, v) -> { + Map m = newMap.get(k); + if (m != null) { + m.putAll(v); + } else { + newMap.put(k, v); + } + }); + rets = newMap; + defret = rets.get(""); + } finally { + loadLock.unlock(); + } + } + + public static RetResult retResult(int retcode) { + if (retcode == 0) { + return RetResult.success(); + } + return new RetResult(retcode, retInfo(retcode)); + } + + public static RetResult retResult(String locale, int retcode) { + if (retcode == 0) { + return RetResult.success(); + } + return new RetResult(retcode, retInfo(locale, retcode)); + } + + public static RetResult retResult(int retcode, Object... args) { + if (retcode == 0) { + return RetResult.success(); + } + if (args == null || args.length < 1) { + return new RetResult(retcode, retInfo(retcode)); + } + String info = MessageFormat.format(retInfo(retcode), args); + return new RetResult(retcode, info); + } + + public static RetResult retResult(String locale, int retcode, Object... args) { + if (retcode == 0) { + return RetResult.success(); + } + if (args == null || args.length < 1) { + return new RetResult(retcode, retInfo(locale, retcode)); + } + String info = MessageFormat.format(retInfo(locale, retcode), args); + return new RetResult(retcode, info); + } + + public static CompletableFuture> retResultFuture(int retcode) { + return CompletableFuture.completedFuture(retResult(retcode)); + } + + public static CompletableFuture> retResultFuture(String locale, int retcode) { + return CompletableFuture.completedFuture(retResult(locale, retcode)); + } + + public static CompletableFuture> retResultFuture(int retcode, Object... args) { + return CompletableFuture.completedFuture(retResult(retcode, args)); + } + + public static CompletableFuture> retResultFuture(String locale, int retcode, Object... args) { + return CompletableFuture.completedFuture(retResult(locale, retcode, args)); + } + + public static RetResult retInfo(RetResult result, int retcode, Object... args) { + if (retcode == 0) { + return result.retcode(0).retinfo(""); + } + if (args == null || args.length < 1) { + return result.retcode(retcode).retinfo(retInfo(retcode)); + } + String info = MessageFormat.format(retInfo(retcode), args); + return result.retcode(retcode).retinfo(info); + } + + public static RetResult retInfo(RetResult result, String locale, int retcode, Object... args) { + if (retcode == 0) { + return result.retcode(0).retinfo(""); + } + if (args == null || args.length < 1) { + return result.retcode(retcode).retinfo(retInfo(locale, retcode)); + } + String info = MessageFormat.format(retInfo(locale, retcode), args); + return result.retcode(retcode).retinfo(info); + } + + public static String retInfo(int retcode) { + if (retcode == 0) { + return "Success"; + } + return defret.getOrDefault(retcode, "Error"); + } + + public static String retInfo(String locale, int retcode) { + if (locale == null || locale.isEmpty()) { + return retInfo(retcode); + } + if (retcode == 0) { + return "Success"; + } + Map map = rets.get(locale); + if (map == null) { + return "Error"; + } + return map.getOrDefault(retcode, "Error"); + } +} diff --git a/src/main/java/org/redkale/service/RetLabel.java b/src/main/java/org/redkale/service/RetLabel.java index 5d3ad0e0e..2017aefdb 100644 --- a/src/main/java/org/redkale/service/RetLabel.java +++ b/src/main/java/org/redkale/service/RetLabel.java @@ -5,17 +5,16 @@ */ package org.redkale.service; -import static java.lang.annotation.ElementType.*; -import static java.lang.annotation.RetentionPolicy.RUNTIME; -import static org.redkale.boot.Application.SYSNAME_APP_CONF_DIR; -import static org.redkale.boot.Application.SYSNAME_APP_HOME; - import java.io.*; import java.lang.annotation.*; +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.reflect.*; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.function.BiFunction; +import static org.redkale.boot.Application.SYSNAME_APP_CONF_DIR; +import static org.redkale.boot.Application.SYSNAME_APP_HOME; import org.redkale.util.RedkaleClassLoader; /** @@ -50,6 +49,10 @@ public @interface RetLabel { public static interface RetInfoTransfer extends BiFunction {} public abstract static class RetLoader { + + private RetLoader() { + throw new IllegalStateException(); + } public static Map> loadMap(Class clazz) { final Map> rets = new LinkedHashMap<>();