0

I have several finite list types that are coded as enums and they all look exactly like this, with the exception, of course, of actual values that vary from one enum to another:

public enum SomeType {

    TYPE_A("A", "Type A Description"),
    TYPE_B("B", "Type B Description"),
    TYPE_C("C", "Type C Description");

    private String key;
    private String label;

    private static Map<String, SomeType> keyMap = new HashMap<String, SomeType>();  

    static {
        for(SomeType type : SomeType.values()) {
            keyMap.put(type.getKey(), type);
        }
    }

    private SomeType(String _key, String _label) {
        this.key = _key;
        this.label = _label;

    }

    public String getKey() {
        return this.key;
    }

    public String getLabel() {
        return this.label;
    }

    public static List<SomeType> getValueList() {
        return Arrays.asList(SomeType.values());
    }

    public static SomeType getByKey(String _key) {
        SomeType result = keyMap.get(_key);

        if(result == null) {
            throw new IllegalArgumentException("Invalid key: " + _key);
        }

        return result;
    }
}

IOW, they should all have the same constructor/format (String _key, String _label) as well as the methods and supporting Map for looking up by String.

Java enums do not support inheritance. Is there a clever workaround whereby I can declare all of the repeated boilerplate in a single base class and then have the enums psudo-inherit them from there?

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • 1
    I guess you mean `public enum ReportType`? – m0skit0 Apr 01 '15 at 21:14
  • What about generics? – m0skit0 Apr 01 '15 at 21:15
  • it's not about extending them, it's about concentrating shared patterns in a single location rather than repeating them in each enum... – amphibient Apr 01 '15 at 21:19
  • 1
    Sorry, then I retract my comment and close vote. – Hovercraft Full Of Eels Apr 01 '15 at 21:21
  • 1
    Actually, Java enums do support inheritance, just not in the form of one enum type extending another; what you *can* do is declare abstract methods in the enum type, which get implemented in each of the enum values; also, enum types can implement interfaces. But the real problem here, I suspect, is that enums are being misused, as "key" and "label" look like they could be the enum "name()" and "toString()" values; and instead of having a "getByKey(String)" method, use the enum values directly (they can be easily stored/recovered from JPA/Hibernate entities, for example). – Rogério Apr 01 '15 at 21:33
  • @Rogério: _declare abstract methods in the enum type, which get implemented in each of the enum values_ - that would produce even more of boilerplate code, wouldn't it? – wypieprz Apr 01 '15 at 22:15
  • @wypieprz I only said that enums allow you to do that; in this particular case, I don't see a reason for it; in other cases, it can be extremely useful. – Rogério Apr 02 '15 at 13:56
  • http://stackoverflow.com/questions/19155405/java-enum-extends-workaround – marcolopes Apr 03 '15 at 03:46

0 Answers0