package com.changhu.common.db; import cn.hutool.core.lang.Dict; import cn.hutool.core.util.ObjectUtil; import com.changhu.common.exception.MessageException; /** * author: luozhun * desc: BaseEnum * createTime: 2023/8/16 17:38 */ public interface BaseEnum { /** * 获取值 * * @return V */ V getValue(); /** * 获取文本 * * @return string */ String getLabel(); @SuppressWarnings("unchecked") static > T valueOf(Class> enumType, P code) { BaseEnum

[] enumConstants = enumType.getEnumConstants(); for (BaseEnum

pBaseEnum : enumConstants) { if (ObjectUtil.equals(code, pBaseEnum.getValue())) { return (T) pBaseEnum; } } throw new MessageException("不存在值为:{} 的【{}】对象!", code.toString(), enumType.componentType().getSimpleName()); } default Object serializer() { return Dict.of( "value", this.getValue(), "label", this.getLabel() ); } }