Skip to content

Instantly share code, notes, and snippets.

@DrAzraelTod
Last active April 10, 2018 09:48
Show Gist options
  • Save DrAzraelTod/e5cd0d8abf3ebb65f63c76e95bfd0569 to your computer and use it in GitHub Desktop.
Save DrAzraelTod/e5cd0d8abf3ebb65f63c76e95bfd0569 to your computer and use it in GitHub Desktop.
get a random enum
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);
}
}
@DrAzraelTod
Copy link
Author

retrieves any enum

no, sadly it's not possible to get the class of the expected current return-value (stupid java)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment