From 1b23313f8679e42cccfb301b9e992f8d181ae3a6 Mon Sep 17 00:00:00 2001 From: Robot Date: Tue, 17 Oct 2023 15:49:07 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E6=9E=9A=E4=B8=BE=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easy-enum-demo/pom.xml | 10 +++---- easy-enum/pom.xml | 2 +- .../src/main/java/com/robot/dict/Dict.java | 18 +++---------- .../main/java/com/robot/dict/DictBean.java | 26 +++++++++++++++++-- .../main/java/com/robot/dict/DictPool.java | 20 +++++++++----- .../test/java/com/robot/dict/DictTest.java | 3 ++- .../test/java/com/robot/dict/enums/Sex.java | 1 + 7 files changed, 47 insertions(+), 33 deletions(-) diff --git a/easy-enum-demo/pom.xml b/easy-enum-demo/pom.xml index 0d561be..268aa9c 100644 --- a/easy-enum-demo/pom.xml +++ b/easy-enum-demo/pom.xml @@ -6,7 +6,7 @@ io.github.luo-zhan EasyEnum - 1.1.0-RELEASE + 1.2.0-RELEASE easy-enum-demo @@ -20,23 +20,19 @@ io.github.luo-zhan easy-enum - 1.1.0-RELEASE + 1.2.0-RELEASE io.github.luo-zhan easy-enum-for-spring - 1.1.0-RELEASE + 1.2.0-RELEASE - org.projectlombok lombok 1.18.26 - compile - - org.springframework.boot spring-boot-starter-web diff --git a/easy-enum/pom.xml b/easy-enum/pom.xml index e8a0a02..a085874 100644 --- a/easy-enum/pom.xml +++ b/easy-enum/pom.xml @@ -6,7 +6,7 @@ io.github.luo-zhan EasyEnum - 1.1.0-RELEASE + 1.2.0-RELEASE easy-enum diff --git a/easy-enum/src/main/java/com/robot/dict/Dict.java b/easy-enum/src/main/java/com/robot/dict/Dict.java index 7cea540..f40477a 100644 --- a/easy-enum/src/main/java/com/robot/dict/Dict.java +++ b/easy-enum/src/main/java/com/robot/dict/Dict.java @@ -1,9 +1,6 @@ package com.robot.dict; -import java.lang.reflect.Field; -import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -71,7 +68,7 @@ static String getTextByCode(Class> clazz, T code) { */ static T getCodeByText(Class> clazz, String text) { return Stream.of(clazz.getEnumConstants()) - .filter((Dict e) -> e.getText().equals(text)) + .filter(e -> e.getText().equals(text)) .map(Dict::getCode) .findAny().orElse(null); } @@ -88,26 +85,17 @@ static T getCodeByText(Class> clazz, String text) { static > K getByCode(Class clazz, T code) { return Stream.of(clazz.getEnumConstants()) .filter(e -> e.getCode().toString().equals(String.valueOf(code))) - .findAny() - .orElse(null); + .findAny().orElse(null); } /** * 获取所有字典枚举项(常用下拉框数据请求) - * 枚举值上标记@Deprecated的不会返回 * * @param clazz 字典枚举类 * @return List */ static List getAll(Class> clazz) { - Map fieldCache = Arrays.stream(clazz.getDeclaredFields()). - filter(Field::isEnumConstant). - collect(Collectors.toMap(Field::getName, Function.identity())); - Dict[] allEnum = clazz.getEnumConstants(); - return Stream.of(allEnum) - .filter(e -> !fieldCache.get(((Enum) e).name()).isAnnotationPresent(Deprecated.class)) - .map(v -> new DictBean(v.getCode(), v.getText())) - .collect(Collectors.toList()); + return DictPool.getAll(clazz); } diff --git a/easy-enum/src/main/java/com/robot/dict/DictBean.java b/easy-enum/src/main/java/com/robot/dict/DictBean.java index fba061f..7b1f7f8 100644 --- a/easy-enum/src/main/java/com/robot/dict/DictBean.java +++ b/easy-enum/src/main/java/com/robot/dict/DictBean.java @@ -1,19 +1,36 @@ package com.robot.dict; + /** * 字典枚举bean,存储枚举值的实际对象 * * @author R */ public class DictBean implements Dict { - + /** + * 字典code + */ private final Object code; - + /** + * 字典text + */ private final String text; + /** + * 是否过期 + * 一般来说,过期的字典项不能用于新增,而查询时需要作文本翻译 + */ + private final boolean isDeprecated; public DictBean(Object code, String text) { this.code = code; this.text = text; + this.isDeprecated = false; + } + + public DictBean(Object code, String text, boolean isDeprecated) { + this.code = code; + this.text = text; + this.isDeprecated = isDeprecated; } @Override @@ -25,4 +42,9 @@ public Object getCode() { public String getText() { return text; } + + public boolean getIsDeprecated() { + return isDeprecated; + } + } \ No newline at end of file diff --git a/easy-enum/src/main/java/com/robot/dict/DictPool.java b/easy-enum/src/main/java/com/robot/dict/DictPool.java index 19e2d29..f47611e 100644 --- a/easy-enum/src/main/java/com/robot/dict/DictPool.java +++ b/easy-enum/src/main/java/com/robot/dict/DictPool.java @@ -11,9 +11,6 @@ * @author R */ class DictPool { - private DictPool() { - } - /** * 存储所有字典 */ @@ -21,17 +18,24 @@ private DictPool() { /** * 枚举类和对应的字典集合 */ - private static final Map, List> DICT_CLASS_MAP = new ConcurrentHashMap<>(); + private static final Map, List> DICT_CLASS_ITEMS_MAP = new ConcurrentHashMap<>(); /** * 放入字典和对应的code、text * 此步骤线程安全 */ static void putDict(Dict dict, T code, String text) { - DictBean dictBean = new DictBean(code, text); + Class dictClass = dict.getClass(); + boolean isEnumDeprecated = false; + try { + isEnumDeprecated = dictClass.isEnum() && dictClass.getDeclaredField(((Enum) dict).name()).isAnnotationPresent(Deprecated.class); + } catch (NoSuchFieldException ignore) { + // impossible + } + DictBean dictBean = new DictBean(code, text, isEnumDeprecated); DICT_MAP.put(dict, dictBean); // 同一个枚举类初始化一定是单线程的,所以使用ArrayList即可 - DICT_CLASS_MAP.computeIfAbsent(dict.getClass(), k -> new ArrayList<>()).add(dictBean); + DICT_CLASS_ITEMS_MAP.computeIfAbsent(dictClass, k -> new ArrayList<>()).add(dictBean); } /** @@ -47,7 +51,9 @@ static DictBean getDict(Dict dict) { static List getAll(Class> clazz) { // 触发实例化枚举对象,避免并发问题导致未实例化拿不到数据,这比任何加锁方式都要简单快速 clazz.getEnumConstants(); - return DICT_CLASS_MAP.get(clazz); + return DICT_CLASS_ITEMS_MAP.get(clazz); } + private DictPool() { + } } diff --git a/easy-enum/src/test/java/com/robot/dict/DictTest.java b/easy-enum/src/test/java/com/robot/dict/DictTest.java index 02bba06..1ad87c2 100644 --- a/easy-enum/src/test/java/com/robot/dict/DictTest.java +++ b/easy-enum/src/test/java/com/robot/dict/DictTest.java @@ -50,6 +50,7 @@ void getByCode() { @Test void getAll() { List all = Dict.getAll(Sex.class); + System.out.println(JSON.toJSONString(all)); assertEquals(3, all.size()); } @@ -62,7 +63,7 @@ void getItems() { @Test void getItemsExclude() { - List items = Dict.getItemsExclude(Sex.UNKNOWN); + List items = Dict.getItemsExclude(Sex.MALE); System.out.println(JSON.toJSONString(items)); assertEquals(2, items.size()); } diff --git a/easy-enum/src/test/java/com/robot/dict/enums/Sex.java b/easy-enum/src/test/java/com/robot/dict/enums/Sex.java index 3c38b4f..e818f1b 100644 --- a/easy-enum/src/test/java/com/robot/dict/enums/Sex.java +++ b/easy-enum/src/test/java/com/robot/dict/enums/Sex.java @@ -8,6 +8,7 @@ public enum Sex implements Dict { MALE(1, "男"), FEMALE(2, "女"), + @Deprecated UNKNOWN(3, "未知"); Sex(Integer code, String text) {