Skip to content

Commit

Permalink
update 枚举过期逻辑优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Robot committed Oct 17, 2023
1 parent 214a5ac commit 1b23313
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 33 deletions.
10 changes: 3 additions & 7 deletions easy-enum-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.luo-zhan</groupId>
<artifactId>EasyEnum</artifactId>
<version>1.1.0-RELEASE</version>
<version>1.2.0-RELEASE</version>
</parent>

<artifactId>easy-enum-demo</artifactId>
Expand All @@ -20,23 +20,19 @@
<dependency>
<groupId>io.github.luo-zhan</groupId>
<artifactId>easy-enum</artifactId>
<version>1.1.0-RELEASE</version>
<version>1.2.0-RELEASE</version>
</dependency>
<dependency>
<groupId>io.github.luo-zhan</groupId>
<artifactId>easy-enum-for-spring</artifactId>
<version>1.1.0-RELEASE</version>
<version>1.2.0-RELEASE</version>
</dependency>


<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>compile</scope>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion easy-enum/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.github.luo-zhan</groupId>
<artifactId>EasyEnum</artifactId>
<version>1.1.0-RELEASE</version>
<version>1.2.0-RELEASE</version>
</parent>

<artifactId>easy-enum</artifactId>
Expand Down
18 changes: 3 additions & 15 deletions easy-enum/src/main/java/com/robot/dict/Dict.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -71,7 +68,7 @@ static <T> String getTextByCode(Class<? extends Dict<T>> clazz, T code) {
*/
static <T> T getCodeByText(Class<? extends Dict<T>> clazz, String text) {
return Stream.of(clazz.getEnumConstants())
.filter((Dict<T> e) -> e.getText().equals(text))
.filter(e -> e.getText().equals(text))
.map(Dict::getCode)
.findAny().orElse(null);
}
Expand All @@ -88,26 +85,17 @@ static <T> T getCodeByText(Class<? extends Dict<T>> clazz, String text) {
static <T, K extends Dict<T>> K getByCode(Class<K> 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 <T> List<DictBean> getAll(Class<? extends Dict<T>> clazz) {
Map<String, Field> fieldCache = Arrays.stream(clazz.getDeclaredFields()).
filter(Field::isEnumConstant).
collect(Collectors.toMap(Field::getName, Function.identity()));
Dict<T>[] 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);
}


Expand Down
26 changes: 24 additions & 2 deletions easy-enum/src/main/java/com/robot/dict/DictBean.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
package com.robot.dict;


/**
* 字典枚举bean,存储枚举值的实际对象
*
* @author R
*/
public class DictBean implements Dict<Object> {

/**
* 字典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
Expand All @@ -25,4 +42,9 @@ public Object getCode() {
public String getText() {
return text;
}

public boolean getIsDeprecated() {
return isDeprecated;
}

}
20 changes: 13 additions & 7 deletions easy-enum/src/main/java/com/robot/dict/DictPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,31 @@
* @author R
*/
class DictPool {
private DictPool() {
}

/**
* 存储所有字典
*/
private static final Map<Dict<?>, DictBean> DICT_MAP = new ConcurrentHashMap<>();
/**
* 枚举类和对应的字典集合
*/
private static final Map<Class<?>, List<DictBean>> DICT_CLASS_MAP = new ConcurrentHashMap<>();
private static final Map<Class<?>, List<DictBean>> DICT_CLASS_ITEMS_MAP = new ConcurrentHashMap<>();

/**
* 放入字典和对应的code、text
* 此步骤线程安全
*/
static <T> void putDict(Dict<T> 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);
}

/**
Expand All @@ -47,7 +51,9 @@ static DictBean getDict(Dict<?> dict) {
static List<DictBean> getAll(Class<? extends Dict<?>> clazz) {
// 触发实例化枚举对象,避免并发问题导致未实例化拿不到数据,这比任何加锁方式都要简单快速
clazz.getEnumConstants();
return DICT_CLASS_MAP.get(clazz);
return DICT_CLASS_ITEMS_MAP.get(clazz);
}

private DictPool() {
}
}
3 changes: 2 additions & 1 deletion easy-enum/src/test/java/com/robot/dict/DictTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void getByCode() {
@Test
void getAll() {
List<DictBean> all = Dict.getAll(Sex.class);
System.out.println(JSON.toJSONString(all));
assertEquals(3, all.size());
}

Expand All @@ -62,7 +63,7 @@ void getItems() {

@Test
void getItemsExclude() {
List<DictBean> items = Dict.getItemsExclude(Sex.UNKNOWN);
List<DictBean> items = Dict.getItemsExclude(Sex.MALE);
System.out.println(JSON.toJSONString(items));
assertEquals(2, items.size());
}
Expand Down
1 change: 1 addition & 0 deletions easy-enum/src/test/java/com/robot/dict/enums/Sex.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public enum Sex implements Dict<Integer> {
MALE(1, "男"),
FEMALE(2, "女"),
@Deprecated
UNKNOWN(3, "未知");

Sex(Integer code, String text) {
Expand Down

0 comments on commit 1b23313

Please sign in to comment.