From 889fac2bdf993dc86f64a8893c0260fdcf848acb Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:40:33 -0400 Subject: Cleanup --- .../main/java/bjc/utils/data/BooleanToggle.java | 22 ++-- .../main/java/bjc/utils/data/CircularIterator.java | 17 ++- .../src/main/java/bjc/utils/data/Either.java | 96 ++++++-------- .../src/main/java/bjc/utils/data/IHolder.java | 18 +-- BJC-Utils2/src/main/java/bjc/utils/data/IPair.java | 24 ++-- BJC-Utils2/src/main/java/bjc/utils/data/ITree.java | 64 +++++----- .../src/main/java/bjc/utils/data/Identity.java | 41 +++--- BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java | 84 ++++++------ .../src/main/java/bjc/utils/data/LazyPair.java | 105 +++++++-------- .../src/main/java/bjc/utils/data/ListHolder.java | 47 +++---- .../src/main/java/bjc/utils/data/Option.java | 42 +++--- BJC-Utils2/src/main/java/bjc/utils/data/Pair.java | 71 +++++------ .../main/java/bjc/utils/data/SingleIterator.java | 8 +- .../main/java/bjc/utils/data/SingleSupplier.java | 12 +- .../src/main/java/bjc/utils/data/Toggle.java | 8 +- .../bjc/utils/data/TopDownTransformIterator.java | 92 +++++++------ .../java/bjc/utils/data/TransformIterator.java | 14 +- BJC-Utils2/src/main/java/bjc/utils/data/Tree.java | 142 ++++++++++----------- .../src/main/java/bjc/utils/data/ValueToggle.java | 28 ++-- .../java/bjc/utils/data/internals/BoundLazy.java | 35 ++--- .../bjc/utils/data/internals/BoundLazyPair.java | 99 +++++++------- .../bjc/utils/data/internals/BoundListHolder.java | 34 ++--- .../utils/data/internals/HalfBoundLazyPair.java | 75 +++++------ .../java/bjc/utils/data/internals/WrappedLazy.java | 26 ++-- .../bjc/utils/data/internals/WrappedOption.java | 34 ++--- 25 files changed, 573 insertions(+), 665 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BooleanToggle.java b/BJC-Utils2/src/main/java/bjc/utils/data/BooleanToggle.java index 68399a0..12e3b2e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BooleanToggle.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BooleanToggle.java @@ -2,7 +2,7 @@ package bjc.utils.data; /** * A simple {@link ValueToggle} that swaps between true and false. - * + * * @author EVE * */ @@ -18,17 +18,17 @@ public class BooleanToggle implements Toggle { /** * Create a flip-flop with the specified initial value. - * + * * @param initial * The initial value of the flip-flop. */ - public BooleanToggle(boolean initial) { + public BooleanToggle(final boolean initial) { val = initial; } @Override public Boolean get() { - boolean res = val; + final boolean res = val; val = !res; @@ -41,7 +41,7 @@ public class BooleanToggle implements Toggle { } @Override - public void set(boolean vl) { + public void set(final boolean vl) { val = vl; } @@ -57,14 +57,14 @@ public class BooleanToggle implements Toggle { } @Override - public boolean equals(Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof BooleanToggle)) return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof BooleanToggle)) return false; - BooleanToggle other = (BooleanToggle) obj; + final BooleanToggle other = (BooleanToggle) obj; - if(val != other.val) return false; + if (val != other.val) return false; return true; } 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 4b84243..a708eba 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java @@ -4,7 +4,7 @@ import java.util.Iterator; /** * An iterator that repeats elements from a provided iterable. - * + * * @author EVE * * @param @@ -30,15 +30,15 @@ public class CircularIterator implements Iterator { /** * Create a new circular iterator. - * + * * @param src * The iterable to iterate from. - * + * * @param circ * Should we actually do circular iteration, or just * repeat the terminal element? */ - public CircularIterator(Iterable src, boolean circ) { + public CircularIterator(final Iterable src, final boolean circ) { source = src; curr = source.iterator(); @@ -47,11 +47,11 @@ public class CircularIterator implements Iterator { /** * Create a new circular iterator that does actual circular iteration. - * + * * @param src * The iterable to iterate from. */ - public CircularIterator(Iterable src) { + public CircularIterator(final Iterable src) { this(src, true); } @@ -64,10 +64,9 @@ public class CircularIterator implements Iterator { @Override public E next() { if (!curr.hasNext()) { - if (doCircle) + if (doCircle) { curr = source.iterator(); - else - return curElm; + } else return curElm; } curElm = curr.next(); 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 d07fbbe..36b3324 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java @@ -25,7 +25,7 @@ public class Either implements IPair { * The value to put on the left * @return An either with the left side occupied */ - public static Either left(LeftType left) { + public static Either left(final LeftType left) { return new Either<>(left, null); } @@ -40,7 +40,7 @@ public class Either implements IPair { * The value to put on the right * @return An either with the right side occupied */ - public static Either right(RightType right) { + public static Either right(final RightType right) { return new Either<>(null, right); } @@ -50,7 +50,7 @@ public class Either implements IPair { private boolean isLeft; - private Either(LeftType left, RightType right) { + private Either(final LeftType left, final RightType right) { if (left == null) { rightVal = right; } else { @@ -62,53 +62,46 @@ public class Either implements IPair { @Override public IPair bind( - BiFunction> binder) { - if (binder == null) - throw new NullPointerException("Binder must not be null"); + final BiFunction> binder) { + if (binder == null) throw new NullPointerException("Binder must not be null"); return binder.apply(leftVal, rightVal); } @Override public IPair bindLeft( - Function> leftBinder) { - if (leftBinder == null) - throw new NullPointerException("Left binder must not be null"); + final Function> leftBinder) { + if (leftBinder == null) throw new NullPointerException("Left binder must not be null"); - if (isLeft) - return leftBinder.apply(leftVal); + if (isLeft) return leftBinder.apply(leftVal); return new Either<>(null, rightVal); } @Override public IPair bindRight( - Function> rightBinder) { - if (rightBinder == null) - throw new NullPointerException("Right binder must not be null"); + final Function> rightBinder) { + if (rightBinder == null) throw new NullPointerException("Right binder must not be null"); - if (isLeft) - return new Either<>(leftVal, null); + if (isLeft) return new Either<>(leftVal, null); return rightBinder.apply(rightVal); } @Override public IPair combine( - IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { + final IPair otherPair, + final BiFunction leftCombiner, + final BiFunction rightCombiner) { 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"); - else if (rightCombiner == null) - throw new NullPointerException("Right combiner must not be null"); + else if (rightCombiner == 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); - }); + if (isLeft) return otherPair.bind((otherLeft, otherRight) -> { + return new Either<>(leftCombiner.apply(leftVal, otherLeft), null); + }); return otherPair.bind((otherLeft, otherRight) -> { return new Either<>(null, rightCombiner.apply(rightVal, otherRight)); @@ -116,31 +109,26 @@ public class Either implements IPair { } @Override - public IPair mapLeft(Function mapper) { - if (mapper == null) - throw new NullPointerException("Mapper must not be null"); + public IPair mapLeft(final Function mapper) { + if (mapper == null) throw new NullPointerException("Mapper must not be null"); - if (isLeft) - return new Either<>(mapper.apply(leftVal), null); + if (isLeft) return new Either<>(mapper.apply(leftVal), null); return new Either<>(null, rightVal); } @Override - public IPair mapRight(Function mapper) { - if (mapper == null) - throw new NullPointerException("Mapper must not be null"); + public IPair mapRight(final Function mapper) { + if (mapper == null) throw new NullPointerException("Mapper must not be null"); - if (isLeft) - return new Either<>(leftVal, null); + if (isLeft) return new Either<>(leftVal, null); return new Either<>(null, mapper.apply(rightVal)); } @Override - public MergedType merge(BiFunction merger) { - if (merger == null) - throw new NullPointerException("Merger must not be null"); + public MergedType merge(final BiFunction merger) { + if (merger == null) throw new NullPointerException("Merger must not be null"); return merger.apply(leftVal, rightVal); } @@ -151,37 +139,29 @@ public class Either implements IPair { int result = 1; result = prime * result + (isLeft ? 1231 : 1237); - result = prime * result + ((leftVal == null) ? 0 : leftVal.hashCode()); - result = prime * result + ((rightVal == null) ? 0 : rightVal.hashCode()); + result = prime * result + (leftVal == null ? 0 : leftVal.hashCode()); + result = prime * result + (rightVal == null ? 0 : rightVal.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof Either)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof Either)) return false; - Either other = (Either) obj; + final Either other = (Either) obj; - if (isLeft != other.isLeft) - return false; + if (isLeft != other.isLeft) return false; if (leftVal == null) { - if (other.leftVal != null) - return false; - } else if (!leftVal.equals(other.leftVal)) - return false; + if (other.leftVal != null) return false; + } else if (!leftVal.equals(other.leftVal)) return false; if (rightVal == null) { - if (other.rightVal != null) - return false; - } else if (!rightVal.equals(other.rightVal)) - return false; + if (other.rightVal != null) return false; + } else if (!rightVal.equals(other.rightVal)) return false; return true; } 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 1d380c8..ca0b2ba 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -1,15 +1,15 @@ package bjc.utils.data; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.UnaryOperator; + import bjc.utils.data.internals.BoundListHolder; import bjc.utils.data.internals.WrappedLazy; import bjc.utils.data.internals.WrappedOption; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.theory.Functor; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.UnaryOperator; - /** * A holder of a single value. * @@ -36,7 +36,7 @@ public interface IHolder extends Functor { * @param action * The action to apply to the value */ - public default void doWith(Consumer action) { + public default void doWith(final Consumer action) { transform(value -> { action.accept(value); @@ -46,15 +46,15 @@ public interface IHolder extends Functor { @Override default Function, Functor> fmap( - Function func) { + final Function func) { return argumentFunctor -> { if (!(argumentFunctor instanceof IHolder)) { - String msg = "This functor only supports mapping over instances of IHolder"; + final String msg = "This functor only supports mapping over instances of IHolder"; throw new IllegalArgumentException(msg); } - IHolder holder = (IHolder) argumentFunctor; + final IHolder holder = (IHolder) argumentFunctor; return holder.map(func); }; @@ -124,7 +124,7 @@ public interface IHolder extends Functor { * The value to hold instead * @return The holder itself */ - public default IHolder replace(ContainedType newValue) { + public default IHolder replace(final ContainedType newValue) { return transform(oldValue -> { return newValue; }); diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java index ae54a88..db8a1cb 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -1,11 +1,11 @@ package bjc.utils.data; -import bjc.utils.funcdata.theory.Bifunctor; - import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Function; +import bjc.utils.funcdata.theory.Bifunctor; + /** * Represents a pair of values * @@ -67,7 +67,7 @@ public interface IPair extends Bifunctor IPair, IPair> combine( - IPair otherPair) { + final IPair otherPair) { return combine(otherPair, Pair::new, Pair::new); } @@ -100,7 +100,7 @@ public interface IPair extends Bifunctor consumer) { + public default void doWith(final BiConsumer consumer) { merge((leftValue, rightValue) -> { consumer.accept(leftValue, rightValue); @@ -110,15 +110,15 @@ public interface IPair extends Bifunctor LeftBifunctorMap fmapLeft( - Function func) { + final Function func) { return argumentPair -> { - if(!(argumentPair instanceof IPair)) { - String msg = "This function can only be applied to instances of IPair"; + if (!(argumentPair instanceof IPair)) { + final String msg = "This function can only be applied to instances of IPair"; throw new IllegalArgumentException(msg); } - IPair argPair = (IPair) argumentPair; + final IPair argPair = (IPair) argumentPair; return argPair.mapLeft(func); }; @@ -127,15 +127,15 @@ public interface IPair extends Bifunctor RightBifunctorMap - fmapRight(Function func) { + fmapRight(final Function func) { return argumentPair -> { - if(!(argumentPair instanceof IPair)) { - String msg = "This function can only be applied to instances of IPair"; + if (!(argumentPair instanceof IPair)) { + final String msg = "This function can only be applied to instances of IPair"; throw new IllegalArgumentException(msg); } - IPair argPair = (IPair) argumentPair; + final IPair argPair = (IPair) argumentPair; return argPair.mapRight(func); }; 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 166fe3f..ff374e8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java @@ -1,18 +1,18 @@ package bjc.utils.data; -import bjc.utils.funcdata.bst.TreeLinearizationMethod; -import bjc.utils.functypes.ListFlattener; - import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.UnaryOperator; +import bjc.utils.funcdata.bst.TreeLinearizationMethod; +import bjc.utils.functypes.ListFlattener; + /** * A node in a homogeneous tree with a unlimited amount of children. * * @author ben - * + * * @param * The type of data contained in the tree nodes. * @@ -28,7 +28,7 @@ public interface ITree { /** * Prepend a child to this node. - * + * * @param child * The child to prepend to this node. */ @@ -39,21 +39,21 @@ public interface ITree { * * @param * The intermediate type being folded. - * + * * @param * The type that is the end result. - * + * * @param leafTransform * The function to use to convert leaf values. - * + * * @param nodeCollapser * 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. - * + * * @return The final transformed state. */ ReturnedType collapse(Function leafTransform, @@ -74,13 +74,13 @@ public interface ITree { * * @param mapper * The function to use to map values into trees. - * + * * @return A tree, with some nodes expanded into trees. */ - default ITree flatMapTree(Function> mapper) { + default ITree flatMapTree(final Function> mapper) { return topDownTransform(dat -> TopDownTransformResult.PUSHDOWN, node -> { if (node.getChildrenCount() > 0) { - ITree parent = node.transformHead(mapper); + final ITree parent = node.transformHead(mapper); node.doForChildren(parent::addChild); @@ -96,10 +96,10 @@ public interface ITree { * * @param childNo * The number of the child to get. - * + * * @return The specified child of this tree. */ - default ITree getChild(int childNo) { + default ITree getChild(final int childNo) { return transformChild(childNo, child -> child); } @@ -124,13 +124,13 @@ public interface ITree { * * @param * The type of the new tree. - * + * * @param leafTransformer * The function to use to transform leaf tokens. - * + * * @param operatorTransformer * The function to use to transform internal tokens. - * + * * @return The tree, with the nodes changed. */ ITree rebuildTree(Function leafTransformer, @@ -141,7 +141,7 @@ public interface ITree { * * @param nodePicker * The predicate to use to pick nodes to transform. - * + * * @param transformer * The function to use to transform picked nodes. */ @@ -152,10 +152,10 @@ public interface ITree { * * @param transformPicker * The function to use to pick how to progress. - * + * * @param transformer * The function used to transform picked subtrees. - * + * * @return The tree with the transform applied to picked subtrees. */ ITree topDownTransform(Function transformPicker, @@ -166,13 +166,13 @@ public interface ITree { * * @param * The type of the transformed value. - * + * * @param childNo * The number of the child to transform. - * + * * @param transformer * The function to use to transform the value. - * + * * @return The transformed value. * * @throws IllegalArgumentException @@ -187,10 +187,10 @@ public interface ITree { * * @param * The type of the transformed value. - * + * * @param transformer * The function to use to transform the value. - * + * * @return The transformed value. */ TransformedType transformHead(Function transformer); @@ -200,13 +200,13 @@ public interface ITree { * * @param * The type of the new tree. - * + * * @param transformer * The function to use to transform tokens. - * + * * @return A tree with the token types transformed. */ - default ITree transformTree(Function transformer) { + default ITree transformTree(final Function transformer) { return rebuildTree(transformer, transformer); } @@ -215,7 +215,7 @@ public interface ITree { * * @param linearizationMethod * The way to traverse the tree. - * + * * @param action * The action to perform on each tree node. */ @@ -223,10 +223,10 @@ public interface ITree { /** * Find the farthest to right child that satisfies the given predicate. - * + * * @param childPred * The predicate to satisfy. - * + * * @return The index of the right-most child that satisfies the * predicate, or -1 if one doesn't exist. */ 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 77e13cf..a8c8d70 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java @@ -32,12 +32,12 @@ public class Identity implements IHolder { * @param value * The value to hold */ - public Identity(ContainedType value) { + public Identity(final ContainedType value) { heldValue = value; } @Override - public IHolder bind(Function> binder) { + public IHolder bind(final Function> binder) { return binder.apply(heldValue); } @@ -46,40 +46,35 @@ public class Identity implements IHolder { final int prime = 31; int result = 1; - result = prime * result + ((heldValue == null) ? 0 : heldValue.hashCode()); + result = prime * result + (heldValue == null ? 0 : heldValue.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof Identity)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof Identity)) return false; - Identity other = (Identity) obj; + final Identity other = (Identity) obj; if (heldValue == null) { - if (other.heldValue != null) - return false; - } else if (!heldValue.equals(other.heldValue)) - return false; + if (other.heldValue != null) return false; + } else if (!heldValue.equals(other.heldValue)) return false; return true; } @Override - public Function> lift(Function func) { + public Function> lift(final Function func) { return (val) -> { return new Identity<>(func.apply(val)); }; } @Override - public IHolder map(Function mapper) { + public IHolder map(final Function mapper) { return new Identity<>(mapper.apply(heldValue)); } @@ -89,32 +84,32 @@ public class Identity implements IHolder { } @Override - public IHolder transform(UnaryOperator transformer) { + public IHolder transform(final UnaryOperator transformer) { heldValue = transformer.apply(heldValue); return this; } @Override - public UnwrappedType unwrap(Function unwrapper) { + public UnwrappedType unwrap(final Function unwrapper) { return unwrapper.apply(heldValue); } /** * Create a new identity container. - * + * * @param val * The contained value. - * + * * @return A new identity container. */ - public static Identity id(ContainedType val) { + public static Identity id(final ContainedType val) { return new Identity<>(val); } /** * Create a new empty identity container. - * + * * @return A new empty identity container. */ public static Identity id() { 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 719b11f..ca41b62 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -1,13 +1,13 @@ package bjc.utils.data; -import bjc.utils.data.internals.BoundLazy; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; - import java.util.function.Function; import java.util.function.Supplier; import java.util.function.UnaryOperator; +import bjc.utils.data.internals.BoundLazy; +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 @@ -31,7 +31,7 @@ public class Lazy implements IHolder { * @param value * The seed value to use */ - public Lazy(ContainedType value) { + public Lazy(final ContainedType value) { heldValue = value; valueMaterialized = true; @@ -43,27 +43,26 @@ public class Lazy implements IHolder { * @param supp * The source of a value to use */ - public Lazy(Supplier supp) { + public Lazy(final Supplier supp) { valueSupplier = new SingleSupplier<>(supp); valueMaterialized = false; } - private Lazy(Supplier supp, IList> pendingActions) { + private Lazy(final Supplier supp, final IList> pendingActions) { valueSupplier = supp; actions = pendingActions; } @Override - public IHolder bind(Function> binder) { - IList> pendingActions = new FunctionalList<>(); + public IHolder bind(final Function> binder) { + final IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); - Supplier supplier = () -> { - if (valueMaterialized) - return heldValue; + final Supplier supplier = () -> { + if (valueMaterialized) return heldValue; return valueSupplier.get(); }; @@ -74,15 +73,15 @@ public class Lazy implements IHolder { } @Override - public Function> lift(Function func) { + public Function> lift(final Function func) { return val -> { return new Lazy<>(func.apply(val)); }; } @Override - public IHolder map(Function mapper) { - IList> pendingActions = new FunctionalList<>(); + public IHolder map(final Function mapper) { + final IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); @@ -103,22 +102,21 @@ public class Lazy implements IHolder { if (valueMaterialized) { if (actions.isEmpty()) return String.format("value[v='%s']", heldValue); - else - return String.format("value[v='%s'] (has pending transforms)", heldValue); + else return String.format("value[v='%s'] (has pending transforms)", heldValue); } return "(unmaterialized)"; } @Override - public IHolder transform(UnaryOperator transformer) { + public IHolder transform(final UnaryOperator transformer) { actions.add(transformer); return this; } @Override - public UnwrappedType unwrap(Function unwrapper) { + public UnwrappedType unwrap(final Function unwrapper) { if (!valueMaterialized) { heldValue = valueSupplier.get(); @@ -139,68 +137,58 @@ public class Lazy implements IHolder { final int prime = 31; int result = 1; - result = prime * result + ((actions == null) ? 0 : actions.hashCode()); - result = prime * result + ((heldValue == null) ? 0 : heldValue.hashCode()); + result = prime * result + (actions == null ? 0 : actions.hashCode()); + result = prime * result + (heldValue == null ? 0 : heldValue.hashCode()); result = prime * result + (valueMaterialized ? 1231 : 1237); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof Lazy)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof Lazy)) return false; - Lazy other = (Lazy) obj; + final Lazy other = (Lazy) obj; - if (valueMaterialized != other.valueMaterialized) - return false; + if (valueMaterialized != other.valueMaterialized) return false; if (valueMaterialized) { if (heldValue == null) { - if (other.heldValue != null) - return false; - } else if (!heldValue.equals(other.heldValue)) - return false; - } else { - return false; - } + if (other.heldValue != null) return false; + } else if (!heldValue.equals(other.heldValue)) return false; + } else return false; if (actions == null) { - if (other.actions != null) - return false; - } else if (actions.getSize() > 0 || other.actions.getSize() > 0) - return false; + if (other.actions != null) return false; + } else if (actions.getSize() > 0 || other.actions.getSize() > 0) return false; return true; } /** * Create a new lazy container with an already present value. - * + * * @param val * The value for the lazy container. - * + * * @return A new lazy container holding that value. */ - public static Lazy lazy(ContainedType val) { + public static Lazy lazy(final ContainedType val) { return new Lazy<>(val); } /** * Create a new lazy container with a suspended value. - * + * * @param supp * The suspended value for the lazy container. - * + * * @return A new lazy container that will un-suspend the value when * necessary. */ - public static Lazy lazy(Supplier supp) { + public static Lazy lazy(final Supplier supp) { return new Lazy<>(supp); } } 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 70768be..5cb85f3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -1,12 +1,12 @@ package bjc.utils.data; -import bjc.utils.data.internals.BoundLazyPair; -import bjc.utils.data.internals.HalfBoundLazyPair; - import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; +import bjc.utils.data.internals.BoundLazyPair; +import bjc.utils.data.internals.HalfBoundLazyPair; + /** * A lazy implementation of a pair * @@ -36,7 +36,7 @@ public class LazyPair implements IPair * @param rightVal * The value for the right side of the pair */ - public LazyPair(LeftType leftVal, RightType rightVal) { + public LazyPair(final LeftType leftVal, final RightType rightVal) { leftValue = leftVal; rightValue = rightVal; @@ -52,7 +52,7 @@ public class LazyPair implements IPair * @param rightSupp * The source for a value on the right side of the pair */ - public LazyPair(Supplier leftSupp, Supplier rightSupp) { + public LazyPair(final Supplier leftSupp, final Supplier rightSupp) { // Use single suppliers to catch double-instantiation bugs leftSupplier = new SingleSupplier<>(leftSupp); rightSupplier = new SingleSupplier<>(rightSupp); @@ -63,16 +63,15 @@ public class LazyPair implements IPair @Override public IPair bind( - BiFunction> binder) { + final BiFunction> binder) { return new BoundLazyPair<>(leftSupplier, rightSupplier, binder); } @Override public IPair bindLeft( - Function> leftBinder) { - Supplier leftSupp = () -> { - if (leftMaterialized) - return leftValue; + final Function> leftBinder) { + final Supplier leftSupp = () -> { + if (leftMaterialized) return leftValue; return leftSupplier.get(); }; @@ -82,10 +81,9 @@ public class LazyPair implements IPair @Override public IPair bindRight( - Function> rightBinder) { - Supplier rightSupp = () -> { - if (rightMaterialized) - return rightValue; + final Function> rightBinder) { + final Supplier rightSupp = () -> { + if (rightMaterialized) return rightValue; return rightSupplier.get(); }; @@ -95,13 +93,13 @@ public class LazyPair implements IPair @Override public IPair combine( - IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { + final IPair otherPair, + final BiFunction leftCombiner, + final BiFunction rightCombiner) { return otherPair.bind((otherLeft, otherRight) -> { return bind((leftVal, rightVal) -> { - CombinedLeft left = leftCombiner.apply(leftVal, otherLeft); - CombinedRight right = rightCombiner.apply(rightVal, otherRight); + final CombinedLeft left = leftCombiner.apply(leftVal, otherLeft); + final CombinedRight right = rightCombiner.apply(rightVal, otherRight); return new LazyPair<>(left, right); }); @@ -131,17 +129,15 @@ public class LazyPair implements IPair } @Override - public IPair mapLeft(Function mapper) { - Supplier leftSupp = () -> { - if (leftMaterialized) - return mapper.apply(leftValue); + public IPair mapLeft(final Function mapper) { + final Supplier leftSupp = () -> { + if (leftMaterialized) return mapper.apply(leftValue); return mapper.apply(leftSupplier.get()); }; - Supplier rightSupp = () -> { - if (rightMaterialized) - return rightValue; + final Supplier rightSupp = () -> { + if (rightMaterialized) return rightValue; return rightSupplier.get(); }; @@ -150,17 +146,15 @@ public class LazyPair implements IPair } @Override - public IPair mapRight(Function mapper) { - Supplier leftSupp = () -> { - if (leftMaterialized) - return leftValue; + public IPair mapRight(final Function mapper) { + final Supplier leftSupp = () -> { + if (leftMaterialized) return leftValue; return leftSupplier.get(); }; - Supplier rightSupp = () -> { - if (rightMaterialized) - return mapper.apply(rightValue); + final Supplier rightSupp = () -> { + if (rightMaterialized) return mapper.apply(rightValue); return mapper.apply(rightSupplier.get()); }; @@ -169,7 +163,7 @@ public class LazyPair implements IPair } @Override - public MergedType merge(BiFunction merger) { + public MergedType merge(final BiFunction merger) { if (!leftMaterialized) { leftValue = leftSupplier.get(); @@ -211,48 +205,35 @@ public class LazyPair implements IPair int result = 1; result = prime * result + (leftMaterialized ? 1231 : 1237); - result = prime * result + ((leftValue == null) ? 0 : leftValue.hashCode()); + result = prime * result + (leftValue == null ? 0 : leftValue.hashCode()); result = prime * result + (rightMaterialized ? 1231 : 1237); - result = prime * result + ((rightValue == null) ? 0 : rightValue.hashCode()); + result = prime * result + (rightValue == null ? 0 : rightValue.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof LazyPair)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof LazyPair)) return false; - LazyPair other = (LazyPair) obj; + final LazyPair other = (LazyPair) obj; - if (leftMaterialized != other.leftMaterialized) - return false; + if (leftMaterialized != other.leftMaterialized) return false; if (leftMaterialized) { if (leftValue == null) { - if (other.leftValue != null) - return false; - } else if (!leftValue.equals(other.leftValue)) - return false; - } else { - return false; - } + if (other.leftValue != null) return false; + } else if (!leftValue.equals(other.leftValue)) return false; + } else return false; - if (rightMaterialized != other.rightMaterialized) - return false; + if (rightMaterialized != other.rightMaterialized) return false; if (rightMaterialized) { if (rightValue == null) { - if (other.rightValue != null) - return false; - } else if (!rightValue.equals(other.rightValue)) - return false; - } else { - return false; - } + if (other.rightValue != null) return false; + } else if (!rightValue.equals(other.rightValue)) return false; + } else return false; return true; } 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 8807312..142057c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java @@ -1,12 +1,12 @@ package bjc.utils.data; +import java.util.function.Function; +import java.util.function.UnaryOperator; + import bjc.utils.data.internals.BoundListHolder; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IList; -import java.util.function.Function; -import java.util.function.UnaryOperator; - /** * A holder that represents a set of non-deterministic computations * @@ -25,50 +25,50 @@ public class ListHolder implements IHolder { * The possible values for the computation */ @SafeVarargs - public ListHolder(ContainedType... values) { + public ListHolder(final ContainedType... values) { heldValues = new FunctionalList<>(); if (values != null) { - for (ContainedType containedValue : values) { + for (final ContainedType containedValue : values) { heldValues.add(containedValue); } } } - private ListHolder(IList toHold) { + private ListHolder(final IList toHold) { heldValues = toHold; } @Override - public IHolder bind(Function> binder) { - IList> boundValues = heldValues.map(binder); + public IHolder bind(final Function> binder) { + final IList> boundValues = heldValues.map(binder); return new BoundListHolder<>(boundValues); } @Override - public Function> lift(Function func) { + public Function> lift(final Function func) { return val -> { return new ListHolder<>(new FunctionalList<>(func.apply(val))); }; } @Override - public IHolder map(Function mapper) { - IList mappedValues = heldValues.map(mapper); + public IHolder map(final Function mapper) { + final IList mappedValues = heldValues.map(mapper); return new ListHolder<>(mappedValues); } @Override - public IHolder transform(UnaryOperator transformer) { + public IHolder transform(final UnaryOperator transformer) { heldValues = heldValues.map(transformer); return this; } @Override - public UnwrappedType unwrap(Function unwrapper) { + public UnwrappedType unwrap(final Function unwrapper) { return unwrapper.apply(heldValues.randItem()); } @@ -82,27 +82,22 @@ public class ListHolder implements IHolder { final int prime = 31; int result = 1; - result = prime * result + ((heldValues == null) ? 0 : heldValues.hashCode()); + result = prime * result + (heldValues == null ? 0 : heldValues.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof ListHolder)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof ListHolder)) return false; - ListHolder other = (ListHolder) obj; + final ListHolder other = (ListHolder) obj; if (heldValues == null) { - if (other.heldValues != null) - return false; - } else if (!heldValues.equals(other.heldValues)) - return false; + if (other.heldValues != null) return false; + } else if (!heldValues.equals(other.heldValues)) return false; return true; } 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 718ab6e..37e0cde 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Option.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Option.java @@ -20,35 +20,33 @@ public class Option implements IHolder { * @param seed * The initial value for the optional */ - public Option(ContainedType seed) { + public Option(final ContainedType seed) { held = seed; } @Override - public IHolder bind(Function> binder) { - if (held == null) - return new Option<>(null); + public IHolder bind(final Function> binder) { + if (held == null) return new Option<>(null); return binder.apply(held); } @Override - public Function> lift(Function func) { + public Function> lift(final Function func) { return val -> { return new Option<>(func.apply(val)); }; } @Override - public IHolder map(Function mapper) { - if (held == null) - return new Option<>(null); + public IHolder map(final Function mapper) { + if (held == null) return new Option<>(null); return new Option<>(mapper.apply(held)); } @Override - public IHolder transform(UnaryOperator transformer) { + public IHolder transform(final UnaryOperator transformer) { if (held != null) { held = transformer.apply(held); } @@ -57,9 +55,8 @@ public class Option implements IHolder { } @Override - public UnwrappedType unwrap(Function unwrapper) { - if (held == null) - return null; + public UnwrappedType unwrap(final Function unwrapper) { + if (held == null) return null; return unwrapper.apply(held); } @@ -74,27 +71,22 @@ public class Option implements IHolder { final int prime = 31; int result = 1; - result = prime * result + ((held == null) ? 0 : held.hashCode()); + result = prime * result + (held == null ? 0 : held.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof Option)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof Option)) return false; - Option other = (Option) obj; + final Option other = (Option) obj; if (held == null) { - if (other.held != null) - return false; - } else if (!held.equals(other.held)) - return false; + if (other.held != null) return false; + } else if (!held.equals(other.held)) return false; return true; } 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 2fc3106..e6796ba 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -35,71 +35,65 @@ public class Pair implements IPair { * @param right * The value of the right side */ - public Pair(LeftType left, RightType right) { + public Pair(final LeftType left, final RightType right) { leftValue = left; rightValue = right; } @Override public IPair bind( - BiFunction> binder) { - if (binder == null) - throw new NullPointerException("Binder must not be null."); + final BiFunction> binder) { + if (binder == null) throw new NullPointerException("Binder must not be null."); return binder.apply(leftValue, rightValue); } @Override public IPair bindLeft( - Function> leftBinder) { - if (leftBinder == null) - throw new NullPointerException("Binder must not be null"); + final Function> leftBinder) { + if (leftBinder == null) throw new NullPointerException("Binder must not be null"); return leftBinder.apply(leftValue); } @Override public IPair bindRight( - Function> rightBinder) { - if (rightBinder == null) - throw new NullPointerException("Binder must not be null"); + final Function> rightBinder) { + if (rightBinder == null) throw new NullPointerException("Binder must not be null"); return rightBinder.apply(rightValue); } @Override public IPair combine( - IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { + final IPair otherPair, + final BiFunction leftCombiner, + final BiFunction rightCombiner) { return otherPair.bind((otherLeft, otherRight) -> { - CombinedLeft left = leftCombiner.apply(leftValue, otherLeft); - CombinedRight right = rightCombiner.apply(rightValue, otherRight); + final CombinedLeft left = leftCombiner.apply(leftValue, otherLeft); + final CombinedRight right = rightCombiner.apply(rightValue, otherRight); return new Pair<>(left, right); }); } @Override - public IPair mapLeft(Function mapper) { - if (mapper == null) - throw new NullPointerException("Mapper must not be null"); + public IPair mapLeft(final Function mapper) { + if (mapper == null) throw new NullPointerException("Mapper must not be null"); return new Pair<>(mapper.apply(leftValue), rightValue); } @Override - public IPair mapRight(Function mapper) { - if (mapper == null) - throw new NullPointerException("Mapper must not be null"); + public IPair mapRight(final Function mapper) { + if (mapper == null) throw new NullPointerException("Mapper must not be null"); return new Pair<>(leftValue, mapper.apply(rightValue)); } @Override - public MergedType merge(BiFunction merger) { - if (merger == null) - throw new NullPointerException("Merger must not be null"); + public MergedType merge(final BiFunction merger) { + if (merger == null) throw new NullPointerException("Merger must not be null"); return merger.apply(leftValue, rightValue); } @@ -114,34 +108,27 @@ public class Pair implements IPair { final int prime = 31; int result = 1; - result = prime * result + ((leftValue == null) ? 0 : leftValue.hashCode()); - result = prime * result + ((rightValue == null) ? 0 : rightValue.hashCode()); + result = prime * result + (leftValue == null ? 0 : leftValue.hashCode()); + result = prime * result + (rightValue == null ? 0 : rightValue.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof Pair)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof Pair)) return false; - Pair other = (Pair) obj; + final Pair other = (Pair) obj; if (leftValue == null) { - if (other.leftValue != null) - return false; - } else if (!leftValue.equals(other.leftValue)) - return false; + if (other.leftValue != null) return false; + } else if (!leftValue.equals(other.leftValue)) return false; if (rightValue == null) { - if (other.rightValue != null) - return false; - } else if (!rightValue.equals(other.rightValue)) - return false; + if (other.rightValue != null) return false; + } else if (!rightValue.equals(other.rightValue)) return false; return true; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/SingleIterator.java b/BJC-Utils2/src/main/java/bjc/utils/data/SingleIterator.java index 0ddb324..4069c3f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/SingleIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/SingleIterator.java @@ -4,24 +4,24 @@ import java.util.Iterator; /** * An iterator that will only ever yield one item. - * + * * @author EVE * * @param * The type of the item. */ public class SingleIterator implements Iterator { - private T itm; + private final T itm; private boolean yielded; /** * Create a iterator that yields a single item. - * + * * @param item * The item to yield. */ - public SingleIterator(T item) { + public SingleIterator(final T item) { itm = item; yielded = false; 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 fde5111..c675ebf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java @@ -15,11 +15,11 @@ import java.util.function.Supplier; public class SingleSupplier implements Supplier { private static long nextID = 0; - private Supplier source; + private final Supplier source; private boolean gotten; - private long id; + private final long id; /* * This is bad practice, but I want to know where the single @@ -33,7 +33,7 @@ public class SingleSupplier implements Supplier { * @param supp * The supplier to give a single value from */ - public SingleSupplier(Supplier supp) { + public SingleSupplier(final Supplier supp) { source = supp; gotten = false; @@ -44,10 +44,10 @@ public class SingleSupplier implements Supplier { @Override public T get() { if (gotten == true) { - String msg = String.format( + final String msg = String.format( "Attempted to retrieve value more than once from single supplier #%d", id); - IllegalStateException isex = new IllegalStateException(msg); + final IllegalStateException isex = new IllegalStateException(msg); isex.initCause(instSite); @@ -58,7 +58,7 @@ public class SingleSupplier implements Supplier { try { throw new IllegalStateException("Previous instantiation here."); - } catch (IllegalStateException isex) { + } catch (final IllegalStateException isex) { instSite = isex; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Toggle.java b/BJC-Utils2/src/main/java/bjc/utils/data/Toggle.java index 8ebc4d8..1e10dae 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Toggle.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Toggle.java @@ -2,7 +2,7 @@ package bjc.utils.data; /** * A stateful holder that swaps between two values of the same type. - * + * * @author EVE * * @param @@ -12,21 +12,21 @@ public interface Toggle { /** * Retrieve the currently-aligned value of this toggle, and swap the * alignment. - * + * * @return The previously-aligned value. */ E get(); /** * Retrieve the currently-aligned value without altering the alignment. - * + * * @return The currently-aligned value. */ E peek(); /** * Change the alignment of the toggle. - * + * * @param isLeft * Whether the toggle should be left-aligned or not. */ 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 14548a3..014458b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java @@ -1,5 +1,7 @@ package bjc.utils.data; +import static bjc.utils.data.TopDownTransformResult.RTRANSFORM; + import java.util.Deque; import java.util.Iterator; import java.util.LinkedList; @@ -8,34 +10,31 @@ import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; -import static bjc.utils.data.TopDownTransformResult.RTRANSFORM; - - /* * FIXME something is broken in here. fix it. */ @SuppressWarnings("javadoc") public class TopDownTransformIterator implements Iterator> { - private Function picker; - private BiFunction, Consumer>>, ITree> transform; + private final Function picker; + private final BiFunction, Consumer>>, ITree> transform; private ITree preParent; private ITree postParent; - private Deque> preChildren; - private Deque> postChildren; + private final Deque> preChildren; + private final Deque> postChildren; private TopDownTransformIterator curChild; private boolean done; private boolean initial; - private Deque>> toYield; - private Iterator> curYield; + private final Deque>> toYield; + private Iterator> curYield; - public TopDownTransformIterator(Function pickr, - BiFunction, Consumer>>, ITree> transfrm, - ITree tree) { + public TopDownTransformIterator(final Function pickr, + final BiFunction, Consumer>>, ITree> transfrm, + final ITree tree) { preParent = tree; preChildren = new LinkedList<>(); @@ -49,8 +48,8 @@ public class TopDownTransformIterator implements Iterator> src) { - if(curYield != null) { + public void addYield(final Iterator> src) { + if (curYield != null) { toYield.push(curYield); } @@ -62,55 +61,52 @@ public class TopDownTransformIterator implements Iterator flushYields(ITree val) { - if(curYield != null) { + public ITree flushYields(final ITree val) { + 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 - return curYield.next(); + } else return curYield.next(); } - } else - return val; + } else return val; } @Override public ITree 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(); + } else return curYield.next(); } } - if(initial) { - TopDownTransformResult res = picker.apply(preParent.getHead()); + if (initial) { + final TopDownTransformResult res = picker.apply(preParent.getHead()); - switch(res) { + switch (res) { case PASSTHROUGH: postParent = new Tree<>(preParent.getHead()); - if(preParent.getChildrenCount() != 0) { - for(int i = 0; i < preParent.getChildrenCount(); i++) { + if (preParent.getChildrenCount() != 0) { + for (int i = 0; i < preParent.getChildrenCount(); i++) { preChildren.add(preParent.getChild(i)); } @@ -130,8 +126,8 @@ public class TopDownTransformIterator implements Iterator implements Iterator intRes = transform.apply(preParent, this::addYield); + final ITree intRes = transform.apply(preParent, this::addYield); postParent = new Tree<>(intRes.getHead()); - if(intRes.getChildrenCount() != 0) { - for(int i = 0; i < intRes.getChildrenCount(); i++) { + if (intRes.getChildrenCount() != 0) { + for (int i = 0; i < intRes.getChildrenCount(); i++) { preChildren.add(intRes.getChild(i)); } @@ -162,16 +158,16 @@ public class TopDownTransformIterator implements Iterator(picker, transform, preChildren.pop()); - ITree res = curChild.next(); + final ITree res = curChild.next(); System.out.println("\t\tTRACE: adding node " + res + " to children"); postChildren.add(res); @@ -179,12 +175,12 @@ 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 (final ITree child : postChildren) { res.addChild(child); } @@ -194,7 +190,7 @@ public class TopDownTransformIterator implements Iterator child : postChildren) { + for (final ITree child : postChildren) { res.addChild(child); } } @@ -203,7 +199,7 @@ public class TopDownTransformIterator implements Iterator res = curChild.next(); + final ITree res = curChild.next(); System.out.println("\t\tTRACE: adding node " + res + " to children"); postChildren.add(res); diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/TransformIterator.java b/BJC-Utils2/src/main/java/bjc/utils/data/TransformIterator.java index a0987af..50f28b1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/TransformIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/TransformIterator.java @@ -5,30 +5,30 @@ import java.util.function.Function; /** * An iterator that transforms values from one type to another. - * + * * @author EVE * * @param * The source iterator type. - * + * * @param * The destination iterator type. */ public class TransformIterator implements Iterator { - private Iterator source; + private final Iterator source; - private Function transform; + private final Function transform; /** * Create a new transform iterator. - * + * * @param source * The source iterator to use. - * + * * @param transform * The transform to apply. */ - public TransformIterator(Iterator source, Function transform) { + public TransformIterator(final Iterator source, final Function transform) { this.source = source; this.transform = transform; } 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 0dc96eb..a52f699 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java @@ -1,15 +1,15 @@ package bjc.utils.data; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.bst.TreeLinearizationMethod; -import bjc.utils.functypes.ListFlattener; - import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.UnaryOperator; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; +import bjc.utils.funcdata.bst.TreeLinearizationMethod; +import bjc.utils.functypes.ListFlattener; + /** * A node in a homogeneous tree. * @@ -33,7 +33,7 @@ public class Tree implements ITree { * @param leaf * The data to store as a leaf node. */ - public Tree(ContainedType leaf) { + public Tree(final ContainedType leaf) { data = leaf; hasChildren = false; @@ -46,11 +46,11 @@ public class Tree implements ITree { * * @param leaf * The data to hold in this node. - * + * * @param childrn * A list of children for this node. */ - public Tree(ContainedType leaf, IList> childrn) { + public Tree(final ContainedType leaf, final IList> childrn) { this(leaf); hasChildren = true; @@ -65,12 +65,12 @@ public class Tree implements ITree { * * @param leaf * The data to hold in this node. - * + * * @param childrn * A list of children for this node. */ @SafeVarargs - public Tree(ContainedType leaf, ITree... childrn) { + public Tree(final ContainedType leaf, final ITree... childrn) { this(leaf); hasChildren = true; @@ -79,7 +79,7 @@ public class Tree implements ITree { children = new FunctionalList<>(); - for (ITree child : childrn) { + for (final ITree child : childrn) { children.add(child); childCount++; @@ -87,7 +87,7 @@ public class Tree implements ITree { } @Override - public void addChild(ITree child) { + public void addChild(final ITree child) { if (hasChildren == false) { hasChildren = true; @@ -100,7 +100,7 @@ public class Tree implements ITree { } @Override - public void prependChild(ITree child) { + public void prependChild(final ITree child) { if (hasChildren == false) { hasChildren = true; @@ -113,7 +113,7 @@ public class Tree implements ITree { } @Override - public void doForChildren(Consumer> action) { + public void doForChildren(final Consumer> action) { if (childCount > 0) { children.forEach(action); } @@ -125,14 +125,12 @@ public class Tree implements ITree { } @Override - public int revFind(Predicate> childPred) { - if (childCount == 0) { + public int revFind(final Predicate> childPred) { + if (childCount == 0) return -1; - } else { + else { for (int i = childCount - 1; i >= 0; i--) { - if (childPred.test(getChild(i))) { - return i; - } + if (childPred.test(getChild(i))) return i; } } @@ -140,12 +138,12 @@ public class Tree implements ITree { } @Override - public void traverse(TreeLinearizationMethod linearizationMethod, Consumer action) { + public void traverse(final TreeLinearizationMethod linearizationMethod, final Consumer action) { if (hasChildren) { switch (linearizationMethod) { case INORDER: if (childCount != 2) { - String msg = "Can only do in-order traversal for binary trees."; + final String msg = "Can only do in-order traversal for binary trees."; throw new IllegalArgumentException(msg); } @@ -176,18 +174,19 @@ public class Tree implements ITree { } @Override - public ReturnedType collapse(Function leafTransform, - Function> nodeCollapser, - Function resultTransformer) { + public ReturnedType collapse(final Function leafTransform, + final Function> nodeCollapser, + final Function resultTransformer) { return resultTransformer.apply(internalCollapse(leafTransform, nodeCollapser)); } @Override - public ITree flatMapTree(Function> mapper) { + public ITree flatMapTree(final Function> mapper) { if (hasChildren) { - ITree flatMappedData = mapper.apply(data); + final ITree flatMappedData = mapper.apply(data); - IList> mappedChildren = children.map(child -> child.flatMapTree(mapper)); + final IList> mappedChildren = children + .map(child -> child.flatMapTree(mapper)); mappedChildren.forEach(child -> flatMappedData.addChild(child)); @@ -197,13 +196,13 @@ public class Tree implements ITree { return mapper.apply(data); } - protected NewType internalCollapse(Function leafTransform, - Function> nodeCollapser) { + protected NewType internalCollapse(final Function leafTransform, + final Function> nodeCollapser) { if (hasChildren) { - Function, NewType> nodeTransformer = nodeCollapser.apply(data); + final Function, NewType> nodeTransformer = nodeCollapser.apply(data); - IList collapsedChildren = children.map(child -> { - NewType collapsed = child.collapse(leafTransform, nodeCollapser, + final IList collapsedChildren = children.map(child -> { + final NewType collapsed = child.collapse(leafTransform, nodeCollapser, subTreeVal -> subTreeVal); return collapsed; @@ -215,7 +214,7 @@ public class Tree implements ITree { return leafTransform.apply(data); } - protected void internalToString(StringBuilder builder, int indentLevel, boolean initial) { + protected void internalToString(final StringBuilder builder, final int indentLevel, final boolean initial) { for (int i = 0; i < indentLevel; i++) { builder.append(">\t"); } @@ -229,7 +228,7 @@ public class Tree implements ITree { if (hasChildren) { children.forEach(child -> { if (child instanceof Tree) { - Tree kid = (Tree) child; + final Tree kid = (Tree) child; kid.internalToString(builder, indentLevel + 1, false); } else { @@ -244,10 +243,10 @@ public class Tree implements ITree { } @Override - public ITree rebuildTree(Function leafTransformer, - Function operatorTransformer) { + public ITree rebuildTree(final Function leafTransformer, + final Function operatorTransformer) { if (hasChildren) { - IList> mappedChildren = children.map(child -> { + final IList> mappedChildren = children.map(child -> { return child.rebuildTree(leafTransformer, operatorTransformer); }); @@ -258,7 +257,8 @@ public class Tree implements ITree { } @Override - public void selectiveTransform(Predicate nodePicker, UnaryOperator transformer) { + public void selectiveTransform(final Predicate nodePicker, + final UnaryOperator transformer) { if (hasChildren) { children.forEach(child -> child.selectiveTransform(nodePicker, transformer)); } else { @@ -267,9 +267,10 @@ public class Tree implements ITree { } @Override - public ITree topDownTransform(Function transformPicker, - UnaryOperator> transformer) { - TopDownTransformResult transformResult = transformPicker.apply(data); + public ITree topDownTransform( + final Function transformPicker, + final UnaryOperator> transformer) { + final TopDownTransformResult transformResult = transformPicker.apply(data); switch (transformResult) { case PASSTHROUGH: @@ -277,7 +278,8 @@ public class Tree implements ITree { if (hasChildren) { children.forEach(child -> { - ITree kid = child.topDownTransform(transformPicker, transformer); + final ITree kid = child.topDownTransform(transformPicker, + transformer); result.addChild(kid); }); @@ -295,7 +297,8 @@ public class Tree implements ITree { if (hasChildren) { children.forEach(child -> { - ITree kid = child.topDownTransform(transformPicker, transformer); + final ITree kid = child.topDownTransform(transformPicker, + transformer); result.addChild(kid); }); @@ -303,40 +306,41 @@ public class Tree implements ITree { return transformer.apply(result); case PULLUP: - ITree intermediateResult = transformer.apply(this); + final ITree intermediateResult = transformer.apply(this); result = new Tree<>(intermediateResult.getHead()); intermediateResult.doForChildren(child -> { - ITree kid = child.topDownTransform(transformPicker, transformer); + final ITree kid = child.topDownTransform(transformPicker, transformer); result.addChild(kid); }); return result; default: - String msg = String.format("Recieved unknown transform result %s", transformResult); + final String msg = String.format("Recieved unknown transform result %s", transformResult); throw new IllegalArgumentException(msg); } } @Override - public TransformedType transformChild(int childNo, - Function, TransformedType> transformer) { + public TransformedType transformChild(final int childNo, + final Function, TransformedType> transformer) { if (childNo < 0 || childNo > childCount - 1) { - String msg = String.format("Child index #%d is invalid", childNo); + final String msg = String.format("Child index #%d is invalid", childNo); throw new IllegalArgumentException(msg); } - ITree selectedKid = children.getByIndex(childNo); + final ITree selectedKid = children.getByIndex(childNo); return transformer.apply(selectedKid); } @Override - public TransformedType transformHead(Function transformer) { + public TransformedType transformHead( + final Function transformer) { return transformer.apply(data); } @@ -346,15 +350,15 @@ public class Tree implements ITree { int result = 1; result = prime * result + childCount; - result = prime * result + ((children == null) ? 0 : children.hashCode()); - result = prime * result + ((data == null) ? 0 : data.hashCode()); + result = prime * result + (children == null ? 0 : children.hashCode()); + result = prime * result + (data == null ? 0 : data.hashCode()); return result; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); internalToString(builder, 1, true); @@ -364,30 +368,22 @@ public class Tree implements ITree { } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof Tree)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof Tree)) return false; - Tree other = (Tree) obj; + final Tree other = (Tree) obj; if (data == null) { - if (other.data != null) - return false; - } else if (!data.equals(other.data)) - return false; + if (other.data != null) return false; + } else if (!data.equals(other.data)) return false; - if (childCount != other.childCount) - return false; + if (childCount != other.childCount) return false; if (children == null) { - if (other.children != null) - return false; - } else if (!children.equals(other.children)) - return false; + if (other.children != null) return false; + } else if (!children.equals(other.children)) return false; return true; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ValueToggle.java b/BJC-Utils2/src/main/java/bjc/utils/data/ValueToggle.java index 5b5cb83..9193896 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ValueToggle.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ValueToggle.java @@ -2,9 +2,9 @@ package bjc.utils.data; /** * A simple implementation of {@link Toggle}. - * + * * @author EVE - * + * * @param * The type of value to toggle between. */ @@ -12,20 +12,20 @@ public class ValueToggle implements Toggle { private final E lft; private final E rght; - private BooleanToggle alignment; + private final BooleanToggle alignment; /** * Create a new toggle. - * + * * All toggles start right-aligned. - * + * * @param left * The value when the toggle is left-aligned. - * + * * @param right * The value when the toggle is right-aligned. */ - public ValueToggle(E left, E right) { + public ValueToggle(final E left, final E right) { lft = left; rght = right; @@ -35,24 +35,20 @@ public class ValueToggle implements Toggle { @Override public E get() { - if(alignment.get()) { + if (alignment.get()) return lft; - } else { - return rght; - } + else return rght; } @Override public E peek() { - if(alignment.peek()) { + if (alignment.peek()) return lft; - } else { - return rght; - } + else return rght; } @Override - public void set(boolean isLeft) { + public void set(final boolean isLeft) { alignment.set(isLeft); } } 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 cc1c0c0..e5f1b95 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 @@ -1,14 +1,14 @@ package bjc.utils.data.internals; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + import bjc.utils.data.IHolder; import bjc.utils.data.Lazy; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IList; -import java.util.function.Function; -import java.util.function.Supplier; -import java.util.function.UnaryOperator; - /* * Implements a lazy holder that has been bound */ @@ -17,12 +17,12 @@ public class BoundLazy implements IHolder> oldSupplier; + private final Supplier> oldSupplier; /* * The function to use to transform the old value into a new value */ - private Function> binder; + private final Function> binder; /* * The bound value being held @@ -37,30 +37,31 @@ public class BoundLazy implements IHolder> actions = new FunctionalList<>(); + private final IList> actions = new FunctionalList<>(); /* * Create a new bound lazy value */ - public BoundLazy(Supplier> supp, Function> binder) { + public BoundLazy(final Supplier> supp, + final Function> binder) { oldSupplier = supp; this.binder = binder; } @Override - public IHolder bind(Function> bindr) { + public IHolder bind(final Function> bindr) { if (bindr == null) throw new NullPointerException("Binder must not be null"); /* * Prepare a list of pending actions */ - IList> pendingActions = new FunctionalList<>(); + final IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); /* * Create the new supplier of a value */ - Supplier> typeSupplier = () -> { + final Supplier> typeSupplier = () -> { IHolder oldHolder = boundHolder; /* @@ -83,7 +84,7 @@ public class BoundLazy implements IHolder Function> lift( - Function func) { + final Function func) { if (func == null) throw new NullPointerException("Function to lift must not be null"); return (val) -> { @@ -92,15 +93,15 @@ public class BoundLazy implements IHolder IHolder map(Function mapper) { + public IHolder map(final Function mapper) { if (mapper == null) throw new NullPointerException("Mapper must not be null"); // Prepare a list of pending actions - IList> pendingActions = new FunctionalList<>(); + final IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); // Prepare the new supplier - Supplier typeSupplier = () -> { + final Supplier typeSupplier = () -> { IHolder oldHolder = boundHolder; // Bound the value if it hasn't been bound @@ -124,7 +125,7 @@ public class BoundLazy implements IHolder transform(UnaryOperator transformer) { + public IHolder transform(final UnaryOperator transformer) { if (transformer == null) throw new NullPointerException("Transformer must not be null"); actions.add(transformer); @@ -133,7 +134,7 @@ public class BoundLazy implements IHolder UnwrappedType unwrap(Function unwrapper) { + public UnwrappedType unwrap(final Function unwrapper) { if (unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); if (!holderBound) { 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 de290a6..9333e15 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 @@ -1,14 +1,14 @@ package bjc.utils.data.internals; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + import bjc.utils.data.IHolder; import bjc.utils.data.IPair; import bjc.utils.data.Identity; import bjc.utils.data.LazyPair; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.Supplier; - /* * Implements a lazy pair that has been bound */ @@ -17,16 +17,16 @@ public class BoundLazyPair implements IPai /* * The supplier of the left value */ - private Supplier leftSupplier; + private final Supplier leftSupplier; /* * The supplier of the right value */ - private Supplier rightSupplier; + private final Supplier rightSupplier; /* * The binder to transform values */ - private BiFunction> binder; + private final BiFunction> binder; /* * The bound pair @@ -38,8 +38,8 @@ public class BoundLazyPair implements IPai */ private boolean pairBound; - public BoundLazyPair(Supplier leftSupp, Supplier rightSupp, - BiFunction> bindr) { + public BoundLazyPair(final Supplier leftSupp, final Supplier rightSupp, + final BiFunction> bindr) { leftSupplier = leftSupp; rightSupplier = rightSupp; binder = bindr; @@ -47,14 +47,14 @@ public class BoundLazyPair implements IPai @Override public IPair bind( - BiFunction> bindr) { - if(bindr == null) throw new NullPointerException("Binder must not be null"); + final BiFunction> bindr) { + if (bindr == null) throw new NullPointerException("Binder must not be null"); - IHolder> newPair = new Identity<>(boundPair); - IHolder newPairMade = new Identity<>(pairBound); + final IHolder> newPair = new Identity<>(boundPair); + final IHolder newPairMade = new Identity<>(pairBound); - Supplier leftSupp = () -> { - if(!newPairMade.getValue()) { + final Supplier leftSupp = () -> { + if (!newPairMade.getValue()) { newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); newPairMade.replace(true); @@ -63,8 +63,8 @@ public class BoundLazyPair implements IPai return newPair.unwrap((pair) -> pair.getLeft()); }; - Supplier rightSupp = () -> { - if(!newPairMade.getValue()) { + final Supplier rightSupp = () -> { + if (!newPairMade.getValue()) { newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); newPairMade.replace(true); @@ -78,13 +78,13 @@ public class BoundLazyPair implements IPai @Override public IPair bindLeft( - Function> leftBinder) { - if(leftBinder == null) throw new NullPointerException("Left binder must not be null"); + final Function> leftBinder) { + if (leftBinder == null) throw new NullPointerException("Left binder must not be null"); - Supplier leftSupp = () -> { + final Supplier leftSupp = () -> { IPair newPair = boundPair; - if(!pairBound) { + if (!pairBound) { newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } @@ -96,13 +96,13 @@ public class BoundLazyPair implements IPai @Override public IPair bindRight( - Function> rightBinder) { - if(rightBinder == null) throw new NullPointerException("Right binder must not be null"); + final Function> rightBinder) { + if (rightBinder == null) throw new NullPointerException("Right binder must not be null"); - Supplier rightSupp = () -> { + final Supplier rightSupp = () -> { IPair newPair = boundPair; - if(!pairBound) { + if (!pairBound) { newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } @@ -114,14 +114,14 @@ public class BoundLazyPair implements IPai @Override public IPair combine( - IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { - if(otherPair == null) + final IPair otherPair, + final BiFunction leftCombiner, + final BiFunction rightCombiner) { + if (otherPair == null) throw new NullPointerException("Other pair must not be null"); - else if(leftCombiner == null) + else if (leftCombiner == null) throw new NullPointerException("Left combiner must not be null"); - else if(rightCombiner == null) throw new NullPointerException("Right combiner must not be null"); + else if (rightCombiner == null) throw new NullPointerException("Right combiner must not be null"); return otherPair.bind((otherLeft, otherRight) -> { return bind((leftVal, rightVal) -> { @@ -132,12 +132,12 @@ public class BoundLazyPair implements IPai } @Override - public IPair mapLeft(Function mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public IPair mapLeft(final 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(); + final Supplier leftSupp = () -> { + if (!pairBound) { + final NewLeft leftVal = binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); return mapper.apply(leftVal); } @@ -145,8 +145,8 @@ public class BoundLazyPair implements IPai return mapper.apply(boundPair.getLeft()); }; - Supplier rightSupp = () -> { - if(!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getRight(); + final Supplier rightSupp = () -> { + if (!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getRight(); return boundPair.getRight(); }; @@ -155,18 +155,19 @@ public class BoundLazyPair implements IPai } @Override - public IPair mapRight(Function mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public IPair mapRight(final 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(); + final Supplier leftSupp = () -> { + if (!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); return boundPair.getLeft(); }; - Supplier rightSupp = () -> { - if(!pairBound) { - NewRight rightVal = binder.apply(leftSupplier.get(), rightSupplier.get()).getRight(); + final Supplier rightSupp = () -> { + if (!pairBound) { + final NewRight rightVal = binder.apply(leftSupplier.get(), rightSupplier.get()) + .getRight(); return mapper.apply(rightVal); } @@ -178,10 +179,10 @@ public class BoundLazyPair implements IPai } @Override - public MergedType merge(BiFunction merger) { - if(merger == null) throw new NullPointerException("Merger must not be null"); + public MergedType merge(final BiFunction merger) { + if (merger == null) throw new NullPointerException("Merger must not be null"); - if(!pairBound) { + if (!pairBound) { boundPair = binder.apply(leftSupplier.get(), rightSupplier.get()); pairBound = true; @@ -192,7 +193,7 @@ public class BoundLazyPair implements IPai @Override public String toString() { - if(pairBound) return boundPair.toString(); + if (pairBound) return boundPair.toString(); return "(un-materialized)"; } 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 c838ce7..65a6f3d 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 @@ -1,28 +1,28 @@ package bjc.utils.data.internals; +import java.util.function.Function; +import java.util.function.UnaryOperator; + import bjc.utils.data.IHolder; import bjc.utils.data.ListHolder; import bjc.utils.funcdata.IList; -import java.util.function.Function; -import java.util.function.UnaryOperator; - /* * Holds a list, converted into a holder */ @SuppressWarnings("javadoc") public class BoundListHolder implements IHolder { - private IList> heldHolders; + private final IList> heldHolders; - public BoundListHolder(IList> toHold) { + public BoundListHolder(final IList> toHold) { heldHolders = toHold; } @Override - public IHolder bind(Function> binder) { - if(binder == null) throw new NullPointerException("Binder must not be null"); + public IHolder bind(final Function> binder) { + if (binder == null) throw new NullPointerException("Binder must not be null"); - IList> boundHolders = heldHolders.map((containedHolder) -> { + final IList> boundHolders = heldHolders.map((containedHolder) -> { return containedHolder.bind(binder); }); @@ -30,8 +30,8 @@ public class BoundListHolder implements IHolder { } @Override - public Function> lift(Function func) { - if(func == null) throw new NullPointerException("Function to lift must not be null"); + public Function> lift(final Function func) { + if (func == null) throw new NullPointerException("Function to lift must not be null"); return (val) -> { return new ListHolder<>(func.apply(val)); @@ -39,10 +39,10 @@ public class BoundListHolder implements IHolder { } @Override - public IHolder map(Function mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public IHolder map(final Function mapper) { + if (mapper == null) throw new NullPointerException("Mapper must not be null"); - IList> mappedHolders = heldHolders.map((containedHolder) -> { + final IList> mappedHolders = heldHolders.map((containedHolder) -> { return containedHolder.map(mapper); }); @@ -50,8 +50,8 @@ public class BoundListHolder implements IHolder { } @Override - public IHolder transform(UnaryOperator transformer) { - if(transformer == null) throw new NullPointerException("Transformer must not be null"); + public IHolder transform(final UnaryOperator transformer) { + if (transformer == null) throw new NullPointerException("Transformer must not be null"); heldHolders.forEach((containedHolder) -> { containedHolder.transform(transformer); @@ -61,8 +61,8 @@ public class BoundListHolder implements IHolder { } @Override - public UnwrappedType unwrap(Function unwrapper) { - if(unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); + public UnwrappedType unwrap(final Function unwrapper) { + if (unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); return heldHolders.randItem().unwrap(unwrapper); } 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 35df1c3..a603a7f 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 @@ -1,39 +1,40 @@ package bjc.utils.data.internals; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; + import bjc.utils.data.IHolder; import bjc.utils.data.IPair; import bjc.utils.data.Identity; import bjc.utils.data.LazyPair; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.Supplier; - /* * A lazy pair, with only one side bound */ @SuppressWarnings("javadoc") public class HalfBoundLazyPair implements IPair { - private Supplier oldSupplier; + private final Supplier oldSupplier; - private Function> binder; + private final Function> binder; private IPair boundPair; private boolean pairBound; - public HalfBoundLazyPair(Supplier oldSupp, Function> bindr) { + public HalfBoundLazyPair(final Supplier oldSupp, + final Function> bindr) { oldSupplier = oldSupp; binder = bindr; } @Override public IPair bind( - BiFunction> bindr) { - IHolder> newPair = new Identity<>(boundPair); - IHolder newPairMade = new Identity<>(pairBound); + final BiFunction> bindr) { + final IHolder> newPair = new Identity<>(boundPair); + final IHolder newPairMade = new Identity<>(pairBound); - Supplier leftSupp = () -> { - if(!newPairMade.getValue()) { + final Supplier leftSupp = () -> { + if (!newPairMade.getValue()) { newPair.replace(binder.apply(oldSupplier.get())); newPairMade.replace(true); } @@ -41,8 +42,8 @@ public class HalfBoundLazyPair implements IPair pair.getLeft()); }; - Supplier rightSupp = () -> { - if(!newPairMade.getValue()) { + final Supplier rightSupp = () -> { + if (!newPairMade.getValue()) { newPair.replace(binder.apply(oldSupplier.get())); newPairMade.replace(true); } @@ -55,11 +56,11 @@ public class HalfBoundLazyPair implements IPair IPair bindLeft( - Function> leftBinder) { - Supplier leftSupp = () -> { + final Function> leftBinder) { + final Supplier leftSupp = () -> { IPair newPair = boundPair; - if(!pairBound) { + if (!pairBound) { newPair = binder.apply(oldSupplier.get()); } @@ -71,11 +72,11 @@ public class HalfBoundLazyPair implements IPair IPair bindRight( - Function> rightBinder) { - Supplier rightSupp = () -> { + final Function> rightBinder) { + final Supplier rightSupp = () -> { IPair newPair = boundPair; - if(!pairBound) { + if (!pairBound) { newPair = binder.apply(oldSupplier.get()); } @@ -87,9 +88,9 @@ public class HalfBoundLazyPair implements IPair IPair combine( - IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { + final IPair otherPair, + final BiFunction leftCombiner, + final BiFunction rightCombiner) { return otherPair.bind((otherLeft, otherRight) -> { return bind((leftVal, rightVal) -> { return new LazyPair<>(leftCombiner.apply(leftVal, otherLeft), @@ -99,17 +100,17 @@ public class HalfBoundLazyPair implements IPair IPair mapLeft(Function mapper) { - Supplier leftSupp = () -> { - if(pairBound) return mapper.apply(boundPair.getLeft()); + public IPair mapLeft(final Function mapper) { + final Supplier leftSupp = () -> { + if (pairBound) return mapper.apply(boundPair.getLeft()); - NewLeft leftVal = binder.apply(oldSupplier.get()).getLeft(); + final NewLeft leftVal = binder.apply(oldSupplier.get()).getLeft(); return mapper.apply(leftVal); }; - Supplier rightSupp = () -> { - if(pairBound) return boundPair.getRight(); + final Supplier rightSupp = () -> { + if (pairBound) return boundPair.getRight(); return binder.apply(oldSupplier.get()).getRight(); }; @@ -118,17 +119,17 @@ public class HalfBoundLazyPair implements IPair IPair mapRight(Function mapper) { - Supplier leftSupp = () -> { - if(pairBound) return boundPair.getLeft(); + public IPair mapRight(final Function mapper) { + final Supplier leftSupp = () -> { + if (pairBound) return boundPair.getLeft(); return binder.apply(oldSupplier.get()).getLeft(); }; - Supplier rightSupp = () -> { - if(pairBound) return mapper.apply(boundPair.getRight()); + final Supplier rightSupp = () -> { + if (pairBound) return mapper.apply(boundPair.getRight()); - NewRight rightVal = binder.apply(oldSupplier.get()).getRight(); + final NewRight rightVal = binder.apply(oldSupplier.get()).getRight(); return mapper.apply(rightVal); }; @@ -137,8 +138,8 @@ public class HalfBoundLazyPair implements IPair MergedType merge(BiFunction merger) { - if(!pairBound) { + public MergedType merge(final BiFunction merger) { + if (!pairBound) { boundPair = binder.apply(oldSupplier.get()); pairBound = true; diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedLazy.java index de161e5..d2e2b98 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedLazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/internals/WrappedLazy.java @@ -1,28 +1,28 @@ package bjc.utils.data.internals; -import bjc.utils.data.IHolder; -import bjc.utils.data.Lazy; - import java.util.function.Function; import java.util.function.UnaryOperator; +import bjc.utils.data.IHolder; +import bjc.utils.data.Lazy; + @SuppressWarnings("javadoc") public class WrappedLazy implements IHolder { - private IHolder> held; + private final IHolder> held; - public WrappedLazy(IHolder wrappedHolder) { + public WrappedLazy(final IHolder wrappedHolder) { held = new Lazy<>(wrappedHolder); } // This has an extra parameter, because otherwise it erases to the same // as the public one - private WrappedLazy(IHolder> wrappedHolder, boolean dummy) { + private WrappedLazy(final IHolder> wrappedHolder, final boolean dummy) { held = wrappedHolder; } @Override - public IHolder bind(Function> binder) { - IHolder> newHolder = held.map((containedHolder) -> { + public IHolder bind(final Function> binder) { + final IHolder> newHolder = held.map((containedHolder) -> { return containedHolder.bind(binder); }); @@ -30,15 +30,15 @@ public class WrappedLazy implements IHolder { } @Override - public Function> lift(Function func) { + public Function> lift(final Function func) { return (val) -> { return new Lazy<>(func.apply(val)); }; } @Override - public IHolder map(Function mapper) { - IHolder> newHolder = held.map((containedHolder) -> { + public IHolder map(final Function mapper) { + final IHolder> newHolder = held.map((containedHolder) -> { return containedHolder.map(mapper); }); @@ -46,7 +46,7 @@ public class WrappedLazy implements IHolder { } @Override - public IHolder transform(UnaryOperator transformer) { + public IHolder transform(final UnaryOperator transformer) { held.transform((containedHolder) -> { return containedHolder.transform(transformer); }); @@ -55,7 +55,7 @@ public class WrappedLazy implements IHolder { } @Override - public UnwrappedType unwrap(Function unwrapper) { + public UnwrappedType unwrap(final Function unwrapper) { return held.unwrap((containedHolder) -> { return containedHolder.unwrap(unwrapper); }); 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 e98332c..da53ab8 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 @@ -1,28 +1,28 @@ package bjc.utils.data.internals; -import bjc.utils.data.IHolder; -import bjc.utils.data.Option; - import java.util.function.Function; import java.util.function.UnaryOperator; +import bjc.utils.data.IHolder; +import bjc.utils.data.Option; + @SuppressWarnings("javadoc") public class WrappedOption implements IHolder { - private IHolder> held; + private final IHolder> held; - public WrappedOption(IHolder seedValue) { + public WrappedOption(final IHolder seedValue) { held = new Option<>(seedValue); } - private WrappedOption(IHolder> toHold, boolean dummy) { + private WrappedOption(final IHolder> toHold, final boolean dummy) { held = toHold; } @Override - public IHolder bind(Function> binder) { - IHolder> newHolder = held.map((containedHolder) -> { + public IHolder bind(final Function> binder) { + final IHolder> newHolder = held.map((containedHolder) -> { return containedHolder.bind((containedValue) -> { - if(containedValue == null) return new Option<>(null); + if (containedValue == null) return new Option<>(null); return binder.apply(containedValue); }); @@ -32,17 +32,17 @@ public class WrappedOption implements IHolder { } @Override - public Function> lift(Function func) { + public Function> lift(final Function func) { return (val) -> { return new Option<>(func.apply(val)); }; } @Override - public IHolder map(Function mapper) { - IHolder> newHolder = held.map((containedHolder) -> { + public IHolder map(final Function mapper) { + final IHolder> newHolder = held.map((containedHolder) -> { return containedHolder.map((containedValue) -> { - if(containedValue == null) return null; + if (containedValue == null) return null; return mapper.apply(containedValue); }); @@ -52,10 +52,10 @@ public class WrappedOption implements IHolder { } @Override - public IHolder transform(UnaryOperator transformer) { + public IHolder transform(final UnaryOperator transformer) { held.transform((containedHolder) -> { return containedHolder.transform((containedValue) -> { - if(containedValue == null) return null; + if (containedValue == null) return null; return transformer.apply(containedValue); }); @@ -65,10 +65,10 @@ public class WrappedOption implements IHolder { } @Override - public UnwrappedType unwrap(Function unwrapper) { + public UnwrappedType unwrap(final Function unwrapper) { return held.unwrap((containedHolder) -> { return containedHolder.unwrap((containedValue) -> { - if(containedValue == null) return null; + if (containedValue == null) return null; return unwrapper.apply(containedValue); }); -- cgit v1.2.3