Skip to content

Commit b9779e8

Browse files
authored
refactor(file/excel): 优化 BaseEnum 转换器 (#10)
1 parent a6c9d33 commit b9779e8

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

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

Lines changed: 8 additions & 9 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.convert.Convert;
2019
import cn.hutool.core.util.ClassUtil;
2120
import com.alibaba.excel.converters.Converter;
2221
import com.alibaba.excel.enums.CellDataTypeEnum;
@@ -34,7 +33,7 @@
3433
* @see BaseEnum
3534
* @since 1.2.0
3635
*/
37-
public class ExcelBaseEnumConverter implements Converter<BaseEnum<Integer>> {
36+
public class ExcelBaseEnumConverter implements Converter<BaseEnum<?>> {
3837

3938
@Override
4039
public Class<BaseEnum> supportJavaTypeKey() {
@@ -50,17 +49,17 @@ public CellDataTypeEnum supportExcelTypeKey() {
5049
* 转换为 Java 数据(读取 Excel)
5150
*/
5251
@Override
53-
public BaseEnum convertToJavaData(ReadCellData<?> cellData,
54-
ExcelContentProperty contentProperty,
55-
GlobalConfiguration globalConfiguration) {
56-
return this.getEnum(BaseEnum.class, Convert.toStr(cellData.getData()));
52+
public BaseEnum<?> convertToJavaData(ReadCellData<?> cellData,
53+
ExcelContentProperty contentProperty,
54+
GlobalConfiguration globalConfiguration) {
55+
return this.getEnum(contentProperty.getField().getType(), cellData.getStringValue());
5756
}
5857

5958
/**
6059
* 转换为 Excel 数据(写入 Excel)
6160
*/
6261
@Override
63-
public WriteCellData<String> convertToExcelData(BaseEnum<Integer> value,
62+
public WriteCellData<String> convertToExcelData(BaseEnum<?> value,
6463
ExcelContentProperty contentProperty,
6564
GlobalConfiguration globalConfiguration) {
6665
if (null == value) {
@@ -76,11 +75,11 @@ public WriteCellData<String> convertToExcelData(BaseEnum<Integer> value,
7675
* @param description 描述
7776
* @return 对应枚举 ,获取不到时为 {@code null}
7877
*/
79-
private BaseEnum<Integer> getEnum(Class<?> enumType, String description) {
78+
private BaseEnum<?> getEnum(Class<?> enumType, String description) {
8079
Object[] enumConstants = enumType.getEnumConstants();
8180
for (Object enumConstant : enumConstants) {
8281
if (ClassUtil.isAssignable(BaseEnum.class, enumType)) {
83-
BaseEnum<Integer> baseEnum = (BaseEnum<Integer>)enumConstant;
82+
BaseEnum<?> baseEnum = (BaseEnum<?>)enumConstant;
8483
if (baseEnum.getDescription().equals(description)) {
8584
return baseEnum;
8685
}

0 commit comments

Comments
 (0)