JAVA Enum工具类

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class EnumUtils {

    public static <T extends Enum> String getValueByCode(Class<T> enumType, String code) {
        if(enumType.isEnum() && StringUtils.isNotEmpty(code)){
            try {
                Method getCodeMethod = enumType.getMethod("getCode");
                Method getValueMethod = enumType.getMethod("getValue");
                T[] enumList =  enumType.getEnumConstants();
                for(T item:enumList){
                    if(code.equals(String.valueOf(getCodeMethod.invoke(item)))){
                        return String.valueOf(getValueMethod.invoke(item));
                    }
                }
            } catch (Exception e) {
               log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public static <T extends Enum> T getEnumByCode(Class<T> enumType, String code) {
        if(enumType.isEnum() && StringUtils.isNotEmpty(code)){
            try {
                Method getCodeMethod = enumType.getMethod("getCode");
                T[] enumList =  enumType.getEnumConstants();
                for(T item:enumList){
                    if(code.equals(String.valueOf(getCodeMethod.invoke(item)))){
                        return item;
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public static <T extends Enum> Map<String, String> getValueMap(Class<T> enumType) throws Exception {
        if(enumType.isEnum()){
            Method getCodeMethod = enumType.getMethod("getCode");
            Method getValueMethod = enumType.getMethod("getValue");
            T[] enumList =  enumType.getEnumConstants();
            Map<String, String> valueMap = new HashMap<>();
            for(T item:enumList){
                valueMap.put(String.valueOf(getCodeMethod.invoke(item)) , String.valueOf(getValueMethod.invoke(item)));
            }
            return valueMap;
        }
        return null;
    }

    public static <T extends Enum> Map<String, T> getEnumMap(Class<T> enumType) throws Exception {
        if(enumType.isEnum()){
            Method getCodeMethod = enumType.getMethod("getCode");
            T[] enumList =  enumType.getEnumConstants();
            Map<String, T> enumMap = new HashMap<>();
            for(T item:enumList){
                String code = String.valueOf(getCodeMethod.invoke(item));
                enumMap.put(code, getEnumByCode(enumType,code));
            }
            return enumMap;
        }
        return null;
    }

    public static <T extends Enum> String getAliasByCode(Class<T> enumType, String code) {
        if(enumType.isEnum() && StringUtils.isNotEmpty(code)){
            try {
                Method getCodeMethod = enumType.getMethod("getCode");
                Method getAliasMethod = enumType.getMethod("getAlias");
                T[] enumList =  enumType.getEnumConstants();
                for(T item:enumList){
                    if(code.equals(String.valueOf(getCodeMethod.invoke(item)))){
                        return String.valueOf(getAliasMethod.invoke(item));
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public static <T extends Enum> String getCodeByValue(Class<T> enumType, String value) {
        if(enumType.isEnum() && StringUtils.isNotEmpty(value)){
            try {
                Method getCodeMethod = enumType.getMethod("getCode");
                Method getValueMethod = enumType.getMethod("getValue");
                T[] enumList =  enumType.getEnumConstants();
                for(T item:enumList){
                    if(value.equals(String.valueOf(getValueMethod.invoke(item)))){
                        return getCodeMethod.invoke(item).toString();
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public static <T extends Enum> String getOrderNumByCode(Class<T> enumType, String code) {
        if(enumType.isEnum() && StringUtils.isNotEmpty(code)){
            try {
                Method getCodeMethod = enumType.getMethod("getCode");
                Method getAliasMethod = enumType.getMethod("getOrderNum");
                T[] enumList =  enumType.getEnumConstants();
                for(T item:enumList){
                    if(code.equals(String.valueOf(getCodeMethod.invoke(item)))){
                        return String.valueOf(getAliasMethod.invoke(item));
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

}

 

posted @ 2023-11-03 14:54  沙琪玛  阅读(76)  评论(0编辑  收藏  举报