From 27bf571d6413c3cc6a5d664b5bddd38d21d7b1cd Mon Sep 17 00:00:00 2001 From: EVE Date: Mon, 13 Mar 2017 16:42:21 -0400 Subject: Formatting --- .../main/java/bjc/utils/data/CircularIterator.java | 4 +- .../src/main/java/bjc/utils/data/Either.java | 57 +++---- .../src/main/java/bjc/utils/data/IHolder.java | 45 +++--- BJC-Utils2/src/main/java/bjc/utils/data/IPair.java | 106 ++++++------- BJC-Utils2/src/main/java/bjc/utils/data/ITree.java | 92 +++++------ .../src/main/java/bjc/utils/data/Identity.java | 19 +-- BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java | 44 +++--- .../src/main/java/bjc/utils/data/LazyPair.java | 56 +++---- .../src/main/java/bjc/utils/data/ListHolder.java | 4 +- .../src/main/java/bjc/utils/data/Option.java | 4 +- BJC-Utils2/src/main/java/bjc/utils/data/Pair.java | 25 +-- .../main/java/bjc/utils/data/SingleSupplier.java | 21 ++- .../bjc/utils/data/TopDownTransformIterator.java | 165 ++++++++++---------- .../java/bjc/utils/data/TransformedIterator.java | 2 +- BJC-Utils2/src/main/java/bjc/utils/data/Tree.java | 170 +++++++++++---------- .../java/bjc/utils/data/internals/BoundLazy.java | 38 ++--- .../bjc/utils/data/internals/BoundLazyPair.java | 71 +++------ .../bjc/utils/data/internals/BoundListHolder.java | 32 ++-- .../utils/data/internals/HalfBoundLazyPair.java | 29 ++-- .../bjc/utils/data/internals/WrappedOption.java | 3 +- 20 files changed, 440 insertions(+), 547 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data') 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 implements Iterator { } 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 - * The type that could be on the left + * The type that could be on the left * @param - * The type that could be on the right + * The type that could be on the right * */ -public class Either - implements IPair { +public class Either implements IPair { /** * Create a new either with the left value occupied * * @param - * The type of the left value + * The type of the left value * @param - * 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 Either fromLeft( - LeftType left) { + public static Either fromLeft(LeftType left) { return new Either<>(left, null); } @@ -35,25 +33,24 @@ public class Either * Create a new either with the right value occupied * * @param - * The type of the empty left value + * The type of the empty left value * @param - * 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 Either fromRight( - RightType right) { + public static Either 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 public IPair bindRight( Function> 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 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 IPair mapLeft( - Function mapper) { + public IPair mapLeft(Function mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -145,8 +136,7 @@ public class Either } @Override - public IPair mapRight( - Function mapper) { + public IPair mapRight(Function mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -159,8 +149,7 @@ public class Either } @Override - public MergedType merge( - BiFunction merger) { + public MergedType merge(BiFunction 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 - * The type of value held + * The type of value held */ public interface IHolder extends Functor { /** * Bind a function across the value in this container * * @param - * 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 IHolder bind( - Function> binder); + public IHolder bind(Function> 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 action) { transform((value) -> { @@ -46,10 +45,8 @@ public interface IHolder extends Functor { } @Override - default Function, - Functor> fmap( - Function func) { + default Function, Functor> fmap( + Function func) { return (argumentFunctor) -> { if (!(argumentFunctor instanceof IHolder)) { throw new IllegalArgumentException( @@ -71,13 +68,12 @@ public interface IHolder extends Functor { * Lifts a function to bind over this holder * * @param - * 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 Function> lift( - Function func); + public Function> lift(Function func); /** * Make this holder lazy @@ -113,19 +109,18 @@ public interface IHolder extends Functor { * Does not change the internal state of this holder * * @param - * 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 IHolder map( - Function mapper); + public IHolder map(Function 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 replace(ContainedType newValue) { @@ -138,22 +133,20 @@ public interface IHolder extends Functor { * 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 transform( - UnaryOperator transformer); + public IHolder transform(UnaryOperator transformer); /** * Unwrap the value contained in this holder so that it is no longer * held * * @param - * The type of the unwrapped value + * 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 unwrap( - Function unwrapper); + public UnwrappedType unwrap(Function 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 - * The type of the left side of the pair + * The type of the left side of the pair * @param - * The type of the right side of the pair + * The type of the right side of the pair * */ -public interface IPair - extends Bifunctor { +public interface IPair extends Bifunctor { /** * Bind a function across the values in this pair * * @param - * The type of the bound left + * The type of the bound left * @param - * 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 IPair bind( - BiFunction> binder); + BiFunction> binder); /** * Bind a function to the left value in this pair * * @param - * 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 IPair bindLeft( @@ -49,9 +47,9 @@ public interface IPair * Bind a function to the right value in this pair * * @param - * 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 IPair bindRight( @@ -61,19 +59,16 @@ public interface IPair * Pairwise combine two pairs together * * @param - * The left type of the other pair + * The left type of the other pair * @param - * 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 IPair, - IPair> combine( - IPair otherPair) { - return combine(otherPair, - (left, otherLeft) -> new Pair<>(left, otherLeft), + public default IPair, IPair> combine( + IPair otherPair) { + return combine(otherPair, (left, otherLeft) -> new Pair<>(left, otherLeft), (right, otherRight) -> new Pair<>(right, otherRight)); } @@ -81,33 +76,30 @@ public interface IPair * Combine the contents of two pairs together * * @param - * The type of the left value of the other pair + * The type of the left value of the other pair * @param - * The type of the right value of the other pair + * The type of the right value of the other pair * @param - * The type of the left value of the combined pair + * The type of the left value of the combined pair * @param - * 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 IPair combine( - IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner); + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction 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 consumer) { merge((leftValue, rightValue) -> { @@ -118,26 +110,22 @@ public interface IPair } @Override - default Function, - Bifunctor> fmapLeft( - Function func) { + default Function, Bifunctor> fmapLeft( + Function func) { return (argumentPair) -> { if (!(argumentPair instanceof IPair)) { throw new IllegalArgumentException( "This function can only be applied to instances of IPair"); } - IPair argPair = (IPair) argumentPair; + IPair argPair = (IPair) argumentPair; return argPair.mapLeft(func); }; } @Override - default Function< - Bifunctor, Bifunctor> + default Function, Bifunctor> fmapRight(Function func) { return (argumentPair) -> { @@ -146,8 +134,7 @@ public interface IPair "This function can only be applied to instances of IPair"); } - IPair argPair = (IPair) argumentPair; + IPair argPair = (IPair) argumentPair; return argPair.mapRight(func); }; @@ -178,38 +165,35 @@ public interface IPair * pair * * @param - * 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 IPair mapLeft( - Function mapper); + public IPair mapLeft(Function 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 - * 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 IPair mapRight( - Function mapper); + public IPair mapRight(Function mapper); /** * Merge the two values in this pair into a single value * * @param - * 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 merge( - BiFunction merger); + public MergedType merge(BiFunction 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 - * The type of data contained in the tree nodes + * The type of data contained in the tree nodes * */ public interface ITree { @@ -21,7 +21,7 @@ public interface ITree { * 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 child); @@ -29,49 +29,46 @@ public interface ITree { * Collapse a tree into a single version * * @param - * The intermediate type being folded + * The intermediate type being folded * @param - * 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 ReturnedType collapse( - Function leafTransform, - Function, NewType>> nodeCollapser, + public ReturnedType collapse(Function leafTransform, + Function, NewType>> nodeCollapser, Function 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> 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 flatMapTree( - Function> mapper); + public ITree flatMapTree(Function> 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 getChild(int childNo) { @@ -98,56 +95,52 @@ public interface ITree { * Rebuild the tree with the same structure, but different nodes * * @param - * 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 ITree rebuildTree( - Function leafTransformer, + public ITree rebuildTree(Function leafTransformer, Function 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 nodePicker, - UnaryOperator transformer); + public void selectiveTransform(Predicate nodePicker, UnaryOperator 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 topDownTransform( - Function transformPicker, + public ITree topDownTransform(Function transformPicker, UnaryOperator> transformer); /** * Transform one of this nodes children * * @param - * 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 transformChild(int childNo, Function, TransformedType> transformer); @@ -156,34 +149,31 @@ public interface ITree { * Transform the value that is the head of this node * * @param - * 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 transformHead( - Function transformer); + public TransformedType transformHead(Function transformer); /** * Transform the tree into a tree with a different type of token * * @param - * 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 ITree transformTree( - Function transformer); + public ITree transformTree(Function 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 action); + public void traverse(TreeLinearizationMethod linearizationMethod, Consumer 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 - * The type contained in the holder + * The type contained in the holder */ public class Identity implements IHolder { private ContainedType heldValue; @@ -30,15 +30,14 @@ public class Identity implements IHolder { * 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 IHolder bind( - Function> binder) { + public IHolder bind(Function> binder) { return binder.apply(heldValue); } @@ -89,16 +88,14 @@ public class Identity implements IHolder { } @Override - public Function> lift( - Function func) { + public Function> lift(Function func) { return (val) -> { return new Identity<>(func.apply(val)); }; } @Override - public IHolder map( - Function mapper) { + public IHolder map(Function mapper) { return new Identity<>(mapper.apply(heldValue)); } @@ -108,16 +105,14 @@ public class Identity implements IHolder { } @Override - public IHolder transform( - UnaryOperator transformer) { + public IHolder transform(UnaryOperator transformer) { heldValue = transformer.apply(heldValue); return this; } @Override - public UnwrappedType unwrap( - Function unwrapper) { + public UnwrappedType unwrap(Function unwrapper) { return unwrapper.apply(heldValue); } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java 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 */ public class Lazy implements IHolder { - private Supplier valueSupplier; + private Supplier valueSupplier; - private IList> actions = new FunctionalList<>(); + private IList> 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 implements IHolder { * 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 supp) { valueSupplier = new SingleSupplier<>(supp); @@ -50,18 +49,15 @@ public class Lazy implements IHolder { valueMaterialized = false; } - private Lazy(Supplier supp, - IList> pendingActions) { + private Lazy(Supplier supp, IList> pendingActions) { valueSupplier = supp; actions = pendingActions; } @Override - public IHolder bind( - Function> binder) { - IList> pendingActions = new FunctionalList<>(); + public IHolder bind(Function> binder) { + IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); @@ -79,18 +75,15 @@ public class Lazy implements IHolder { } @Override - public Function> lift( - Function func) { + public Function> lift(Function func) { return (val) -> { return new Lazy<>(func.apply(val)); }; } @Override - public IHolder map( - Function mapper) { - IList> pendingActions = new FunctionalList<>(); + public IHolder map(Function mapper) { + IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); @@ -101,8 +94,7 @@ public class Lazy implements IHolder { currVal = valueSupplier.get(); } - return pendingActions.reduceAux(currVal, - UnaryOperator::apply, + return pendingActions.reduceAux(currVal, UnaryOperator::apply, (value) -> mapper.apply(value)); }); } @@ -121,16 +113,14 @@ public class Lazy implements IHolder { } @Override - public IHolder transform( - UnaryOperator transformer) { + public IHolder transform(UnaryOperator transformer) { actions.add(transformer); return this; } @Override - public UnwrappedType unwrap( - Function unwrapper) { + public UnwrappedType unwrap(Function 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 - * The type on the left side of the pair + * The type on the left side of the pair * @param - * The type on the right side of the pair + * The type on the right side of the pair * */ -public class LazyPair - implements IPair { - private LeftType leftValue; - private RightType rightValue; +public class LazyPair implements IPair { + private LeftType leftValue; + private RightType rightValue; - private Supplier leftSupplier; - private Supplier rightSupplier; + private Supplier leftSupplier; + private Supplier 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 * 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 leftSupp, - Supplier rightSupp) { + public LazyPair(Supplier leftSupp, Supplier rightSupp) { // Use single suppliers to catch double-instantiation bugs leftSupplier = new SingleSupplier<>(leftSupp); rightSupplier = new SingleSupplier<>(rightSupp); @@ -70,7 +68,8 @@ public class LazyPair } @Override - public IPair bindLeft(Function> leftBinder) { + public IPair bindLeft( + Function> leftBinder) { Supplier leftSupp = () -> { if (leftMaterialized) { return leftValue; @@ -97,17 +96,13 @@ public class LazyPair } @Override - public IPair combine( - IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction 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 } @Override - public IPair mapLeft( - Function mapper) { + public IPair mapLeft(Function mapper) { Supplier leftSupp = () -> { if (leftMaterialized) { return mapper.apply(leftValue); @@ -158,8 +152,7 @@ public class LazyPair } @Override - public IPair mapRight( - Function mapper) { + public IPair mapRight(Function mapper) { Supplier leftSupp = () -> { if (leftMaterialized) { return leftValue; @@ -180,8 +173,7 @@ public class LazyPair } @Override - public MergedType merge( - BiFunction merger) { + public MergedType merge(BiFunction 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 - * The type of contained value + * The type of contained value */ public class ListHolder implements IHolder { private IList heldValues; @@ -22,7 +22,7 @@ public class ListHolder implements IHolder { * 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 - * 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 implements IHolder { private ContainedType held; @@ -18,7 +18,7 @@ public class Option implements IHolder { * 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 - * The type of the left value + * The type of the left value * @param - * The type of the right value + * The type of the right value */ public class Pair implements IPair { // 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 implements IPair { * 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 implements IPair { } @Override - public IPair bindLeft(Function> leftBinder) { + public IPair bindLeft( + Function> leftBinder) { if (leftBinder == null) { throw new NullPointerException("Binder must not be null"); } @@ -60,7 +61,8 @@ public class Pair implements IPair { } @Override - public IPair bindRight(Function> rightBinder) { + public IPair bindRight( + Function> rightBinder) { if (rightBinder == null) { throw new NullPointerException("Binder must not be null"); } @@ -73,9 +75,10 @@ public class Pair implements IPair { IPair otherPair, BiFunction leftCombiner, BiFunction 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 */ public class SingleSupplier implements Supplier { - private static long nextID = 0; + private static long nextID = 0; - private Supplier source; + private Supplier 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 supp) { source = supp; @@ -42,10 +41,8 @@ public class SingleSupplier implements Supplier { @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 implements Iterator> { private Function picker; - private BiFunction, - Consumer>>, - ITree> transform; + private BiFunction, Consumer>>, ITree> transform; private ITree preParent; private ITree postParent; - + private Deque> preChildren; private Deque> postChildren; @@ -28,29 +26,29 @@ public class TopDownTransformIterator implements Iterator>> toYield; - private Iterator> curYield; + private Iterator> curYield; - public TopDownTransformIterator(Function pickr, + public TopDownTransformIterator(Function pickr, BiFunction, Consumer>>, ITree> transfrm, ITree 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> src) { - if(curYield != null) { + if (curYield != null) { toYield.push(curYield); } - + curYield = src; } @@ -59,17 +57,17 @@ public class TopDownTransformIterator implements Iterator flushYields(ITree 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 implements Iterator 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 implements Iterator(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 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 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 res = curChild.next(); System.out.println("\t\tTRACE: adding node " + res + " to children"); postChildren.add(res); @@ -175,21 +175,22 @@ public class TopDownTransformIterator implements Iterator 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 child : postChildren) { + for (ITree 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 child : postChildren) { + for (ITree 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 implements Iterator src, Function 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 */ public class Tree implements ITree { - private ContainedType data; + private ContainedType data; - private IList> children; - private boolean hasChildren; - private int childCount = 0; + private IList> children; + private boolean hasChildren; + private int childCount = 0; private int ID; private static int nextID = 0; @@ -30,7 +30,7 @@ public class Tree implements ITree { * 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 implements ITree { * 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> childrn) { this(leaf); @@ -60,9 +60,9 @@ public class Tree implements ITree { * 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... childrn) { @@ -95,8 +95,7 @@ public class Tree implements ITree { } @Override - public ReturnedType collapse( - Function leafTransform, + public ReturnedType collapse(Function leafTransform, Function, NewType>> nodeCollapser, Function resultTransformer) { @@ -127,14 +126,13 @@ public class Tree implements ITree { return childCount; } - protected NewType internalCollapse( - Function leafTransform, + protected NewType internalCollapse(Function leafTransform, Function, NewType>> nodeCollapser) { if (hasChildren) { Function, NewType> nodeTransformer = nodeCollapser.apply(data); IList collapsedChildren = (IList) 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 implements ITree { } 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 implements ITree { if (hasChildren) { children.forEach((child) -> { - ((Tree) child).internalToString(builder, indentLevel+1, false); + ((Tree) child).internalToString(builder, indentLevel + 1, false); }); } } @Override - public ITree rebuildTree( - Function leafTransformer, + public ITree rebuildTree(Function leafTransformer, Function operatorTransformer) { if (hasChildren) { IList> 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 implements ITree { } @Override - public ITree topDownTransform( - Function transformPicker, + public ITree topDownTransform(Function transformPicker, UnaryOperator> transformer) { TopDownTransformResult transformResult = transformPicker.apply(data); switch (transformResult) { - case PASSTHROUGH: - ITree result = new Tree<>(data); + case PASSTHROUGH: + ITree 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 intermediateResult = transformer.apply(this); + return transformer.apply(result); + case PULLUP: + ITree 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 implements ITree { } @Override - public TransformedType transformChild( - int childNo, Function, TransformedType> transformer) { + public TransformedType transformChild(int childNo, + Function, TransformedType> transformer) { if (childNo < 0 || childNo > (childCount - 1)) { throw new IllegalArgumentException("Child index #" + childNo + " is invalid"); } @@ -264,7 +259,8 @@ public class Tree implements ITree { @Override public ITree transformTree(Function transformer) { if (hasChildren) { - IList> transformedChildren = children.map((child) -> child.transformTree(transformer)); + IList> transformedChildren = children + .map((child) -> child.transformTree(transformer)); return new Tree<>(transformer.apply(data), transformedChildren); } @@ -276,29 +272,30 @@ public class Tree implements ITree { public void traverse(TreeLinearizationMethod linearizationMethod, Consumer 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 implements ITree { } public boolean equals(Object other) { - if(!(other instanceof Tree)) return false; + if (!(other instanceof Tree)) + return false; @SuppressWarnings("unchecked") Tree otr = (Tree) 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 child : children) { - if(!otr.children.getByIndex(childNo).equals(child)) return false; + for (ITree 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 - implements IHolder { +public class BoundLazy implements IHolder { /* * The old value */ - private Supplier> oldSupplier; + private Supplier> oldSupplier; /* * The function to use to transform the old value into a new value */ - private Function> binder; + private Function> binder; /* * The bound value being held */ - private IHolder boundHolder; + private IHolder 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> actions = new FunctionalList<>(); + private IList> actions = new FunctionalList<>(); /* * Create a new bound lazy value */ - public BoundLazy(Supplier> supp, - Function> binder) { + public BoundLazy(Supplier> supp, Function> binder) { oldSupplier = supp; this.binder = binder; } @Override - public IHolder bind( - Function> bindr) { + public IHolder bind(Function> bindr) { if (bindr == null) { throw new NullPointerException("Binder must not be null"); } @@ -89,8 +86,7 @@ public class BoundLazy public Function> lift( Function 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 } @Override - public IHolder map( - Function mapper) { + public IHolder map(Function mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } @@ -118,10 +113,9 @@ public class BoundLazy 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 } @Override - public IHolder transform( - UnaryOperator transformer) { + public IHolder transform(UnaryOperator transformer) { if (transformer == null) { throw new NullPointerException("Transformer must not be null"); } @@ -149,8 +142,7 @@ public class BoundLazy } @Override - public UnwrappedType unwrap( - Function unwrapper) { + public UnwrappedType unwrap(Function 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 - implements IPair { +public class BoundLazyPair implements IPair { /* * The supplier of the left value */ - private Supplier leftSupplier; + private Supplier leftSupplier; /* * The supplier of the right value */ - private Supplier rightSupplier; + private Supplier rightSupplier; /* * The binder to transform values */ - private BiFunction> binder; + private BiFunction> binder; /* * The bound pair */ - private IPair boundPair; + private IPair boundPair; /* * Whether the pair has been bound yet */ - private boolean pairBound; + private boolean pairBound; - public BoundLazyPair(Supplier leftSupp, - Supplier rightSupp, + public BoundLazyPair(Supplier leftSupp, Supplier rightSupp, BiFunction> bindr) { leftSupplier = leftSupp; rightSupplier = rightSupp; @@ -53,14 +51,12 @@ public class BoundLazyPair throw new NullPointerException("Binder must not be null"); } - IHolder> newPair = new Identity<>( - boundPair); + IHolder> newPair = new Identity<>(boundPair); IHolder newPairMade = new Identity<>(pairBound); Supplier 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 Supplier 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 IPair 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 public IPair bindRight( Function> rightBinder) { if (rightBinder == null) { - throw new NullPointerException( - "Right binder must not be null"); + throw new NullPointerException("Right binder must not be null"); } Supplier rightSupp = () -> { IPair 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 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 IPair mapLeft( - Function mapper) { + public IPair mapLeft(Function mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } Supplier 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 Supplier 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 } @Override - public IPair mapRight( - Function mapper) { + public IPair mapRight(Function mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } Supplier 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 Supplier 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 } @Override - public MergedType merge( - BiFunction merger) { + public MergedType merge(BiFunction 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 implements IHolder { } @Override - public IHolder bind( - Function> binder) { + public IHolder bind(Function> binder) { if (binder == null) { throw new NullPointerException("Binder must not be null"); } - IList> boundHolders = heldHolders - .map((containedHolder) -> { - return containedHolder.bind(binder); - }); + IList> boundHolders = heldHolders.map((containedHolder) -> { + return containedHolder.bind(binder); + }); return new BoundListHolder<>(boundHolders); } @Override - public Function> lift( - Function func) { + public Function> lift(Function 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 implements IHolder { } @Override - public IHolder map( - Function mapper) { + public IHolder map(Function mapper) { if (mapper == null) { throw new NullPointerException("Mapper must not be null"); } - IList> mappedHolders = heldHolders - .map((containedHolder) -> { - return containedHolder.map(mapper); - }); + IList> mappedHolders = heldHolders.map((containedHolder) -> { + return containedHolder.map(mapper); + }); return new BoundListHolder<>(mappedHolders); } @Override - public IHolder transform( - UnaryOperator transformer) { + public IHolder transform(UnaryOperator transformer) { if (transformer == null) { throw new NullPointerException("Transformer must not be null"); } @@ -75,8 +68,7 @@ public class BoundListHolder implements IHolder { } @Override - public UnwrappedType unwrap( - Function unwrapper) { + public UnwrappedType unwrap(Function 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 - implements IPair { - private Supplier oldSupplier; +public class HalfBoundLazyPair implements IPair { + private Supplier oldSupplier; - private Function> binder; + private Function> binder; - private IPair boundPair; - private boolean pairBound; + private IPair boundPair; + private boolean pairBound; - public HalfBoundLazyPair(Supplier oldSupp, - Function> bindr) { + public HalfBoundLazyPair(Supplier oldSupp, Function> bindr) { oldSupplier = oldSupp; binder = bindr; } @@ -30,8 +28,7 @@ public class HalfBoundLazyPair @Override public IPair bind( BiFunction> bindr) { - IHolder> newPair = new Identity<>( - boundPair); + IHolder> newPair = new Identity<>(boundPair); IHolder newPairMade = new Identity<>(pairBound); Supplier leftSupp = () -> { @@ -94,16 +91,14 @@ public class HalfBoundLazyPair BiFunction 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 IPair mapLeft( - Function mapper) { + public IPair mapLeft(Function mapper) { Supplier leftSupp = () -> { if (pairBound) { return mapper.apply(boundPair.getLeft()); @@ -126,8 +121,7 @@ public class HalfBoundLazyPair } @Override - public IPair mapRight( - Function mapper) { + public IPair mapRight(Function mapper) { Supplier leftSupp = () -> { if (pairBound) { return boundPair.getLeft(); @@ -150,8 +144,7 @@ public class HalfBoundLazyPair } @Override - public MergedType merge( - BiFunction merger) { + public MergedType merge(BiFunction 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 implements IHolder { held = toHold; } - @Override public IHolder bind(Function> binder) { + @Override + public IHolder bind(Function> binder) { IHolder> newHolder = held.map((containedHolder) -> { return containedHolder.bind((containedValue) -> { if (containedValue == null) { -- cgit v1.2.3