迭代genric枚举实例

时间:2018-02-06 05:42:43

标签: java generics enums

如下所示,我有三个声明的枚举,每个类都有一个名为getEnumByName()的方法,它恢复一个名称并返回具有该名称的枚举。

我注意到我在每个枚举上复制了此方法的相同功能。

有没有办法将此方法更改为通用方法,它接收给定的枚举类型并执行相同的逻辑?

public class Enums {

    public enum A {
        APPLY("Apply", "abcde");

        private String id;
        private String name;

        A(String name, String id) {
            this.name = name;
            this.id = id;
        }

        public static A getEnumByName(String name) throws Exception {
            for (A instance : A.values()) {
                if (instance.getName().equals(name)) return instance;
            }
            throw new Exception("There is no operations matches :" + name);
        }

        public String getName() {
            return name;
        }

        public String getId() {
            return id;
        }
    }

    public enum B {
        APPLY("Apply", "1"),
        SAVE("Save", "2"),
        REVERT("Revert", "2"),
        REVERT_CHILD("Revert->Revert", "4"),
        REVERT_APPLY("Revert->Revert Apply", "5"),
        SYNC("Sync", "6"),
        OPERATIONS("Operations", "7"),
        IMPORT("Import", "8"),
        EXPORT("Export", "9"),
        DIFF("Diff", "10");

        private String id;
        private String name;

        B(String name, String id) {
            this.name = name;
            this.id = id;
        }

        public static B getEnumByName(String name) throws Exception {
            for (B instance : B.values()) {
                if (instance.getName().equals(name)) return instance;
            }
            throw new Exception("There is no operations matches :" + name);
        }

        public String getName() {
            return name;
        }

        public String getId() {
            return id;
        }
    }

    public enum C {
        UPDATE_POLICES("Update Policies", "A"),
        OPERATIONS("Operations", "B"),
        IMPORT_CONFIGURATION_FILE("Import Configuration File", "c"),
        EXPORT_CONFIGURATION_FILE("Export Configuration File", "d"),
        EXPORT_LOG_SUPPORT_FILE("Export Log Support File", "f"),
        EXPORT_TECHNICAL_SUPPORT_FILE("Export Technical Support File", "g"),
        UPDATE_SOFTWARE_VERSION("Update Software Version", "g"),
        UPDATE_SECURITY_SINGAUTES("Update Security Signatures", "h"),
        DIFF("Diff", "k");

        private String id;
        private String name;

        C(String name, String id) {
            this.name = name;
            this.id = id;
        }

        public static C getEnumByName(String name) throws Exception {
            for (C instance : C.values()) {
                if (instance.getName().equals(name)) return instance;
            }
            throw new Exception("There is no operations matches :" + name);
        }

        public String getName() {
            return name;
        }

        public String getId() {
            return id;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

一种选择是让它们都实现一个名为Named的公共接口:

interface Named {
    String getName();
}

现在您可以创建如下通用方法:

static <E extends Enum<E> & Named> E getEnumByName(Class<E> enumClass, String name) throws Exception {
    return Arrays.stream(enumClass.getEnumConstants())
            .filter(e -> e.getName().equals(name))
            .findAny()
            .orElseThrow(() -> new Exception("There is no operations matches :" + name));
}

并称之为:

A a = getEnumByName(A.class, "Apply");

答案 1 :(得分:1)

考虑使用静态枚举valueOf()方法。您可以按如下方式调用它,或直接调用它。有关详细信息,请参阅this answer

static <E extends Enum<E>> E getEnumByName(Class<E> enumClass, String name) {
    return Enum.valueOf(enumClass, name);
}