diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-10-08 22:39:59 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-10-08 22:39:59 -0300 |
| commit | c82e3b3b2de0633317ec8fc85925e91422820597 (patch) | |
| tree | 96567416ce23c5ce85601f9cedc3a94bb1c55cba /base/src/main/java/bjc/utils/funcutils/EnumUtils.java | |
| parent | b3ac1c8690c3e14c879913e5dcc03a5f5e14876e (diff) | |
Start splitting into maven modules
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils/EnumUtils.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/funcutils/EnumUtils.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/EnumUtils.java b/base/src/main/java/bjc/utils/funcutils/EnumUtils.java new file mode 100644 index 0000000..e4c0bda --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/EnumUtils.java @@ -0,0 +1,63 @@ +package bjc.utils.funcutils; + +import java.util.Random; +import java.util.function.Consumer; + +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; + +/** + * Utility methods on enums + * + * @author ben + * + */ +public class EnumUtils { + /** + * Do an action for a random number of enum values + * + * @param <E> + * The type of the enum + * @param clasz + * The enum class + * @param nValues + * The number of values to execute the action on + * @param action + * The action to perform on random values + * @param rnd + * The source of randomness to use + */ + public static <E extends Enum<E>> void doForValues(final Class<E> clasz, final int nValues, + final Consumer<E> action, final Random rnd) { + final E[] enumValues = clasz.getEnumConstants(); + + final IList<E> valueList = new FunctionalList<>(enumValues); + + final int randomValueCount = enumValues.length - nValues; + + for (int i = 0; i <= randomValueCount; i++) { + final E rDir = valueList.randItem(rnd::nextInt); + + valueList.removeMatching(rDir); + } + + valueList.forEach(action); + } + + /** + * Get a random value from an enum + * + * @param <E> + * The type of the enum + * @param clasz + * The class of the enum + * @param rnd + * The random source to use + * @return A random value from the specified enum + */ + public static <E extends Enum<E>> E getRandomValue(final Class<E> clasz, final Random rnd) { + final E[] enumValues = clasz.getEnumConstants(); + + return new FunctionalList<>(enumValues).randItem(rnd::nextInt); + } +} |
