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?