Skip to content

Commit 279d72b

Browse files
committed
feat(core): BaseEnum 新增 getByValue、getByDescription、isValidValue 方法
1 parent 80c0700 commit 279d72b

File tree

2 files changed

+48
-22
lines changed
  • continew-starter-core/src/main/java/top/continew/starter/core/enums
  • continew-starter-file/continew-starter-file-excel/src/main/java/top/continew/starter/file/excel/converter

2 files changed

+48
-22
lines changed

continew-starter-core/src/main/java/top/continew/starter/core/enums/BaseEnum.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package top.continew.starter.core.enums;
1818

1919
import java.io.Serializable;
20+
import java.util.Objects;
2021

2122
/**
2223
* 枚举接口
@@ -49,4 +50,50 @@ public interface BaseEnum<T extends Serializable> {
4950
default String getColor() {
5051
return null;
5152
}
53+
54+
/**
55+
* 根据枚举值获取
56+
*
57+
* @param value 枚举值
58+
* @param clazz 枚举类
59+
* @return 枚举对象
60+
* @since 2.8.1
61+
*/
62+
static <E extends Enum<E> & BaseEnum, T> E getByValue(T value, Class<E> clazz) {
63+
for (E e : clazz.getEnumConstants()) {
64+
if (Objects.equals(e.getValue(), value)) {
65+
return e;
66+
}
67+
}
68+
return null;
69+
}
70+
71+
/**
72+
* 根据枚举描述获取
73+
*
74+
* @param description 枚举描述
75+
* @param clazz 枚举类
76+
* @return 枚举对象
77+
* @since 2.8.1
78+
*/
79+
static <E extends Enum<E> & BaseEnum> E getByDescription(String description, Class<?> clazz) {
80+
for (Object e : clazz.getEnumConstants()) {
81+
if (e instanceof BaseEnum<?> baseEnum && Objects.equals(baseEnum.getDescription(), description)) {
82+
return (E)baseEnum;
83+
}
84+
}
85+
return null;
86+
}
87+
88+
/**
89+
* 判断枚举值是否有效
90+
*
91+
* @param value 枚举值
92+
* @param clazz 枚举类
93+
* @return 是否有效
94+
* @since 2.8.1
95+
*/
96+
static <E extends Enum<E> & BaseEnum, T> boolean isValidValue(T value, Class<E> clazz) {
97+
return getByValue(value, clazz) != null;
98+
}
5299
}

continew-starter-file/continew-starter-file-excel/src/main/java/top/continew/starter/file/excel/converter/ExcelBaseEnumConverter.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package top.continew.starter.file.excel.converter;
1818

19-
import cn.hutool.core.util.ClassUtil;
2019
import com.alibaba.excel.converters.Converter;
2120
import com.alibaba.excel.enums.CellDataTypeEnum;
2221
import com.alibaba.excel.metadata.GlobalConfiguration;
@@ -52,7 +51,7 @@ public CellDataTypeEnum supportExcelTypeKey() {
5251
public BaseEnum<?> convertToJavaData(ReadCellData<?> cellData,
5352
ExcelContentProperty contentProperty,
5453
GlobalConfiguration globalConfiguration) {
55-
return this.getEnum(contentProperty.getField().getType(), cellData.getStringValue());
54+
return BaseEnum.getByDescription(cellData.getStringValue(), contentProperty.getField().getType());
5655
}
5756

5857
/**
@@ -67,24 +66,4 @@ public WriteCellData<String> convertToExcelData(BaseEnum<?> value,
6766
}
6867
return new WriteCellData<>(value.getDescription());
6968
}
70-
71-
/**
72-
* 通过 value 获取枚举对象,获取不到时为 {@code null}
73-
*
74-
* @param enumType 枚举类型
75-
* @param description 描述
76-
* @return 对应枚举 ,获取不到时为 {@code null}
77-
*/
78-
private BaseEnum<?> getEnum(Class<?> enumType, String description) {
79-
Object[] enumConstants = enumType.getEnumConstants();
80-
for (Object enumConstant : enumConstants) {
81-
if (ClassUtil.isAssignable(BaseEnum.class, enumType)) {
82-
BaseEnum<?> baseEnum = (BaseEnum<?>)enumConstant;
83-
if (baseEnum.getDescription().equals(description)) {
84-
return baseEnum;
85-
}
86-
}
87-
}
88-
return null;
89-
}
9069
}

0 commit comments

Comments
 (0)