diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-28 16:44:36 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-07-28 16:44:36 -0400 |
| commit | dca8e9f586fd595a7995f07788318fb92b8cce79 (patch) | |
| tree | 4fdf216d4a30c2c663d4a429f79cfa471c8579c4 /BJC-Utils2/src/main/java/bjc/utils/data | |
| parent | b1317e5e62bb044cd8a676cb3fc2da86e9922caf (diff) | |
Format/Cleanup pass
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data')
16 files changed, 334 insertions, 349 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 1256e31..b6cd715 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java @@ -15,28 +15,27 @@ class BoundLazy<OldType, BoundContainedType> /* * The old value */ - private Supplier<IHolder<OldType>> oldSupplier; + private Supplier<IHolder<OldType>> oldSupplier; /* * The function to use to transform the old value into a new value */ - private Function<OldType, IHolder<BoundContainedType>> binder; + private Function<OldType, IHolder<BoundContainedType>> binder; /* * The bound value being held */ - private IHolder<BoundContainedType> boundHolder; + private IHolder<BoundContainedType> boundHolder; /* * Whether the bound value has been actualized or not */ - private boolean holderBound; + private boolean holderBound; /* * Transformations currently pending on the bound value */ - private IList<UnaryOperator<BoundContainedType>> actions = - new FunctionalList<>(); + private IList<UnaryOperator<BoundContainedType>> actions = new FunctionalList<>(); /* * Create a new bound lazy value @@ -48,13 +47,12 @@ class BoundLazy<OldType, BoundContainedType> } @Override - public <BoundType> IHolder<BoundType> - bind(Function<BoundContainedType, IHolder<BoundType>> bindr) { + public <BoundType> IHolder<BoundType> bind( + Function<BoundContainedType, IHolder<BoundType>> bindr) { /* * Prepare a list of pending actions */ - IList<UnaryOperator<BoundContainedType>> pendingActions = - new FunctionalList<>(); + IList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); /* @@ -82,11 +80,18 @@ class BoundLazy<OldType, BoundContainedType> } @Override - public <MappedType> IHolder<MappedType> - map(Function<BoundContainedType, MappedType> mapper) { + public <NewType> Function<BoundContainedType, IHolder<NewType>> lift( + Function<BoundContainedType, NewType> func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + + @Override + public <MappedType> IHolder<MappedType> map( + Function<BoundContainedType, MappedType> mapper) { // Prepare a list of pending actions - IList<UnaryOperator<BoundContainedType>> pendingActions = - new FunctionalList<>(); + IList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); // Prepare the new supplier @@ -117,28 +122,20 @@ class BoundLazy<OldType, BoundContainedType> } @Override - public IHolder<BoundContainedType> - transform(UnaryOperator<BoundContainedType> transformer) { + public IHolder<BoundContainedType> transform( + UnaryOperator<BoundContainedType> transformer) { actions.add(transformer); return this; } @Override - public <UnwrappedType> UnwrappedType - unwrap(Function<BoundContainedType, UnwrappedType> unwrapper) { + public <UnwrappedType> UnwrappedType unwrap( + Function<BoundContainedType, UnwrappedType> unwrapper) { if (!holderBound) { boundHolder = oldSupplier.get().unwrap(binder::apply); } return boundHolder.unwrap(unwrapper); } - - @Override - public <NewType> Function<BoundContainedType, IHolder<NewType>> - lift(Function<BoundContainedType, NewType> func) { - return (val) -> { - return new Lazy<>(func.apply(val)); - }; - } }
\ 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 2b20349..c527d94 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java @@ -90,25 +90,17 @@ class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> } @Override - public <MergedType> MergedType merge( - BiFunction<NewLeft, NewRight, MergedType> merger) { - if (!pairBound) { - boundPair = binder.apply(leftSupplier.get(), - rightSupplier.get()); - - pairBound = true; - } - - return boundPair.merge(merger); - } - - @Override - public String toString() { - if (pairBound) { - return boundPair.toString(); - } - - return "(un-materialized)"; + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return bind((leftVal, rightVal) -> { + return new LazyPair<>( + leftCombiner.apply(leftVal, otherLeft), + rightCombiner.apply(rightVal, otherRight)); + }); + }); } @Override @@ -168,16 +160,24 @@ class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - IPair<OtherLeft, OtherRight> otherPair, - BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); - }); - }); + public <MergedType> MergedType merge( + BiFunction<NewLeft, NewRight, MergedType> merger) { + if (!pairBound) { + boundPair = binder.apply(leftSupplier.get(), + rightSupplier.get()); + + pairBound = true; + } + + return boundPair.merge(merger); + } + + @Override + public String toString() { + if (pairBound) { + return boundPair.toString(); + } + + return "(un-materialized)"; } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java index 85ec8f6..fe47dcc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundListHolder.java @@ -8,8 +8,7 @@ import bjc.utils.funcdata.IList; class BoundListHolder<ContainedType> implements IHolder<ContainedType> { private IList<IHolder<ContainedType>> heldHolders; - public BoundListHolder( - IList<IHolder<ContainedType>> toHold) { + public BoundListHolder(IList<IHolder<ContainedType>> toHold) { heldHolders = toHold; } @@ -25,6 +24,14 @@ class BoundListHolder<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new ListHolder<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { IList<IHolder<MappedType>> mappedHolders = heldHolders @@ -50,12 +57,4 @@ class BoundListHolder<ContainedType> implements IHolder<ContainedType> { Function<ContainedType, UnwrappedType> unwrapper) { return heldHolders.randItem().unwrap(unwrapper); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> func) { - return (val) -> { - return new ListHolder<>(func.apply(val)); - }; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java index 8787888..9418882 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java @@ -15,21 +15,6 @@ import java.util.function.Function; */ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { - private LeftType leftVal; - private RightType rightVal; - - private boolean isLeft; - - private Either(LeftType left, RightType right) { - if (left == null) { - rightVal = right; - } else { - leftVal = left; - - isLeft = true; - } - } - /** * Create a new either with the left value occupied * @@ -41,11 +26,10 @@ public class Either<LeftType, RightType> * The value to put on the left * @return An either with the left side occupied */ - public static <LeftType, RightType> Either<LeftType, RightType> - fromLeft(LeftType left) { + public static <LeftType, RightType> Either<LeftType, RightType> fromLeft( + LeftType left) { return new Either<>(left, null); } - /** * Create a new either with the right value occupied * @@ -57,11 +41,27 @@ public class Either<LeftType, RightType> * The value to put on the right * @return An either with the right side occupied */ - public static <LeftType, RightType> Either<LeftType, RightType> - fromRight(RightType right) { + public static <LeftType, RightType> Either<LeftType, RightType> fromRight( + RightType right) { return new Either<>(null, right); } + private LeftType leftVal; + + private RightType rightVal; + + private boolean isLeft; + + private Either(LeftType left, RightType right) { + if (left == null) { + rightVal = right; + } else { + leftVal = left; + + isLeft = true; + } + } + @Override public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) { @@ -89,8 +89,26 @@ public class Either<LeftType, RightType> } @Override - public <NewLeft> IPair<NewLeft, RightType> - mapLeft(Function<LeftType, NewLeft> mapper) { + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { + if (isLeft) { + return otherPair.bind((otherLeft, otherRight) -> { + return new Either<>(leftCombiner.apply(leftVal, otherLeft), + null); + }); + } + + return otherPair.bind((otherLeft, otherRight) -> { + return new Either<>(null, + rightCombiner.apply(rightVal, otherRight)); + }); + } + + @Override + public <NewLeft> IPair<NewLeft, RightType> mapLeft( + Function<LeftType, NewLeft> mapper) { if (isLeft) { return new Either<>(mapper.apply(leftVal), null); } @@ -99,8 +117,8 @@ public class Either<LeftType, RightType> } @Override - public <NewRight> IPair<LeftType, NewRight> - mapRight(Function<RightType, NewRight> mapper) { + public <NewRight> IPair<LeftType, NewRight> mapRight( + Function<RightType, NewRight> mapper) { if (isLeft) { return new Either<>(leftVal, null); } @@ -109,27 +127,8 @@ public class Either<LeftType, RightType> } @Override - public <MergedType> MergedType - merge(BiFunction<LeftType, RightType, MergedType> merger) { + public <MergedType> MergedType merge( + BiFunction<LeftType, RightType, MergedType> merger) { return merger.apply(leftVal, rightVal); } - - @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> - IPair<CombinedLeft, CombinedRight> - combine(IPair<OtherLeft, OtherRight> otherPair, - BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { - if (isLeft) { - return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(leftCombiner.apply(leftVal, otherLeft), - null); - }); - } - - return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(null, - rightCombiner.apply(rightVal, otherRight)); - }); - } } 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 198dd96..d91ede2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/HalfBoundLazyPair.java @@ -80,20 +80,22 @@ class HalfBoundLazyPair<OldType, NewLeft, NewRight> } @Override - public <MergedType> MergedType - merge(BiFunction<NewLeft, NewRight, MergedType> merger) { - if (!pairBound) { - boundPair = binder.apply(oldSupplier.get()); - - pairBound = true; - } - - return boundPair.merge(merger); + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return bind((leftVal, rightVal) -> { + return new LazyPair<>( + leftCombiner.apply(leftVal, otherLeft), + rightCombiner.apply(rightVal, otherRight)); + }); + }); } @Override - public <NewLeftType> IPair<NewLeftType, NewRight> - mapLeft(Function<NewLeft, NewLeftType> mapper) { + public <NewLeftType> IPair<NewLeftType, NewRight> mapLeft( + Function<NewLeft, NewLeftType> mapper) { Supplier<NewLeftType> leftSupp = () -> { if (pairBound) { return mapper.apply(boundPair.getLeft()); @@ -116,8 +118,8 @@ class HalfBoundLazyPair<OldType, NewLeft, NewRight> } @Override - public <NewRightType> IPair<NewLeft, NewRightType> - mapRight(Function<NewRight, NewRightType> mapper) { + public <NewRightType> IPair<NewLeft, NewRightType> mapRight( + Function<NewRight, NewRightType> mapper) { Supplier<NewLeft> leftSupp = () -> { if (pairBound) { return boundPair.getLeft(); @@ -138,19 +140,16 @@ class HalfBoundLazyPair<OldType, NewLeft, NewRight> return new LazyPair<>(leftSupp, rightSupp); } - + @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> - IPair<CombinedLeft, CombinedRight> - combine(IPair<OtherLeft, OtherRight> otherPair, - BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); - }); - }); + public <MergedType> MergedType merge( + BiFunction<NewLeft, NewRight, MergedType> 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 index 58ae5db..b2e4369 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -25,8 +25,8 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * The function to bind to the value * @return A holder from binding the value */ - public <BoundType> IHolder<BoundType> - bind(Function<ContainedType, IHolder<BoundType>> binder); + public <BoundType> IHolder<BoundType> bind( + Function<ContainedType, IHolder<BoundType>> binder); /** * Apply an action to the value @@ -43,9 +43,8 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { } @Override - default <ArgType, ReturnType> - Function<Functor<ArgType>, Functor<ReturnType>> - fmap(Function<ArgType, ReturnType> func) { + default <ArgType, ReturnType> Function<Functor<ArgType>, Functor<ReturnType>> fmap( + Function<ArgType, ReturnType> func) { return (argumentFunctor) -> { if (!(argumentFunctor instanceof IHolder<?>)) { throw new IllegalArgumentException( @@ -72,8 +71,8 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * The function to lift over the holder * @return The function lifted over the holder */ - public <NewType> Function<ContainedType, IHolder<NewType>> - lift(Function<ContainedType, NewType> func); + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func); /** * Make this holder lazy @@ -114,8 +113,8 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * The function to do mapping with * @return A holder with the mapped value */ - public <MappedType> IHolder<MappedType> - map(Function<ContainedType, MappedType> mapper); + public <MappedType> IHolder<MappedType> map( + Function<ContainedType, MappedType> mapper); /** * Replace the held value with a new one @@ -137,8 +136,8 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * The function to transform the value with * @return The holder itself, for easy chaining */ - public IHolder<ContainedType> - transform(UnaryOperator<ContainedType> transformer); + public IHolder<ContainedType> transform( + UnaryOperator<ContainedType> transformer); /** * Unwrap the value contained in this holder so that it is no longer @@ -150,6 +149,6 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * The function to use to unwrap the value * @return The unwrapped held value */ - public <UnwrappedType> UnwrappedType - unwrap(Function<ContainedType, UnwrappedType> unwrapper); + public <UnwrappedType> UnwrappedType unwrap( + Function<ContainedType, UnwrappedType> unwrapper); } 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 f94d656..a2a635f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -57,6 +57,46 @@ public interface IPair<LeftType, RightType> Function<RightType, IPair<LeftType, BoundRight>> rightBinder); /** + * Pairwise combine two pairs together + * + * @param <OtherLeft> + * The left type of the other pair + * @param <OtherRight> + * The right type of the other pair + * @param otherPair + * The pair to combine with + * @return The pairs, pairwise combined together + */ + public default <OtherLeft, OtherRight> IPair<IPair<LeftType, OtherLeft>, IPair<RightType, OtherRight>> combine( + IPair<OtherLeft, OtherRight> otherPair) { + return combine(otherPair, + (left, otherLeft) -> new Pair<>(left, otherLeft), + (right, otherRight) -> new Pair<>(right, otherRight)); + } + + /** + * Combine the contents of two pairs together + * + * @param <OtherLeft> + * The type of the left value of the other pair + * @param <OtherRight> + * The type of the right value of the other pair + * @param <CombinedLeft> + * The type of the left value of the combined pair + * @param <CombinedRight> + * The type of the right value of the combined pair + * @param otherPair + * The other pair to combine with + * @param leftCombiner + * @param rightCombiner + * @return A pair with its values combined + */ + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<RightType, OtherRight, CombinedRight> rightCombiner); + + /** * Immediately perfom the specified action with the contents of this * pair * @@ -72,25 +112,22 @@ public interface IPair<LeftType, RightType> } @Override - default <OldLeft, OldRight, NewLeft> - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> - fmapLeft(Function<OldLeft, NewLeft> func) { + default <OldLeft, OldRight, NewLeft> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> fmapLeft( + Function<OldLeft, NewLeft> func) { return (argumentPair) -> { if (!(argumentPair instanceof IPair<?, ?>)) { throw new IllegalArgumentException( "This function can only be applied to instances of IPair"); } - IPair<OldLeft, OldRight> argPair = - (IPair<OldLeft, OldRight>) argumentPair; + IPair<OldLeft, OldRight> argPair = (IPair<OldLeft, OldRight>) argumentPair; return argPair.mapLeft(func); }; } @Override - default <OldLeft, OldRight, NewRight> - Function<Bifunctor<OldLeft, OldRight>, Bifunctor<OldLeft, NewRight>> + default <OldLeft, OldRight, NewRight> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<OldLeft, NewRight>> fmapRight(Function<OldRight, NewRight> func) { return (argumentPair) -> { @@ -99,8 +136,7 @@ public interface IPair<LeftType, RightType> "This function can only be applied to instances of IPair"); } - IPair<OldLeft, OldRight> argPair = - (IPair<OldLeft, OldRight>) argumentPair; + IPair<OldLeft, OldRight> argPair = (IPair<OldLeft, OldRight>) argumentPair; return argPair.mapRight(func); }; @@ -137,8 +173,8 @@ public interface IPair<LeftType, RightType> * pair * @return The pair, with its left part transformed */ - public <NewLeft> IPair<NewLeft, RightType> - mapLeft(Function<LeftType, NewLeft> mapper); + public <NewLeft> IPair<NewLeft, RightType> mapLeft( + Function<LeftType, NewLeft> mapper); /** * Transform the value on the right side of the pair. Doesn't modify @@ -151,8 +187,8 @@ public interface IPair<LeftType, RightType> * pair * @return The pair, with its right part transformed */ - public <NewRight> IPair<LeftType, NewRight> - mapRight(Function<RightType, NewRight> mapper); + public <NewRight> IPair<LeftType, NewRight> mapRight( + Function<RightType, NewRight> mapper); /** * Merge the two values in this pair into a single value @@ -163,48 +199,6 @@ public interface IPair<LeftType, RightType> * The function to use for merging * @return The pair, merged into a single value */ - public <MergedType> MergedType - merge(BiFunction<LeftType, RightType, MergedType> merger); - - /** - * Combine the contents of two pairs together - * - * @param <OtherLeft> - * The type of the left value of the other pair - * @param <OtherRight> - * The type of the right value of the other pair - * @param <CombinedLeft> - * The type of the left value of the combined pair - * @param <CombinedRight> - * The type of the right value of the combined pair - * @param otherPair - * The other pair to combine with - * @param leftCombiner - * @param rightCombiner - * @return A pair with its values combined - */ - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> - IPair<CombinedLeft, CombinedRight> - combine(IPair<OtherLeft, OtherRight> otherPair, - BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<RightType, OtherRight, CombinedRight> rightCombiner); - - /** - * Pairwise combine two pairs together - * - * @param <OtherLeft> - * The left type of the other pair - * @param <OtherRight> - * The right type of the other pair - * @param otherPair - * The pair to combine with - * @return The pairs, pairwise combined together - */ - public default <OtherLeft, OtherRight> - IPair<IPair<LeftType, OtherLeft>, IPair<RightType, OtherRight>> - combine(IPair<OtherLeft, OtherRight> otherPair) { - return combine(otherPair, - (left, otherLeft) -> new Pair<>(left, otherLeft), - (right, otherRight) -> new Pair<>(right, otherRight)); - } + public <MergedType> MergedType merge( + BiFunction<LeftType, RightType, MergedType> merger); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java index f42ceb7..8fcaf98 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java @@ -89,6 +89,14 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new Identity<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { return new Identity<>(mapper.apply(heldValue)); @@ -112,12 +120,4 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { Function<ContainedType, UnwrappedType> unwrapper) { return unwrapper.apply(heldValue); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> func) { - return (val) -> { - return new Identity<>(func.apply(val)); - }; - } }
\ 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 index 22f948c..f05204b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -76,6 +76,14 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>(); @@ -133,12 +141,4 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { return unwrapper.apply(heldValue); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> func) { - return (val) -> { - return new Lazy<>(func.apply(val)); - }; - } } 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 490c4fc..df4c3ac 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -96,6 +96,20 @@ public class LazyPair<LeftType, RightType> } @Override + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return bind((leftVal, rightVal) -> { + return new LazyPair<>( + leftCombiner.apply(leftVal, otherLeft), + rightCombiner.apply(rightVal, otherRight)); + }); + }); + } + + @Override public LeftType getLeft() { if (!leftMaterialized) { leftValue = leftSupplier.get(); @@ -118,47 +132,6 @@ public class LazyPair<LeftType, RightType> } @Override - public <MergedType> MergedType merge( - BiFunction<LeftType, RightType, MergedType> merger) { - if (!leftMaterialized) { - leftValue = leftSupplier.get(); - - leftMaterialized = true; - } - - if (!rightMaterialized) { - rightValue = rightSupplier.get(); - - rightMaterialized = true; - } - - return merger.apply(leftValue, rightValue); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder("pair[l="); - - if (leftMaterialized) { - sb.append(leftValue.toString()); - } else { - sb.append("(un-materialized)"); - } - - sb.append(", r="); - - if (rightMaterialized) { - sb.append(rightValue.toString()); - } else { - sb.append("(un-materialized)"); - } - - sb.append("]"); - - return sb.toString(); - } - - @Override public <NewLeft> IPair<NewLeft, RightType> mapLeft( Function<LeftType, NewLeft> mapper) { Supplier<NewLeft> leftSupp = () -> { @@ -203,16 +176,43 @@ public class LazyPair<LeftType, RightType> } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - IPair<OtherLeft, OtherRight> otherPair, - BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); - }); - }); + public <MergedType> MergedType merge( + BiFunction<LeftType, RightType, MergedType> merger) { + if (!leftMaterialized) { + leftValue = leftSupplier.get(); + + leftMaterialized = true; + } + + if (!rightMaterialized) { + rightValue = rightSupplier.get(); + + rightMaterialized = true; + } + + return merger.apply(leftValue, rightValue); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("pair[l="); + + if (leftMaterialized) { + sb.append(leftValue.toString()); + } else { + sb.append("(un-materialized)"); + } + + sb.append(", r="); + + if (rightMaterialized) { + sb.append(rightValue.toString()); + } else { + sb.append("(un-materialized)"); + } + + sb.append("]"); + + return sb.toString(); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java index 03765ed..fc6180b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java @@ -17,10 +17,6 @@ import bjc.utils.funcdata.IList; public class ListHolder<ContainedType> implements IHolder<ContainedType> { private IList<ContainedType> heldValues; - private ListHolder(IList<ContainedType> toHold) { - heldValues = toHold; - } - /** * Create a new list holder * @@ -38,16 +34,27 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { } } + private ListHolder(IList<ContainedType> toHold) { + heldValues = toHold; + } + @Override public <BoundType> IHolder<BoundType> bind( Function<ContainedType, IHolder<BoundType>> binder) { - IList<IHolder<BoundType>> boundValues = heldValues - .map(binder); + IList<IHolder<BoundType>> boundValues = heldValues.map(binder); return new BoundListHolder<>(boundValues); } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new ListHolder<>(new FunctionalList<>(func.apply(val))); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { IList<MappedType> mappedValues = heldValues.map(mapper); @@ -68,12 +75,4 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { Function<ContainedType, UnwrappedType> unwrapper) { return unwrapper.apply(heldValues.randItem()); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> func) { - return (val) -> { - return new ListHolder<>(new FunctionalList<>(func.apply(val))); - }; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Option.java b/BJC-Utils2/src/main/java/bjc/utils/data/Option.java index 9f6d448..16d90e3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Option.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Option.java @@ -35,6 +35,14 @@ public class Option<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new Option<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { if (held == null) { @@ -63,12 +71,4 @@ public class Option<ContainedType> implements IHolder<ContainedType> { return unwrapper.apply(held); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> func) { - return (val) -> { - return new Option<>(func.apply(val)); - }; - } } 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 1fc0d19..1000fc0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -70,24 +70,19 @@ public class Pair<LeftType, RightType> } @Override - public <MergedType> MergedType - merge(BiFunction<LeftType, RightType, MergedType> merger) { - if (merger == null) { - throw new NullPointerException("Merger must not be null"); - } - - return merger.apply(leftValue, rightValue); - } - - @Override - public String toString() { - return "pair[l=" + leftValue.toString() + ", r=" - + rightValue.toString() + "]"; + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( + IPair<OtherLeft, OtherRight> otherPair, + BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> { + return new Pair<>(leftCombiner.apply(leftValue, otherLeft), + rightCombiner.apply(rightValue, otherRight)); + }); } @Override - public <NewLeft> IPair<NewLeft, RightType> - mapLeft(Function<LeftType, NewLeft> mapper) { + public <NewLeft> IPair<NewLeft, RightType> mapLeft( + Function<LeftType, NewLeft> mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -96,8 +91,8 @@ public class Pair<LeftType, RightType> } @Override - public <NewRight> IPair<LeftType, NewRight> - mapRight(Function<RightType, NewRight> mapper) { + public <NewRight> IPair<LeftType, NewRight> mapRight( + Function<RightType, NewRight> mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -106,14 +101,18 @@ public class Pair<LeftType, RightType> } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> - IPair<CombinedLeft, CombinedRight> - combine(IPair<OtherLeft, OtherRight> otherPair, - BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return new Pair<>(leftCombiner.apply(leftValue, otherLeft), - rightCombiner.apply(rightValue, otherRight)); - }); + public <MergedType> MergedType merge( + BiFunction<LeftType, RightType, MergedType> merger) { + if (merger == null) { + throw new NullPointerException("Merger must not be null"); + } + + return merger.apply(leftValue, rightValue); + } + + @Override + public String toString() { + return "pair[l=" + leftValue.toString() + ", r=" + + rightValue.toString() + "]"; } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java index 989f1a5..f40ab29 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java @@ -3,6 +3,8 @@ package bjc.utils.data; import java.util.function.Supplier; public class SingleSupplier<T> implements Supplier<T> { + private static long nextID = 0; + private Supplier<T> source; private boolean gotten; @@ -13,8 +15,6 @@ public class SingleSupplier<T> implements Supplier<T> { // instantiation was, in case of duplicate initiations private Exception instSite; - private static long nextID = 0; - public SingleSupplier(Supplier<T> supp) { source = supp; diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/WrappedLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/WrappedLazy.java index 737482c..8ca29bc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/WrappedLazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/WrappedLazy.java @@ -6,6 +6,10 @@ import java.util.function.UnaryOperator; class WrappedLazy<ContainedType> implements IHolder<ContainedType> { private IHolder<IHolder<ContainedType>> held; + public WrappedLazy(IHolder<ContainedType> wrappedHolder) { + held = new Lazy<>(wrappedHolder); + } + // This has an extra parameter, because otherwise it erases to the same // as the public one private WrappedLazy(IHolder<IHolder<ContainedType>> wrappedHolder, @@ -13,10 +17,6 @@ class WrappedLazy<ContainedType> implements IHolder<ContainedType> { held = wrappedHolder; } - public WrappedLazy(IHolder<ContainedType> wrappedHolder) { - held = new Lazy<>(wrappedHolder); - } - @Override public <BoundType> IHolder<BoundType> bind( Function<ContainedType, IHolder<BoundType>> binder) { @@ -29,6 +29,14 @@ class WrappedLazy<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new Lazy<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { IHolder<IHolder<MappedType>> newHolder = held @@ -58,12 +66,4 @@ class WrappedLazy<ContainedType> implements IHolder<ContainedType> { return containedHolder.unwrap(unwrapper); }); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> func) { - return (val) -> { - return new Lazy<>(func.apply(val)); - }; - } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/WrappedOption.java b/BJC-Utils2/src/main/java/bjc/utils/data/WrappedOption.java index c36cafa..5be55cc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/WrappedOption.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/WrappedOption.java @@ -6,15 +6,15 @@ import java.util.function.UnaryOperator; class WrappedOption<ContainedType> implements IHolder<ContainedType> { private IHolder<IHolder<ContainedType>> held; + public WrappedOption(IHolder<ContainedType> seedValue) { + held = new Option<>(seedValue); + } + private WrappedOption(IHolder<IHolder<ContainedType>> toHold, @SuppressWarnings("unused") boolean dummy) { held = toHold; } - public WrappedOption(IHolder<ContainedType> seedValue) { - held = new Option<>(seedValue); - } - @Override public <BoundType> IHolder<BoundType> bind( Function<ContainedType, IHolder<BoundType>> binder) { @@ -33,6 +33,14 @@ class WrappedOption<ContainedType> implements IHolder<ContainedType> { } @Override + public <NewType> Function<ContainedType, IHolder<NewType>> lift( + Function<ContainedType, NewType> func) { + return (val) -> { + return new Option<>(func.apply(val)); + }; + } + + @Override public <MappedType> IHolder<MappedType> map( Function<ContainedType, MappedType> mapper) { IHolder<IHolder<MappedType>> newHolder = held @@ -78,12 +86,4 @@ class WrappedOption<ContainedType> implements IHolder<ContainedType> { }); }); } - - @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> func) { - return (val) -> { - return new Option<>(func.apply(val)); - }; - } } |
