Java枚举常用工具类

工具类详解:

1.判断枚举值是否存在于指定枚举数组中

2.根据枚举值获取其对应的名字

3.根据枚举名称获取对应的枚举值

4.根据枚举值获取对应的枚举对象

5.获取枚举列表

import com.uav.biz.constant.BaseEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;


@Slf4j
public class EnumUtils {

    /**
     * 判断枚举值是否存在于指定枚举数组中
     *
     * @param enums 枚举数组
     * @param code  枚举值
     * @return 是否存在
     */
    public static <T> boolean isExist(BaseEnum<T>[] enums, T code) {
        if (code == null) {
            return false;
        }
        for (BaseEnum<T> e : enums) {
            if (Objects.equals(e.getValue(), code)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 根据枚举值获取其对应的名字
     *
     * @param enums 枚举列表
     * @param code  枚举值
     * @return 枚举名称
     */
    public static <T> String getValueByCode(BaseEnum<T>[] enums, T code) {
        if (code == null) {
            return null;
        }
        for (BaseEnum<T> e : enums) {
            if (Objects.equals(e.getValue(), code)) {
                return e.getName();
            }
        }
        return null;
    }

    /**
     * 根据枚举名称获取对应的枚举值
     *
     * @param enums 枚举列表
     * @param msg   枚举名
     * @return 枚举值
     */
    public static <T> Integer getCodeByMsg(BaseEnum<T>[] enums, String msg) {
        if (StringUtils.isEmpty(msg)) {
            return null;
        }
        for (BaseEnum<T> e : enums) {
            if (Objects.equals(e.getName(), msg)) {
                return (Integer) e.getValue();
            }
        }
        return null;
    }

    public static <T> String getValueByName(BaseEnum<T>[] enums, String msg) {
        if (StringUtils.isEmpty(msg)) {
            return "";
        }
        for (BaseEnum<T> e : enums) {
            if (Objects.equals(e.getName(), msg)) {
                return (String) e.getValue();
            }
        }
        return "0";
    }

    /**
     * 根据枚举值获取对应的枚举对象
     *
     * @param enumClass 枚举class
     * @return 枚举对象
     */
    public static <N extends BaseEnum<T>, T> N getEnumByCode(Class<N> enumClass, T code) {
        for (N typeEnum : enumClass.getEnumConstants()) {
            if (typeEnum.getValue().equals(code)) {
                return typeEnum;
            }
        }
        return null;
    }

    private static final String ENUM_CLASSPATH ="java.lang.Enum";

    /**
     * 获取枚举列表
     * @param enumClass
     * @return
     */
    public static List<Map<String, Object>> enumToListMap(Class<?> enumClass) {
        List<Map<String, Object>> resultList= new ArrayList<>();
        if (!ENUM_CLASSPATH.equals(enumClass.getSuperclass().getCanonicalName())) {
            return resultList;
        }
        // 获取所有public方法
        Method[] methods = enumClass.getMethods();
        List<Field> fieldList = new ArrayList<>();
        //1.通过get方法提取字段,避免get作为自定义方法的开头,建议使用 ‘find’或其余命名
        Arrays.stream(methods)
                .map(Method::getName)
                .filter(
                        methodName -> methodName.startsWith("get") && !"getDeclaringClass".equals(methodName) && !"getClass".equals(methodName)
                ).forEachOrdered(methodName -> {
                    try {
                        Field field = enumClass.getDeclaredField(StringUtils.uncapitalize(methodName.substring(3)));
                        fieldList.add(field);
                    } catch (NoSuchFieldException | SecurityException e) {
                        e.printStackTrace();
                    }
                });

        //2.将字段作为key,逐一把枚举值作为value 存入list
        if (CollectionUtils.isEmpty(fieldList)) {
            return resultList;
        }

        Enum[] enums = (Enum[]) enumClass.getEnumConstants();
        for (Enum anEnum : enums) {
            Map<String, Object> map = new HashMap<>(fieldList.size());
            for (Field field : fieldList) {
                field.setAccessible(true);
                try {
                    // 向map集合添加字段名称 和 字段值
                    map.put(field.getName(), field.get(anEnum));
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            // 将Map添加到集合中
            resultList.add(map);
        }
        return resultList;
    }
}
public interface BaseEnum<T> {

    T getValue();

    String getName();
}

猜你喜欢

转载自blog.csdn.net/gracexiao168/article/details/129079706