Disclaimer: not very strong with generics.
enum WeekDays {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}
enum Months {
JANUARY, FEBRAURY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
}
enum Directions {
East, West, North, South, Up, Down
}
Let's say I have above enums and there is a service which sends me random Strings.
I need to verify if the targeted String response is a valid enum value or not.
I was thinking of coding of something on lines as below:
static <T> boolean isValidEnum(String value, T enumClass ){
try {
enumClass.valueOf(value);
return true;
} catch (Exception e) {
}
return false;
}
The call would be like:
isValidEnum("TUESDAY", WeekDays)
but line enumClass.valueOf(value); is not happy about my function.
Any pointers is appreciated! TIA