From 77fcc58d1facffbc3af50be8c05985350e9f1355 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 17 Apr 2016 15:01:44 -0400 Subject: Code maintenace and changes --- .../src/main/java/bjc/utils/data/BoundLazy.java | 99 +++++++ .../main/java/bjc/utils/data/BoundLazyPair.java | 104 +++++++ .../java/bjc/utils/data/HalfBoundLazyPair.java | 93 ++++++ .../src/main/java/bjc/utils/data/IHolder.java | 101 +++++++ BJC-Utils2/src/main/java/bjc/utils/data/IPair.java | 97 +++++++ .../src/main/java/bjc/utils/data/Identity.java | 115 ++++++++ BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java | 136 +++++++++ .../src/main/java/bjc/utils/data/LazyPair.java | 134 +++++++++ BJC-Utils2/src/main/java/bjc/utils/data/Pair.java | 63 ++++ .../java/bjc/utils/data/experimental/IHolder.java | 101 ------- .../java/bjc/utils/data/experimental/IPair.java | 97 ------- .../java/bjc/utils/data/experimental/Identity.java | 115 -------- .../java/bjc/utils/data/experimental/Lazy.java | 228 --------------- .../java/bjc/utils/data/experimental/LazyPair.java | 317 --------------------- .../java/bjc/utils/data/experimental/Pair.java | 63 ---- .../bjc/utils/data/experimental/package-info.java | 7 - .../src/main/java/bjc/utils/data/package-info.java | 7 + 17 files changed, 949 insertions(+), 928 deletions(-) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/IPair.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/Identity.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/Pair.java delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/experimental/IHolder.java delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/experimental/IPair.java delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/experimental/Identity.java delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/experimental/Lazy.java delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/experimental/LazyPair.java delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/experimental/Pair.java delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/experimental/package-info.java create mode 100644 BJC-Utils2/src/main/java/bjc/utils/data/package-info.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/data') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java new file mode 100644 index 0000000..dda65a7 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java @@ -0,0 +1,99 @@ +package bjc.utils.data; + +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IFunctionalList; + +class BoundLazy + implements IHolder { + private Supplier> oldSupplier; + + private Function> binder; + + private IHolder boundHolder; + + private boolean holderBound; + + private IFunctionalList> actions = new FunctionalList<>(); + + public BoundLazy(Supplier> supp, + Function> binder) { + oldSupplier = supp; + this.binder = binder; + } + + @Override + public IHolder bind( + Function> bindr) { + IFunctionalList> pendingActions = new FunctionalList<>(); + + actions.forEach(pendingActions::add); + + Supplier> typeSupplier = () -> { + IHolder oldHolder = boundHolder; + + if (!holderBound) { + oldHolder = oldSupplier.get().unwrap(binder); + } + + return pendingActions.reduceAux(oldHolder, (action, state) -> { + return state.transform(action); + }, (value) -> value); + }; + + return new BoundLazy<>(typeSupplier, bindr); + } + + @Override + public IHolder map( + Function mapper) { + IFunctionalList> pendingActions = new FunctionalList<>(); + + actions.forEach(pendingActions::add); + + Supplier typeSupplier = () -> { + IHolder oldHolder = boundHolder; + + if (!holderBound) { + oldHolder = oldSupplier.get().unwrap(binder); + } + + return pendingActions.reduceAux(oldHolder.getValue(), + (action, state) -> { + return action.apply(state); + }, (value) -> mapper.apply(value)); + }; + + return new Lazy<>(typeSupplier); + } + + @Override + public IHolder transform( + UnaryOperator transformer) { + actions.add(transformer); + + return this; + } + + @Override + public UnwrappedType unwrap( + Function unwrapper) { + if (!holderBound) { + boundHolder = oldSupplier.get().unwrap(binder::apply); + } + + return boundHolder.unwrap(unwrapper); + } + + @Override + public String toString() { + if (holderBound) { + return boundHolder.toString(); + } + + return "(unmaterialized)"; + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java new file mode 100644 index 0000000..41ab99b --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java @@ -0,0 +1,104 @@ +package bjc.utils.data; + +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + +class BoundLazyPair + implements IPair { + private Supplier leftSupplier; + private Supplier rightSupplier; + + private BiFunction> binder; + + private IPair boundPair; + + private boolean pairBound; + + public BoundLazyPair(Supplier leftSupp, + Supplier rightSupp, + BiFunction> bindr) { + leftSupplier = leftSupp; + rightSupplier = rightSupp; + binder = bindr; + } + + @Override + public IPair bindLeft( + Function> leftBinder) { + Supplier leftSupp = () -> { + IPair newPair = boundPair; + + if (!pairBound) { + newPair = binder.apply(leftSupplier.get(), + rightSupplier.get()); + } + + return newPair.getLeft(); + }; + + return new HalfBoundLazyPair<>(leftSupp, leftBinder); + } + + @Override + public IPair bindRight( + Function> rightBinder) { + Supplier rightSupp = () -> { + IPair newPair = boundPair; + + if (!pairBound) { + newPair = binder.apply(leftSupplier.get(), + rightSupplier.get()); + } + + return newPair.getRight(); + }; + + return new HalfBoundLazyPair<>(rightSupp, rightBinder); + } + + @Override + public IPair bind( + BiFunction> bindr) { + IHolder> newPair = new Identity<>( + boundPair); + IHolder newPairMade = new Identity<>(pairBound); + + Supplier leftSupp = () -> { + if (!newPairMade.getValue()) { + newPair.replace(binder.apply(leftSupplier.get(), + rightSupplier.get())); + + newPairMade.replace(false); + } + + return newPair.unwrap((pair) -> pair.getLeft()); + }; + + Supplier rightSupp = () -> { + if (!newPairMade.getValue()) { + newPair.replace(binder.apply(leftSupplier.get(), + rightSupplier.get())); + + newPairMade.replace(false); + } + + return newPair.unwrap((pair) -> pair.getRight()); + }; + + return new BoundLazyPair<>(leftSupp, rightSupp, bindr); + } + + @Override + public MergedType merge( + BiFunction merger) { + if (!pairBound) { + boundPair = binder.apply(leftSupplier.get(), + rightSupplier.get()); + + pairBound = true; + } + + return boundPair.merge(merger); + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java new file mode 100644 index 0000000..f32f58f --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java @@ -0,0 +1,93 @@ +package bjc.utils.data; + +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + +class HalfBoundLazyPair + implements IPair { + private Supplier oldSupplier; + + private Function> binder; + + private IPair boundPair; + private boolean pairBound; + + public HalfBoundLazyPair(Supplier oldSupp, + Function> bindr) { + oldSupplier = oldSupp; + binder = bindr; + } + + @Override + public IPair bindLeft( + Function> leftBinder) { + Supplier leftSupp = () -> { + IPair newPair = boundPair; + + if (!pairBound) { + newPair = binder.apply(oldSupplier.get()); + } + + return newPair.getLeft(); + }; + + return new HalfBoundLazyPair<>(leftSupp, leftBinder); + } + + @Override + public IPair bindRight( + Function> rightBinder) { + Supplier rightSupp = () -> { + IPair newPair = boundPair; + + if (!pairBound) { + newPair = binder.apply(oldSupplier.get()); + } + + return newPair.getRight(); + }; + + return new HalfBoundLazyPair<>(rightSupp, rightBinder); + } + + @Override + public IPair bind( + BiFunction> bindr) { + IHolder> newPair = new Identity<>( + boundPair); + IHolder newPairMade = new Identity<>(pairBound); + + Supplier leftSupp = () -> { + if (!newPairMade.getValue()) { + newPair.replace(binder.apply(oldSupplier.get())); + newPairMade.replace(true); + } + + return newPair.unwrap((pair) -> pair.getLeft()); + }; + + Supplier rightSupp = () -> { + if (!newPairMade.getValue()) { + newPair.replace(binder.apply(oldSupplier.get())); + newPairMade.replace(true); + } + + return newPair.unwrap((pair) -> pair.getRight()); + }; + + return new BoundLazyPair<>(leftSupp, rightSupp, bindr); + } + + @Override + public MergedType merge( + BiFunction merger) { + if (!pairBound) { + boundPair = binder.apply(oldSupplier.get()); + + pairBound = true; + } + + return boundPair.merge(merger); + } +} \ No newline at end of file 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..77eb899 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -0,0 +1,101 @@ +package bjc.utils.data; + +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.UnaryOperator; + +/** + * A holder of a single value. + * + * @author ben + * + * @param + * The type of value held + */ +public interface IHolder { + /** + * Bind a function across the value in this container + * + * @param + * The type of value in this container + * @param binder + * The function to bind to the value + * @return A holder from binding the value + */ + public IHolder bind( + Function> binder); + + /** + * Apply an action to the value + * + * @param action + * The action to apply to the value + */ + public default void doWith(Consumer action) { + transform((value) -> { + action.accept(value); + + return value; + }); + } + + /** + * Get the value contained in this holder without changing it. + * + * @return The value held in this holder + */ + public default ContainedType getValue() { + return unwrap((value) -> value); + } + + /** + * Create a new holder with a mapped version of the value in this + * holder. + * + * Does not change the internal state of this holder + * + * @param + * The type of the mapped value + * @param mapper + * The function to do mapping with + * @return A holder with the mapped value + */ + public IHolder map( + Function mapper); + + /** + * Transform the value held in this holder + * + * @param transformer + * The function to transform the value with + * @return The holder itself, for easy chaining + */ + public IHolder transform( + UnaryOperator transformer); + + /** + * Unwrap the value contained in this holder so that it is no longer + * held + * + * @param + * The type of the unwrapped value + * @param unwrapper + * The function to use to unwrap the value + * @return The unwrapped held value + */ + public UnwrappedType unwrap( + Function unwrapper); + + /** + * Replace the held value with a new one + * + * @param newValue + * The value to hold instead + * @return The holder itself + */ + public default IHolder replace(ContainedType newValue) { + return transform((oldValue) -> { + return newValue; + }); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java new file mode 100644 index 0000000..3d4998c --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -0,0 +1,97 @@ +package bjc.utils.data; + +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Function; + +/** + * Represents a pair of values + * + * @author ben + * @param + * The type of the left side of the pair + * @param + * The type of the right side of the pair + * + */ +public interface IPair { + /** + * Bind a function to the left value in this pair + * + * @param + * The type of the bound value + * @param leftBinder + * The function to use to bind + * @return A pair with the left type bound + */ + public IPair bindLeft( + Function> leftBinder); + + /** + * Bind a function to the right value in this pair + * + * @param + * The type of the bound value + * @param rightBinder + * The function to use to bind + * @return A pair with the right type bound + */ + public IPair bindRight( + Function> rightBinder); + + /** + * Bind a function across the values in this pair + * + * @param + * The type of the bound left + * @param + * The type of the bound right + * @param binder + * The function to bind with + * @return The bound pair + */ + public IPair bind( + BiFunction> binder); + + /** + * Merge the two values in this pair into a single value + * + * @param + * The type of the single value + * @param merger + * The function to use for merging + * @return The pair, merged into a single value + */ + public MergedType merge( + BiFunction merger); + + /** + * Get the value on the left side of the pair + * + * @return The value on the left side of the pair + */ + public default LeftType getLeft() { + return merge((leftValue, rightValue) -> leftValue); + } + + /** + * Get the value on the right side of the pair + * + * @return The value on the right side of the pair + */ + public default RightType getRight() { + return merge((leftValue, rightValue) -> rightValue); + } + + /** + * Immediately perfom the specified action with the contents of this pair + * @param consumer The action to perform on the pair + */ + public default void doWith(BiConsumer consumer) { + merge((leftValue, rightValue) -> { + consumer.accept(leftValue, rightValue); + + return null; + }); + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java new file mode 100644 index 0000000..dcc7bef --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java @@ -0,0 +1,115 @@ +package bjc.utils.data; + +import java.util.function.Function; +import java.util.function.UnaryOperator; + +/** + * @author ben + * + * @param + */ +/** + * Simple implementation of IHolder that has no hidden behavior + * + * @author ben + * + * @param + * The type contained in the holder + */ +public class Identity implements IHolder { + private ContainedType heldValue; + + /** + * Create a holder holding null + */ + public Identity() { + heldValue = null; + } + + /** + * Create a holder holding the specified value + * + * @param value + * The value to hold + */ + public Identity(ContainedType value) { + heldValue = value; + } + + @Override + public IHolder bind( + Function> binder) { + return binder.apply(heldValue); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (obj == null) { + return false; + } else if (getClass() != obj.getClass()) { + return false; + } + + Identity other = (Identity) obj; + + if (heldValue == null) { + if (other.heldValue != null) { + return false; + } + } else if (!heldValue.equals(other.heldValue)) { + return false; + } + + return true; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + + int result = 1; + + int fieldHash = (heldValue == null) ? 0 : heldValue.hashCode(); + + result = prime * result + fieldHash; + + return result; + } + + @Override + public IHolder map( + Function mapper) { + return new Identity<>(mapper.apply(heldValue)); + } + + @Override + public String toString() { + return "holding[v=" + heldValue + "]"; + } + + @Override + public IHolder transform( + UnaryOperator transformer) { + heldValue = transformer.apply(heldValue); + + return this; + } + + @Override + public UnwrappedType unwrap( + Function unwrapper) { + return unwrapper.apply(heldValue); + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java new file mode 100644 index 0000000..f4bc24a --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -0,0 +1,136 @@ +package bjc.utils.data; + +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IFunctionalList; + +/** + * A holder that holds a means to create a value, but doesn't actually + * compute the value until it's needed + * + * @author ben + * + * @param + */ +public class Lazy implements IHolder { + private Supplier valueSupplier; + + private IFunctionalList> actions = new FunctionalList<>(); + + private boolean valueMaterialized; + + private ContainedType heldValue; + + /** + * Create a new lazy value from the specified seed value + * + * @param value + * The seed value to use + */ + public Lazy(ContainedType value) { + heldValue = value; + + valueMaterialized = true; + } + + /** + * Create a new lazy value from the specified value source + * + * @param supp + * The source of a value to use + */ + public Lazy(Supplier supp) { + valueSupplier = supp; + + valueMaterialized = false; + } + + private Lazy(Supplier supp, + IFunctionalList> pendingActions) { + valueSupplier = supp; + + actions = pendingActions; + } + + @Override + public IHolder bind( + Function> binder) { + IFunctionalList> pendingActions = new FunctionalList<>(); + + actions.forEach(pendingActions::add); + + Supplier supplier = () -> { + if (valueMaterialized) { + return heldValue; + } + + return valueSupplier.get(); + }; + + return new BoundLazy<>(() -> { + return new Lazy<>(supplier, pendingActions); + }, binder); + } + + @Override + public IHolder map( + Function mapper) { + IFunctionalList> pendingActions = new FunctionalList<>(); + + actions.forEach(pendingActions::add); + + return new Lazy<>(() -> { + ContainedType currVal = heldValue; + + if (!valueMaterialized) { + currVal = valueSupplier.get(); + } + + return pendingActions.reduceAux(currVal, + UnaryOperator::apply, + (value) -> mapper.apply(value)); + }); + } + + @Override + public IHolder transform( + UnaryOperator transformer) { + actions.add(transformer); + + return this; + } + + @Override + public UnwrappedType unwrap( + Function unwrapper) { + if (!valueMaterialized) { + heldValue = valueSupplier.get(); + + valueMaterialized = true; + } + + actions.forEach((action) -> { + heldValue = action.apply(heldValue); + }); + + actions = new FunctionalList<>(); + + return unwrapper.apply(heldValue); + } + + @Override + public String toString() { + if (valueMaterialized) { + if (actions.isEmpty()) { + return "value[v='" + heldValue + "']"; + } + + return "value[v='" + heldValue + "'] (has pending transforms)"; + } + + return "(unmaterialized)"; + } +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java new file mode 100644 index 0000000..fd432c1 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -0,0 +1,134 @@ +package bjc.utils.data; + +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + +/** + * A lazy implementation of a pair + * + * @author ben + * + * @param + * The type on the left side of the pair + * @param + * The type on the right side of the pair + */ +public class LazyPair + implements IPair { + private LeftType leftValue; + private RightType rightValue; + + private Supplier leftSupplier; + private Supplier rightSupplier; + + private boolean leftMaterialized; + private boolean rightMaterialized; + + /** + * Create a new lazy pair, using the set value s + * + * @param leftVal + * The value for the left side of the pair + * @param rightVal + * The value for the right side of the pair + */ + public LazyPair(LeftType leftVal, RightType rightVal) { + leftValue = leftVal; + rightValue = rightVal; + + leftMaterialized = true; + rightMaterialized = true; + } + + /** + * Create a new lazy pair from the given value sources + * + * @param leftSupp + * The source for a value on the left side of the pair + * @param rightSupp + * The source for a value on the right side of the pair + */ + public LazyPair(Supplier leftSupp, + Supplier rightSupp) { + leftSupplier = leftSupp; + rightSupplier = rightSupp; + + leftMaterialized = false; + rightMaterialized = false; + } + + @Override + public IPair bindLeft( + Function> leftBinder) { + Supplier leftSupp = () -> { + if (leftMaterialized) { + return leftValue; + } + + return leftSupplier.get(); + }; + + return new HalfBoundLazyPair<>(leftSupp, leftBinder); + } + + @Override + public IPair bindRight( + Function> rightBinder) { + Supplier rightSupp = () -> { + if (rightMaterialized) { + return rightValue; + } + + return rightSupplier.get(); + }; + + return new HalfBoundLazyPair<>(rightSupp, rightBinder); + } + + @Override + public IPair bind( + BiFunction> binder) { + return new BoundLazyPair<>(leftSupplier, rightSupplier, binder); + } + + @Override + public MergedType merge( + BiFunction merger) { + if (!leftMaterialized) { + leftValue = leftSupplier.get(); + + leftMaterialized = true; + } + + if (!rightMaterialized) { + rightValue = rightSupplier.get(); + + rightMaterialized = true; + } + + return merger.apply(leftValue, rightValue); + } + + @Override + public LeftType getLeft() { + if (!leftMaterialized) { + leftValue = leftSupplier.get(); + + leftMaterialized = true; + } + + return leftValue; + } + + @Override + public RightType getRight() { + if (!rightMaterialized) { + rightValue = rightSupplier.get(); + + rightMaterialized = true; + } + + return rightValue; + } +} 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..958df40 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -0,0 +1,63 @@ +package bjc.utils.data; + +import java.util.function.BiFunction; +import java.util.function.Function; + +/** + * A pair of values, with nothing special about them. + * + * @author ben + * + * @param + * The type of the left value + * @param + * The type of the right value + */ +public class Pair + implements IPair { + private LeftType leftValue; + private RightType rightValue; + + /** + * Create a new pair with both sides set to null + */ + public Pair() { + } + + /** + * Create a new pair with both sides set to the specified values + * + * @param left + * The value of the left side + * @param right + * The value of the right side + */ + public Pair(LeftType left, RightType right) { + leftValue = left; + rightValue = right; + } + + @Override + public IPair bindLeft( + Function> leftBinder) { + return leftBinder.apply(leftValue); + } + + @Override + public IPair bindRight( + Function> rightBinder) { + return rightBinder.apply(rightValue); + } + + @Override + public IPair bind( + BiFunction> binder) { + return binder.apply(leftValue, rightValue); + } + + @Override + public MergedType merge( + BiFunction merger) { + return merger.apply(leftValue, rightValue); + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/IHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/experimental/IHolder.java deleted file mode 100644 index 6ccf115..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/IHolder.java +++ /dev/null @@ -1,101 +0,0 @@ -package bjc.utils.data.experimental; - -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.UnaryOperator; - -/** - * A holder of a single value. - * - * @author ben - * - * @param - * The type of value held - */ -public interface IHolder { - /** - * Bind a function across the value in this container - * - * @param - * The type of value in this container - * @param binder - * The function to bind to the value - * @return A holder from binding the value - */ - public IHolder bind( - Function> binder); - - /** - * Apply an action to the value - * - * @param action - * The action to apply to the value - */ - public default void doWith(Consumer action) { - transform((value) -> { - action.accept(value); - - return value; - }); - } - - /** - * Get the value contained in this holder without changing it. - * - * @return The value held in this holder - */ - public default ContainedType getValue() { - return unwrap((value) -> value); - } - - /** - * Create a new holder with a mapped version of the value in this - * holder. - * - * Does not change the internal state of this holder - * - * @param - * The type of the mapped value - * @param mapper - * The function to do mapping with - * @return A holder with the mapped value - */ - public IHolder map( - Function mapper); - - /** - * Transform the value held in this holder - * - * @param transformer - * The function to transform the value with - * @return The holder itself, for easy chaining - */ - public IHolder transform( - UnaryOperator transformer); - - /** - * Unwrap the value contained in this holder so that it is no longer - * held - * - * @param - * The type of the unwrapped value - * @param unwrapper - * The function to use to unwrap the value - * @return The unwrapped held value - */ - public UnwrappedType unwrap( - Function unwrapper); - - /** - * Replace the held value with a new one - * - * @param newValue - * The value to hold instead - * @return The holder itself - */ - public default IHolder replace(ContainedType newValue) { - return transform((oldValue) -> { - return newValue; - }); - } -} diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/IPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/experimental/IPair.java deleted file mode 100644 index f5ca240..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/IPair.java +++ /dev/null @@ -1,97 +0,0 @@ -package bjc.utils.data.experimental; - -import java.util.function.BiConsumer; -import java.util.function.BiFunction; -import java.util.function.Function; - -/** - * Represents a pair of values - * - * @author ben - * @param - * The type of the left side of the pair - * @param - * The type of the right side of the pair - * - */ -public interface IPair { - /** - * Bind a function to the left value in this pair - * - * @param - * The type of the bound value - * @param leftBinder - * The function to use to bind - * @return A pair with the left type bound - */ - public IPair bindLeft( - Function> leftBinder); - - /** - * Bind a function to the right value in this pair - * - * @param - * The type of the bound value - * @param rightBinder - * The function to use to bind - * @return A pair with the right type bound - */ - public IPair bindRight( - Function> rightBinder); - - /** - * Bind a function across the values in this pair - * - * @param - * The type of the bound left - * @param - * The type of the bound right - * @param binder - * The function to bind with - * @return The bound pair - */ - public IPair bind( - BiFunction> binder); - - /** - * Merge the two values in this pair into a single value - * - * @param - * The type of the single value - * @param merger - * The function to use for merging - * @return The pair, merged into a single value - */ - public MergedType merge( - BiFunction merger); - - /** - * Get the value on the left side of the pair - * - * @return The value on the left side of the pair - */ - public default LeftType getLeft() { - return merge((leftValue, rightValue) -> leftValue); - } - - /** - * Get the value on the right side of the pair - * - * @return The value on the right side of the pair - */ - public default RightType getRight() { - return merge((leftValue, rightValue) -> rightValue); - } - - /** - * Immediately perfom the specified action with the contents of this pair - * @param consumer The action to perform on the pair - */ - public default void doWith(BiConsumer consumer) { - merge((leftValue, rightValue) -> { - consumer.accept(leftValue, rightValue); - - return null; - }); - } -} diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/Identity.java b/BJC-Utils2/src/main/java/bjc/utils/data/experimental/Identity.java deleted file mode 100644 index 1780f2d..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/Identity.java +++ /dev/null @@ -1,115 +0,0 @@ -package bjc.utils.data.experimental; - -import java.util.function.Function; -import java.util.function.UnaryOperator; - -/** - * @author ben - * - * @param - */ -/** - * Simple implementation of IHolder that has no hidden behavior - * - * @author ben - * - * @param - * The type contained in the holder - */ -public class Identity implements IHolder { - private ContainedType heldValue; - - /** - * Create a holder holding null - */ - public Identity() { - heldValue = null; - } - - /** - * Create a holder holding the specified value - * - * @param value - * The value to hold - */ - public Identity(ContainedType value) { - heldValue = value; - } - - @Override - public IHolder bind( - Function> binder) { - return binder.apply(heldValue); - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } else if (obj == null) { - return false; - } else if (getClass() != obj.getClass()) { - return false; - } - - Identity other = (Identity) obj; - - if (heldValue == null) { - if (other.heldValue != null) { - return false; - } - } else if (!heldValue.equals(other.heldValue)) { - return false; - } - - return true; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - final int prime = 31; - - int result = 1; - - int fieldHash = (heldValue == null) ? 0 : heldValue.hashCode(); - - result = prime * result + fieldHash; - - return result; - } - - @Override - public IHolder map( - Function mapper) { - return new Identity<>(mapper.apply(heldValue)); - } - - @Override - public String toString() { - return "holding[v=" + heldValue + "]"; - } - - @Override - public IHolder transform( - UnaryOperator transformer) { - heldValue = transformer.apply(heldValue); - - return this; - } - - @Override - public UnwrappedType unwrap( - Function unwrapper) { - return unwrapper.apply(heldValue); - } -} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/experimental/Lazy.java deleted file mode 100644 index 0d6fcef..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/Lazy.java +++ /dev/null @@ -1,228 +0,0 @@ -package bjc.utils.data.experimental; - -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.function.UnaryOperator; - -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IFunctionalList; - -/** - * A holder that holds a means to create a value, but doesn't actually - * compute the value until it's needed - * - * @author ben - * - * @param - */ -public class Lazy implements IHolder { - private static class BoundLazy - implements IHolder { - private Supplier> oldSupplier; - - private Function> binder; - - private IHolder boundHolder; - - private boolean holderBound; - - private IFunctionalList> actions = new FunctionalList<>(); - - public BoundLazy(Supplier> supp, - Function> binder) { - oldSupplier = supp; - this.binder = binder; - } - - @Override - public IHolder bind( - Function> bindr) { - IFunctionalList> pendingActions = new FunctionalList<>(); - - actions.forEach(pendingActions::add); - - Supplier> typeSupplier = () -> { - IHolder oldHolder = boundHolder; - - if (!holderBound) { - oldHolder = oldSupplier.get().unwrap(binder); - } - - return pendingActions.reduceAux(oldHolder, - (action, state) -> { - return state.transform(action); - }, (value) -> value); - }; - - return new BoundLazy<>(typeSupplier, bindr); - } - - @Override - public IHolder map( - Function mapper) { - IFunctionalList> pendingActions = new FunctionalList<>(); - - actions.forEach(pendingActions::add); - - Supplier typeSupplier = () -> { - IHolder oldHolder = boundHolder; - - if (!holderBound) { - oldHolder = oldSupplier.get().unwrap(binder); - } - - return pendingActions.reduceAux(oldHolder.getValue(), - (action, state) -> { - return action.apply(state); - }, (value) -> mapper.apply(value)); - }; - - return new Lazy<>(typeSupplier); - } - - @Override - public IHolder transform( - UnaryOperator transformer) { - actions.add(transformer); - - return this; - } - - @Override - public UnwrappedType unwrap( - Function unwrapper) { - if (!holderBound) { - boundHolder = oldSupplier.get().unwrap(binder::apply); - } - - return boundHolder.unwrap(unwrapper); - } - - @Override - public String toString() { - if (holderBound) { - return boundHolder.toString(); - } - - return "(unmaterialized)"; - } - } - - private Supplier valueSupplier; - - private IFunctionalList> actions = new FunctionalList<>(); - - private boolean valueMaterialized; - - private ContainedType heldValue; - - /** - * Create a new lazy value from the specified seed value - * - * @param value - * The seed value to use - */ - public Lazy(ContainedType value) { - heldValue = value; - - valueMaterialized = true; - } - - /** - * Create a new lazy value from the specified value source - * - * @param supp - * The source of a value to use - */ - public Lazy(Supplier supp) { - valueSupplier = supp; - - valueMaterialized = false; - } - - private Lazy(Supplier supp, - IFunctionalList> pendingActions) { - valueSupplier = supp; - - actions = pendingActions; - } - - @Override - public IHolder bind( - Function> binder) { - IFunctionalList> pendingActions = new FunctionalList<>(); - - actions.forEach(pendingActions::add); - - Supplier supplier = () -> { - if (valueMaterialized) { - return heldValue; - } - - return valueSupplier.get(); - }; - - return new BoundLazy<>(() -> { - return new Lazy<>(supplier, pendingActions); - }, binder); - } - - @Override - public IHolder map( - Function mapper) { - IFunctionalList> pendingActions = new FunctionalList<>(); - - actions.forEach(pendingActions::add); - - return new Lazy<>(() -> { - ContainedType currVal = heldValue; - - if (!valueMaterialized) { - currVal = valueSupplier.get(); - } - - return pendingActions.reduceAux(currVal, - UnaryOperator::apply, - (value) -> mapper.apply(value)); - }); - } - - @Override - public IHolder transform( - UnaryOperator transformer) { - actions.add(transformer); - - return this; - } - - @Override - public UnwrappedType unwrap( - Function unwrapper) { - if (!valueMaterialized) { - heldValue = valueSupplier.get(); - - valueMaterialized = true; - } - - actions.forEach((action) -> { - heldValue = action.apply(heldValue); - }); - - actions = new FunctionalList<>(); - - return unwrapper.apply(heldValue); - } - - @Override - public String toString() { - if (valueMaterialized) { - if (actions.isEmpty()) { - return "value[v='" + heldValue + "']"; - } - - return "value[v='" + heldValue + "'] (has pending transforms)"; - } - - return "(unmaterialized)"; - } -} diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/experimental/LazyPair.java deleted file mode 100644 index 04a4b61..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/LazyPair.java +++ /dev/null @@ -1,317 +0,0 @@ -package bjc.utils.data.experimental; - -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.Supplier; - -/** - * A lazy implementation of a pair - * - * @author ben - * - * @param - * The type on the left side of the pair - * @param - * The type on the right side of the pair - */ -public class LazyPair - implements IPair { - private static class HalfBoundLazyPair - implements IPair { - private Supplier oldSupplier; - - private Function> binder; - - private IPair boundPair; - private boolean pairBound; - - public HalfBoundLazyPair(Supplier oldSupp, - Function> bindr) { - oldSupplier = oldSupp; - binder = bindr; - } - - @Override - public IPair bindLeft( - Function> leftBinder) { - Supplier leftSupp = () -> { - IPair newPair = boundPair; - - if (!pairBound) { - newPair = binder.apply(oldSupplier.get()); - } - - return newPair.getLeft(); - }; - - return new HalfBoundLazyPair<>(leftSupp, leftBinder); - } - - @Override - public IPair bindRight( - Function> rightBinder) { - Supplier rightSupp = () -> { - IPair newPair = boundPair; - - if (!pairBound) { - newPair = binder.apply(oldSupplier.get()); - } - - return newPair.getRight(); - }; - - return new HalfBoundLazyPair<>(rightSupp, rightBinder); - } - - @Override - public IPair bind( - BiFunction> bindr) { - IHolder> newPair = new Identity<>( - boundPair); - IHolder newPairMade = new Identity<>(pairBound); - - Supplier leftSupp = () -> { - if (!newPairMade.getValue()) { - newPair.replace(binder.apply(oldSupplier.get())); - newPairMade.replace(true); - } - - return newPair.unwrap((pair) -> pair.getLeft()); - }; - - Supplier rightSupp = () -> { - if (!newPairMade.getValue()) { - newPair.replace(binder.apply(oldSupplier.get())); - newPairMade.replace(true); - } - - return newPair.unwrap((pair) -> pair.getRight()); - }; - - return new BoundLazyPair<>(leftSupp, rightSupp, bindr); - } - - @Override - public MergedType merge( - BiFunction merger) { - if (!pairBound) { - boundPair = binder.apply(oldSupplier.get()); - - pairBound = true; - } - - return boundPair.merge(merger); - } - } - - private static class BoundLazyPair - implements IPair { - private Supplier leftSupplier; - private Supplier rightSupplier; - - private BiFunction> binder; - - private IPair boundPair; - - private boolean pairBound; - - public BoundLazyPair(Supplier leftSupp, - Supplier rightSupp, - BiFunction> bindr) { - leftSupplier = leftSupp; - rightSupplier = rightSupp; - binder = bindr; - } - - @Override - public IPair bindLeft( - Function> leftBinder) { - Supplier leftSupp = () -> { - IPair newPair = boundPair; - - if (!pairBound) { - newPair = binder.apply(leftSupplier.get(), - rightSupplier.get()); - } - - return newPair.getLeft(); - }; - - return new HalfBoundLazyPair<>(leftSupp, leftBinder); - } - - @Override - public IPair bindRight( - Function> rightBinder) { - Supplier rightSupp = () -> { - IPair newPair = boundPair; - - if (!pairBound) { - newPair = binder.apply(leftSupplier.get(), - rightSupplier.get()); - } - - return newPair.getRight(); - }; - - return new HalfBoundLazyPair<>(rightSupp, rightBinder); - } - - @Override - public IPair bind( - BiFunction> bindr) { - IHolder> newPair = new Identity<>( - boundPair); - IHolder newPairMade = new Identity<>(pairBound); - - return new BoundLazyPair<>(() -> { - if (!newPairMade.getValue()) { - newPair.replace(binder.apply(leftSupplier.get(), - rightSupplier.get())); - - newPairMade.replace(false); - } - - return newPair.unwrap((pair) -> pair.getLeft()); - }, () -> { - if (!newPairMade.getValue()) { - newPair.replace(binder.apply(leftSupplier.get(), - rightSupplier.get())); - - newPairMade.replace(false); - } - - return newPair.unwrap((pair) -> pair.getRight()); - }, bindr); - } - - @Override - public MergedType merge( - BiFunction merger) { - if (!pairBound) { - boundPair = binder.apply(leftSupplier.get(), - rightSupplier.get()); - - pairBound = true; - } - - return boundPair.merge(merger); - } - } - - private LeftType leftValue; - private RightType rightValue; - - private Supplier leftSupplier; - private Supplier rightSupplier; - - private boolean leftMaterialized; - private boolean rightMaterialized; - - /** - * Create a new lazy pair, using the set value s - * - * @param leftVal - * The value for the left side of the pair - * @param rightVal - * The value for the right side of the pair - */ - public LazyPair(LeftType leftVal, RightType rightVal) { - leftValue = leftVal; - rightValue = rightVal; - - leftMaterialized = true; - rightMaterialized = true; - } - - /** - * Create a new lazy pair from the given value sources - * - * @param leftSupp - * The source for a value on the left side of the pair - * @param rightSupp - * The source for a value on the right side of the pair - */ - public LazyPair(Supplier leftSupp, - Supplier rightSupp) { - leftSupplier = leftSupp; - rightSupplier = rightSupp; - - leftMaterialized = false; - rightMaterialized = false; - } - - @Override - public IPair bindLeft( - Function> leftBinder) { - Supplier leftSupp = () -> { - if (leftMaterialized) { - return leftValue; - } - - return leftSupplier.get(); - }; - - return new HalfBoundLazyPair<>(leftSupp, leftBinder); - } - - @Override - public IPair bindRight( - Function> rightBinder) { - Supplier rightSupp = () -> { - if (rightMaterialized) { - return rightValue; - } - - return rightSupplier.get(); - }; - - return new HalfBoundLazyPair<>(rightSupp, rightBinder); - } - - @Override - public IPair bind( - BiFunction> binder) { - return new BoundLazyPair<>(leftSupplier, rightSupplier, binder); - } - - @Override - public MergedType merge( - BiFunction merger) { - if (!leftMaterialized) { - leftValue = leftSupplier.get(); - - leftMaterialized = true; - } - - if (!rightMaterialized) { - rightValue = rightSupplier.get(); - - rightMaterialized = true; - } - - return merger.apply(leftValue, rightValue); - } - - @Override - public LeftType getLeft() { - if (!leftMaterialized) { - leftValue = leftSupplier.get(); - - leftMaterialized = true; - } - - return leftValue; - } - - @Override - public RightType getRight() { - if (!rightMaterialized) { - rightValue = rightSupplier.get(); - - rightMaterialized = true; - } - - return rightValue; - } -} diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/experimental/Pair.java deleted file mode 100644 index 87378d7..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/Pair.java +++ /dev/null @@ -1,63 +0,0 @@ -package bjc.utils.data.experimental; - -import java.util.function.BiFunction; -import java.util.function.Function; - -/** - * A pair of values, with nothing special about them. - * - * @author ben - * - * @param - * The type of the left value - * @param - * The type of the right value - */ -public class Pair - implements IPair { - private LeftType leftValue; - private RightType rightValue; - - /** - * Create a new pair with both sides set to null - */ - public Pair() { - } - - /** - * Create a new pair with both sides set to the specified values - * - * @param left - * The value of the left side - * @param right - * The value of the right side - */ - public Pair(LeftType left, RightType right) { - leftValue = left; - rightValue = right; - } - - @Override - public IPair bindLeft( - Function> leftBinder) { - return leftBinder.apply(leftValue); - } - - @Override - public IPair bindRight( - Function> rightBinder) { - return rightBinder.apply(rightValue); - } - - @Override - public IPair bind( - BiFunction> binder) { - return binder.apply(leftValue, rightValue); - } - - @Override - public MergedType merge( - BiFunction merger) { - return merger.apply(leftValue, rightValue); - } -} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/package-info.java b/BJC-Utils2/src/main/java/bjc/utils/data/experimental/package-info.java deleted file mode 100644 index 72e1007..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/data/experimental/package-info.java +++ /dev/null @@ -1,7 +0,0 @@ -/** - * Experimental redoing of data structures - * - * @author ben - * - */ -package bjc.utils.data.experimental; \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/package-info.java b/BJC-Utils2/src/main/java/bjc/utils/data/package-info.java new file mode 100644 index 0000000..a6f05dd --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/package-info.java @@ -0,0 +1,7 @@ +/** + * Experimental redoing of data structures + * + * @author ben + * + */ +package bjc.utils.data; \ No newline at end of file -- cgit v1.2.3