I have a couple of enums which I want to make available to a web-API like
/get/enum-values?type=<enum-Name>
Enums are as follows:
public enum Color {RED, GREEN, BLUE};
public enum Status {OPEN, DONE, CLOSED};
public enum Type {CAR, BIKE, ON_FOOT};
And the code I want to write for generically handling all of these enums in the web-API is as follows:
Class<?>[] enumsArray = new Class<?>[] {
Color.class, Status.class, Type.class
};
List<String> getValuesForEnum(String enumName) {
List<String> returnValue = new ArrayList<>();
// Loop over the above array to find enum corresponding to the argument
for (Object e: foundEnum.values()) {
returnValue.add(e.toString());
}
return returnValue;
}
The above function does not compile because I am not able to treat the enums generically. Is there any way to make this work?
I do not want to fallback on treating each enum independently.
Perhaps I can extend all the enums from some common enum?
Map<String, List<Enum<?>>>(or maybeMap<String, List<String>>instead of just aList