diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data')
8 files changed, 149 insertions, 149 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java index dda65a7..ce47eb9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java @@ -71,6 +71,15 @@ class BoundLazy<OldType, BoundContainedType> } @Override + public String toString() { + if (holderBound) { + return boundHolder.toString(); + } + + return "(unmaterialized)"; + } + + @Override public IHolder<BoundContainedType> transform( UnaryOperator<BoundContainedType> transformer) { actions.add(transformer); @@ -87,13 +96,4 @@ class BoundLazy<OldType, BoundContainedType> 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 index 634f2bd..a49bb9a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java @@ -24,6 +24,38 @@ class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> } @Override + public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( + BiFunction<NewLeft, NewRight, IPair<BoundLeft, BoundRight>> bindr) { + IHolder<IPair<NewLeft, NewRight>> newPair = new Identity<>( + boundPair); + IHolder<Boolean> newPairMade = new Identity<>(pairBound); + + Supplier<NewLeft> leftSupp = () -> { + if (!newPairMade.getValue()) { + newPair.replace(binder.apply(leftSupplier.get(), + rightSupplier.get())); + + newPairMade.replace(false); + } + + return newPair.unwrap((pair) -> pair.getLeft()); + }; + + Supplier<NewRight> 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 <BoundLeft> IPair<BoundLeft, NewRight> bindLeft( Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) { Supplier<NewLeft> leftSupp = () -> { @@ -58,38 +90,6 @@ class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> } @Override - public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( - BiFunction<NewLeft, NewRight, IPair<BoundLeft, BoundRight>> bindr) { - IHolder<IPair<NewLeft, NewRight>> newPair = new Identity<>( - boundPair); - IHolder<Boolean> newPairMade = new Identity<>(pairBound); - - Supplier<NewLeft> leftSupp = () -> { - if (!newPairMade.getValue()) { - newPair.replace(binder.apply(leftSupplier.get(), - rightSupplier.get())); - - newPairMade.replace(false); - } - - return newPair.unwrap((pair) -> pair.getLeft()); - }; - - Supplier<NewRight> 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> MergedType merge( BiFunction<NewLeft, NewRight, MergedType> merger) { if (!pairBound) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java index f32f58f..037dddf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java @@ -20,6 +20,34 @@ class HalfBoundLazyPair<OldType, NewLeft, NewRight> } @Override + public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( + BiFunction<NewLeft, NewRight, IPair<BoundLeft, BoundRight>> bindr) { + IHolder<IPair<NewLeft, NewRight>> newPair = new Identity<>( + boundPair); + IHolder<Boolean> newPairMade = new Identity<>(pairBound); + + Supplier<NewLeft> leftSupp = () -> { + if (!newPairMade.getValue()) { + newPair.replace(binder.apply(oldSupplier.get())); + newPairMade.replace(true); + } + + return newPair.unwrap((pair) -> pair.getLeft()); + }; + + Supplier<NewRight> 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 <BoundLeft> IPair<BoundLeft, NewRight> bindLeft( Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) { Supplier<NewLeft> leftSupp = () -> { @@ -52,34 +80,6 @@ class HalfBoundLazyPair<OldType, NewLeft, NewRight> } @Override - public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( - BiFunction<NewLeft, NewRight, IPair<BoundLeft, BoundRight>> bindr) { - IHolder<IPair<NewLeft, NewRight>> newPair = new Identity<>( - boundPair); - IHolder<Boolean> newPairMade = new Identity<>(pairBound); - - Supplier<NewLeft> leftSupp = () -> { - if (!newPairMade.getValue()) { - newPair.replace(binder.apply(oldSupplier.get())); - newPairMade.replace(true); - } - - return newPair.unwrap((pair) -> pair.getLeft()); - }; - - Supplier<NewRight> 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> MergedType merge( BiFunction<NewLeft, NewRight, MergedType> merger) { if (!pairBound) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java index 77eb899..ecf3f14 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -64,6 +64,19 @@ public interface IHolder<ContainedType> { Function<ContainedType, MappedType> mapper); /** + * Replace the held value with a new one + * + * @param newValue + * The value to hold instead + * @return The holder itself + */ + public default IHolder<ContainedType> replace(ContainedType newValue) { + return transform((oldValue) -> { + return newValue; + }); + } + + /** * Transform the value held in this holder * * @param transformer @@ -85,17 +98,4 @@ public interface IHolder<ContainedType> { */ public <UnwrappedType> UnwrappedType unwrap( Function<ContainedType, UnwrappedType> unwrapper); - - /** - * Replace the held value with a new one - * - * @param newValue - * The value to hold instead - * @return The holder itself - */ - public default IHolder<ContainedType> 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 index ce38d60..316074e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -16,6 +16,20 @@ import java.util.function.Function; */ public interface IPair<LeftType, RightType> { /** + * Bind a function across the values in this pair + * + * @param <BoundLeft> + * The type of the bound left + * @param <BoundRight> + * The type of the bound right + * @param binder + * The function to bind with + * @return The bound pair + */ + public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( + BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder); + + /** * Bind a function to the left value in this pair * * @param <BoundLeft> @@ -40,30 +54,19 @@ public interface IPair<LeftType, RightType> { Function<RightType, IPair<LeftType, BoundRight>> rightBinder); /** - * Bind a function across the values in this pair + * Immediately perfom the specified action with the contents of this + * pair * - * @param <BoundLeft> - * The type of the bound left - * @param <BoundRight> - * The type of the bound right - * @param binder - * The function to bind with - * @return The bound pair + * @param consumer + * The action to perform on the pair */ - public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( - BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder); + public default void doWith(BiConsumer<LeftType, RightType> consumer) { + merge((leftValue, rightValue) -> { + consumer.accept(leftValue, rightValue); - /** - * Merge the two values in this pair into a single value - * - * @param <MergedType> - * The type of the single value - * @param merger - * The function to use for merging - * @return The pair, merged into a single value - */ - public <MergedType> MergedType merge( - BiFunction<LeftType, RightType, MergedType> merger); + return null; + }); + } /** * Get the value on the left side of the pair @@ -84,17 +87,14 @@ public interface IPair<LeftType, RightType> { } /** - * Immediately perfom the specified action with the contents of this - * pair + * Merge the two values in this pair into a single value * - * @param consumer - * The action to perform on the pair + * @param <MergedType> + * The type of the single value + * @param merger + * The function to use for merging + * @return The pair, merged into a single value */ - public default void doWith(BiConsumer<LeftType, RightType> consumer) { - merge((leftValue, rightValue) -> { - consumer.accept(leftValue, rightValue); - - return null; - }); - } + public <MergedType> MergedType merge( + BiFunction<LeftType, RightType, MergedType> merger); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java index f4bc24a..061486e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -96,6 +96,19 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { } @Override + public String toString() { + if (valueMaterialized) { + if (actions.isEmpty()) { + return "value[v='" + heldValue + "']"; + } + + return "value[v='" + heldValue + "'] (has pending transforms)"; + } + + return "(unmaterialized)"; + } + + @Override public IHolder<ContainedType> transform( UnaryOperator<ContainedType> transformer) { actions.add(transformer); @@ -120,17 +133,4 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { 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 index d69a7bb..b02d9cb 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -59,6 +59,12 @@ public class LazyPair<LeftType, RightType> } @Override + public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( + BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) { + return new BoundLazyPair<>(leftSupplier, rightSupplier, binder); + } + + @Override public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { Supplier<LeftType> leftSupp = () -> { @@ -87,49 +93,43 @@ public class LazyPair<LeftType, RightType> } @Override - public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( - BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) { - return new BoundLazyPair<>(leftSupplier, rightSupplier, binder); - } - - @Override - public <MergedType> MergedType merge( - BiFunction<LeftType, RightType, MergedType> merger) { + public LeftType getLeft() { if (!leftMaterialized) { leftValue = leftSupplier.get(); leftMaterialized = true; } + return leftValue; + } + + @Override + public RightType getRight() { if (!rightMaterialized) { rightValue = rightSupplier.get(); rightMaterialized = true; } - return merger.apply(leftValue, rightValue); + return rightValue; } @Override - public LeftType getLeft() { + public <MergedType> MergedType merge( + BiFunction<LeftType, RightType, MergedType> merger) { if (!leftMaterialized) { leftValue = leftSupplier.get(); leftMaterialized = true; } - return leftValue; - } - - @Override - public RightType getRight() { if (!rightMaterialized) { rightValue = rightSupplier.get(); rightMaterialized = true; } - return rightValue; + return merger.apply(leftValue, rightValue); } @Override diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java index 66c50de..05955b6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -38,6 +38,12 @@ public class Pair<LeftType, RightType> } @Override + public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( + BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) { + return binder.apply(leftValue, rightValue); + } + + @Override public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { return leftBinder.apply(leftValue); @@ -50,12 +56,6 @@ public class Pair<LeftType, RightType> } @Override - public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( - BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) { - return binder.apply(leftValue, rightValue); - } - - @Override public <MergedType> MergedType merge( BiFunction<LeftType, RightType, MergedType> merger) { return merger.apply(leftValue, rightValue); |
