Last active
April 10, 2018 09:48
-
-
Save DrAzraelTod/e5cd0d8abf3ebb65f63c76e95bfd0569 to your computer and use it in GitHub Desktop.
get a random enum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static <ENUM_KIND extends Enum<?>> ENUM_KIND rndEnum(final Class<ENUM_KIND> enumType) { | |
final ENUM_KIND[] enumValues = enumType.getEnumConstants(); | |
if (enumValues.length > 0) { | |
int rnd = new Random().nextInt(enumValues.length); | |
return enumValues[rnd]; | |
} else { | |
// yeah, ok.. nobody knows that kind of exception | |
// do something else instead | |
throw EnumNotImplementedException.of(enumType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
retrieves any enum
no, sadly it's not possible to get the class of the expected current return-value (stupid java)