From a3a95154666eb0fbae948173d13ad72c5509b1c4 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 29 Feb 2016 09:05:36 -0500 Subject: Factored interface out of GenHolder --- .../src/main/java/bjc/utils/data/GenHolder.java | 37 ++++++++---------- .../src/main/java/bjc/utils/data/IHolder.java | 45 ++++++++++++++++++++++ .../java/bjc/utils/funcdata/FunctionalList.java | 3 +- .../main/java/bjc/utils/gen/WeightedRandom.java | 3 +- .../src/main/java/bjc/utils/graph/Graph.java | 5 ++- 5 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java (limited to 'BJC-Utils2/src/main/java') 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 * The type of the data being held */ -public class GenHolder { +public class GenHolder implements IHolder { /** * The state this holder is responsible for. */ @@ -36,39 +38,34 @@ public class GenHolder { 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 GenHolder map(Function f) { + @Override + public IHolder map(Function f) { return new GenHolder(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 transform(Function f) { + @Override + public IHolder transform(Function 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 unwrap(Function 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 + * The type of data being stored + */ +public interface IHolder { + + /** + * 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 + */ + IHolder map(Function 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 transform(Function 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 unwrap(Function 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 implements Cloneable { */ public F reduceAux(T val, BiFunction bf, Function finl) { - GenHolder acum = new GenHolder<>(val); + IHolder 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 { */ public E genVal() { GenHolder v = new GenHolder<>(src.nextInt(totalChance)); - GenHolder res = new GenHolder(); + IHolder res = new GenHolder(); GenHolder 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 { Set visited = new HashSet(); // Start at the initial vertex and visit it - GenHolder src = new GenHolder<>(getInitial()); + IHolder src = new GenHolder<>(getInitial()); visited.add(src.unwrap(vl -> vl)); // Make sure we visit all the nodes @@ -149,7 +150,7 @@ public class Graph { src.unwrap(vl -> vl), tgt, weight))); // Get the edge with the minimum distance - GenHolder> minEdge = new GenHolder<>( + IHolder> minEdge = new GenHolder<>( availEdges.poll()); // Only consider edges where we haven't visited the target of -- cgit v1.2.3