Skip to content

Instantly share code, notes, and snippets.

@ufuk
Last active July 8, 2018 14:36
Show Gist options
  • Save ufuk/6fe9a87d807d54bcd754 to your computer and use it in GitHub Desktop.
Save ufuk/6fe9a87d807d54bcd754 to your computer and use it in GitHub Desktop.
Getting a random enum value by Enum type
package ...;
import ...RandomUtils;
public final class ArrayUtils {
private ArrayUtils() {
}
public static <T> T random(T[] array) {
return array[RandomUtils.nextInt(0, array.length)];
}
}
package ...;
import ...ArrayUtils;
public final class EnumUtils {
private EnumUtils() {
}
public static <T extends Enum> T random(Class<T> enumType) {
return ArrayUtils.random(enumType.getEnumConstants());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment