0
点赞
收藏
分享

微信扫一扫

获取枚举实例的方法

橙子好吃吗 2021-10-15 阅读 32
技术分享
public class EnumUtil {

    /**
     * 传入的参数ordinal指的是需要的枚举值在设定的枚举类中的顺序,以0开始
      *
     * @param clazz
     * @param ordinal
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>> T indexOf(Class<T> clazz, int ordinal) {
        return clazz.getEnumConstants()[ordinal];
    }

    /**
     * 传入的参数name指的是枚举值的名称,一般是大写加下划线的
     *
     * @param clazz
     * @param name
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>> T nameOf(Class<T> clazz, String name) {
        return (T) Enum.valueOf(clazz, name);
    }

    /**
     * 使用枚举类型对应的typeCode获取枚举类型
     *
     * @param clazz
     * @param getTypeCodeMethodName
     * @param typeCode
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>> T getByStringTypeCode(Class<T> clazz, String getTypeCodeMethodName, String typeCode) {
        T result = null;
        try {
            T[] arr = clazz.getEnumConstants();
            Method targetMethod = clazz.getDeclaredMethod(getTypeCodeMethodName);
            String typeCodeVal = null;
            for (T entity : arr) {
                typeCodeVal = targetMethod.invoke(entity).toString();
                if (typeCodeVal.contentEquals(typeCode)) {
                    result = entity;
                    break;
                }
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 使用枚举类型对应的typeCode获取枚举类型
     *
     * @param clazz
     * @param getTypeCodeMethodName
     * @param typeCode
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>> T getByIntegerTypeCode(Class<T> clazz, String getTypeCodeMethodName, Integer typeCode) {
        T result = null;
        try {
            T[] arr = clazz.getEnumConstants();
            Method targetMethod = clazz.getDeclaredMethod(getTypeCodeMethodName);
            Integer typeCodeVal = null;
            for (T entity : arr) {
                typeCodeVal = Integer.valueOf(targetMethod.invoke(entity).toString());
                if (typeCodeVal.equals(typeCode)) {
                    result = entity;
                    break;
                }
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 使用枚举类型对应的typeName获取枚举类型
     *
     * @param clazz
     * @param getTypeNameMethodName
     * @param typeName
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>> T getByStringTypeName(Class<T> clazz, String getTypeNameMethodName, String typeName) {
        T result = null;
        try {
            T[] arr = clazz.getEnumConstants();
            Method targetMethod = clazz.getDeclaredMethod(getTypeNameMethodName);
            String typeNameVal = null;
            for (T entity : arr) {
                typeNameVal = targetMethod.invoke(entity).toString();
                if (typeNameVal.contentEquals(typeName)) {
                    result = entity;
                    break;
                }
            }
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
        }
        return result;
    }
}

测试:
@ToString
@AllArgsConstructor
@NoArgsConstructor
public enum EnumDemo {

    /**
     * 南京
     */
    NAN_JING("1001", "南京"),

    /**
     * 北京
     */
    SU_ZHOU("1002", "北京"),

    /**
     * 上海
     */
    YANG_ZHOU("1003", "上海");

    private String code;

    private String name;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@Test
public void testIndexOf() {
    EnumDemo enumDemo = EnumUtil.indexOf(EnumDemo.class, 0);
    System.out.println(enumDemo);
}

@Test
public void testNameOf() {
    EnumDemo enumDemo = EnumUtil.nameOf(EnumDemo.class, "NAN_JING");
    System.out.println(enumDemo);
}

@Test
public void testGetByStringTypeCode() {
    EnumDemo enumDemo = EnumUtil.getByStringTypeCode(EnumDemo.class, "getCode", "1001");
    System.out.println(enumDemo);
}

@Test
public void testGetByIntegerTypeCode() {
    EnumDemo enumDemo = EnumUtil.getByIntegerTypeCode(EnumDemo.class, "getCode", 1001);
    System.out.println(enumDemo);
}

@Test
public void testGetByStringTypeName() {
    EnumDemo enumDemo = EnumUtil.getByStringTypeName(EnumDemo.class, "getName", "苏州");
    System.out.println(enumDemo);
}
举报

相关推荐

0 条评论