From 279d72b7242bf996f9b88d38ed0ea7aa0a0d1c46 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Mon, 6 Jan 2025 20:56:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20BaseEnum=20=E6=96=B0=E5=A2=9E=20g?= =?UTF-8?q?etByValue=E3=80=81getByDescription=E3=80=81isValidValue=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../continew/starter/core/enums/BaseEnum.java | 47 +++++++++++++++++++ .../converter/ExcelBaseEnumConverter.java | 23 +-------- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/enums/BaseEnum.java b/continew-starter-core/src/main/java/top/continew/starter/core/enums/BaseEnum.java index 5c5893d8..bbc112e1 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/enums/BaseEnum.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/enums/BaseEnum.java @@ -17,6 +17,7 @@ package top.continew.starter.core.enums; import java.io.Serializable; +import java.util.Objects; /** * 枚举接口 @@ -49,4 +50,50 @@ public interface BaseEnum { default String getColor() { return null; } + + /** + * 根据枚举值获取 + * + * @param value 枚举值 + * @param clazz 枚举类 + * @return 枚举对象 + * @since 2.8.1 + */ + static & BaseEnum, T> E getByValue(T value, Class clazz) { + for (E e : clazz.getEnumConstants()) { + if (Objects.equals(e.getValue(), value)) { + return e; + } + } + return null; + } + + /** + * 根据枚举描述获取 + * + * @param description 枚举描述 + * @param clazz 枚举类 + * @return 枚举对象 + * @since 2.8.1 + */ + static & BaseEnum> E getByDescription(String description, Class clazz) { + for (Object e : clazz.getEnumConstants()) { + if (e instanceof BaseEnum baseEnum && Objects.equals(baseEnum.getDescription(), description)) { + return (E)baseEnum; + } + } + return null; + } + + /** + * 判断枚举值是否有效 + * + * @param value 枚举值 + * @param clazz 枚举类 + * @return 是否有效 + * @since 2.8.1 + */ + static & BaseEnum, T> boolean isValidValue(T value, Class clazz) { + return getByValue(value, clazz) != null; + } } diff --git a/continew-starter-file/continew-starter-file-excel/src/main/java/top/continew/starter/file/excel/converter/ExcelBaseEnumConverter.java b/continew-starter-file/continew-starter-file-excel/src/main/java/top/continew/starter/file/excel/converter/ExcelBaseEnumConverter.java index 7ebe1317..e6a09222 100644 --- a/continew-starter-file/continew-starter-file-excel/src/main/java/top/continew/starter/file/excel/converter/ExcelBaseEnumConverter.java +++ b/continew-starter-file/continew-starter-file-excel/src/main/java/top/continew/starter/file/excel/converter/ExcelBaseEnumConverter.java @@ -16,7 +16,6 @@ package top.continew.starter.file.excel.converter; -import cn.hutool.core.util.ClassUtil; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.GlobalConfiguration; @@ -52,7 +51,7 @@ public class ExcelBaseEnumConverter implements Converter> { public BaseEnum convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { - return this.getEnum(contentProperty.getField().getType(), cellData.getStringValue()); + return BaseEnum.getByDescription(cellData.getStringValue(), contentProperty.getField().getType()); } /** @@ -67,24 +66,4 @@ public class ExcelBaseEnumConverter implements Converter> { } return new WriteCellData<>(value.getDescription()); } - - /** - * 通过 value 获取枚举对象,获取不到时为 {@code null} - * - * @param enumType 枚举类型 - * @param description 描述 - * @return 对应枚举 ,获取不到时为 {@code null} - */ - private BaseEnum getEnum(Class enumType, String description) { - Object[] enumConstants = enumType.getEnumConstants(); - for (Object enumConstant : enumConstants) { - if (ClassUtil.isAssignable(BaseEnum.class, enumType)) { - BaseEnum baseEnum = (BaseEnum)enumConstant; - if (baseEnum.getDescription().equals(description)) { - return baseEnum; - } - } - } - return null; - } }