I've found using a static initialization block in an Enum to be great for implementing a custom valueOf function as described here.
public static RandomEnum getEnum(String strVal) {
return strValMap.get(strVal);
}
private static final Map<String, RandomEnum> strValMap;
static {
final Map<String, RandomEnum> tmpMap = Maps.newHashMap();
for(final RandomEnum en : RandomEnum.values()) {
tmpMap.put(en.strVal, en);
}
strValMap = ImmutableMap.copyOf(tmpMap);
}
Now, I have about two dozen of Enum classes and I want to add a custom valueOf for all of them -- is there a way to do that without copy-pasting it into every single type/file?