diff options
| author | EVE <EVE@EVE-PC> | 2017-03-13 16:42:21 -0400 |
|---|---|---|
| committer | EVE <EVE@EVE-PC> | 2017-03-13 16:42:21 -0400 |
| commit | 27bf571d6413c3cc6a5d664b5bddd38d21d7b1cd (patch) | |
| tree | 847fb52acb091c1c613d37b8477094d5762c6988 /BJC-Utils2/src/main/java/bjc/utils/data | |
| parent | aa807a96cae2c47259fb38f710640883060339e9 (diff) | |
Formatting
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data')
20 files changed, 440 insertions, 547 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java b/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java index 04a93fa..8e0bf86 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java @@ -27,8 +27,8 @@ public class CircularIterator<E> implements Iterator<E> { } public E next() { - if(!curr.hasNext()) { - if(doCircle) { + if (!curr.hasNext()) { + if (doCircle) { curr = source.iterator(); } else { return curElm; 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 3ab4c00..7ec1720 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java @@ -8,26 +8,24 @@ import java.util.function.Function; * * @author ben * @param <LeftType> - * The type that could be on the left + * The type that could be on the left * @param <RightType> - * The type that could be on the right + * The type that could be on the right * */ -public class Either<LeftType, RightType> - implements IPair<LeftType, RightType> { +public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { /** * Create a new either with the left value occupied * * @param <LeftType> - * The type of the left value + * The type of the left value * @param <RightType> - * The type of the empty right value + * The type of the empty right value * @param left - * The value to put on the left + * 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); } @@ -35,25 +33,24 @@ public class Either<LeftType, RightType> * Create a new either with the right value occupied * * @param <LeftType> - * The type of the empty left value + * The type of the empty left value * @param <RightType> - * The type of the right value + * The type of the right value * @param right - * The value to put on the right + * 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 LeftType leftVal; - private RightType rightVal; + private RightType rightVal; - private boolean isLeft; + private boolean isLeft; - private Either( LeftType left, RightType right) { + private Either(LeftType left, RightType right) { if (left == null) { rightVal = right; } else { @@ -91,8 +88,7 @@ public class Either<LeftType, RightType> public <BoundRight> IPair<LeftType, BoundRight> bindRight( Function<RightType, IPair<LeftType, BoundRight>> rightBinder) { if (rightBinder == null) { - throw new NullPointerException( - "Right binder must not be null"); + throw new NullPointerException("Right binder must not be null"); } if (isLeft) { @@ -110,29 +106,24 @@ public class Either<LeftType, RightType> if (otherPair == null) { throw new NullPointerException("Other pair must not be null"); } else if (leftCombiner == null) { - throw new NullPointerException( - "Left combiner must not be null"); + throw new NullPointerException("Left combiner must not be null"); } else if (rightCombiner == null) { - throw new NullPointerException( - "Right combiner must not be null"); + throw new NullPointerException("Right combiner must not be null"); } if (isLeft) { return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(leftCombiner.apply(leftVal, otherLeft), - null); + return new Either<>(leftCombiner.apply(leftVal, otherLeft), null); }); } return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(null, - rightCombiner.apply(rightVal, otherRight)); + return new Either<>(null, rightCombiner.apply(rightVal, 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"); } @@ -145,8 +136,7 @@ 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 (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -159,8 +149,7 @@ public class Either<LeftType, RightType> } @Override - public <MergedType> MergedType merge( - BiFunction<LeftType, RightType, MergedType> merger) { + public <MergedType> MergedType merge(BiFunction<LeftType, RightType, MergedType> merger) { if (merger == null) { throw new NullPointerException("Merger must not be null"); } 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 a05d90e..7b6c8d5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -16,26 +16,25 @@ import bjc.utils.funcdata.theory.Functor; * @author ben * * @param <ContainedType> - * The type of value held + * The type of value held */ public interface IHolder<ContainedType> extends Functor<ContainedType> { /** * Bind a function across the value in this container * * @param <BoundType> - * The type of value in this container + * The type of value in this container * @param binder - * The function to bind to the value + * 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 * * @param action - * The action to apply to the value + * The action to apply to the value */ public default void doWith(Consumer<? super ContainedType> action) { transform((value) -> { @@ -46,10 +45,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( @@ -71,13 +68,12 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * Lifts a function to bind over this holder * * @param <NewType> - * The type of the functions return + * The type of the functions return * @param func - * The function to lift over the holder + * 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 @@ -113,19 +109,18 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * Does not change the internal state of this holder * * @param <MappedType> - * The type of the mapped value + * The type of the mapped value * @param mapper - * The function to do mapping with + * 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 * * @param newValue - * The value to hold instead + * The value to hold instead * @return The holder itself */ public default IHolder<ContainedType> replace(ContainedType newValue) { @@ -138,22 +133,20 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * Transform the value held in this holder * * @param transformer - * The function to transform the value with + * 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 * held * * @param <UnwrappedType> - * The type of the unwrapped value + * The type of the unwrapped value * @param unwrapper - * The function to use to unwrap the value + * 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 c82cc8e..1405d75 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -11,35 +11,33 @@ import bjc.utils.funcdata.theory.Bifunctor; * * @author ben * @param <LeftType> - * The type of the left side of the pair + * The type of the left side of the pair * @param <RightType> - * The type of the right side of the pair + * The type of the right side of the pair * */ -public interface IPair<LeftType, RightType> - extends Bifunctor<LeftType, RightType> { +public interface IPair<LeftType, RightType> extends Bifunctor<LeftType, RightType> { /** * Bind a function across the values in this pair * * @param <BoundLeft> - * The type of the bound left + * The type of the bound left * @param <BoundRight> - * The type of the bound right + * The type of the bound right * @param binder - * The function to bind with + * The function to bind with * @return The bound pair */ public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( - BiFunction<LeftType, RightType, - IPair<BoundLeft, BoundRight>> binder); + BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder); /** * Bind a function to the left value in this pair * * @param <BoundLeft> - * The type of the bound value + * The type of the bound value * @param leftBinder - * The function to use to bind + * The function to use to bind * @return A pair with the left type bound */ public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( @@ -49,9 +47,9 @@ public interface IPair<LeftType, RightType> * Bind a function to the right value in this pair * * @param <BoundRight> - * The type of the bound value + * The type of the bound value * @param rightBinder - * The function to use to bind + * The function to use to bind * @return A pair with the right type bound */ public <BoundRight> IPair<LeftType, BoundRight> bindRight( @@ -61,19 +59,16 @@ public interface IPair<LeftType, RightType> * Pairwise combine two pairs together * * @param <OtherLeft> - * The left type of the other pair + * The left type of the other pair * @param <OtherRight> - * The right type of the other pair + * The right type of the other pair * @param otherPair - * The pair to combine with + * 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), + 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)); } @@ -81,33 +76,30 @@ public interface IPair<LeftType, RightType> * Combine the contents of two pairs together * * @param <OtherLeft> - * The type of the left value of the other pair + * The type of the left value of the other pair * @param <OtherRight> - * The type of the right value of the other pair + * The type of the right value of the other pair * @param <CombinedLeft> - * The type of the left value of the combined pair + * The type of the left value of the combined pair * @param <CombinedRight> - * The type of the right value of the combined pair + * The type of the right value of the combined pair * @param otherPair - * The other pair to combine with + * 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); + 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 * * @param consumer - * The action to perform on the pair + * The action to perform on the pair */ public default void doWith(BiConsumer<LeftType, RightType> consumer) { merge((leftValue, rightValue) -> { @@ -118,26 +110,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) -> { @@ -146,8 +134,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); }; @@ -178,38 +165,35 @@ public interface IPair<LeftType, RightType> * pair * * @param <NewLeft> - * The new type of the left part of the pair + * The new type of the left part of the pair * @param mapper - * The function to use to transform the left part of the - * pair + * The function to use to transform the left part of the + * 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 - * the pair + * Transform the value on the right side of the pair. Doesn't modify the + * pair * * @param <NewRight> - * The new type of the right part of the pair + * The new type of the right part of the pair * @param mapper - * The function to use to transform the right part of the - * pair + * The function to use to transform the right part of the + * 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 * * @param <MergedType> - * The type of the single value + * The type of the single value * @param merger - * The function to use for merging + * The function to use for merging * @return The pair, merged into a single value */ - public <MergedType> MergedType merge( - BiFunction<LeftType, RightType, MergedType> merger); + public <MergedType> MergedType merge(BiFunction<LeftType, RightType, MergedType> merger); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java b/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java index 7d5988f..4b6725c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java @@ -13,7 +13,7 @@ import bjc.utils.funcdata.bst.TreeLinearizationMethod; * * @author ben * @param <ContainedType> - * The type of data contained in the tree nodes + * The type of data contained in the tree nodes * */ public interface ITree<ContainedType> { @@ -21,7 +21,7 @@ public interface ITree<ContainedType> { * Add a child to this node * * @param child - * The child to add to this node + * The child to add to this node */ public void addChild(ITree<ContainedType> child); @@ -29,49 +29,46 @@ public interface ITree<ContainedType> { * Collapse a tree into a single version * * @param <NewType> - * The intermediate type being folded + * The intermediate type being folded * @param <ReturnedType> - * The type that is the end result + * The type that is the end result * @param leafTransform - * The function to use to convert leaf values + * The function to use to convert leaf values * @param nodeCollapser - * The function to use to convert internal nodes and their - * children + * The function to use to convert internal nodes and + * their children * @param resultTransformer - * The function to use to convert a state to the returned - * version + * The function to use to convert a state to the returned + * version * @return The final transformed state */ - public <NewType, ReturnedType> ReturnedType collapse( - Function<ContainedType, NewType> leafTransform, - Function<ContainedType, - Function<IList<NewType>, NewType>> nodeCollapser, + public <NewType, ReturnedType> ReturnedType collapse(Function<ContainedType, NewType> leafTransform, + Function<ContainedType, Function<IList<NewType>, NewType>> nodeCollapser, Function<NewType, ReturnedType> resultTransformer); /** * Execute a given action for each of this tree's children * * @param action - * The action to execute for each child + * The action to execute for each child */ void doForChildren(Consumer<ITree<ContainedType>> action); /** - * Expand the nodes of a tree into trees, and then merge the contents - * of those trees into a single tree + * Expand the nodes of a tree into trees, and then merge the contents of + * those trees into a single tree * * @param mapper - * The function to use to map values into trees + * The function to use to map values into trees * @return A tree, with some nodes expanded into trees */ - public ITree<ContainedType> flatMapTree( - Function<ContainedType, ITree<ContainedType>> mapper); + public ITree<ContainedType> flatMapTree(Function<ContainedType, ITree<ContainedType>> mapper); /** * Get the specified child of this tree * * @param childNo - * The number of the child to get + * The number of the child to get * @return The specified child of this tree */ default ITree<ContainedType> getChild(int childNo) { @@ -98,56 +95,52 @@ public interface ITree<ContainedType> { * Rebuild the tree with the same structure, but different nodes * * @param <MappedType> - * The type of the new tree + * The type of the new tree * @param leafTransformer - * The function to use to transform leaf tokens + * The function to use to transform leaf tokens * @param operatorTransformer - * The function to use to transform internal tokens + * The function to use to transform internal tokens * @return The tree, with the nodes changed */ - public <MappedType> ITree<MappedType> rebuildTree( - Function<ContainedType, MappedType> leafTransformer, + public <MappedType> ITree<MappedType> rebuildTree(Function<ContainedType, MappedType> leafTransformer, Function<ContainedType, MappedType> operatorTransformer); /** * Transform some of the nodes in this tree * * @param nodePicker - * The predicate to use to pick nodes to transform + * The predicate to use to pick nodes to transform * @param transformer - * The function to use to transform picked nodes + * The function to use to transform picked nodes */ - public void selectiveTransform(Predicate<ContainedType> nodePicker, - UnaryOperator<ContainedType> transformer); + public void selectiveTransform(Predicate<ContainedType> nodePicker, UnaryOperator<ContainedType> transformer); /** * Do a top-down transform of the tree * * @param transformPicker - * The function to use to pick how to progress + * The function to use to pick how to progress * @param transformer - * The function used to transform picked subtrees + * The function used to transform picked subtrees * @return The tree with the transform applied to picked subtrees */ - public ITree<ContainedType> topDownTransform( - Function<ContainedType, - TopDownTransformResult> transformPicker, + public ITree<ContainedType> topDownTransform(Function<ContainedType, TopDownTransformResult> transformPicker, UnaryOperator<ITree<ContainedType>> transformer); /** * Transform one of this nodes children * * @param <TransformedType> - * The type of the transformed value + * The type of the transformed value * @param childNo - * The number of the child to transform + * The number of the child to transform * @param transformer - * The function to use to transform the value + * The function to use to transform the value * @return The transformed value * * @throws IllegalArgumentException - * if the childNo is out of bounds (0 <= childNo <= - * childCount()) + * if the childNo is out of bounds (0 <= childNo <= + * childCount()) */ public <TransformedType> TransformedType transformChild(int childNo, Function<ITree<ContainedType>, TransformedType> transformer); @@ -156,34 +149,31 @@ public interface ITree<ContainedType> { * Transform the value that is the head of this node * * @param <TransformedType> - * The type of the transformed value + * The type of the transformed value * @param transformer - * The function to use to transform the value + * The function to use to transform the value * @return The transformed value */ - public <TransformedType> TransformedType transformHead( - Function<ContainedType, TransformedType> transformer); + public <TransformedType> TransformedType transformHead(Function<ContainedType, TransformedType> transformer); /** * Transform the tree into a tree with a different type of token * * @param <MappedType> - * The type of the new tree + * The type of the new tree * @param transformer - * The function to use to transform tokens + * The function to use to transform tokens * @return A tree with the token types transformed */ - public <MappedType> ITree<MappedType> transformTree( - Function<ContainedType, MappedType> transformer); + public <MappedType> ITree<MappedType> transformTree(Function<ContainedType, MappedType> transformer); /** * Perform an action on each part of the tree * * @param linearizationMethod - * The way to traverse the tree + * The way to traverse the tree * @param action - * The action to perform on each tree node + * The action to perform on each tree node */ - public void traverse(TreeLinearizationMethod linearizationMethod, - Consumer<ContainedType> action); + public void traverse(TreeLinearizationMethod linearizationMethod, Consumer<ContainedType> action); } 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 8fcaf98..aa3f7aa 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java @@ -14,7 +14,7 @@ import java.util.function.UnaryOperator; * @author ben * * @param <ContainedType> - * The type contained in the holder + * The type contained in the holder */ public class Identity<ContainedType> implements IHolder<ContainedType> { private ContainedType heldValue; @@ -30,15 +30,14 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { * Create a holder holding the specified value * * @param value - * The value to hold + * The value to hold */ public Identity(ContainedType value) { heldValue = value; } @Override - public <BoundType> IHolder<BoundType> bind( - Function<ContainedType, IHolder<BoundType>> binder) { + public <BoundType> IHolder<BoundType> bind(Function<ContainedType, IHolder<BoundType>> binder) { return binder.apply(heldValue); } @@ -89,16 +88,14 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> func) { + 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) { + public <MappedType> IHolder<MappedType> map(Function<ContainedType, MappedType> mapper) { return new Identity<>(mapper.apply(heldValue)); } @@ -108,16 +105,14 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { } @Override - public IHolder<ContainedType> transform( - UnaryOperator<ContainedType> transformer) { + public IHolder<ContainedType> transform(UnaryOperator<ContainedType> transformer) { heldValue = transformer.apply(heldValue); return this; } @Override - public <UnwrappedType> UnwrappedType unwrap( - Function<ContainedType, UnwrappedType> unwrapper) { + public <UnwrappedType> UnwrappedType unwrap(Function<ContainedType, UnwrappedType> 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 index 6339795..3a037d7 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -9,28 +9,27 @@ import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IList; /** - * A holder that holds a means to create a value, but doesn't actually - * compute the value until it's needed + * A holder that holds a means to create a value, but doesn't actually compute + * the value until it's needed * * @author ben * * @param <ContainedType> */ public class Lazy<ContainedType> implements IHolder<ContainedType> { - private Supplier<ContainedType> valueSupplier; + private Supplier<ContainedType> valueSupplier; - private IList<UnaryOperator< - ContainedType>> actions = new FunctionalList<>(); + private IList<UnaryOperator<ContainedType>> actions = new FunctionalList<>(); - private boolean valueMaterialized; + private boolean valueMaterialized; - private ContainedType heldValue; + private ContainedType heldValue; /** * Create a new lazy value from the specified seed value * * @param value - * The seed value to use + * The seed value to use */ public Lazy(ContainedType value) { heldValue = value; @@ -42,7 +41,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { * Create a new lazy value from the specified value source * * @param supp - * The source of a value to use + * The source of a value to use */ public Lazy(Supplier<ContainedType> supp) { valueSupplier = new SingleSupplier<>(supp); @@ -50,18 +49,15 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { valueMaterialized = false; } - private Lazy(Supplier<ContainedType> supp, - IList<UnaryOperator<ContainedType>> pendingActions) { + private Lazy(Supplier<ContainedType> supp, IList<UnaryOperator<ContainedType>> pendingActions) { valueSupplier = supp; actions = pendingActions; } @Override - public <BoundType> IHolder<BoundType> bind( - Function<ContainedType, IHolder<BoundType>> binder) { - IList<UnaryOperator< - ContainedType>> pendingActions = new FunctionalList<>(); + public <BoundType> IHolder<BoundType> bind(Function<ContainedType, IHolder<BoundType>> binder) { + IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); @@ -79,18 +75,15 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> func) { + 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<>(); + public <MappedType> IHolder<MappedType> map(Function<ContainedType, MappedType> mapper) { + IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); @@ -101,8 +94,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { currVal = valueSupplier.get(); } - return pendingActions.reduceAux(currVal, - UnaryOperator<ContainedType>::apply, + return pendingActions.reduceAux(currVal, UnaryOperator<ContainedType>::apply, (value) -> mapper.apply(value)); }); } @@ -121,16 +113,14 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { } @Override - public IHolder<ContainedType> transform( - UnaryOperator<ContainedType> transformer) { + public IHolder<ContainedType> transform(UnaryOperator<ContainedType> transformer) { actions.add(transformer); return this; } @Override - public <UnwrappedType> UnwrappedType unwrap( - Function<ContainedType, UnwrappedType> unwrapper) { + public <UnwrappedType> UnwrappedType unwrap(Function<ContainedType, UnwrappedType> unwrapper) { if (!valueMaterialized) { heldValue = valueSupplier.get(); 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 e03b46e..29e37ac 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -13,29 +13,28 @@ import bjc.utils.data.internals.HalfBoundLazyPair; * @author ben * * @param <LeftType> - * The type on the left side of the pair + * The type on the left side of the pair * @param <RightType> - * The type on the right side of the pair + * The type on the right side of the pair * */ -public class LazyPair<LeftType, RightType> - implements IPair<LeftType, RightType> { - private LeftType leftValue; - private RightType rightValue; +public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> { + private LeftType leftValue; + private RightType rightValue; - private Supplier<LeftType> leftSupplier; - private Supplier<RightType> rightSupplier; + private Supplier<LeftType> leftSupplier; + private Supplier<RightType> rightSupplier; - private boolean leftMaterialized; - private boolean rightMaterialized; + private boolean leftMaterialized; + private boolean rightMaterialized; /** * Create a new lazy pair, using the set values * * @param leftVal - * The value for the left side of the pair + * The value for the left side of the pair * @param rightVal - * The value for the right side of the pair + * The value for the right side of the pair */ public LazyPair(LeftType leftVal, RightType rightVal) { leftValue = leftVal; @@ -49,12 +48,11 @@ public class LazyPair<LeftType, RightType> * Create a new lazy pair from the given value sources * * @param leftSupp - * The source for a value on the left side of the pair + * 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 + * The source for a value on the right side of the pair */ - public LazyPair(Supplier<LeftType> leftSupp, - Supplier<RightType> rightSupp) { + public LazyPair(Supplier<LeftType> leftSupp, Supplier<RightType> rightSupp) { // Use single suppliers to catch double-instantiation bugs leftSupplier = new SingleSupplier<>(leftSupp); rightSupplier = new SingleSupplier<>(rightSupp); @@ -70,7 +68,8 @@ public class LazyPair<LeftType, RightType> } @Override - public <BoundLeft> IPair<BoundLeft, RightType> bindLeft(Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { + public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( + Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { Supplier<LeftType> leftSupp = () -> { if (leftMaterialized) { return leftValue; @@ -97,17 +96,13 @@ 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) { + 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), + return new LazyPair<>(leftCombiner.apply(leftVal, otherLeft), rightCombiner.apply(rightVal, otherRight)); }); }); @@ -136,8 +131,7 @@ public class LazyPair<LeftType, RightType> } @Override - public <NewLeft> IPair<NewLeft, RightType> mapLeft( - Function<LeftType, NewLeft> mapper) { + public <NewLeft> IPair<NewLeft, RightType> mapLeft(Function<LeftType, NewLeft> mapper) { Supplier<NewLeft> leftSupp = () -> { if (leftMaterialized) { return mapper.apply(leftValue); @@ -158,8 +152,7 @@ public class LazyPair<LeftType, RightType> } @Override - public <NewRight> IPair<LeftType, NewRight> mapRight( - Function<RightType, NewRight> mapper) { + public <NewRight> IPair<LeftType, NewRight> mapRight(Function<RightType, NewRight> mapper) { Supplier<LeftType> leftSupp = () -> { if (leftMaterialized) { return leftValue; @@ -180,8 +173,7 @@ public class LazyPair<LeftType, RightType> } @Override - public <MergedType> MergedType merge( - BiFunction<LeftType, RightType, MergedType> merger) { + public <MergedType> MergedType merge(BiFunction<LeftType, RightType, MergedType> merger) { if (!leftMaterialized) { leftValue = leftSupplier.get(); 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 a77cd16..5c14475 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java @@ -13,7 +13,7 @@ import bjc.utils.funcdata.IList; * @author ben * * @param <ContainedType> - * The type of contained value + * The type of contained value */ public class ListHolder<ContainedType> implements IHolder<ContainedType> { private IList<ContainedType> heldValues; @@ -22,7 +22,7 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { * Create a new list holder * * @param values - * The possible values for the computation + * The possible values for the computation */ @SafeVarargs public ListHolder(ContainedType... values) { 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 33b6327..210d2aa 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Option.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Option.java @@ -9,7 +9,7 @@ import java.util.function.UnaryOperator; * @author ben * * @param <ContainedType> - * The type of the value that may or may not be held + * The type of the value that may or may not be held */ public class Option<ContainedType> implements IHolder<ContainedType> { private ContainedType held; @@ -18,7 +18,7 @@ public class Option<ContainedType> implements IHolder<ContainedType> { * Create a new optional, using the given initial value * * @param seed - * The initial value for the optional + * The initial value for the optional */ public Option(ContainedType seed) { held = seed; 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 74040e8..b22fd28 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -9,16 +9,16 @@ import java.util.function.Function; * @author ben * * @param <LeftType> - * The type of the left value + * The type of the left value * @param <RightType> - * The type of the right value + * The type of the right value */ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { // The left value - private LeftType leftValue; + private LeftType leftValue; // The right value - private RightType rightValue; + private RightType rightValue; /** * Create a new pair with both sides set to null @@ -31,9 +31,9 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { * Create a new pair with both sides set to the specified values * * @param left - * The value of the left side + * The value of the left side * @param right - * The value of the right side + * The value of the right side */ public Pair(LeftType left, RightType right) { leftValue = left; @@ -51,7 +51,8 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { } @Override - public <BoundLeft> IPair<BoundLeft, RightType> bindLeft(Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { + public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( + Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { if (leftBinder == null) { throw new NullPointerException("Binder must not be null"); } @@ -60,7 +61,8 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { } @Override - public <BoundRight> IPair<LeftType, BoundRight> bindRight(Function<RightType, IPair<LeftType, BoundRight>> rightBinder) { + public <BoundRight> IPair<LeftType, BoundRight> bindRight( + Function<RightType, IPair<LeftType, BoundRight>> rightBinder) { if (rightBinder == null) { throw new NullPointerException("Binder must not be null"); } @@ -73,9 +75,10 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { 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)); - }); + return otherPair.bind((otherLeft, otherRight) -> { + return new Pair<>(leftCombiner.apply(leftValue, otherLeft), + rightCombiner.apply(rightValue, otherRight)); + }); } @Override 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 b5ff1e3..bf8b0a0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java @@ -5,31 +5,30 @@ import java.util.function.Supplier; /** * A supplier that can only supply one value. * - * Attempting to retrieve another value will cause an exception to be - * thrown. + * Attempting to retrieve another value will cause an exception to be thrown. * * @author ben * * @param <T> */ public class SingleSupplier<T> implements Supplier<T> { - private static long nextID = 0; + private static long nextID = 0; - private Supplier<T> source; + private Supplier<T> source; - private boolean gotten; + private boolean gotten; - private long id; + private long id; // This is bad practice, but I want to know where the single // instantiation was, in case of duplicate initiations - private Exception instSite; + private Exception instSite; /** * Create a new single supplier from an existing value * * @param supp - * The supplier to give a single value from + * The supplier to give a single value from */ public SingleSupplier(Supplier<T> supp) { source = supp; @@ -42,10 +41,8 @@ public class SingleSupplier<T> implements Supplier<T> { @Override public T get() { if (gotten == true) { - IllegalStateException isex = new IllegalStateException( - "Attempted to get value more than once" - + " from single supplier #" + id - + ". Previous instantiation below."); + IllegalStateException isex = new IllegalStateException("Attempted to get value more than once" + + " from single supplier #" + id + ". Previous instantiation below."); isex.initCause(instSite); diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java b/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java index d37123f..8a24512 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java @@ -12,13 +12,11 @@ import static bjc.utils.data.TopDownTransformResult.*; public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<ContainedType>> { private Function<ContainedType, TopDownTransformResult> picker; - private BiFunction<ITree<ContainedType>, - Consumer<Iterator<ITree<ContainedType>>>, - ITree<ContainedType>> transform; + private BiFunction<ITree<ContainedType>, Consumer<Iterator<ITree<ContainedType>>>, ITree<ContainedType>> transform; private ITree<ContainedType> preParent; private ITree<ContainedType> postParent; - + private Deque<ITree<ContainedType>> preChildren; private Deque<ITree<ContainedType>> postChildren; @@ -28,29 +26,29 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C private boolean initial; private Deque<Iterator<ITree<ContainedType>>> toYield; - private Iterator<ITree<ContainedType>> curYield; + private Iterator<ITree<ContainedType>> curYield; - public TopDownTransformIterator(Function<ContainedType, TopDownTransformResult> pickr, + public TopDownTransformIterator(Function<ContainedType, TopDownTransformResult> pickr, BiFunction<ITree<ContainedType>, Consumer<Iterator<ITree<ContainedType>>>, ITree<ContainedType>> transfrm, ITree<ContainedType> tree) { preParent = tree; - preChildren = new LinkedList<>(); + preChildren = new LinkedList<>(); postChildren = new LinkedList<>(); - toYield = new LinkedList<>(); + toYield = new LinkedList<>(); - picker = pickr; + picker = pickr; transform = transfrm; - done = false; + done = false; initial = true; } public void addYield(Iterator<ITree<ContainedType>> src) { - if(curYield != null) { + if (curYield != null) { toYield.push(curYield); } - + curYield = src; } @@ -59,17 +57,17 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C } public ITree<ContainedType> flushYields(ITree<ContainedType> val) { - if(curYield != null) { + if (curYield != null) { toYield.add(new SingleIterator<>(val)); - if(curYield.hasNext()) { + if (curYield.hasNext()) { return curYield.next(); } else { - while(toYield.size() != 0 && !curYield.hasNext()) { + while (toYield.size() != 0 && !curYield.hasNext()) { curYield = toYield.pop(); } - if(toYield.size() == 0 && !curYield.hasNext()) { + if (toYield.size() == 0 && !curYield.hasNext()) { curYield = null; return val; } else { @@ -82,17 +80,18 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C } public ITree<ContainedType> next() { - if(done) throw new NoSuchElementException(); + if (done) + throw new NoSuchElementException(); - if(curYield != null) { - if(curYield.hasNext()) { + if (curYield != null) { + if (curYield.hasNext()) { return curYield.next(); } else { - while(toYield.size() != 0 && !curYield.hasNext()) { + while (toYield.size() != 0 && !curYield.hasNext()) { curYield = toYield.pop(); } - if(toYield.size() == 0 && !curYield.hasNext()) { + if (toYield.size() == 0 && !curYield.hasNext()) { curYield = null; } else { return curYield.next(); @@ -100,73 +99,74 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C } } - if(initial) { + if (initial) { TopDownTransformResult res = picker.apply(preParent.getHead()); - switch(res) { - case PASSTHROUGH: - postParent = new Tree<>(preParent.getHead()); - - if(preParent.getChildrenCount() != 0) { - for(int i = 0; i < preParent.getChildrenCount(); i++) { - preChildren.add(preParent.getChild(i)); - } - - // Return whatever the first child is - break; - } else { - done = true; - return flushYields(postParent); + switch (res) { + case PASSTHROUGH: + postParent = new Tree<>(preParent.getHead()); + + if (preParent.getChildrenCount() != 0) { + for (int i = 0; i < preParent.getChildrenCount(); i++) { + preChildren.add(preParent.getChild(i)); } - case SKIP: - done = true; - return flushYields(preParent); - case TRANSFORM: + + // Return whatever the first child is + break; + } else { done = true; - return flushYields(transform.apply(preParent, this::addYield)); - case RTRANSFORM: - preParent = transform.apply(preParent, this::addYield); - return flushYields(preParent); - case PUSHDOWN: - if(preParent.getChildrenCount() != 0) { - for(int i = 0; i < preParent.getChildrenCount(); i++) { - preChildren.add(preParent.getChild(i)); - } - - // Return whatever the first child is - break; - } else { - done = true; - return flushYields(transform.apply(new Tree<>(preParent.getHead()), this::addYield)); + return flushYields(postParent); + } + case SKIP: + done = true; + return flushYields(preParent); + case TRANSFORM: + done = true; + return flushYields(transform.apply(preParent, this::addYield)); + case RTRANSFORM: + preParent = transform.apply(preParent, this::addYield); + return flushYields(preParent); + case PUSHDOWN: + if (preParent.getChildrenCount() != 0) { + for (int i = 0; i < preParent.getChildrenCount(); i++) { + preChildren.add(preParent.getChild(i)); } - case PULLUP: - ITree<ContainedType> intRes = transform.apply(preParent, this::addYield); - - postParent = new Tree<>(intRes.getHead()); - - if(intRes.getChildrenCount() != 0) { - for(int i = 0; i < intRes.getChildrenCount(); i++) { - preChildren.add(intRes.getChild(i)); - } - - // Return whatever the first child is - break; - } else { - done = true; - return flushYields(postParent); + + // Return whatever the first child is + break; + } else { + done = true; + return flushYields(transform.apply(new Tree<>(preParent.getHead()), + this::addYield)); + } + case PULLUP: + ITree<ContainedType> intRes = transform.apply(preParent, this::addYield); + + postParent = new Tree<>(intRes.getHead()); + + if (intRes.getChildrenCount() != 0) { + for (int i = 0; i < intRes.getChildrenCount(); i++) { + preChildren.add(intRes.getChild(i)); } - default: - throw new IllegalArgumentException("Unknown result type " + res); + + // Return whatever the first child is + break; + } else { + done = true; + return flushYields(postParent); + } + default: + throw new IllegalArgumentException("Unknown result type " + res); } - if(res != RTRANSFORM) initial = false; + if (res != RTRANSFORM) + initial = false; } - - if(curChild == null || !curChild.hasNext()) { - if(preChildren.size() != 0) { + if (curChild == null || !curChild.hasNext()) { + if (preChildren.size() != 0) { curChild = new TopDownTransformIterator<>(picker, transform, preChildren.pop()); - + ITree<ContainedType> res = curChild.next(); System.out.println("\t\tTRACE: adding node " + res + " to children"); postChildren.add(res); @@ -175,21 +175,22 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C } else { ITree<ContainedType> res = null; - if(postParent == null) { + if (postParent == null) { res = new Tree<>(preParent.getHead()); System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); - for(ITree<ContainedType> child : postChildren) { + for (ITree<ContainedType> child : postChildren) { res.addChild(child); } - - // res = transform.apply(res, this::addYield); + + // res = transform.apply(res, + // this::addYield); } else { res = postParent; System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); - for(ITree<ContainedType> child : postChildren) { + for (ITree<ContainedType> child : postChildren) { res.addChild(child); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/TransformedIterator.java b/BJC-Utils2/src/main/java/bjc/utils/data/TransformedIterator.java index 848b6ac..742a73b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/TransformedIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/TransformedIterator.java @@ -10,7 +10,7 @@ public class TransformedIterator<PreType, PostType> implements Iterator<PostType public TransformedIterator(Iterator<PreType> src, Function<PreType, PostType> trans) { source = src; - trans = transform; + trans = transform; } public boolean hasNext() { diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java index 5fed73f..6a16491 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java @@ -17,11 +17,11 @@ import bjc.utils.funcdata.bst.TreeLinearizationMethod; * @param <ContainedType> */ public class Tree<ContainedType> implements ITree<ContainedType> { - private ContainedType data; + private ContainedType data; - private IList<ITree<ContainedType>> children; - private boolean hasChildren; - private int childCount = 0; + private IList<ITree<ContainedType>> children; + private boolean hasChildren; + private int childCount = 0; private int ID; private static int nextID = 0; @@ -30,7 +30,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { * Create a new leaf node in a tree * * @param leaf - * The data to store as a leaf node + * The data to store as a leaf node */ public Tree(ContainedType leaf) { data = leaf; @@ -44,9 +44,9 @@ public class Tree<ContainedType> implements ITree<ContainedType> { * Create a new tree node with the specified children * * @param leaf - * The data to hold in this node + * The data to hold in this node * @param childrn - * A list of children for this node + * A list of children for this node */ public Tree(ContainedType leaf, IList<ITree<ContainedType>> childrn) { this(leaf); @@ -60,9 +60,9 @@ public class Tree<ContainedType> implements ITree<ContainedType> { * Create a new tree node with the specified children * * @param leaf - * The data to hold in this node + * The data to hold in this node * @param childrn - * A list of children for this node + * A list of children for this node */ @SafeVarargs public Tree(ContainedType leaf, ITree<ContainedType>... childrn) { @@ -95,8 +95,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } @Override - public <NewType, ReturnedType> ReturnedType collapse( - Function<ContainedType, NewType> leafTransform, + public <NewType, ReturnedType> ReturnedType collapse(Function<ContainedType, NewType> leafTransform, Function<ContainedType, Function<IList<NewType>, NewType>> nodeCollapser, Function<NewType, ReturnedType> resultTransformer) { @@ -127,14 +126,13 @@ public class Tree<ContainedType> implements ITree<ContainedType> { return childCount; } - protected <NewType> NewType internalCollapse( - Function<ContainedType, NewType> leafTransform, + protected <NewType> NewType internalCollapse(Function<ContainedType, NewType> leafTransform, Function<ContainedType, Function<IList<NewType>, NewType>> nodeCollapser) { if (hasChildren) { Function<IList<NewType>, NewType> nodeTransformer = nodeCollapser.apply(data); IList<NewType> collapsedChildren = (IList<NewType>) children.map((child) -> { - return child.collapse(leafTransform, nodeCollapser, (subTreeVal) -> subTreeVal); + return child.collapse(leafTransform, nodeCollapser, (subTreeVal) -> subTreeVal); }); return nodeTransformer.apply(collapsedChildren); @@ -144,7 +142,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } protected void internalToString(StringBuilder builder, int indentLevel, boolean initial) { - for(int i = 0; i < indentLevel; i++) { + for (int i = 0; i < indentLevel; i++) { builder.append(">\t"); } @@ -156,19 +154,17 @@ public class Tree<ContainedType> implements ITree<ContainedType> { if (hasChildren) { children.forEach((child) -> { - ((Tree<ContainedType>) child).internalToString(builder, indentLevel+1, false); + ((Tree<ContainedType>) child).internalToString(builder, indentLevel + 1, false); }); } } @Override - public <MappedType> ITree<MappedType> rebuildTree( - Function<ContainedType, MappedType> leafTransformer, + public <MappedType> ITree<MappedType> rebuildTree(Function<ContainedType, MappedType> leafTransformer, Function<ContainedType, MappedType> operatorTransformer) { if (hasChildren) { IList<ITree<MappedType>> mappedChildren = children.map((child) -> { - return child.rebuildTree(leafTransformer, - operatorTransformer); + return child.rebuildTree(leafTransformer, operatorTransformer); }); return new Tree<>(operatorTransformer.apply(data), mappedChildren); @@ -187,50 +183,49 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } @Override - public ITree<ContainedType> topDownTransform( - Function<ContainedType, TopDownTransformResult> transformPicker, + public ITree<ContainedType> topDownTransform(Function<ContainedType, TopDownTransformResult> transformPicker, UnaryOperator<ITree<ContainedType>> transformer) { TopDownTransformResult transformResult = transformPicker.apply(data); switch (transformResult) { - case PASSTHROUGH: - ITree<ContainedType> result = new Tree<>(data); + case PASSTHROUGH: + ITree<ContainedType> result = new Tree<>(data); - if (hasChildren) { - children.forEach((child) -> { - result.addChild(child.topDownTransform(transformPicker, transformer)); - }); - } + if (hasChildren) { + children.forEach((child) -> { + result.addChild(child.topDownTransform(transformPicker, transformer)); + }); + } - return result; - case SKIP: - return this; - case TRANSFORM: - return transformer.apply(this); - case RTRANSFORM: - return transformer.apply(this).topDownTransform(transformPicker, transformer); - case PUSHDOWN: - result = new Tree<>(data); - - if (hasChildren) { - children.forEach((child) -> { - result.addChild(child.topDownTransform(transformPicker, transformer)); - }); - } + return result; + case SKIP: + return this; + case TRANSFORM: + return transformer.apply(this); + case RTRANSFORM: + return transformer.apply(this).topDownTransform(transformPicker, transformer); + case PUSHDOWN: + result = new Tree<>(data); + + if (hasChildren) { + children.forEach((child) -> { + result.addChild(child.topDownTransform(transformPicker, transformer)); + }); + } - return transformer.apply(result); - case PULLUP: - ITree<ContainedType> intermediateResult = transformer.apply(this); + return transformer.apply(result); + case PULLUP: + ITree<ContainedType> intermediateResult = transformer.apply(this); - result = new Tree<>(intermediateResult.getHead()); + result = new Tree<>(intermediateResult.getHead()); - intermediateResult.doForChildren((child) -> { - result.addChild(child.topDownTransform(transformPicker, transformer)); - }); + intermediateResult.doForChildren((child) -> { + result.addChild(child.topDownTransform(transformPicker, transformer)); + }); - return result; - default: - throw new IllegalArgumentException("Recieved unknown transform result " + transformResult); + return result; + default: + throw new IllegalArgumentException("Recieved unknown transform result " + transformResult); } } @@ -247,8 +242,8 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } @Override - public <TransformedType> TransformedType transformChild( - int childNo, Function<ITree<ContainedType>, TransformedType> transformer) { + public <TransformedType> TransformedType transformChild(int childNo, + Function<ITree<ContainedType>, TransformedType> transformer) { if (childNo < 0 || childNo > (childCount - 1)) { throw new IllegalArgumentException("Child index #" + childNo + " is invalid"); } @@ -264,7 +259,8 @@ public class Tree<ContainedType> implements ITree<ContainedType> { @Override public <MappedType> ITree<MappedType> transformTree(Function<ContainedType, MappedType> transformer) { if (hasChildren) { - IList<ITree<MappedType>> transformedChildren = children.map((child) -> child.transformTree(transformer)); + IList<ITree<MappedType>> transformedChildren = children + .map((child) -> child.transformTree(transformer)); return new Tree<>(transformer.apply(data), transformedChildren); } @@ -276,29 +272,30 @@ public class Tree<ContainedType> implements ITree<ContainedType> { public void traverse(TreeLinearizationMethod linearizationMethod, Consumer<ContainedType> action) { if (hasChildren) { switch (linearizationMethod) { - case INORDER: - if (childCount != 2) { - throw new IllegalArgumentException("Can only do in-order traversal for binary trees."); - } + case INORDER: + if (childCount != 2) { + throw new IllegalArgumentException( + "Can only do in-order traversal for binary trees."); + } - children.getByIndex(0).traverse(linearizationMethod, action); + children.getByIndex(0).traverse(linearizationMethod, action); - action.accept(data); + action.accept(data); - children.getByIndex(1).traverse(linearizationMethod, action); - break; - case POSTORDER: - children.forEach((child) -> child.traverse(linearizationMethod, action)); + children.getByIndex(1).traverse(linearizationMethod, action); + break; + case POSTORDER: + children.forEach((child) -> child.traverse(linearizationMethod, action)); - action.accept(data); - break; - case PREORDER: - action.accept(data); + action.accept(data); + break; + case PREORDER: + action.accept(data); - children.forEach((child) -> child.traverse(linearizationMethod, action)); - break; - default: - break; + children.forEach((child) -> child.traverse(linearizationMethod, action)); + break; + default: + break; } } else { @@ -307,24 +304,31 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } public boolean equals(Object other) { - if(!(other instanceof Tree<?>)) return false; + if (!(other instanceof Tree<?>)) + return false; @SuppressWarnings("unchecked") Tree<ContainedType> otr = (Tree<ContainedType>) other; - if(!otr.data.equals(data)) return false; + if (!otr.data.equals(data)) + return false; - if(children == null && otr.children == null) return true; + if (children == null && otr.children == null) + return true; - if(children == null && otr.children != null) return false; - if(children != null && otr.children == null) return false; + if (children == null && otr.children != null) + return false; + if (children != null && otr.children == null) + return false; - if(children.getSize() != otr.children.getSize()) return false; + if (children.getSize() != otr.children.getSize()) + return false; int childNo = 0; - for(ITree<ContainedType> child : children) { - if(!otr.children.getByIndex(childNo).equals(child)) return false; + for (ITree<ContainedType> child : children) { + if (!otr.children.getByIndex(childNo).equals(child)) + return false; childNo += 1; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java index beb2465..57bf006 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazy.java @@ -12,45 +12,42 @@ import bjc.utils.funcdata.IList; /** * Implements a lazy holder that has been bound */ -public class BoundLazy<OldType, BoundContainedType> - implements IHolder<BoundContainedType> { +public class BoundLazy<OldType, BoundContainedType> implements IHolder<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 */ - public BoundLazy(Supplier<IHolder<OldType>> supp, - Function<OldType, IHolder<BoundContainedType>> binder) { + public BoundLazy(Supplier<IHolder<OldType>> supp, Function<OldType, IHolder<BoundContainedType>> binder) { oldSupplier = supp; this.binder = binder; } @Override - public <BoundType> IHolder<BoundType> bind( - Function<BoundContainedType, IHolder<BoundType>> bindr) { + public <BoundType> IHolder<BoundType> bind(Function<BoundContainedType, IHolder<BoundType>> bindr) { if (bindr == null) { throw new NullPointerException("Binder must not be null"); } @@ -89,8 +86,7 @@ public class BoundLazy<OldType, BoundContainedType> public <NewType> Function<BoundContainedType, IHolder<NewType>> lift( Function<BoundContainedType, NewType> func) { if (func == null) { - throw new NullPointerException( - "Function to lift must not be null"); + throw new NullPointerException("Function to lift must not be null"); } return (val) -> { @@ -99,8 +95,7 @@ public class BoundLazy<OldType, BoundContainedType> } @Override - public <MappedType> IHolder<MappedType> map( - Function<BoundContainedType, MappedType> mapper) { + public <MappedType> IHolder<MappedType> map(Function<BoundContainedType, MappedType> mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -118,10 +113,9 @@ public class BoundLazy<OldType, BoundContainedType> oldHolder = oldSupplier.get().unwrap(binder); } - return pendingActions.reduceAux(oldHolder.getValue(), - (action, state) -> { - return action.apply(state); - }, (value) -> mapper.apply(value)); + return pendingActions.reduceAux(oldHolder.getValue(), (action, state) -> { + return action.apply(state); + }, (value) -> mapper.apply(value)); }; return new Lazy<>(typeSupplier); @@ -137,8 +131,7 @@ public class BoundLazy<OldType, BoundContainedType> } @Override - public IHolder<BoundContainedType> transform( - UnaryOperator<BoundContainedType> transformer) { + public IHolder<BoundContainedType> transform(UnaryOperator<BoundContainedType> transformer) { if (transformer == null) { throw new NullPointerException("Transformer must not be null"); } @@ -149,8 +142,7 @@ public class BoundLazy<OldType, BoundContainedType> } @Override - public <UnwrappedType> UnwrappedType unwrap( - Function<BoundContainedType, UnwrappedType> unwrapper) { + public <UnwrappedType> UnwrappedType unwrap(Function<BoundContainedType, UnwrappedType> unwrapper) { if (unwrapper == null) { throw new NullPointerException("Unwrapper must not be null"); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazyPair.java index 977fb37..d425d99 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundLazyPair.java @@ -12,34 +12,32 @@ import bjc.utils.data.LazyPair; /** * Implements a lazy pair that has been bound */ -public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> - implements IPair<NewLeft, NewRight> { +public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPair<NewLeft, NewRight> { /* * The supplier of the left value */ - private Supplier<OldLeft> leftSupplier; + private Supplier<OldLeft> leftSupplier; /* * The supplier of the right value */ - private Supplier<OldRight> rightSupplier; + private Supplier<OldRight> rightSupplier; /* * The binder to transform values */ - private BiFunction<OldLeft, OldRight, IPair<NewLeft, NewRight>> binder; + private BiFunction<OldLeft, OldRight, IPair<NewLeft, NewRight>> binder; /* * The bound pair */ - private IPair<NewLeft, NewRight> boundPair; + private IPair<NewLeft, NewRight> boundPair; /* * Whether the pair has been bound yet */ - private boolean pairBound; + private boolean pairBound; - public BoundLazyPair(Supplier<OldLeft> leftSupp, - Supplier<OldRight> rightSupp, + public BoundLazyPair(Supplier<OldLeft> leftSupp, Supplier<OldRight> rightSupp, BiFunction<OldLeft, OldRight, IPair<NewLeft, NewRight>> bindr) { leftSupplier = leftSupp; rightSupplier = rightSupp; @@ -53,14 +51,12 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> throw new NullPointerException("Binder must not be null"); } - IHolder<IPair<NewLeft, NewRight>> newPair = new Identity<>( - boundPair); + 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())); + newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); newPairMade.replace(true); } @@ -70,8 +66,7 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> Supplier<NewRight> rightSupp = () -> { if (!newPairMade.getValue()) { - newPair.replace(binder.apply(leftSupplier.get(), - rightSupplier.get())); + newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); newPairMade.replace(true); } @@ -93,8 +88,7 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> IPair<NewLeft, NewRight> newPair = boundPair; if (!pairBound) { - newPair = binder.apply(leftSupplier.get(), - rightSupplier.get()); + newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } return newPair.getLeft(); @@ -107,16 +101,14 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> public <BoundRight> IPair<NewLeft, BoundRight> bindRight( Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) { if (rightBinder == null) { - throw new NullPointerException( - "Right binder must not be null"); + throw new NullPointerException("Right binder must not be null"); } Supplier<NewRight> rightSupp = () -> { IPair<NewLeft, NewRight> newPair = boundPair; if (!pairBound) { - newPair = binder.apply(leftSupplier.get(), - rightSupplier.get()); + newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } return newPair.getRight(); @@ -133,34 +125,28 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> if (otherPair == null) { throw new NullPointerException("Other pair must not be null"); } else if (leftCombiner == null) { - throw new NullPointerException( - "Left combiner must not be null"); + throw new NullPointerException("Left combiner must not be null"); } else if (rightCombiner == null) { - throw new NullPointerException( - "Right combiner must not be null"); + throw new NullPointerException("Right combiner must not be null"); } return otherPair.bind((otherLeft, otherRight) -> { return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), + 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) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } Supplier<NewLeftType> leftSupp = () -> { if (!pairBound) { - NewLeft leftVal = binder - .apply(leftSupplier.get(), rightSupplier.get()) - .getLeft(); + NewLeft leftVal = binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); return mapper.apply(leftVal); } @@ -170,9 +156,7 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> Supplier<NewRight> rightSupp = () -> { if (!pairBound) { - return binder - .apply(leftSupplier.get(), rightSupplier.get()) - .getRight(); + return binder.apply(leftSupplier.get(), rightSupplier.get()).getRight(); } return boundPair.getRight(); @@ -182,17 +166,14 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> } @Override - public <NewRightType> IPair<NewLeft, NewRightType> mapRight( - Function<NewRight, NewRightType> mapper) { + public <NewRightType> IPair<NewLeft, NewRightType> mapRight(Function<NewRight, NewRightType> mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } Supplier<NewLeft> leftSupp = () -> { if (!pairBound) { - return binder - .apply(leftSupplier.get(), rightSupplier.get()) - .getLeft(); + return binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); } return boundPair.getLeft(); @@ -200,9 +181,7 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> Supplier<NewRightType> rightSupp = () -> { if (!pairBound) { - NewRight rightVal = binder - .apply(leftSupplier.get(), rightSupplier.get()) - .getRight(); + NewRight rightVal = binder.apply(leftSupplier.get(), rightSupplier.get()).getRight(); return mapper.apply(rightVal); } @@ -214,15 +193,13 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> } @Override - public <MergedType> MergedType merge( - BiFunction<NewLeft, NewRight, MergedType> merger) { + public <MergedType> MergedType merge(BiFunction<NewLeft, NewRight, MergedType> merger) { if (merger == null) { throw new NullPointerException("Merger must not be null"); } if (!pairBound) { - boundPair = binder.apply(leftSupplier.get(), - rightSupplier.get()); + boundPair = binder.apply(leftSupplier.get(), rightSupplier.get()); pairBound = true; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundListHolder.java index 7a3eaa8..f363b7e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/BoundListHolder.java @@ -18,26 +18,22 @@ public class BoundListHolder<ContainedType> implements IHolder<ContainedType> { } @Override - public <BoundType> IHolder<BoundType> bind( - Function<ContainedType, IHolder<BoundType>> binder) { + public <BoundType> IHolder<BoundType> bind(Function<ContainedType, IHolder<BoundType>> binder) { if (binder == null) { throw new NullPointerException("Binder must not be null"); } - IList<IHolder<BoundType>> boundHolders = heldHolders - .map((containedHolder) -> { - return containedHolder.bind(binder); - }); + IList<IHolder<BoundType>> boundHolders = heldHolders.map((containedHolder) -> { + return containedHolder.bind(binder); + }); return new BoundListHolder<>(boundHolders); } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift( - Function<ContainedType, NewType> func) { + public <NewType> Function<ContainedType, IHolder<NewType>> lift(Function<ContainedType, NewType> func) { if (func == null) { - throw new NullPointerException( - "Function to lift must not be null"); + throw new NullPointerException("Function to lift must not be null"); } return (val) -> { @@ -46,23 +42,20 @@ public class BoundListHolder<ContainedType> implements IHolder<ContainedType> { } @Override - public <MappedType> IHolder<MappedType> map( - Function<ContainedType, MappedType> mapper) { + public <MappedType> IHolder<MappedType> map(Function<ContainedType, MappedType> mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } - IList<IHolder<MappedType>> mappedHolders = heldHolders - .map((containedHolder) -> { - return containedHolder.map(mapper); - }); + IList<IHolder<MappedType>> mappedHolders = heldHolders.map((containedHolder) -> { + return containedHolder.map(mapper); + }); return new BoundListHolder<>(mappedHolders); } @Override - public IHolder<ContainedType> transform( - UnaryOperator<ContainedType> transformer) { + public IHolder<ContainedType> transform(UnaryOperator<ContainedType> transformer) { if (transformer == null) { throw new NullPointerException("Transformer must not be null"); } @@ -75,8 +68,7 @@ public class BoundListHolder<ContainedType> implements IHolder<ContainedType> { } @Override - public <UnwrappedType> UnwrappedType unwrap( - Function<ContainedType, UnwrappedType> unwrapper) { + public <UnwrappedType> UnwrappedType unwrap(Function<ContainedType, UnwrappedType> unwrapper) { if (unwrapper == null) { throw new NullPointerException("Unwrapper must not be null"); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java index 1e8d109..a8bdb6a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/HalfBoundLazyPair.java @@ -12,17 +12,15 @@ import bjc.utils.data.LazyPair; /* * A lazy pair, with only one side bound */ -public class HalfBoundLazyPair<OldType, NewLeft, NewRight> - implements IPair<NewLeft, NewRight> { - private Supplier<OldType> oldSupplier; +public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewLeft, NewRight> { + private Supplier<OldType> oldSupplier; - private Function<OldType, IPair<NewLeft, NewRight>> binder; + private Function<OldType, IPair<NewLeft, NewRight>> binder; - private IPair<NewLeft, NewRight> boundPair; - private boolean pairBound; + private IPair<NewLeft, NewRight> boundPair; + private boolean pairBound; - public HalfBoundLazyPair(Supplier<OldType> oldSupp, - Function<OldType, IPair<NewLeft, NewRight>> bindr) { + public HalfBoundLazyPair(Supplier<OldType> oldSupp, Function<OldType, IPair<NewLeft, NewRight>> bindr) { oldSupplier = oldSupp; binder = bindr; } @@ -30,8 +28,7 @@ public 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<IPair<NewLeft, NewRight>> newPair = new Identity<>(boundPair); IHolder<Boolean> newPairMade = new Identity<>(pairBound); Supplier<NewLeft> leftSupp = () -> { @@ -94,16 +91,14 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { return otherPair.bind((otherLeft, otherRight) -> { return bind((leftVal, rightVal) -> { - return new LazyPair<>( - leftCombiner.apply(leftVal, otherLeft), + 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()); @@ -126,8 +121,7 @@ public 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(); @@ -150,8 +144,7 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> } @Override - public <MergedType> MergedType merge( - BiFunction<NewLeft, NewRight, MergedType> merger) { + public <MergedType> MergedType merge(BiFunction<NewLeft, NewRight, MergedType> merger) { if (!pairBound) { boundPair = binder.apply(oldSupplier.get()); diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedOption.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedOption.java index 65c2463..2b03f62 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedOption.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedOption.java @@ -17,7 +17,8 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { held = toHold; } - @Override public <BoundType> IHolder<BoundType> bind(Function<ContainedType, IHolder<BoundType>> binder) { + @Override + public <BoundType> IHolder<BoundType> bind(Function<ContainedType, IHolder<BoundType>> binder) { IHolder<IHolder<BoundType>> newHolder = held.map((containedHolder) -> { return containedHolder.bind((containedValue) -> { if (containedValue == null) { |
