From 275a627719fc2231b16caea41130ff09f0f2b6a1 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 8 Apr 2016 13:28:09 -0400 Subject: Switch functional data to use interfaces --- .../main/java/bjc/utils/funcutils/EnumUtils.java | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java new file mode 100644 index 0000000..f490727 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java @@ -0,0 +1,64 @@ +package bjc.utils.funcutils; + +import java.util.Random; +import java.util.function.Consumer; + +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IFunctionalList; + +/** + * Utility methods on enums + * + * @author ben + * + */ +public class EnumUtils { + /** + * Get a random value from an enum + * + * @param + * The type of the enum + * @param enumClass + * The class of the enum + * @param rnd + * The random source to use + * @return A random value from the specified enum + */ + public static > E getRandomValue(Class enumClass, + Random rnd) { + E[] enumValues = enumClass.getEnumConstants(); + + return new FunctionalList<>(enumValues).randItem(rnd); + } + + /** + * Do an action for a random number of enum values + * + * @param + * The type of the enum + * @param enumClass + * 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 > void doForValues(Class enumClass, + int nValues, Consumer action, Random rnd) { + E[] enumValues = enumClass.getEnumConstants(); + + IFunctionalList valueList = new FunctionalList<>(enumValues); + + int randomValueCount = enumValues.length - nValues; + + for (int i = 0; i <= randomValueCount; i++) { + E rDir = valueList.randItem(rnd); + + valueList.removeMatching(rDir); + } + + valueList.forEach(action); + } +} -- cgit v1.2.3