From f8215c428f0b46b459c59d0783b4bc4dadfc38a3 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 28 Sep 2015 22:41:55 -0400 Subject: Basic data structure code. Still importing things from the old project. --- .../src/main/java/bjc/utils/data/GenHolder.java | 66 ++++++++++++++++++++++ BJC-Utils2/src/main/java/bjc/utils/data/Pair.java | 54 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/Pair.java (limited to 'BJC-Utils2/src') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java new file mode 100644 index 0000000..f969960 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java @@ -0,0 +1,66 @@ +package bjc.utils.data; + +import java.util.function.Function; + +/** + * Holds a single value of a specific type. + * This is used for indirect references to data, and more specifically + * for accessing non-final variables from a lambda. + * AKA the identity monad + * @author ben + * + * @param The type of the data being held + */ +public class GenHolder { + /** + * The state this holder is responsible for. + */ + public T held; + + /** + * Creates a new empty holder, with its state set to null + */ + public GenHolder() { + held = null; + } + + /** + * Creates a new holder, with its state initialized to the provided value + * + * @param held The state to initialize this holder to. + */ + public GenHolder(T hld) { + held = hld; + } + + /** + * 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 + */ + public GenHolder transform(Function f) { + held = f.apply(held); + + return this; + } + + /** + * 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 + */ + public GenHolder map(Function f) { + return new GenHolder(f.apply(held)); + } + + /** + * 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 + */ + public E unwrap(Function f) { + return f.apply(held); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java new file mode 100644 index 0000000..14ac52d --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -0,0 +1,54 @@ +package bjc.utils.data; + +import java.util.function.BiFunction; +import java.util.function.Function; + +/** + * Holds a pair of values of two different types. + * @author ben + * + * @param The type of the thing held on the left (first) + * @param The type of the thing held on the right (second) + */ +public class Pair { + public L l; + public R r; + + /** + * Create a new pair that holds two nulls. + */ + public Pair() { + + } + + /** + * Create a new pair holding the specified values. + * @param left The value to hold on the left. + * @param right The value to hold on the right. + */ + public Pair(L left, R right) { + l = left; + r = right; + } + + /** + * Create a new pair by applying the given functions to the left/right. + * Does not change the internal contents of this pair. + * @param lf The function to apply to the left value. + * @param rf The function to apply to the right value. + * @return A new pair containing the two modified values. + */ + public Pair apply(Function lf, Function rf) { + return new Pair(lf.apply(l), rf.apply(r)); + } + + /** + * Collapse this pair to a single value. + * Does not change the internal contents of this pair. + * @param bf The function to use to collapse the pair. + * @return The collapsed value. + */ + public E merge(BiFunction bf) { + return bf.apply(l, r); + } +} -- cgit v1.2.3