summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-08 13:28:09 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-08 13:28:09 -0400
commit275a627719fc2231b16caea41130ff09f0f2b6a1 (patch)
tree757e8ca2061ba6ed9b2063f7155edbe954b72bdb /BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java
parent79d3a4a47cbc1fcf17c77c6fc12ff826a3077bac (diff)
Switch functional data to use interfaces
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/EnumUtils.java64
1 files changed, 64 insertions, 0 deletions
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 <E>
+ * 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 extends Enum<E>> E getRandomValue(Class<E> enumClass,
+ Random rnd) {
+ E[] enumValues = enumClass.getEnumConstants();
+
+ return new FunctionalList<>(enumValues).randItem(rnd);
+ }
+
+ /**
+ * Do an action for a random number of enum values
+ *
+ * @param <E>
+ * 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 <E extends Enum<E>> void doForValues(Class<E> enumClass,
+ int nValues, Consumer<E> action, Random rnd) {
+ E[] enumValues = enumClass.getEnumConstants();
+
+ IFunctionalList<E> 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);
+ }
+}