package com.changhu.common.db; import cn.hutool.core.util.ObjectUtil; import com.changhu.common.cache.GlobalCacheManager; import com.changhu.common.exception.MessageException; import com.changhu.common.pojo.vo.SelectNodeVo; import java.util.Map; /** * 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) { Map, SelectNodeVo> nodeVoMap = GlobalCacheManager.ENUM_CACHE.get(enumType); for (Map.Entry, SelectNodeVo> mapEntry : nodeVoMap.entrySet()) { if (ObjectUtil.equals(code, mapEntry.getValue().getValue())) { return (T) mapEntry.getKey(); } } throw new MessageException("不存在值为:{} 的【{}】对象!", code.toString(), enumType.componentType().getSimpleName()); } @SuppressWarnings("unchecked") default SelectNodeVo serializer() { Map, SelectNodeVo> map = GlobalCacheManager.ENUM_CACHE.get(this.getClass()); if (map != null) { return (SelectNodeVo) map.get(this); } return null; } }