mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-31 08:58:37 +08:00 
			
		
		
		
	feat(core): BaseEnum 新增 getByValue、getByDescription、isValidValue 方法
This commit is contained in:
		| @@ -17,6 +17,7 @@ | |||||||
| package top.continew.starter.core.enums; | package top.continew.starter.core.enums; | ||||||
|  |  | ||||||
| import java.io.Serializable; | import java.io.Serializable; | ||||||
|  | import java.util.Objects; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 枚举接口 |  * 枚举接口 | ||||||
| @@ -49,4 +50,50 @@ public interface BaseEnum<T extends Serializable> { | |||||||
|     default String getColor() { |     default String getColor() { | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * 根据枚举值获取 | ||||||
|  |      * | ||||||
|  |      * @param value 枚举值 | ||||||
|  |      * @param clazz 枚举类 | ||||||
|  |      * @return 枚举对象 | ||||||
|  |      * @since 2.8.1 | ||||||
|  |      */ | ||||||
|  |     static <E extends Enum<E> & BaseEnum, T> E getByValue(T value, Class<E> 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 <E extends Enum<E> & 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 <E extends Enum<E> & BaseEnum, T> boolean isValidValue(T value, Class<E> clazz) { | ||||||
|  |         return getByValue(value, clazz) != null; | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -16,7 +16,6 @@ | |||||||
|  |  | ||||||
| package top.continew.starter.file.excel.converter; | package top.continew.starter.file.excel.converter; | ||||||
|  |  | ||||||
| import cn.hutool.core.util.ClassUtil; |  | ||||||
| import com.alibaba.excel.converters.Converter; | import com.alibaba.excel.converters.Converter; | ||||||
| import com.alibaba.excel.enums.CellDataTypeEnum; | import com.alibaba.excel.enums.CellDataTypeEnum; | ||||||
| import com.alibaba.excel.metadata.GlobalConfiguration; | import com.alibaba.excel.metadata.GlobalConfiguration; | ||||||
| @@ -52,7 +51,7 @@ public class ExcelBaseEnumConverter implements Converter<BaseEnum<?>> { | |||||||
|     public BaseEnum<?> convertToJavaData(ReadCellData<?> cellData, |     public BaseEnum<?> convertToJavaData(ReadCellData<?> cellData, | ||||||
|                                          ExcelContentProperty contentProperty, |                                          ExcelContentProperty contentProperty, | ||||||
|                                          GlobalConfiguration globalConfiguration) { |                                          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<BaseEnum<?>> { | |||||||
|         } |         } | ||||||
|         return new WriteCellData<>(value.getDescription()); |         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; |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user