summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java37
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java45
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java3
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java3
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java5
5 files changed, 69 insertions, 24 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java
index 390982a..d6266ca 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java
@@ -7,12 +7,14 @@ import java.util.function.Function;
* references to data, and more specifically for accessing non-final
* variables from a lambda. AKA the identity monad
*
+ * This is an eager variant of {@link IHolder}
+ *
* @author ben
*
* @param <T>
* The type of the data being held
*/
-public class GenHolder<T> {
+public class GenHolder<T> implements IHolder<T> {
/**
* The state this holder is responsible for.
*/
@@ -36,39 +38,34 @@ public class GenHolder<T> {
held = hld;
}
- /**
- * Return the result of applying the given transformation to the held
- * value Doesn't change the held value
+ /*
+ * (non-Javadoc)
*
- * @param f
- * The transformation to apply
- * @return A holder with the transformed value
+ * @see bjc.utils.data.IHolder#map(java.util.function.Function)
*/
- public <NewT> GenHolder<NewT> map(Function<T, NewT> f) {
+ @Override
+ public <NewT> IHolder<NewT> map(Function<T, NewT> f) {
return new GenHolder<NewT>(f.apply(held));
}
- /**
- * Apply the given transformation to the held value. Returns the holder
- * for allowing chaining of transforms
+ /*
+ * (non-Javadoc)
*
- * @param f
- * The transform to apply to the value
- * @return The holder
+ * @see bjc.utils.data.IHolder#transform(java.util.function.Function)
*/
- public GenHolder<T> transform(Function<T, T> f) {
+ @Override
+ public IHolder<T> transform(Function<T, T> f) {
held = f.apply(held);
return this;
}
- /**
- * Returns a raw mapped value, not contained in a GenHolder
+ /*
+ * (non-Javadoc)
*
- * @param f
- * The function to use for mapping the value
- * @return The mapped value outside of a GenHolder
+ * @see bjc.utils.data.IHolder#unwrap(java.util.function.Function)
*/
+ @Override
public <E> E unwrap(Function<T, E> f) {
return f.apply(held);
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java
new file mode 100644
index 0000000..aa7d5bd
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java
@@ -0,0 +1,45 @@
+package bjc.utils.data;
+
+import java.util.function.Function;
+
+/**
+ * Generic interface for things that store a single value in a roughly
+ * monadic fashion
+ *
+ * @author ben
+ *
+ * @param <T>
+ * The type of data being stored
+ */
+public interface IHolder<T> {
+
+ /**
+ * Return the result of applying the given transformation to the held
+ * value Doesn't change the held value
+ *
+ * @param f
+ * The transformation to apply
+ * @return A holder with the transformed value
+ */
+ <NewT> IHolder<NewT> map(Function<T, NewT> f);
+
+ /**
+ * Apply the given transformation to the held value. Returns the holder
+ * for allowing chaining of transforms
+ *
+ * @param f
+ * The transform to apply to the value
+ * @return The holder
+ */
+ IHolder<T> transform(Function<T, T> f);
+
+ /**
+ * Returns a raw mapped value, not contained in a GenHolder
+ *
+ * @param f
+ * The function to use for mapping the value
+ * @return The mapped value outside of a GenHolder
+ */
+ <E> E unwrap(Function<T, E> f);
+
+} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
index d33ec28..3fd05da 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
@@ -13,6 +13,7 @@ import java.util.function.Function;
import java.util.function.Predicate;
import bjc.utils.data.GenHolder;
+import bjc.utils.data.IHolder;
import bjc.utils.data.Pair;
/**
@@ -350,7 +351,7 @@ public class FunctionalList<E> implements Cloneable {
*/
public <T, F> F reduceAux(T val, BiFunction<E, T, T> bf,
Function<T, F> finl) {
- GenHolder<T> acum = new GenHolder<>(val);
+ IHolder<T> acum = new GenHolder<>(val);
wrap.forEach(e -> acum.transform((vl) -> bf.apply(e, vl)));
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
index 649a240..bf3b4b6 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java
@@ -3,6 +3,7 @@ package bjc.utils.gen;
import java.util.Random;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.data.GenHolder;
+import bjc.utils.data.IHolder;
import bjc.utils.data.Pair;
/**
@@ -68,7 +69,7 @@ public class WeightedRandom<E> {
*/
public E genVal() {
GenHolder<Integer> v = new GenHolder<>(src.nextInt(totalChance));
- GenHolder<E> res = new GenHolder<E>();
+ IHolder<E> res = new GenHolder<E>();
GenHolder<Boolean> bl = new GenHolder<>(true);
probs.forEachIndexed((i, p) -> {
diff --git a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java
index ca3d06f..3b54b27 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/graph/Graph.java
@@ -14,6 +14,7 @@ import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import bjc.utils.data.GenHolder;
+import bjc.utils.data.IHolder;
/**
* A directed weighted graph, where the vertices have some arbitrary label
@@ -136,7 +137,7 @@ public class Graph<T> {
Set<T> visited = new HashSet<T>();
// Start at the initial vertex and visit it
- GenHolder<T> src = new GenHolder<>(getInitial());
+ IHolder<T> src = new GenHolder<>(getInitial());
visited.add(src.unwrap(vl -> vl));
// Make sure we visit all the nodes
@@ -149,7 +150,7 @@ public class Graph<T> {
src.unwrap(vl -> vl), tgt, weight)));
// Get the edge with the minimum distance
- GenHolder<Edge<T>> minEdge = new GenHolder<>(
+ IHolder<Edge<T>> minEdge = new GenHolder<>(
availEdges.poll());
// Only consider edges where we haven't visited the target of