Last active
July 8, 2018 14:36
-
-
Save ufuk/6fe9a87d807d54bcd754 to your computer and use it in GitHub Desktop.
Getting a random enum value by Enum type
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
package ...; | |
import ...RandomUtils; | |
public final class ArrayUtils { | |
private ArrayUtils() { | |
} | |
public static <T> T random(T[] array) { | |
return array[RandomUtils.nextInt(0, array.length)]; | |
} | |
} |
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
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