diff options
Diffstat (limited to 'src/main/java/bjc/data')
29 files changed, 921 insertions, 712 deletions
diff --git a/src/main/java/bjc/data/ArrayIterator.java b/src/main/java/bjc/data/ArrayIterator.java index 6037e34..6d11a1d 100644 --- a/src/main/java/bjc/data/ArrayIterator.java +++ b/src/main/java/bjc/data/ArrayIterator.java @@ -1,22 +1,24 @@ package bjc.data; import java.util.Iterator; + /** * Represents an iterator over an array of values. * - * @param <T> The type of values in the array. - * + * @param <T> + * The type of values in the array. + * * @author Ben Culkin */ public class ArrayIterator<T> implements Iterator<T> { private Object[] arr; - private int idx; + private int idx; /** * Create a new array iterator. * * @param elms - * The array that will be iterated over. + * The array that will be iterated over. */ @SafeVarargs public ArrayIterator(T... elms) { @@ -32,8 +34,9 @@ public class ArrayIterator<T> implements Iterator<T> { @SuppressWarnings("unchecked") @Override public T next() { - if (idx >= arr.length) return null; + if (idx >= arr.length) + return null; - return (T)(arr[idx++]); + return (T) (arr[idx++]); } } diff --git a/src/main/java/bjc/data/BooleanToggle.java b/src/main/java/bjc/data/BooleanToggle.java index 209e97c..af083f5 100644 --- a/src/main/java/bjc/data/BooleanToggle.java +++ b/src/main/java/bjc/data/BooleanToggle.java @@ -21,7 +21,7 @@ public class BooleanToggle implements Toggle<Boolean> { * Create a flip-flop with the specified initial value. * * @param initial - * The initial value of the flip-flop. + * The initial value of the flip-flop. */ public BooleanToggle(final boolean initial) { val = initial; @@ -59,13 +59,17 @@ public class BooleanToggle implements Toggle<Boolean> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof BooleanToggle)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof BooleanToggle)) + return false; final BooleanToggle other = (BooleanToggle) obj; - if(val != other.val) return false; + if (val != other.val) + return false; return true; } diff --git a/src/main/java/bjc/data/CircularIterator.java b/src/main/java/bjc/data/CircularIterator.java index 6842b26..ce6b4c3 100644 --- a/src/main/java/bjc/data/CircularIterator.java +++ b/src/main/java/bjc/data/CircularIterator.java @@ -8,7 +8,7 @@ import java.util.Iterator; * @author EVE * * @param <E> - * The type of the iterable. + * The type of the iterable. */ public class CircularIterator<E> implements Iterator<E> { /* The iterable, and our current iterator into it. */ @@ -19,8 +19,7 @@ public class CircularIterator<E> implements Iterator<E> { private E curElm; /* - * Should we actually get new iterators, or just repeat the last - * element? + * Should we actually get new iterators, or just repeat the last element? */ private boolean doCircle; @@ -28,11 +27,11 @@ public class CircularIterator<E> implements Iterator<E> { * Create a new circular iterator. * * @param src - * The iterable to iterate from. + * The iterable to iterate from. * * @param circ - * Should we actually do circular iteration, or just - * repeat the terminal element? + * Should we actually do circular iteration, or just repeat the + * terminal element? */ public CircularIterator(final Iterable<E> src, final boolean circ) { source = src; @@ -45,7 +44,7 @@ public class CircularIterator<E> implements Iterator<E> { * Create a new circular iterator that does actual circular iteration. * * @param src - * The iterable to iterate from. + * The iterable to iterate from. */ public CircularIterator(final Iterable<E> src) { this(src, true); @@ -63,7 +62,7 @@ public class CircularIterator<E> implements Iterator<E> { if (!doCircle) { return curElm; } - + curr = source.iterator(); } diff --git a/src/main/java/bjc/data/Either.java b/src/main/java/bjc/data/Either.java index 0588496..55518d8 100644 --- a/src/main/java/bjc/data/Either.java +++ b/src/main/java/bjc/data/Either.java @@ -9,10 +9,10 @@ import java.util.function.Function; * @author ben * * @param <LeftType> - * The type that could be on the left. + * The type that could be on the left. * * @param <RightType> - * The type that could be on the right. + * The type that could be on the right. * */ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { @@ -20,17 +20,18 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { * Create a new either with the left value occupied. * * @param <LeftType> - * The type of the left value. + * The type of the left value. * * @param <RightType> - * The type of the empty right value. - * + * The type of the empty right value. + * * @param left - * The value to put on the left. + * The value to put on the left. * * @return An either with the left side occupied. */ - public static <LeftType, RightType> Either<LeftType, RightType> left(final LeftType left) { + public static <LeftType, RightType> Either<LeftType, RightType> + left(final LeftType left) { return new Either<>(left, null); } @@ -38,17 +39,18 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { * Create a new either with the right value occupied. * * @param <LeftType> - * The type of the empty left value. + * The type of the empty left value. * * @param <RightType> - * The type of the right value. + * The type of the right value. * * @param right - * The value to put on the right. + * The value to put on the right. * * @return An either with the right side occupied. */ - public static <LeftType, RightType> Either<LeftType, RightType> right(final RightType right) { + public static <LeftType, RightType> Either<LeftType, RightType> + right(final RightType right) { return new Either<>(null, right); } @@ -61,7 +63,7 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { /* Create a new either with specifed values. */ private Either(final LeftType left, final RightType right) { - if(left == null) { + if (left == null) { rightVal = right; } else { leftVal = left; @@ -73,17 +75,20 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( final BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) { - if(binder == null) throw new NullPointerException("Binder must not be null"); + if (binder == null) + throw new NullPointerException("Binder must not be null"); return binder.apply(leftVal, rightVal); } @Override - public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( - final Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { - if(leftBinder == null) throw new NullPointerException("Left binder must not be null"); + public <BoundLeft> IPair<BoundLeft, RightType> + bindLeft(final Function<LeftType, IPair<BoundLeft, RightType>> 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); } @@ -91,27 +96,31 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <BoundRight> IPair<LeftType, BoundRight> bindRight( final Function<RightType, IPair<LeftType, BoundRight>> rightBinder) { - if(rightBinder == null) throw new NullPointerException("Right binder must not be null"); + 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 <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - final IPair<OtherLeft, OtherRight> otherPair, - final BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - final BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { - if(otherPair == null) { + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> + IPair<CombinedLeft, CombinedRight> + combine(final IPair<OtherLeft, OtherRight> otherPair, + final BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + final BiFunction<RightType, OtherRight, + CombinedRight> 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) { + } else if (rightCombiner == null) { throw new NullPointerException("Right combiner must not be null"); } - if(isLeft) { + if (isLeft) { return otherPair.bind((otherLeft, otherRight) -> { CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); @@ -127,26 +136,34 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { } @Override - public <NewLeft> IPair<NewLeft, RightType> mapLeft(final Function<LeftType, NewLeft> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <NewLeft> IPair<NewLeft, RightType> + mapLeft(final Function<LeftType, NewLeft> 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 <NewRight> IPair<LeftType, NewRight> mapRight(final Function<RightType, NewRight> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <NewRight> IPair<LeftType, NewRight> + mapRight(final Function<RightType, NewRight> 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> MergedType merge(final BiFunction<LeftType, RightType, MergedType> merger) { - if(merger == null) throw new NullPointerException("Merger must not be null"); + public <MergedType> MergedType + merge(final BiFunction<LeftType, RightType, MergedType> merger) { + if (merger == null) + throw new NullPointerException("Merger must not be null"); return merger.apply(leftVal, rightVal); } @@ -165,27 +182,36 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof Either<?, ?>)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Either<?, ?>)) + return false; 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 (leftVal == null) { + 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 (rightVal == null) { + if (other.rightVal != null) + return false; + } else if (!rightVal.equals(other.rightVal)) + return false; return true; } @Override public String toString() { - return String.format("Either [leftVal='%s', rightVal='%s', isLeft=%s]", leftVal, rightVal, isLeft); + return String.format("Either [leftVal='%s', rightVal='%s', isLeft=%s]", leftVal, + rightVal, isLeft); } } diff --git a/src/main/java/bjc/data/GeneratingIterator.java b/src/main/java/bjc/data/GeneratingIterator.java index 9adda6f..ffa92cf 100644 --- a/src/main/java/bjc/data/GeneratingIterator.java +++ b/src/main/java/bjc/data/GeneratingIterator.java @@ -6,11 +6,11 @@ import java.util.function.UnaryOperator; /** * An iterator that generates a series of elements from a single element. - * + * * @author bjculkin * * @param <E> - * The type of element generated. + * The type of element generated. */ public class GeneratingIterator<E> implements Iterator<E> { /* Our current state. */ @@ -22,18 +22,19 @@ public class GeneratingIterator<E> implements Iterator<E> { /** * Create a new generative iterator. - * + * * @param initial - * The initial state of the generator. - * + * The initial state of the generator. + * * @param transition - * The function to apply to the state. - * + * The function to apply to the state. + * * @param stopper - * The predicate applied to the current state to determine when - * to stop. + * The predicate applied to the current state to determine + * when to stop. */ - public GeneratingIterator(E initial, UnaryOperator<E> transition, Predicate<E> stopper) { + public GeneratingIterator(E initial, UnaryOperator<E> transition, + Predicate<E> stopper) { state = initial; transtion = transition; stpper = stopper; @@ -47,9 +48,9 @@ public class GeneratingIterator<E> implements Iterator<E> { /* * @NOTE * - * As this currently is, it only works correctly assuming that next() is - * only called when hasNext() is true. Should we safeguard against - * people who are not doing the right thing? + * As this currently is, it only works correctly assuming that next() is only + * called when hasNext() is true. Should we safeguard against people who are not + * doing the right thing? */ @Override public E next() { diff --git a/src/main/java/bjc/data/IHolder.java b/src/main/java/bjc/data/IHolder.java index e56e23e..faa3f64 100644 --- a/src/main/java/bjc/data/IHolder.java +++ b/src/main/java/bjc/data/IHolder.java @@ -16,27 +16,28 @@ import bjc.funcdata.theory.Functor; * @author ben * * @param <ContainedType> - * The type of value held. + * The type of value held. */ public interface IHolder<ContainedType> extends Functor<ContainedType> { /** * Bind a function across the value in this container. * * @param <BoundType> - * The type of value in this container. + * The type of value in this container. * * @param binder - * The function to bind to the value. + * The function to bind to the value. * * @return A holder from binding the value. */ - public <BoundType> IHolder<BoundType> bind(Function<ContainedType, IHolder<BoundType>> binder); + public <BoundType> IHolder<BoundType> + bind(Function<ContainedType, IHolder<BoundType>> binder); /** * Apply an action to the value. * * @param action - * The action to apply to the value. + * The action to apply to the value. */ public default void doWith(final Consumer<? super ContainedType> action) { transform(value -> { @@ -47,11 +48,12 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { } @Override - default <ArgType, ReturnType> Function<Functor<ArgType>, Functor<ReturnType>> fmap( - final Function<ArgType, ReturnType> func) { + default <ArgType, ReturnType> Function<Functor<ArgType>, Functor<ReturnType>> + fmap(final Function<ArgType, ReturnType> func) { return argumentFunctor -> { - if(!(argumentFunctor instanceof IHolder<?>)) { - final String msg = "This functor only supports mapping over instances of IHolder"; + if (!(argumentFunctor instanceof IHolder<?>)) { + final String msg + = "This functor only supports mapping over instances of IHolder"; throw new IllegalArgumentException(msg); } @@ -71,14 +73,15 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * Lifts a function to bind over this holder. * * @param <NewType> - * The type of the functions return. + * The type of the functions return. * * @param func - * The function to lift over the holder. + * The function to lift over the holder. * * @return The function lifted over the holder. */ - public <NewType> Function<ContainedType, IHolder<NewType>> lift(Function<ContainedType, NewType> func); + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(Function<ContainedType, NewType> func); /** * Make this holder lazy. @@ -108,56 +111,54 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { } /** - * Create a new holder with a mapped version of the value in this - * holder. + * Create a new holder with a mapped version of the value in this holder. * * Does not change the internal state of this holder. * * @param <MappedType> - * The type of the mapped value. + * The type of the mapped value. * * @param mapper - * The function to do mapping with. + * The function to do mapping with. * * @return A holder with the mapped value */ - public <MappedType> IHolder<MappedType> map(Function<ContainedType, MappedType> mapper); + public <MappedType> IHolder<MappedType> + map(Function<ContainedType, MappedType> mapper); /** * Replace the held value with a new one. * * @param newValue - * The value to hold instead. + * The value to hold instead. * * @return The holder itself. */ public default IHolder<ContainedType> replace(final ContainedType newValue) { - return transform(oldValue -> { - return newValue; - }); + return transform(oldValue -> newValue); } /** * Transform the value held in this holder. * * @param transformer - * The function to transform the value with. + * The function to transform the value with. * * @return The holder itself, for easy chaining. */ public IHolder<ContainedType> transform(UnaryOperator<ContainedType> transformer); /** - * Unwrap the value contained in this holder so that it is no longer - * held. + * Unwrap the value contained in this holder so that it is no longer held. * * @param <UnwrappedType> - * The type of the unwrapped value. + * The type of the unwrapped value. * * @param unwrapper - * The function to use to unwrap the value. + * The function to use to unwrap the value. * * @return The unwrapped held value. */ - public <UnwrappedType> UnwrappedType unwrap(Function<ContainedType, UnwrappedType> unwrapper); + public <UnwrappedType> UnwrappedType + unwrap(Function<ContainedType, UnwrappedType> unwrapper); } diff --git a/src/main/java/bjc/data/IPair.java b/src/main/java/bjc/data/IPair.java index 0c63f16..5b1298e 100644 --- a/src/main/java/bjc/data/IPair.java +++ b/src/main/java/bjc/data/IPair.java @@ -12,10 +12,10 @@ import bjc.funcdata.theory.Bifunctor; * @author ben * * @param <LeftType> - * The type of the left side of the pair. + * The type of the left side of the pair. * * @param <RightType> - * The type of the right side of the pair. + * The type of the right side of the pair. * */ public interface IPair<LeftType, RightType> extends Bifunctor<LeftType, RightType> { @@ -23,103 +23,105 @@ public interface IPair<LeftType, RightType> extends Bifunctor<LeftType, RightTyp * Bind a function across the values in this pair. * * @param <BoundLeft> - * The type of the bound left. + * The type of the bound left. * * @param <BoundRight> - * The type of the bound right. + * The type of the bound right. * * @param binder - * The function to bind with. + * The function to bind with. * * @return The bound pair. */ - public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( - BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder); + public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> + bind(BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder); /** * Bind a function to the left value in this pair. * * @param <BoundLeft> - * The type of the bound value. + * The type of the bound value. * * @param leftBinder - * The function to use to bind. + * The function to use to bind. * * @return A pair with the left type bound. */ - public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( - Function<LeftType, IPair<BoundLeft, RightType>> leftBinder); + public <BoundLeft> IPair<BoundLeft, RightType> + bindLeft(Function<LeftType, IPair<BoundLeft, RightType>> leftBinder); /** * Bind a function to the right value in this pair. * * @param <BoundRight> - * The type of the bound value. + * The type of the bound value. * * @param rightBinder - * The function to use to bind. + * The function to use to bind. * * @return A pair with the right type bound. */ - public <BoundRight> IPair<LeftType, BoundRight> bindRight( - Function<RightType, IPair<LeftType, BoundRight>> rightBinder); + public <BoundRight> IPair<LeftType, BoundRight> + bindRight(Function<RightType, IPair<LeftType, BoundRight>> rightBinder); /** * Pairwise combine two pairs together. * * @param <OtherLeft> - * The left type of the other pair. + * The left type of the other pair. * * @param <OtherRight> - * The right type of the other pair. + * The right type of the other pair. * * @param otherPair - * The pair to combine with. + * The pair to combine with. * * @return The pairs, pairwise combined together. */ - public default <OtherLeft, OtherRight> IPair<IPair<LeftType, OtherLeft>, IPair<RightType, OtherRight>> combine( - final IPair<OtherLeft, OtherRight> otherPair) { - return combine(otherPair, Pair<LeftType, OtherLeft>::new, Pair<RightType, OtherRight>::new); + public default <OtherLeft, OtherRight> + IPair<IPair<LeftType, OtherLeft>, IPair<RightType, OtherRight>> + combine(final IPair<OtherLeft, OtherRight> otherPair) { + return combine(otherPair, Pair<LeftType, OtherLeft>::new, + Pair<RightType, OtherRight>::new); } /** * Combine the contents of two pairs together. * * @param <OtherLeft> - * The type of the left value of the other pair. + * The type of the left value of the other pair. * * @param <OtherRight> - * The type of the right value of the other pair. + * The type of the right value of the other pair. * * @param <CombinedLeft> - * The type of the left value of the combined pair. + * The type of the left value of the combined pair. * * @param <CombinedRight> - * The type of the right value of the combined pair. + * The type of the right value of the combined pair. * * @param otherPair - * The other pair to combine with. + * The other pair to combine with. * * @param leftCombiner - * The function to combine the left values with. + * The function to combine the left values with. * * @param rightCombiner - * The function to combine the right values with. + * The function to combine the right values with. * * @return A pair with its values combined. */ - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - IPair<OtherLeft, OtherRight> otherPair, - BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<RightType, OtherRight, CombinedRight> rightCombiner); + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> + IPair<CombinedLeft, CombinedRight> + combine(IPair<OtherLeft, OtherRight> otherPair, + BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + BiFunction<RightType, OtherRight, CombinedRight> rightCombiner); /** - * Immediately perfom the specified action with the contents of this - * pair. + * 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(final BiConsumer<LeftType, RightType> consumer) { merge((leftValue, rightValue) -> { @@ -130,32 +132,36 @@ public interface IPair<LeftType, RightType> extends Bifunctor<LeftType, RightTyp } @Override - default <OldLeft, OldRight, NewLeft> LeftBifunctorMap<OldLeft, OldRight, NewLeft> fmapLeft( - final Function<OldLeft, NewLeft> func) { + default <OldLeft, OldRight, NewLeft> LeftBifunctorMap<OldLeft, OldRight, NewLeft> + fmapLeft(final Function<OldLeft, NewLeft> func) { return argumentPair -> { if (!(argumentPair instanceof IPair<?, ?>)) { - final String msg = "This function can only be applied to instances of IPair"; + final String msg + = "This function can only be applied to instances of IPair"; throw new IllegalArgumentException(msg); } - final IPair<OldLeft, OldRight> argPair = (IPair<OldLeft, OldRight>) argumentPair; + final IPair<OldLeft, OldRight> argPair + = (IPair<OldLeft, OldRight>) argumentPair; return argPair.mapLeft(func); }; } @Override - default <OldLeft, OldRight, NewRight> RightBifunctorMap<OldLeft, OldRight, NewRight> fmapRight( - final Function<OldRight, NewRight> func) { + default <OldLeft, OldRight, NewRight> RightBifunctorMap<OldLeft, OldRight, NewRight> + fmapRight(final Function<OldRight, NewRight> func) { return argumentPair -> { if (!(argumentPair instanceof IPair<?, ?>)) { - final String msg = "This function can only be applied to instances of IPair"; + final String msg + = "This function can only be applied to instances of IPair"; throw new IllegalArgumentException(msg); } - final IPair<OldLeft, OldRight> argPair = (IPair<OldLeft, OldRight>) argumentPair; + final IPair<OldLeft, OldRight> argPair + = (IPair<OldLeft, OldRight>) argumentPair; return argPair.mapRight(func); }; @@ -187,15 +193,15 @@ public interface IPair<LeftType, RightType> extends Bifunctor<LeftType, RightTyp * Doesn't modify the pair. * * @param <NewLeft> - * The new type of the left part of the pair. + * The new type of the left part of the pair. * * @param mapper - * The function to use to transform the left part of the - * pair. + * The function to use to transform the left part of the pair. * * @return The pair, with its left part transformed. */ - public <NewLeft> IPair<NewLeft, RightType> mapLeft(Function<LeftType, NewLeft> mapper); + public <NewLeft> IPair<NewLeft, RightType> + mapLeft(Function<LeftType, NewLeft> mapper); /** * Transform the value on the right side of the pair. @@ -203,36 +209,38 @@ public interface IPair<LeftType, RightType> extends Bifunctor<LeftType, RightTyp * Doesn't modify the pair. * * @param <NewRight> - * The new type of the right part of the pair. + * The new type of the right part of the pair. * * @param mapper - * The function to use to transform the right part of the - * pair. + * The function to use to transform the right part of the + * pair. * * @return The pair, with its right part transformed. */ - public <NewRight> IPair<LeftType, NewRight> mapRight(Function<RightType, NewRight> mapper); + public <NewRight> IPair<LeftType, NewRight> + mapRight(Function<RightType, NewRight> mapper); /** * Merge the two values in this pair into a single value. * * @param <MergedType> - * The type of the single value. + * The type of the single value. * * @param merger - * The function to use for merging. + * The function to use for merging. * * @return The pair, merged into a single value. */ - public <MergedType> MergedType merge(BiFunction<LeftType, RightType, MergedType> merger); + public <MergedType> MergedType + merge(BiFunction<LeftType, RightType, MergedType> merger); /** * Static pair constructor. - * + * * @param left - * The left side of the pair. + * The left side of the pair. * @param right - * The right side of the pair. + * The right side of the pair. * @return A pair, with the specified left/right side. */ public static <T1, T2> IPair<T1, T2> pair(T1 left, T2 right) { diff --git a/src/main/java/bjc/data/ITree.java b/src/main/java/bjc/data/ITree.java index 4088c10..e9c829e 100644 --- a/src/main/java/bjc/data/ITree.java +++ b/src/main/java/bjc/data/ITree.java @@ -15,7 +15,7 @@ import bjc.funcdata.bst.TreeLinearizationMethod; * @author ben * * @param <ContainedType> - * The type of data contained in the tree nodes. + * The type of data contained in the tree nodes. * */ public interface ITree<ContainedType> { @@ -23,7 +23,7 @@ public interface ITree<ContainedType> { * Append a child to this node. * * @param child - * The child to append to this node. + * The child to append to this node. */ void addChild(ITree<ContainedType> child); @@ -31,7 +31,7 @@ public interface ITree<ContainedType> { * Append a child to this node. * * @param child - * The child to append to this node. + * The child to append to this node. */ void addChild(ContainedType child); @@ -39,7 +39,7 @@ public interface ITree<ContainedType> { * Prepend a child to this node. * * @param child - * The child to prepend to this node. + * The child to prepend to this node. */ void prependChild(ITree<ContainedType> child); @@ -47,25 +47,26 @@ public interface ITree<ContainedType> { * Collapse a tree into a single version. * * @param <NewType> - * The intermediate type being folded. + * The intermediate type being folded. * * @param <ReturnedType> - * The type that is the end result. + * The type that is the end result. * * @param leafTransform - * The function to use to convert leaf values. + * The function to use to convert leaf values. * * @param nodeCollapser - * The function to use to convert internal nodes and their - * children. + * The function to use to convert internal nodes and + * their children. * * @param resultTransformer - * The function to use to convert a state to the returned - * version. + * The function to use to convert a state to the + * returned version. * * @return The final transformed state. */ - <NewType, ReturnedType> ReturnedType collapse(Function<ContainedType, NewType> leafTransform, + <NewType, ReturnedType> ReturnedType collapse( + Function<ContainedType, NewType> leafTransform, BiFunction<ContainedType, IList<NewType>, NewType> nodeCollapser, Function<NewType, ReturnedType> resultTransformer); @@ -73,22 +74,23 @@ public interface ITree<ContainedType> { * Execute a given action for each of this tree's children. * * @param action - * The action to execute for each child. + * The action to execute for each child. */ void doForChildren(Consumer<ITree<ContainedType>> action); /** - * Expand the nodes of a tree into trees, and then merge the contents of - * those trees into a single tree. + * Expand the nodes of a tree into trees, and then merge the contents of those + * trees into a single tree. * * @param mapper - * The function to use to map values into trees. + * The function to use to map values into trees. * * @return A tree, with some nodes expanded into trees. */ - default ITree<ContainedType> flatMapTree(final Function<ContainedType, ITree<ContainedType>> mapper) { + default ITree<ContainedType> + flatMapTree(final Function<ContainedType, ITree<ContainedType>> mapper) { return topDownTransform(dat -> TopDownTransformResult.PUSHDOWN, node -> { - if(node.getChildrenCount() > 0) { + if (node.getChildrenCount() > 0) { final ITree<ContainedType> parent = node.transformHead(mapper); node.doForChildren(parent::addChild); @@ -104,7 +106,7 @@ public interface ITree<ContainedType> { * 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. */ @@ -141,61 +143,64 @@ public interface ITree<ContainedType> { * Rebuild the tree with the same structure, but different nodes. * * @param <MappedType> - * The type of the new tree. + * The type of the new tree. * * @param leafTransformer - * The function to use to transform leaf tokens. + * The function to use to transform leaf tokens. * * @param internalTransformer - * The function to use to transform internal tokens. + * The function to use to transform internal tokens. * * @return The tree, with the nodes changed. */ - <MappedType> ITree<MappedType> rebuildTree(Function<ContainedType, MappedType> leafTransformer, + <MappedType> ITree<MappedType> rebuildTree( + Function<ContainedType, MappedType> leafTransformer, Function<ContainedType, MappedType> internalTransformer); /** * 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. */ - void selectiveTransform(Predicate<ContainedType> nodePicker, UnaryOperator<ContainedType> transformer); + void selectiveTransform(Predicate<ContainedType> nodePicker, + UnaryOperator<ContainedType> transformer); /** * Do a top-down transform of the tree. * * @param transformPicker - * The function to use to pick how to progress. + * The function to use to pick how to progress. * * @param transformer - * The function used to transform picked subtrees. + * The function used to transform picked subtrees. * * @return The tree with the transform applied to picked subtrees. */ - ITree<ContainedType> topDownTransform(Function<ContainedType, TopDownTransformResult> transformPicker, + ITree<ContainedType> topDownTransform( + Function<ContainedType, TopDownTransformResult> transformPicker, UnaryOperator<ITree<ContainedType>> transformer); /** * Transform one of this nodes children. * * @param <TransformedType> - * The type of the transformed value. + * The type of the transformed value. * * @param childNo - * The number of the child to transform. + * The number of the child to transform. * * @param transformer - * The function to use to transform the value. + * The function to use to transform the value. * * @return The transformed value. * * @throws IllegalArgumentException - * if the childNo is out of bounds (0 <= childNo <= - * childCount()). + * if the childNo is out of bounds (0 <= + * childNo <= childCount()). */ <TransformedType> TransformedType transformChild(int childNo, Function<ITree<ContainedType>, TransformedType> transformer); @@ -204,27 +209,29 @@ public interface ITree<ContainedType> { * Transform the value that is the head of this node. * * @param <TransformedType> - * The type of the transformed value. + * The type of the transformed value. * * @param transformer - * The function to use to transform the value. + * The function to use to transform the value. * * @return The transformed value. */ - <TransformedType> TransformedType transformHead(Function<ContainedType, TransformedType> transformer); + <TransformedType> TransformedType + transformHead(Function<ContainedType, TransformedType> transformer); /** * Transform the tree into a tree with a different type of token. * * @param <MappedType> - * The type of the new tree. + * The type of the new tree. * * @param transformer - * The function to use to transform tokens. + * The function to use to transform tokens. * * @return A tree with the token types transformed. */ - default <MappedType> ITree<MappedType> transformTree(final Function<ContainedType, MappedType> transformer) { + default <MappedType> ITree<MappedType> + transformTree(final Function<ContainedType, MappedType> transformer) { return rebuildTree(transformer, transformer); } @@ -232,21 +239,22 @@ public interface ITree<ContainedType> { * 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. */ - void traverse(TreeLinearizationMethod linearizationMethod, Consumer<ContainedType> action); + void traverse(TreeLinearizationMethod linearizationMethod, + Consumer<ContainedType> action); /** * Find the farthest to right child that satisfies the given predicate. * * @param childPred - * The predicate to satisfy. + * The predicate to satisfy. * - * @return The index of the right-most child that satisfies the - * predicate, or -1 if one doesn't exist. + * @return The index of the right-most child that satisfies the predicate, or -1 + * if one doesn't exist. */ int revFind(Predicate<ITree<ContainedType>> childPred); @@ -254,15 +262,16 @@ public interface ITree<ContainedType> { * Check if this tree contains any nodes that satisfy the predicate. * * @param pred - * The predicate to look for. - * + * The predicate to look for. + * * @return Whether or not any items satisfied the predicate. */ default boolean containsMatching(Predicate<ContainedType> pred) { Toggle<Boolean> tog = new OneWayToggle<>(false, true); - traverse(TreeLinearizationMethod.POSTORDER, (val) -> { - if(pred.test(val)) tog.get(); + traverse(TreeLinearizationMethod.POSTORDER, val -> { + if (pred.test(val)) + tog.get(); }); return tog.get(); @@ -272,7 +281,7 @@ public interface ITree<ContainedType> { * Set the head of the tree. * * @param dat - * The value to set as the head of the tree. + * The value to set as the head of the tree. */ void setHead(ContainedType dat); } diff --git a/src/main/java/bjc/data/Identity.java b/src/main/java/bjc/data/Identity.java index 8645c4c..c80c7e1 100644 --- a/src/main/java/bjc/data/Identity.java +++ b/src/main/java/bjc/data/Identity.java @@ -9,7 +9,7 @@ import java.util.function.UnaryOperator; * @author ben * * @param <ContainedType> - * The type contained in the holder. + * The type contained in the holder. */ public class Identity<ContainedType> implements IHolder<ContainedType> { /* The held value. */ @@ -24,14 +24,15 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { * Create a holder holding the specified value. * * @param value - * The value to hold. + * The value to hold. */ public Identity(final ContainedType value) { heldValue = value; } @Override - public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) { + public <BoundType> IHolder<BoundType> + bind(final Function<ContainedType, IHolder<BoundType>> binder) { return binder.apply(heldValue); } @@ -47,28 +48,35 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof Identity)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Identity)) + return false; final Identity<?> other = (Identity<?>) obj; - if(heldValue == null) { - if(other.heldValue != null) return false; - } else if(!heldValue.equals(other.heldValue)) return false; + if (heldValue == null) { + if (other.heldValue != null) + return false; + } else if (!heldValue.equals(other.heldValue)) + return false; return true; } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) { - return (val) -> { + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(final Function<ContainedType, NewType> func) { + return val -> { return new Identity<>(func.apply(val)); }; } @Override - public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) { + public <MappedType> IHolder<MappedType> + map(final Function<ContainedType, MappedType> mapper) { return new Identity<>(mapper.apply(heldValue)); } @@ -78,14 +86,16 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { } @Override - public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { + public IHolder<ContainedType> + transform(final UnaryOperator<ContainedType> transformer) { heldValue = transformer.apply(heldValue); return this; } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { + public <UnwrappedType> UnwrappedType + unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { return unwrapper.apply(heldValue); } @@ -93,7 +103,7 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { * Create a new identity container. * * @param val - * The contained value. + * The contained value. * * @return A new identity container. */ diff --git a/src/main/java/bjc/data/Lazy.java b/src/main/java/bjc/data/Lazy.java index 0702665..a425232 100644 --- a/src/main/java/bjc/data/Lazy.java +++ b/src/main/java/bjc/data/Lazy.java @@ -15,7 +15,7 @@ import bjc.funcdata.IList; * @author ben * * @param <ContainedType> - * The type of the value being held. + * The type of the value being held. */ public class Lazy<ContainedType> implements IHolder<ContainedType> { /* The supplier of the type. */ @@ -32,7 +32,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { * Create a new lazy value from the specified seed value. * * @param value - * The seed value to use. + * The seed value to use. */ public Lazy(final ContainedType value) { heldValue = value; @@ -44,7 +44,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { * Create a new lazy value from the specified value source. * * @param supp - * The source of a value to use. + * The source of a value to use. */ public Lazy(final Supplier<ContainedType> supp) { valueSupplier = new SingleSupplier<>(supp); @@ -53,38 +53,39 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { } /* Create a new value from a supplier and a list of actions. */ - private Lazy(final Supplier<ContainedType> supp, final IList<UnaryOperator<ContainedType>> pendingActions) { + private Lazy(final Supplier<ContainedType> supp, + final IList<UnaryOperator<ContainedType>> pendingActions) { valueSupplier = supp; actions = pendingActions; } @Override - public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) { + public <BoundType> IHolder<BoundType> + bind(final Function<ContainedType, IHolder<BoundType>> binder) { final IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); final Supplier<ContainedType> supplier = () -> { - if(valueMaterialized) return heldValue; + if (valueMaterialized) + return heldValue; return valueSupplier.get(); }; - return new BoundLazy<>(() -> { - return new Lazy<>(supplier, pendingActions); - }, binder); + return new BoundLazy<>(() -> new Lazy<>(supplier, pendingActions), binder); } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) { - return val -> { - return new Lazy<>(func.apply(val)); - }; + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(final Function<ContainedType, NewType> func) { + return val -> new Lazy<>(func.apply(val)); } @Override - public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) { + public <MappedType> IHolder<MappedType> + map(final Function<ContainedType, MappedType> mapper) { final IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); @@ -92,7 +93,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { return new Lazy<>(() -> { ContainedType currVal = heldValue; - if(!valueMaterialized) { + if (!valueMaterialized) { currVal = valueSupplier.get(); } @@ -103,8 +104,8 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { @Override public String toString() { - if(valueMaterialized) { - if(actions.isEmpty()) { + if (valueMaterialized) { + if (actions.isEmpty()) { return String.format("value[v='%s']", heldValue); } @@ -115,15 +116,17 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { } @Override - public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { + public IHolder<ContainedType> + transform(final UnaryOperator<ContainedType> transformer) { actions.add(transformer); return this; } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { - if(!valueMaterialized) { + public <UnwrappedType> UnwrappedType + unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { + if (!valueMaterialized) { heldValue = valueSupplier.get(); valueMaterialized = true; @@ -152,24 +155,32 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof Lazy<?>)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Lazy<?>)) + return false; 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; + if (valueMaterialized) { + if (heldValue == null) { + 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 (actions == null) { + if (other.actions != null) + return false; + } else if (actions.getSize() > 0 || other.actions.getSize() > 0) + return false; return true; } @@ -178,7 +189,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { * Create a new lazy container with an already present value. * * @param val - * The value for the lazy container. + * The value for the lazy container. * * @return A new lazy container holding that value. */ @@ -190,12 +201,12 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { * Create a new lazy container with a suspended value. * * @param supp - * The suspended value for the lazy container. + * The suspended value for the lazy container. * - * @return A new lazy container that will un-suspend the value when - * necessary. + * @return A new lazy container that will un-suspend the value when necessary. */ - public static <ContainedType> Lazy<ContainedType> lazy(final Supplier<ContainedType> supp) { + public static <ContainedType> Lazy<ContainedType> + lazy(final Supplier<ContainedType> supp) { return new Lazy<>(supp); } } diff --git a/src/main/java/bjc/data/LazyPair.java b/src/main/java/bjc/data/LazyPair.java index 1633c65..e668cd4 100644 --- a/src/main/java/bjc/data/LazyPair.java +++ b/src/main/java/bjc/data/LazyPair.java @@ -13,10 +13,10 @@ import bjc.data.internals.HalfBoundLazyPair; * @author ben * * @param <LeftType> - * The type on the left side of the pair. + * The type on the left side of the pair. * * @param <RightType> - * The type on the right side of the pair. + * The type on the right side of the pair. */ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> { /* The supplier for the left value. */ @@ -37,10 +37,10 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> * 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(final LeftType leftVal, final RightType rightVal) { leftValue = leftVal; @@ -54,12 +54,13 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> * Create a new lazy pair from the given value sources. * * @param leftSupp - * The source for a value on the left side of the pair. + * The source for a value on the left side of the pair. * * @param rightSupp - * The source for a value on the right side of the pair. + * The source for a value on the right side of the pair. */ - public LazyPair(final Supplier<LeftType> leftSupp, final Supplier<RightType> rightSupp) { + public LazyPair(final Supplier<LeftType> leftSupp, + final Supplier<RightType> rightSupp) { /* Use single suppliers to catch double-instantiation bugs. */ leftSupplier = new SingleSupplier<>(leftSupp); rightSupplier = new SingleSupplier<>(rightSupp); @@ -75,10 +76,11 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> } @Override - public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( - final Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { + public <BoundLeft> IPair<BoundLeft, RightType> + bindLeft(final Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { final Supplier<LeftType> leftSupp = () -> { - if(leftMaterialized) return leftValue; + if (leftMaterialized) + return leftValue; return leftSupplier.get(); }; @@ -90,7 +92,8 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> public <BoundRight> IPair<LeftType, BoundRight> bindRight( final Function<RightType, IPair<LeftType, BoundRight>> rightBinder) { final Supplier<RightType> rightSupp = () -> { - if(rightMaterialized) return rightValue; + if (rightMaterialized) + return rightValue; return rightSupplier.get(); }; @@ -99,23 +102,23 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - final IPair<OtherLeft, OtherRight> otherPair, - final BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - final BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - final CombinedLeft left = leftCombiner.apply(leftVal, otherLeft); - final CombinedRight right = rightCombiner.apply(rightVal, otherRight); - - return new LazyPair<>(left, right); - }); - }); + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> + IPair<CombinedLeft, CombinedRight> + combine(final IPair<OtherLeft, OtherRight> otherPair, + final BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + final BiFunction<RightType, OtherRight, + CombinedRight> rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> bind((leftVal, rightVal) -> { + final CombinedLeft left = leftCombiner.apply(leftVal, otherLeft); + final CombinedRight right = rightCombiner.apply(rightVal, otherRight); + + return new LazyPair<>(left, right); + })); } @Override public LeftType getLeft() { - if(!leftMaterialized) { + if (!leftMaterialized) { leftValue = leftSupplier.get(); leftMaterialized = true; @@ -126,7 +129,7 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public RightType getRight() { - if(!rightMaterialized) { + if (!rightMaterialized) { rightValue = rightSupplier.get(); rightMaterialized = true; @@ -136,15 +139,18 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> } @Override - public <NewLeft> IPair<NewLeft, RightType> mapLeft(final Function<LeftType, NewLeft> mapper) { + public <NewLeft> IPair<NewLeft, RightType> + mapLeft(final Function<LeftType, NewLeft> mapper) { final Supplier<NewLeft> leftSupp = () -> { - if(leftMaterialized) return mapper.apply(leftValue); + if (leftMaterialized) + return mapper.apply(leftValue); return mapper.apply(leftSupplier.get()); }; final Supplier<RightType> rightSupp = () -> { - if(rightMaterialized) return rightValue; + if (rightMaterialized) + return rightValue; return rightSupplier.get(); }; @@ -153,15 +159,18 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> } @Override - public <NewRight> IPair<LeftType, NewRight> mapRight(final Function<RightType, NewRight> mapper) { + public <NewRight> IPair<LeftType, NewRight> + mapRight(final Function<RightType, NewRight> mapper) { final Supplier<LeftType> leftSupp = () -> { - if(leftMaterialized) return leftValue; + if (leftMaterialized) + return leftValue; return leftSupplier.get(); }; final Supplier<NewRight> rightSupp = () -> { - if(rightMaterialized) return mapper.apply(rightValue); + if (rightMaterialized) + return mapper.apply(rightValue); return mapper.apply(rightSupplier.get()); }; @@ -170,14 +179,15 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> } @Override - public <MergedType> MergedType merge(final BiFunction<LeftType, RightType, MergedType> merger) { - if(!leftMaterialized) { + public <MergedType> MergedType + merge(final BiFunction<LeftType, RightType, MergedType> merger) { + if (!leftMaterialized) { leftValue = leftSupplier.get(); leftMaterialized = true; } - if(!rightMaterialized) { + if (!rightMaterialized) { rightValue = rightSupplier.get(); rightMaterialized = true; @@ -191,13 +201,13 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> String leftVal; String rightVal; - if(leftMaterialized) { + if (leftMaterialized) { leftVal = leftValue.toString(); } else { leftVal = "(un-materialized)"; } - if(rightMaterialized) { + if (rightMaterialized) { rightVal = rightValue.toString(); } else { rightVal = "(un-materialized)"; @@ -221,26 +231,35 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof LazyPair<?, ?>)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof LazyPair<?, ?>)) + return false; 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; + if (leftMaterialized) { + if (leftValue == null) { + 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) { - if(rightValue == null) { - if(other.rightValue != null) return false; - } else if(!rightValue.equals(other.rightValue)) 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; diff --git a/src/main/java/bjc/data/ListHolder.java b/src/main/java/bjc/data/ListHolder.java index 077c0fb..ab3bfa8 100644 --- a/src/main/java/bjc/data/ListHolder.java +++ b/src/main/java/bjc/data/ListHolder.java @@ -13,7 +13,7 @@ import bjc.funcdata.IList; * @author ben * * @param <ContainedType> - * The type of contained value. + * The type of contained value. */ public class ListHolder<ContainedType> implements IHolder<ContainedType> { private IList<ContainedType> heldValues; @@ -22,14 +22,14 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { * Create a new list holder. * * @param values - * The possible values for the computation. + * The possible values for the computation. */ @SafeVarargs public ListHolder(final ContainedType... values) { heldValues = new FunctionalList<>(); - if(values != null) { - for(final ContainedType containedValue : values) { + if (values != null) { + for (final ContainedType containedValue : values) { heldValues.add(containedValue); } } @@ -41,35 +41,38 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { } @Override - public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) { + public <BoundType> IHolder<BoundType> + bind(final Function<ContainedType, IHolder<BoundType>> binder) { final IList<IHolder<BoundType>> boundValues = heldValues.map(binder); return new BoundListHolder<>(boundValues); } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) { - return val -> { - return new ListHolder<>(new FunctionalList<>(func.apply(val))); - }; + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(final Function<ContainedType, NewType> func) { + return val -> new ListHolder<>(new FunctionalList<>(func.apply(val))); } @Override - public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) { + public <MappedType> IHolder<MappedType> + map(final Function<ContainedType, MappedType> mapper) { final IList<MappedType> mappedValues = heldValues.map(mapper); return new ListHolder<>(mappedValues); } @Override - public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { + public IHolder<ContainedType> + transform(final UnaryOperator<ContainedType> transformer) { heldValues = heldValues.map(transformer); return this; } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { + public <UnwrappedType> UnwrappedType + unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { return unwrapper.apply(heldValues.randItem()); } @@ -90,15 +93,20 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof ListHolder<?>)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof ListHolder<?>)) + return false; final ListHolder<?> other = (ListHolder<?>) obj; - if(heldValues == null) { - if(other.heldValues != null) return false; - } else if(!heldValues.equals(other.heldValues)) return false; + if (heldValues == null) { + if (other.heldValues != null) + return false; + } else if (!heldValues.equals(other.heldValues)) + return false; return true; } diff --git a/src/main/java/bjc/data/OneWayToggle.java b/src/main/java/bjc/data/OneWayToggle.java index a4557cd..c2e9e4b 100644 --- a/src/main/java/bjc/data/OneWayToggle.java +++ b/src/main/java/bjc/data/OneWayToggle.java @@ -3,7 +3,7 @@ package bjc.data; /** * A toggle that will only give the first value once, only yielding the second * value afterwards. - * + * * @author student * * @param <E> @@ -17,11 +17,11 @@ public class OneWayToggle<E> implements Toggle<E> { /** * Create a new one-way toggle - * + * * @param first - * The value to offer first, and only once + * The value to offer first, and only once * @param second - * The value to offer second and repeatedly + * The value to offer second and repeatedly */ public OneWayToggle(E first, E second) { this.first = first; diff --git a/src/main/java/bjc/data/Option.java b/src/main/java/bjc/data/Option.java index a180ef9..b5d6d5e 100644 --- a/src/main/java/bjc/data/Option.java +++ b/src/main/java/bjc/data/Option.java @@ -9,7 +9,7 @@ import java.util.function.UnaryOperator; * @author ben * * @param <ContainedType> - * The type of the value that may or may not be held. + * The type of the value that may or may not be held. */ public class Option<ContainedType> implements IHolder<ContainedType> { private ContainedType held; @@ -18,36 +18,40 @@ public class Option<ContainedType> implements IHolder<ContainedType> { * Create a new optional, using the given initial value. * * @param seed - * The initial value for the optional. + * The initial value for the optional. */ public Option(final ContainedType seed) { held = seed; } @Override - public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) { - if(held == null) return new Option<>(null); + public <BoundType> IHolder<BoundType> + bind(final Function<ContainedType, IHolder<BoundType>> binder) { + if (held == null) + return new Option<>(null); return binder.apply(held); } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) { - return val -> { - return new Option<>(func.apply(val)); - }; + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(final Function<ContainedType, NewType> func) { + return val -> new Option<>(func.apply(val)); } @Override - public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) { - if(held == null) return new Option<>(null); + public <MappedType> IHolder<MappedType> + map(final Function<ContainedType, MappedType> mapper) { + if (held == null) + return new Option<>(null); return new Option<>(mapper.apply(held)); } @Override - public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { - if(held != null) { + public IHolder<ContainedType> + transform(final UnaryOperator<ContainedType> transformer) { + if (held != null) { held = transformer.apply(held); } @@ -55,8 +59,10 @@ public class Option<ContainedType> implements IHolder<ContainedType> { } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { - if(held == null) return null; + public <UnwrappedType> UnwrappedType + unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { + if (held == null) + return null; return unwrapper.apply(held); } @@ -78,15 +84,20 @@ public class Option<ContainedType> implements IHolder<ContainedType> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof Option<?>)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Option<?>)) + return false; final Option<?> other = (Option<?>) obj; - if(held == null) { - if(other.held != null) return false; - } else if(!held.equals(other.held)) return false; + if (held == null) { + if (other.held != null) + return false; + } else if (!held.equals(other.held)) + return false; return true; } diff --git a/src/main/java/bjc/data/Pair.java b/src/main/java/bjc/data/Pair.java index 3c1b7b5..610e89c 100644 --- a/src/main/java/bjc/data/Pair.java +++ b/src/main/java/bjc/data/Pair.java @@ -9,10 +9,10 @@ import java.util.function.Function; * @author ben * * @param <LeftType> - * The type of the left value. + * The type of the left value. * * @param <RightType> - * The type of the right value. + * The type of the right value. */ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { /* The left value. */ @@ -29,10 +29,10 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { * Create a new pair with both sides set to the specified values. * * @param left - * The value of the left side. + * The value of the left side. * * @param right - * The value of the right side. + * The value of the right side. */ public Pair(final LeftType left, final RightType right) { leftValue = left; @@ -42,15 +42,17 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( final BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) { - if(binder == null) throw new NullPointerException("Binder must not be null."); + if (binder == null) + throw new NullPointerException("Binder must not be null."); return binder.apply(leftValue, rightValue); } @Override - public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( - final Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { - if(leftBinder == null) throw new NullPointerException("Binder must not be null"); + public <BoundLeft> IPair<BoundLeft, RightType> + bindLeft(final Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { + if (leftBinder == null) + throw new NullPointerException("Binder must not be null"); return leftBinder.apply(leftValue); } @@ -58,16 +60,19 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <BoundRight> IPair<LeftType, BoundRight> bindRight( final Function<RightType, IPair<LeftType, BoundRight>> rightBinder) { - if(rightBinder == null) throw new NullPointerException("Binder must not be null"); + if (rightBinder == null) + throw new NullPointerException("Binder must not be null"); return rightBinder.apply(rightValue); } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - final IPair<OtherLeft, OtherRight> otherPair, - final BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - final BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> + IPair<CombinedLeft, CombinedRight> + combine(final IPair<OtherLeft, OtherRight> otherPair, + final BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + final BiFunction<RightType, OtherRight, + CombinedRight> rightCombiner) { return otherPair.bind((otherLeft, otherRight) -> { final CombinedLeft left = leftCombiner.apply(leftValue, otherLeft); final CombinedRight right = rightCombiner.apply(rightValue, otherRight); @@ -77,35 +82,44 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { } @Override - public <NewLeft> IPair<NewLeft, RightType> mapLeft(final Function<LeftType, NewLeft> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <NewLeft> IPair<NewLeft, RightType> + mapLeft(final Function<LeftType, NewLeft> mapper) { + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); return new Pair<>(mapper.apply(leftValue), rightValue); } @Override - public <NewRight> IPair<LeftType, NewRight> mapRight(final Function<RightType, NewRight> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <NewRight> IPair<LeftType, NewRight> + mapRight(final Function<RightType, NewRight> mapper) { + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); return new Pair<>(leftValue, mapper.apply(rightValue)); } @Override - public <MergedType> MergedType merge(final BiFunction<LeftType, RightType, MergedType> merger) { - if(merger == null) throw new NullPointerException("Merger must not be null"); + public <MergedType> MergedType + merge(final BiFunction<LeftType, RightType, MergedType> merger) { + if (merger == null) + throw new NullPointerException("Merger must not be null"); return merger.apply(leftValue, rightValue); } @Override public String toString() { - return String.format("Pair [leftValue='%s', rightValue='%s']", leftValue, rightValue); + return String.format("Pair [leftValue='%s', rightValue='%s']", leftValue, + rightValue); } + @Override public LeftType getLeft() { return leftValue; } + @Override public RightType getRight() { return rightValue; } @@ -123,19 +137,26 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof Pair<?, ?>)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Pair<?, ?>)) + return false; final Pair<?, ?> other = (Pair<?, ?>) obj; - if(leftValue == null) { - 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 (leftValue == null) { + 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; return true; } diff --git a/src/main/java/bjc/data/QueuedIterator.java b/src/main/java/bjc/data/QueuedIterator.java index ebacb52..f10014e 100644 --- a/src/main/java/bjc/data/QueuedIterator.java +++ b/src/main/java/bjc/data/QueuedIterator.java @@ -6,10 +6,11 @@ import java.util.Iterator; /** * An iterator that supports queuing elements after/before the current iterator; - * + * * @author bjculkin * - * @param <E> The type of element this iterator iterates over + * @param <E> + * The type of element this iterator iterates over */ public class QueuedIterator<E> implements Iterator<E> { private Iterator<E> cur; @@ -18,7 +19,7 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Static method for constructing iterators. - * + * * @return A queued iterator. */ public static <E> QueuedIterator<E> queued() { @@ -27,9 +28,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Static method for constructing iterators. - * + * * @param vals - * The values to iterate over. + * The values to iterate over. * * @return A queued iterator. */ @@ -40,9 +41,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Static method for constructing iterators. - * + * * @param itrs - * The iterators to use. + * The iterators to use. * * @return A queued iterator over the provided iterators. */ @@ -53,9 +54,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Static method for constructing iterators. - * + * * @param itrs - * The iterables to use. + * The iterables to use. * * @return A queued iterator over the provided iterables. */ @@ -73,9 +74,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Create a new queued iterator with a set of initial sources. - * + * * @param inits - * The set of initial iterators to use. + * The set of initial iterators to use. */ @SafeVarargs public QueuedIterator(Iterator<E>... inits) { @@ -88,9 +89,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Create a new queued iterator with a set of initial sources. - * + * * @param inits - * The set of initial iterables to use. + * The set of initial iterables to use. */ @SafeVarargs public QueuedIterator(Iterable<E>... inits) { @@ -103,9 +104,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Create a new queued iterator with a set of initial values. - * + * * @param vals - * The set of initial values to use. + * The set of initial values to use. */ @SafeVarargs public QueuedIterator(E... vals) { @@ -114,9 +115,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Add a new iterator who we will iterate through first. - * + * * @param itr - * The iterator to go through first. + * The iterator to go through first. */ public void before(Iterator<E> itr) { pending.push(cur); @@ -126,19 +127,19 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Add a new iterable who we will iterate through first. - * + * * @param itr - * The iterable to go through first. + * The iterable to go through first. */ public void before(Iterable<E> itr) { before(itr.iterator()); } - + /** * Add a new set of values who we will iterate through first. - * + * * @param vals - * Values to iterate over first. + * Values to iterate over first. */ public void before(@SuppressWarnings("unchecked") E... vals) { before(new ArrayIterator<>(vals)); @@ -146,9 +147,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Add a new iterator who we will iterate through next. - * + * * @param itr - * The iterator to go through next. + * The iterator to go through next. */ public void after(Iterator<E> itr) { pending.push(itr); @@ -156,9 +157,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Add a new iterable who we will iterate through next. - * + * * @param itr - * The iterable to go through next. + * The iterable to go through next. */ public void after(Iterable<E> itr) { after(itr.iterator()); @@ -166,19 +167,19 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Add a new set of values who we will iterate through next. - * + * * @param vals - * The values to iterate over next. + * The values to iterate over next. */ public void after(@SuppressWarnings("unchecked") E... vals) { after(new ArrayIterator<>(vals)); } - + /** * Add a new iterator who we will iterate through last. - * + * * @param itr - * The iterator to go through last. + * The iterator to go through last. */ public void last(Iterator<E> itr) { pending.add(itr); @@ -186,9 +187,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Add a new iterable who we will iterate through last. - * + * * @param itr - * The iterable to go through last. + * The iterable to go through last. */ public void last(Iterable<E> itr) { last(itr.iterator()); @@ -196,9 +197,9 @@ public class QueuedIterator<E> implements Iterator<E> { /** * Add a new set of values who we will iterate through last. - * + * * @param vals - * The values we will iterate over. + * The values we will iterate over. */ public void last(@SuppressWarnings("unchecked") E... vals) { last(new ArrayIterator<>(vals)); @@ -207,7 +208,8 @@ public class QueuedIterator<E> implements Iterator<E> { @Override public boolean hasNext() { while (cur == null || !cur.hasNext()) { - if (pending.isEmpty()) return false; + if (pending.isEmpty()) + return false; cur = pending.pop(); } @@ -218,7 +220,8 @@ public class QueuedIterator<E> implements Iterator<E> { @Override public E next() { while (cur == null || !cur.hasNext()) { - if (pending.isEmpty()) return null; + if (pending.isEmpty()) + return null; cur = pending.pop(); } diff --git a/src/main/java/bjc/data/SingleIterator.java b/src/main/java/bjc/data/SingleIterator.java index 3144447..1598774 100644 --- a/src/main/java/bjc/data/SingleIterator.java +++ b/src/main/java/bjc/data/SingleIterator.java @@ -8,7 +8,7 @@ import java.util.Iterator; * @author EVE * * @param <T> - * The type of the item. + * The type of the item. */ public class SingleIterator<T> implements Iterator<T> { /* The item being held. */ @@ -20,7 +20,7 @@ public class SingleIterator<T> implements Iterator<T> { * Create a iterator that yields a single item. * * @param item - * The item to yield. + * The item to yield. */ public SingleIterator(final T item) { itm = item; diff --git a/src/main/java/bjc/data/SingleSupplier.java b/src/main/java/bjc/data/SingleSupplier.java index bc1fbc4..010b06e 100644 --- a/src/main/java/bjc/data/SingleSupplier.java +++ b/src/main/java/bjc/data/SingleSupplier.java @@ -10,7 +10,7 @@ import java.util.function.Supplier; * @author ben * * @param <T> - * The supplied type + * The supplied type */ public class SingleSupplier<T> implements Supplier<T> { /* The next supplier ID. */ @@ -26,8 +26,8 @@ public class SingleSupplier<T> implements Supplier<T> { * The place where the supplier was instantiated. * * @NOTE This is both slow to create, and generally bad practice to keep - * exceptions around without throwing them. However, it is very useful - * to find where the first instantiation was. + * exceptions around without throwing them. However, it is very useful to find + * where the first instantiation was. */ private Exception instSite; @@ -35,7 +35,7 @@ public class SingleSupplier<T> implements Supplier<T> { * 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(final Supplier<T> supp) { source = supp; @@ -47,9 +47,10 @@ public class SingleSupplier<T> implements Supplier<T> { @Override public T get() { - if(gotten == true) { + if (gotten == true) { final String msg = String.format( - "Attempted to retrieve value more than once from single supplier #%d", id); + "Attempted to retrieve value more than once from single supplier #%d", + id); final IllegalStateException isex = new IllegalStateException(msg); @@ -62,7 +63,7 @@ public class SingleSupplier<T> implements Supplier<T> { try { throw new IllegalStateException("Previous instantiation here."); - } catch(final IllegalStateException isex) { + } catch (final IllegalStateException isex) { instSite = isex; } @@ -71,6 +72,7 @@ public class SingleSupplier<T> implements Supplier<T> { @Override public String toString() { - return String.format("SingleSupplier [source='%s', gotten=%s, id=%s]", source, gotten, id); + return String.format("SingleSupplier [source='%s', gotten=%s, id=%s]", source, + gotten, id); } } diff --git a/src/main/java/bjc/data/Toggle.java b/src/main/java/bjc/data/Toggle.java index 14c77d3..17c5a8e 100644 --- a/src/main/java/bjc/data/Toggle.java +++ b/src/main/java/bjc/data/Toggle.java @@ -6,12 +6,12 @@ package bjc.data; * @author EVE * * @param <E> - * The value stored in the toggle. + * The value stored in the toggle. */ public interface Toggle<E> { /** - * Retrieve the currently-aligned value of this toggle, and swap the - * value to the new one. + * Retrieve the currently-aligned value of this toggle, and swap the value to + * the new one. * * @return The previously-aligned value. */ @@ -28,7 +28,7 @@ public interface Toggle<E> { * Change the alignment of the toggle. * * @param isLeft - * Whether the toggle should be left-aligned or not. + * Whether the toggle should be left-aligned or not. */ void set(boolean isLeft); } diff --git a/src/main/java/bjc/data/TopDownTransformIterator.java b/src/main/java/bjc/data/TopDownTransformIterator.java index 66faafe..3b4b997 100644 --- a/src/main/java/bjc/data/TopDownTransformIterator.java +++ b/src/main/java/bjc/data/TopDownTransformIterator.java @@ -12,30 +12,31 @@ import java.util.function.Function; /* * @TODO 10/11/17 Ben Culkin :TopDownStep - * + * * Figure out what is broken with this, and fix it so that step-wise * iteration works correctly. */ /** * An iterative top-down transform of a tree. - * + * * @author EVE * * @param <ContainedType> - * The type of the nodes in the tree. + * The type of the nodes in the tree. */ -public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<ContainedType>> { +public class TopDownTransformIterator<ContainedType> + implements Iterator<ITree<ContainedType>> { /** * Alias type for a tree transformation. - * + * * @author student * * @param <ContainedType> - * The type contained in the tree. + * The type contained in the tree. */ - public interface TreeTransform<ContainedType> - extends BiFunction<ITree<ContainedType>, Consumer<Iterator<ITree<ContainedType>>>, ITree<ContainedType>> { + public interface TreeTransform<ContainedType> extends BiFunction<ITree<ContainedType>, + Consumer<Iterator<ITree<ContainedType>>>, ITree<ContainedType>> { // Alias type; no body is needed } @@ -64,32 +65,34 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C /** * Create a new tree iterator. - * + * * @param pickr - * The function to use to pick how to process nodes. + * The function to use to pick how to process nodes. * @param transfrm - * The transform to apply to the nodes. + * The transform to apply to the nodes. * @param tree - * The tree to transform. + * The tree to transform. */ - public TopDownTransformIterator(final Function<ContainedType, TopDownTransformResult> pickr, - final TreeTransform<ContainedType> transfrm, final ITree<ContainedType> tree) { + public TopDownTransformIterator( + final Function<ContainedType, TopDownTransformResult> pickr, + final TreeTransform<ContainedType> transfrm, + final ITree<ContainedType> tree) { preParent = tree; - preChildren = new LinkedList<>(); + preChildren = new LinkedList<>(); postChildren = new LinkedList<>(); - toYield = new LinkedList<>(); + toYield = new LinkedList<>(); - picker = pickr; + picker = pickr; transform = transfrm; - done = false; + done = false; initial = true; } /** * Add a set of nodes to yield. - * + * * @param src * The nodes to yield. */ @@ -108,10 +111,10 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C /** * Get the next yielded value. - * + * * @param val * The sentinel value to yield. - * + * * @return The next yielded value. */ public ITree<ContainedType> flushYields(final ITree<ContainedType> val) { @@ -158,7 +161,7 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C if (yeld != null) return yeld; } - + if (initial) { /* * Get the way we are transforming. @@ -200,9 +203,11 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C } done = true; - return flushYields(transform.apply(new Tree<>(preParent.getHead()), this::addYield)); + return flushYields( + transform.apply(new Tree<>(preParent.getHead()), this::addYield)); case PULLUP: - final ITree<ContainedType> intRes = transform.apply(preParent, this::addYield); + final ITree<ContainedType> intRes + = transform.apply(preParent, this::addYield); postParent = new Tree<>(intRes.getHead()); @@ -228,10 +233,11 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C if (curChild == null || !curChild.hasNext()) { if (preChildren.size() != 0) { - curChild = new TopDownTransformIterator<>(picker, transform, preChildren.pop()); + curChild = new TopDownTransformIterator<>(picker, transform, + preChildren.pop()); final ITree<ContainedType> res = curChild.next(); - //System.out.println("\t\tTRACE: adding node " + res + " to children"); + // System.out.println("\t\tTRACE: adding node " + res + " to children"); postChildren.add(res); return flushYields(res); @@ -242,7 +248,8 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C if (postParent == null) { res = new Tree<>(preParent.getHead()); - //System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); + // System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + + // res); for (final ITree<ContainedType> child : postChildren) { res.addChild(child); @@ -253,7 +260,8 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C } else { res = postParent; - //System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); + // System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + + // res); for (final ITree<ContainedType> child : postChildren) { res.addChild(child); } @@ -264,7 +272,7 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C } final ITree<ContainedType> res = curChild.next(); - //System.out.println("\t\tTRACE: adding node " + res + " to children"); + // System.out.println("\t\tTRACE: adding node " + res + " to children"); postChildren.add(res); return flushYields(res); diff --git a/src/main/java/bjc/data/TransformIterator.java b/src/main/java/bjc/data/TransformIterator.java index 0fc127a..29f91e2 100644 --- a/src/main/java/bjc/data/TransformIterator.java +++ b/src/main/java/bjc/data/TransformIterator.java @@ -9,10 +9,10 @@ import java.util.function.Function; * @author EVE * * @param <S> - * The source iterator type. + * The source iterator type. * * @param <D> - * The destination iterator type. + * The destination iterator type. */ public class TransformIterator<S, D> implements Iterator<D> { /* Our source of values. */ @@ -24,10 +24,10 @@ public class TransformIterator<S, D> implements Iterator<D> { * Create a new transform iterator. * * @param source - * The source iterator to use. + * The source iterator to use. * * @param transform - * The transform to apply. + * The transform to apply. */ public TransformIterator(final Iterator<S> source, final Function<S, D> transform) { this.source = source; diff --git a/src/main/java/bjc/data/Tree.java b/src/main/java/bjc/data/Tree.java index d9ccb34..27dcf11 100644 --- a/src/main/java/bjc/data/Tree.java +++ b/src/main/java/bjc/data/Tree.java @@ -16,7 +16,7 @@ import bjc.funcdata.bst.TreeLinearizationMethod; * @author ben * * @param <ContainedType> - * The type contained in the tree. + * The type contained in the tree. */ public class Tree<ContainedType> implements ITree<ContainedType> { /* The data/label for this node. */ @@ -27,10 +27,9 @@ public class Tree<ContainedType> implements ITree<ContainedType> { /* Whether this node has children. */ /* - * @NOTE Why have both this boolean and childCount? Why not just do a - * childCount == 0 whenever you'd check hasChildren? - * - Because hasChildren is set once and not reset, and really what - * it indicates is that children has been allocated. + * @NOTE Why have both this boolean and childCount? Why not just do a childCount + * == 0 whenever you'd check hasChildren? - Because hasChildren is set once and + * not reset, and really what it indicates is that children has been allocated. */ private boolean hasChildren; /* The number of children this node has. */ @@ -52,7 +51,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { * Create a new leaf node in a tree. * * @param leaf - * The data to store as a leaf node. + * The data to store as a leaf node. */ public Tree(final ContainedType leaf) { data = leaf; @@ -66,10 +65,10 @@ public class Tree<ContainedType> implements ITree<ContainedType> { * Create a new tree node with the specified children. * * @param leaf - * The data to hold in this node. + * The data to hold in this node. * * @param childrn - * A list of children for this node. + * A list of children for this node. */ public Tree(final ContainedType leaf, final IList<ITree<ContainedType>> childrn) { this(leaf); @@ -85,10 +84,10 @@ public class Tree<ContainedType> implements ITree<ContainedType> { * Create a new tree node with the specified children. * * @param leaf - * The data to hold in this node. + * The data to hold in this node. * * @param childrn - * A list of children for this node. + * A list of children for this node. */ @SafeVarargs public Tree(final ContainedType leaf, final ITree<ContainedType>... childrn) { @@ -100,7 +99,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { children = new FunctionalList<>(); - for(final ITree<ContainedType> child : childrn) { + for (final ITree<ContainedType> child : childrn) { children.add(child); childCount++; @@ -114,7 +113,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { @Override public void addChild(final ITree<ContainedType> child) { - if(hasChildren == false) { + if (hasChildren == false) { hasChildren = true; children = new FunctionalList<>(); @@ -127,7 +126,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { @Override public void prependChild(final ITree<ContainedType> child) { - if(hasChildren == false) { + if (hasChildren == false) { hasChildren = true; children = new FunctionalList<>(); @@ -140,7 +139,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { @Override public void doForChildren(final Consumer<ITree<ContainedType>> action) { - if(childCount > 0) { + if (childCount > 0) { children.forEach(action); } } @@ -152,23 +151,25 @@ public class Tree<ContainedType> implements ITree<ContainedType> { @Override public int revFind(final Predicate<ITree<ContainedType>> childPred) { - if(childCount == 0) { + if (childCount == 0) { return -1; } - for(int i = childCount - 1; i >= 0; i--) { - if(childPred.test(getChild(i))) return i; + for (int i = childCount - 1; i >= 0; i--) { + if (childPred.test(getChild(i))) + return i; } return -1; } @Override - public void traverse(final TreeLinearizationMethod linearizationMethod, final Consumer<ContainedType> action) { - if(hasChildren) { - switch(linearizationMethod) { + public void traverse(final TreeLinearizationMethod linearizationMethod, + final Consumer<ContainedType> action) { + if (hasChildren) { + switch (linearizationMethod) { case INORDER: - if(childCount != 2) { + if (childCount != 2) { final String msg = "Can only do in-order traversal for binary trees."; throw new IllegalArgumentException(msg); @@ -181,14 +182,14 @@ public class Tree<ContainedType> implements ITree<ContainedType> { children.getByIndex(1).traverse(linearizationMethod, action); break; case POSTORDER: - children.forEach((child) -> child.traverse(linearizationMethod, action)); + children.forEach(child -> child.traverse(linearizationMethod, action)); action.accept(data); break; case PREORDER: action.accept(data); - children.forEach((child) -> child.traverse(linearizationMethod, action)); + children.forEach(child -> child.traverse(linearizationMethod, action)); break; default: break; @@ -200,19 +201,21 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } @Override - public <NewType, ReturnedType> ReturnedType collapse(final Function<ContainedType, NewType> leafTransform, + public <NewType, ReturnedType> ReturnedType collapse( + final Function<ContainedType, NewType> leafTransform, final BiFunction<ContainedType, IList<NewType>, NewType> nodeCollapser, final Function<NewType, ReturnedType> resultTransformer) { return resultTransformer.apply(internalCollapse(leafTransform, nodeCollapser)); } @Override - public ITree<ContainedType> flatMapTree(final Function<ContainedType, ITree<ContainedType>> mapper) { - if(hasChildren) { + public ITree<ContainedType> + flatMapTree(final Function<ContainedType, ITree<ContainedType>> mapper) { + if (hasChildren) { final ITree<ContainedType> flatMappedData = mapper.apply(data); - final IList<ITree<ContainedType>> mappedChildren = children - .map(child -> child.flatMapTree(mapper)); + final IList<ITree<ContainedType>> mappedChildren + = children.map(child -> child.flatMapTree(mapper)); mappedChildren.forEach(flatMappedData::addChild); @@ -225,13 +228,14 @@ public class Tree<ContainedType> implements ITree<ContainedType> { /* * Do a collapse of this tree. * - * @NOTE Why is this protected? I can't see any good reason someone'd - * want to override it. + * @NOTE Why is this protected? I can't see any good reason someone'd want to + * override it. */ - protected <NewType> NewType internalCollapse(final Function<ContainedType, NewType> leafTransform, + protected <NewType> NewType internalCollapse( + final Function<ContainedType, NewType> leafTransform, final BiFunction<ContainedType, IList<NewType>, NewType> nodeCollapser) { - if(hasChildren) { + if (hasChildren) { final IList<NewType> collapsedChildren = children.map(child -> { final NewType collapsed = child.collapse(leafTransform, nodeCollapser, subTreeVal -> subTreeVal); @@ -245,9 +249,10 @@ public class Tree<ContainedType> implements ITree<ContainedType> { return leafTransform.apply(data); } - protected void internalToString(final StringBuilder builder, final int indentLevel, final boolean initial) { - if(!initial) { - for(int i = 0; i < indentLevel; i++) { + protected void internalToString(final StringBuilder builder, final int indentLevel, + final boolean initial) { + if (!initial) { + for (int i = 0; i < indentLevel; i++) { builder.append(">\t"); } } @@ -258,14 +263,14 @@ public class Tree<ContainedType> implements ITree<ContainedType> { builder.append(data == null ? "(null)" : data.toString()); builder.append("\n"); - if(hasChildren) { + if (hasChildren) { children.forEach(child -> { - if(child instanceof Tree<?>) { + if (child instanceof Tree<?>) { final Tree<ContainedType> kid = (Tree<ContainedType>) child; kid.internalToString(builder, indentLevel + 1, false); } else { - for(int i = 0; i < indentLevel + 1; i++) { + for (int i = 0; i < indentLevel + 1; i++) { builder.append(">\t"); } @@ -278,12 +283,11 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } @Override - public <MappedType> ITree<MappedType> rebuildTree(final Function<ContainedType, MappedType> leafTransformer, + public <MappedType> ITree<MappedType> rebuildTree( + final Function<ContainedType, MappedType> leafTransformer, final Function<ContainedType, MappedType> operatorTransformer) { - if(hasChildren) { - final IList<ITree<MappedType>> mappedChildren = children.map(child -> { - return child.rebuildTree(leafTransformer, operatorTransformer); - }); + if (hasChildren) { + final IList<ITree<MappedType>> mappedChildren = children.map(child -> child.rebuildTree(leafTransformer, operatorTransformer)); final MappedType mapData = operatorTransformer.apply(data); return new Tree<>(mapData, mappedChildren); @@ -295,7 +299,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { @Override public void selectiveTransform(final Predicate<ContainedType> nodePicker, final UnaryOperator<ContainedType> transformer) { - if(hasChildren) { + if (hasChildren) { children.forEach(child -> child.selectiveTransform(nodePicker, transformer)); } else { data = transformer.apply(data); @@ -308,14 +312,14 @@ public class Tree<ContainedType> implements ITree<ContainedType> { final UnaryOperator<ITree<ContainedType>> transformer) { final TopDownTransformResult transformResult = transformPicker.apply(data); - switch(transformResult) { + switch (transformResult) { case PASSTHROUGH: ITree<ContainedType> result = new Tree<>(data); - if(hasChildren) { + if (hasChildren) { children.forEach(child -> { - final ITree<ContainedType> kid = child.topDownTransform(transformPicker, - transformer); + final ITree<ContainedType> kid + = child.topDownTransform(transformPicker, transformer); result.addChild(kid); }); @@ -331,10 +335,10 @@ public class Tree<ContainedType> implements ITree<ContainedType> { case PUSHDOWN: result = new Tree<>(data); - if(hasChildren) { + if (hasChildren) { children.forEach(child -> { - final ITree<ContainedType> kid = child.topDownTransform(transformPicker, - transformer); + final ITree<ContainedType> kid + = child.topDownTransform(transformPicker, transformer); result.addChild(kid); }); @@ -347,14 +351,16 @@ public class Tree<ContainedType> implements ITree<ContainedType> { result = new Tree<>(intermediateResult.getHead()); intermediateResult.doForChildren(child -> { - final ITree<ContainedType> kid = child.topDownTransform(transformPicker, transformer); + final ITree<ContainedType> kid + = child.topDownTransform(transformPicker, transformer); result.addChild(kid); }); return result; default: - final String msg = String.format("Recieved unknown transform result type %s", transformResult); + final String msg = String.format("Recieved unknown transform result type %s", + transformResult); throw new IllegalArgumentException(msg); } @@ -363,7 +369,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { @Override public <TransformedType> TransformedType transformChild(final int childNo, final Function<ITree<ContainedType>, TransformedType> transformer) { - if(childNo < 0 || childNo > childCount - 1) { + if (childNo < 0 || childNo > childCount - 1) { final String msg = String.format("Child index #%d is invalid", childNo); throw new IllegalArgumentException(msg); @@ -375,8 +381,8 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } @Override - public <TransformedType> TransformedType transformHead( - final Function<ContainedType, TransformedType> transformer) { + public <TransformedType> TransformedType + transformHead(final Function<ContainedType, TransformedType> transformer) { return transformer.apply(data); } @@ -411,21 +417,29 @@ public class Tree<ContainedType> implements ITree<ContainedType> { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof Tree<?>)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Tree<?>)) + return false; final Tree<?> other = (Tree<?>) obj; - if(data == null) { - if(other.data != null) return false; - } else if(!data.equals(other.data)) return false; + if (data == null) { + 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 (children == null) { + if (other.children != null) + return false; + } else if (!children.equals(other.children)) + return false; return true; } diff --git a/src/main/java/bjc/data/ValueToggle.java b/src/main/java/bjc/data/ValueToggle.java index e79169f..041a2d5 100644 --- a/src/main/java/bjc/data/ValueToggle.java +++ b/src/main/java/bjc/data/ValueToggle.java @@ -6,7 +6,7 @@ package bjc.data; * @author EVE * * @param <E> - * The type of value to toggle between. + * The type of value to toggle between. */ public class ValueToggle<E> implements Toggle<E> { /* Our left value. */ @@ -22,10 +22,10 @@ public class ValueToggle<E> implements Toggle<E> { * All toggles start right-aligned. * * @param left - * The value when the toggle is left-aligned. + * The value when the toggle is left-aligned. * * @param right - * The value when the toggle is right-aligned. + * The value when the toggle is right-aligned. */ public ValueToggle(final E left, final E right) { lft = left; @@ -37,7 +37,7 @@ public class ValueToggle<E> implements Toggle<E> { @Override public E get() { - if(alignment.get()) { + if (alignment.get()) { return lft; } @@ -46,7 +46,7 @@ public class ValueToggle<E> implements Toggle<E> { @Override public E peek() { - if(alignment.peek()) { + if (alignment.peek()) { return lft; } diff --git a/src/main/java/bjc/data/internals/BoundLazy.java b/src/main/java/bjc/data/internals/BoundLazy.java index 728af8e..9c984eb 100644 --- a/src/main/java/bjc/data/internals/BoundLazy.java +++ b/src/main/java/bjc/data/internals/BoundLazy.java @@ -15,7 +15,8 @@ import bjc.funcdata.IList; * @author Ben Culkin */ @SuppressWarnings("javadoc") -public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundContainedType> { +public class BoundLazy<OldType, BoundContainedType> + implements IHolder<BoundContainedType> { /* The old value. */ private final Supplier<IHolder<OldType>> oldSupplier; @@ -29,16 +30,17 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont private boolean holderBound; /* Transformations currently pending on the bound value. */ - private final IList<UnaryOperator<BoundContainedType>> actions = new FunctionalList<>(); + private final IList<UnaryOperator<BoundContainedType>> actions + = new FunctionalList<>(); /** * Create a new bound lazy value. * * @param supp - * The supplier of the old value. + * The supplier of the old value. * * @param binder - * The function to use to bind the old value to the new one. + * The function to use to bind the old value to the new one. */ public BoundLazy(final Supplier<IHolder<OldType>> supp, final Function<OldType, IHolder<BoundContainedType>> binder) { @@ -47,11 +49,14 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont } @Override - public <BoundType> IHolder<BoundType> bind(final Function<BoundContainedType, IHolder<BoundType>> bindr) { - if(bindr == null) throw new NullPointerException("Binder must not be null"); + public <BoundType> IHolder<BoundType> + bind(final Function<BoundContainedType, IHolder<BoundType>> bindr) { + if (bindr == null) + throw new NullPointerException("Binder must not be null"); /* Prepare a list of pending actions. */ - final IList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>(); + final IList<UnaryOperator<BoundContainedType>> pendingActions + = new FunctionalList<>(); actions.forEach(pendingActions::add); /* Create the new supplier of a value. */ @@ -59,35 +64,37 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont IHolder<BoundContainedType> oldHolder = boundHolder; /* Bind the value if it hasn't been bound before. */ - if(!holderBound) { + if (!holderBound) { oldHolder = oldSupplier.get().unwrap(binder); } /* Apply all the pending actions. */ - return pendingActions.reduceAux(oldHolder, (action, state) -> { - return state.transform(action); - }, (value) -> value); + return pendingActions.reduceAux(oldHolder, (action, state) -> state.transform(action), value -> value); }; return new BoundLazy<>(typeSupplier, bindr); } @Override - public <NewType> Function<BoundContainedType, IHolder<NewType>> lift( - final Function<BoundContainedType, NewType> func) { - if(func == null) throw new NullPointerException("Function to lift must not be null"); + public <NewType> Function<BoundContainedType, IHolder<NewType>> + lift(final Function<BoundContainedType, NewType> func) { + if (func == null) + throw new NullPointerException("Function to lift must not be null"); - return (val) -> { + return val -> { return new Lazy<>(func.apply(val)); }; } @Override - public <MappedType> IHolder<MappedType> map(final Function<BoundContainedType, MappedType> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <MappedType> IHolder<MappedType> + map(final Function<BoundContainedType, MappedType> mapper) { + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); /* Prepare a list of pending actions. */ - final IList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>(); + final IList<UnaryOperator<BoundContainedType>> pendingActions + = new FunctionalList<>(); actions.forEach(pendingActions::add); /* Prepare the new supplier. */ @@ -95,14 +102,12 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont IHolder<BoundContainedType> oldHolder = boundHolder; /* Bound the value if it hasn't been bound. */ - if(!holderBound) { + if (!holderBound) { oldHolder = oldSupplier.get().unwrap(binder); } /* Apply pending actions. */ - return pendingActions.reduceAux(oldHolder.getValue(), (action, state) -> { - return action.apply(state); - }, (value) -> mapper.apply(value)); + return pendingActions.reduceAux(oldHolder.getValue(), (action, state) -> action.apply(state), value -> mapper.apply(value)); }; return new Lazy<>(typeSupplier); @@ -110,14 +115,17 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont @Override public String toString() { - if(holderBound) return boundHolder.toString(); + if (holderBound) + return boundHolder.toString(); return "(unmaterialized)"; } @Override - public IHolder<BoundContainedType> transform(final UnaryOperator<BoundContainedType> transformer) { - if(transformer == null) throw new NullPointerException("Transformer must not be null"); + public IHolder<BoundContainedType> + transform(final UnaryOperator<BoundContainedType> transformer) { + if (transformer == null) + throw new NullPointerException("Transformer must not be null"); actions.add(transformer); @@ -125,10 +133,12 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<BoundContainedType, UnwrappedType> unwrapper) { - if(unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); + public <UnwrappedType> UnwrappedType + unwrap(final Function<BoundContainedType, UnwrappedType> unwrapper) { + if (unwrapper == null) + throw new NullPointerException("Unwrapper must not be null"); - if(!holderBound) { + if (!holderBound) { boundHolder = oldSupplier.get().unwrap(binder::apply); } diff --git a/src/main/java/bjc/data/internals/BoundLazyPair.java b/src/main/java/bjc/data/internals/BoundLazyPair.java index 105b410..e081c04 100644 --- a/src/main/java/bjc/data/internals/BoundLazyPair.java +++ b/src/main/java/bjc/data/internals/BoundLazyPair.java @@ -15,7 +15,8 @@ import bjc.data.LazyPair; * @author Ben Culkin */ @SuppressWarnings("javadoc") -public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPair<NewLeft, NewRight> { +public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> + implements IPair<NewLeft, NewRight> { /* The supplier of the left value. */ private final Supplier<OldLeft> leftSupplier; /* The supplier of the right value. */ @@ -34,16 +35,17 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai * Create a new bound lazy pair. * * @param leftSupp - * The supplier for the left value. + * The supplier for the left value. * * @param rightSupp - * The supplier for the right value. + * The supplier for the right value. * * @param bindr - * The function to use to bind the left and right into a new - * pair. + * The function to use to bind the left and right into a new + * pair. */ - public BoundLazyPair(final Supplier<OldLeft> leftSupp, final Supplier<OldRight> rightSupp, + public BoundLazyPair(final Supplier<OldLeft> leftSupp, + final Supplier<OldRight> rightSupp, final BiFunction<OldLeft, OldRight, IPair<NewLeft, NewRight>> bindr) { leftSupplier = leftSupp; rightSupplier = rightSupp; @@ -53,54 +55,53 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai @Override public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( final BiFunction<NewLeft, NewRight, IPair<BoundLeft, BoundRight>> bindr) { - if(bindr == null) throw new NullPointerException("Binder must not be null"); + if (bindr == null) + throw new NullPointerException("Binder must not be null"); final IHolder<IPair<NewLeft, NewRight>> newPair = new Identity<>(boundPair); final IHolder<Boolean> newPairMade = new Identity<>(pairBound); final Supplier<NewLeft> leftSupp = () -> { - if(!newPairMade.getValue()) { + if (!newPairMade.getValue()) { /* - * If the pair hasn't been bound before, bind - * it. + * If the pair hasn't been bound before, bind it. */ newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); newPairMade.replace(true); } - return newPair.unwrap((pair) -> pair.getLeft()); + return newPair.unwrap(pair -> pair.getLeft()); }; final Supplier<NewRight> rightSupp = () -> { - if(!newPairMade.getValue()) { + if (!newPairMade.getValue()) { /* - * If the pair hasn't been bound before, bind - * it. + * If the pair hasn't been bound before, bind it. */ newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); newPairMade.replace(true); } - return newPair.unwrap((pair) -> pair.getRight()); + return newPair.unwrap(pair -> pair.getRight()); }; return new BoundLazyPair<>(leftSupp, rightSupp, bindr); } @Override - public <BoundLeft> IPair<BoundLeft, NewRight> bindLeft( - final Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) { - if(leftBinder == null) throw new NullPointerException("Left binder must not be null"); + public <BoundLeft> IPair<BoundLeft, NewRight> + bindLeft(final Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) { + if (leftBinder == null) + throw new NullPointerException("Left binder must not be null"); final Supplier<NewLeft> leftSupp = () -> { IPair<NewLeft, NewRight> newPair = boundPair; - if(!pairBound) { + if (!pairBound) { /* - * If the pair hasn't been bound before, bind - * it. + * If the pair hasn't been bound before, bind it. */ newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } @@ -112,17 +113,17 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai } @Override - public <BoundRight> IPair<NewLeft, BoundRight> bindRight( - final Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) { - if(rightBinder == null) throw new NullPointerException("Right binder must not be null"); + public <BoundRight> IPair<NewLeft, BoundRight> + bindRight(final Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) { + if (rightBinder == null) + throw new NullPointerException("Right binder must not be null"); final Supplier<NewRight> rightSupp = () -> { IPair<NewLeft, NewRight> newPair = boundPair; - if(!pairBound) { + if (!pairBound) { /* - * If the pair hasn't been bound before, bind - * it. + * If the pair hasn't been bound before, bind it. */ newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } @@ -134,35 +135,37 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - final IPair<OtherLeft, OtherRight> otherPair, - final BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, - final BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { - if(otherPair == null) { + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> + IPair<CombinedLeft, CombinedRight> + combine(final IPair<OtherLeft, OtherRight> otherPair, + final BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, + final BiFunction<NewRight, OtherRight, CombinedRight> 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) { + } else if (rightCombiner == null) { throw new NullPointerException("Right combiner must not be null"); } - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); - CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); + return otherPair.bind((otherLeft, otherRight) -> bind((leftVal, rightVal) -> { + CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); + CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); - return new LazyPair<>(cLeft, cRight); - }); - }); + return new LazyPair<>(cLeft, cRight); + })); } @Override - public <NewLeftType> IPair<NewLeftType, NewRight> mapLeft(final Function<NewLeft, NewLeftType> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <NewLeftType> IPair<NewLeftType, NewRight> + mapLeft(final Function<NewLeft, NewLeftType> mapper) { + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); final Supplier<NewLeftType> leftSupp = () -> { - if(!pairBound) { - final NewLeft leftVal = binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); + if (!pairBound) { + final NewLeft leftVal + = binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); return mapper.apply(leftVal); } @@ -171,7 +174,8 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai }; final Supplier<NewRight> rightSupp = () -> { - if(!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getRight(); + if (!pairBound) + return binder.apply(leftSupplier.get(), rightSupplier.get()).getRight(); return boundPair.getRight(); }; @@ -180,19 +184,22 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai } @Override - public <NewRightType> IPair<NewLeft, NewRightType> mapRight(final Function<NewRight, NewRightType> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <NewRightType> IPair<NewLeft, NewRightType> + mapRight(final Function<NewRight, NewRightType> mapper) { + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); final Supplier<NewLeft> leftSupp = () -> { - if(!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); + if (!pairBound) + return binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); return boundPair.getLeft(); }; final Supplier<NewRightType> rightSupp = () -> { - if(!pairBound) { - final NewRight rightVal = binder.apply(leftSupplier.get(), rightSupplier.get()) - .getRight(); + if (!pairBound) { + final NewRight rightVal = binder + .apply(leftSupplier.get(), rightSupplier.get()).getRight(); return mapper.apply(rightVal); } @@ -204,10 +211,12 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai } @Override - public <MergedType> MergedType merge(final BiFunction<NewLeft, NewRight, MergedType> merger) { - if(merger == null) throw new NullPointerException("Merger must not be null"); + public <MergedType> MergedType + merge(final BiFunction<NewLeft, NewRight, MergedType> merger) { + if (merger == null) + throw new NullPointerException("Merger must not be null"); - if(!pairBound) { + if (!pairBound) { /* * If the pair isn't bound yet, bind it. */ @@ -221,7 +230,8 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai @Override public String toString() { - if(pairBound) return boundPair.toString(); + if (pairBound) + return boundPair.toString(); return "(un-materialized)"; } diff --git a/src/main/java/bjc/data/internals/BoundListHolder.java b/src/main/java/bjc/data/internals/BoundListHolder.java index 8f8cab4..1193c8d 100644 --- a/src/main/java/bjc/data/internals/BoundListHolder.java +++ b/src/main/java/bjc/data/internals/BoundListHolder.java @@ -21,48 +21,58 @@ public class BoundListHolder<ContainedType> implements IHolder<ContainedType> { * Create a new list of holders. * * @param toHold - * The list of holders to, well, hold. + * The list of holders to, well, hold. */ public BoundListHolder(final IList<IHolder<ContainedType>> toHold) { heldHolders = toHold; } @Override - public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) { - if(binder == null) throw new NullPointerException("Binder must not be null"); + public <BoundType> IHolder<BoundType> + bind(final Function<ContainedType, IHolder<BoundType>> binder) { + if (binder == null) + throw new NullPointerException("Binder must not be null"); - final IList<IHolder<BoundType>> boundHolders = heldHolders.map((containedHolder) -> { - return containedHolder.bind(binder); - }); + final IList<IHolder<BoundType>> boundHolders + = heldHolders.map(containedHolder -> { + return containedHolder.bind(binder); + }); return new BoundListHolder<>(boundHolders); } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) { - if(func == null) throw new NullPointerException("Function to lift must not be null"); + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(final Function<ContainedType, NewType> func) { + if (func == null) + throw new NullPointerException("Function to lift must not be null"); - return (val) -> { + return val -> { return new ListHolder<>(func.apply(val)); }; } @Override - public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <MappedType> IHolder<MappedType> + map(final Function<ContainedType, MappedType> mapper) { + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); - final IList<IHolder<MappedType>> mappedHolders = heldHolders.map((containedHolder) -> { - return containedHolder.map(mapper); - }); + final IList<IHolder<MappedType>> mappedHolders + = heldHolders.map(containedHolder -> { + return containedHolder.map(mapper); + }); return new BoundListHolder<>(mappedHolders); } @Override - public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { - if(transformer == null) throw new NullPointerException("Transformer must not be null"); + public IHolder<ContainedType> + transform(final UnaryOperator<ContainedType> transformer) { + if (transformer == null) + throw new NullPointerException("Transformer must not be null"); - heldHolders.forEach((containedHolder) -> { + heldHolders.forEach(containedHolder -> { containedHolder.transform(transformer); }); @@ -70,8 +80,10 @@ public class BoundListHolder<ContainedType> implements IHolder<ContainedType> { } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { - if(unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); + public <UnwrappedType> UnwrappedType + unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { + if (unwrapper == null) + throw new NullPointerException("Unwrapper must not be null"); /* * @NOTE Is there another way we could want to do this? diff --git a/src/main/java/bjc/data/internals/HalfBoundLazyPair.java b/src/main/java/bjc/data/internals/HalfBoundLazyPair.java index 4f28012..6bcb6ae 100644 --- a/src/main/java/bjc/data/internals/HalfBoundLazyPair.java +++ b/src/main/java/bjc/data/internals/HalfBoundLazyPair.java @@ -23,7 +23,8 @@ import bjc.data.LazyPair; * @author Ben Culkin */ @SuppressWarnings("javadoc") -public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewLeft, NewRight> { +public class HalfBoundLazyPair<OldType, NewLeft, NewRight> + implements IPair<NewLeft, NewRight> { /* The supplier of the old value. */ private final Supplier<OldType> oldSupplier; @@ -39,10 +40,10 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL * Create a new half-bound lazy pair. * * @param oldSupp - * The supplier of the old value. + * The supplier of the old value. * * @param bindr - * The function to use to create the pair from the old value. + * The function to use to create the pair from the old value. */ public HalfBoundLazyPair(final Supplier<OldType> oldSupp, final Function<OldType, IPair<NewLeft, NewRight>> bindr) { @@ -57,35 +58,35 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL final IHolder<Boolean> newPairMade = new Identity<>(pairBound); final Supplier<NewLeft> leftSupp = () -> { - if(!newPairMade.getValue()) { + if (!newPairMade.getValue()) { /* Bind the pair if it hasn't been bound yet. */ newPair.replace(binder.apply(oldSupplier.get())); newPairMade.replace(true); } - return newPair.unwrap((pair) -> pair.getLeft()); + return newPair.unwrap(pair -> pair.getLeft()); }; final Supplier<NewRight> rightSupp = () -> { - if(!newPairMade.getValue()) { + if (!newPairMade.getValue()) { /* Bind the pair if it hasn't been bound yet. */ newPair.replace(binder.apply(oldSupplier.get())); newPairMade.replace(true); } - return newPair.unwrap((pair) -> pair.getRight()); + return newPair.unwrap(pair -> pair.getRight()); }; return new BoundLazyPair<>(leftSupp, rightSupp, bindr); } @Override - public <BoundLeft> IPair<BoundLeft, NewRight> bindLeft( - final Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) { + public <BoundLeft> IPair<BoundLeft, NewRight> + bindLeft(final Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) { final Supplier<NewLeft> leftSupp = () -> { IPair<NewLeft, NewRight> newPair = boundPair; - if(!pairBound) { + if (!pairBound) { newPair = binder.apply(oldSupplier.get()); } @@ -96,12 +97,12 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL } @Override - public <BoundRight> IPair<NewLeft, BoundRight> bindRight( - final Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) { + public <BoundRight> IPair<NewLeft, BoundRight> + bindRight(final Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) { final Supplier<NewRight> rightSupp = () -> { IPair<NewLeft, NewRight> newPair = boundPair; - if(!pairBound) { + if (!pairBound) { newPair = binder.apply(oldSupplier.get()); } @@ -112,24 +113,25 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - final IPair<OtherLeft, OtherRight> otherPair, - final BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, - final BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); - CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); - - return new LazyPair<>(cLeft, cRight); - }); - }); + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> + IPair<CombinedLeft, CombinedRight> + combine(final IPair<OtherLeft, OtherRight> otherPair, + final BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, + final BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> bind((leftVal, rightVal) -> { + CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); + CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); + + return new LazyPair<>(cLeft, cRight); + })); } @Override - public <NewLeftType> IPair<NewLeftType, NewRight> mapLeft(final Function<NewLeft, NewLeftType> mapper) { + public <NewLeftType> IPair<NewLeftType, NewRight> + mapLeft(final Function<NewLeft, NewLeftType> mapper) { final Supplier<NewLeftType> leftSupp = () -> { - if(pairBound) return mapper.apply(boundPair.getLeft()); + if (pairBound) + return mapper.apply(boundPair.getLeft()); final NewLeft leftVal = binder.apply(oldSupplier.get()).getLeft(); @@ -137,7 +139,8 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL }; final Supplier<NewRight> rightSupp = () -> { - if(pairBound) return boundPair.getRight(); + if (pairBound) + return boundPair.getRight(); return binder.apply(oldSupplier.get()).getRight(); }; @@ -146,15 +149,18 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL } @Override - public <NewRightType> IPair<NewLeft, NewRightType> mapRight(final Function<NewRight, NewRightType> mapper) { + public <NewRightType> IPair<NewLeft, NewRightType> + mapRight(final Function<NewRight, NewRightType> mapper) { final Supplier<NewLeft> leftSupp = () -> { - if(pairBound) return boundPair.getLeft(); + if (pairBound) + return boundPair.getLeft(); return binder.apply(oldSupplier.get()).getLeft(); }; final Supplier<NewRightType> rightSupp = () -> { - if(pairBound) return mapper.apply(boundPair.getRight()); + if (pairBound) + return mapper.apply(boundPair.getRight()); final NewRight rightVal = binder.apply(oldSupplier.get()).getRight(); @@ -165,8 +171,9 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL } @Override - public <MergedType> MergedType merge(final BiFunction<NewLeft, NewRight, MergedType> merger) { - if(!pairBound) { + public <MergedType> MergedType + merge(final BiFunction<NewLeft, NewRight, MergedType> merger) { + if (!pairBound) { boundPair = binder.apply(oldSupplier.get()); pairBound = true; diff --git a/src/main/java/bjc/data/internals/WrappedLazy.java b/src/main/java/bjc/data/internals/WrappedLazy.java index 17d2309..cda86fd 100644 --- a/src/main/java/bjc/data/internals/WrappedLazy.java +++ b/src/main/java/bjc/data/internals/WrappedLazy.java @@ -11,7 +11,7 @@ import bjc.data.Lazy; * * @author Ben Culkin * @param <ContainedType> - * The type of the wrapped value. + * The type of the wrapped value. */ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { /* Held value. */ @@ -21,19 +21,18 @@ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { * Create a new wrapped lazy value. * * @param wrappedHolder - * The holder to make lazy. + * The holder to make lazy. */ public WrappedLazy(final IHolder<ContainedType> wrappedHolder) { held = new Lazy<>(wrappedHolder); } /* - * This has an extra parameter, because otherwise it erases to the same - * as the public one. + * This has an extra parameter, because otherwise it erases to the same as the + * public one. * - * This is a case where reified generics would be useful, because then - * the compiler could know which one we meant without the dummy - * parameter. + * This is a case where reified generics would be useful, because then the + * compiler could know which one we meant without the dummy parameter. */ private WrappedLazy(final IHolder<IHolder<ContainedType>> wrappedHolder, @SuppressWarnings("unused") final boolean dummy) { @@ -41,8 +40,9 @@ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { } @Override - public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) { - final IHolder<IHolder<BoundType>> newHolder = held.map((containedHolder) -> { + public <BoundType> IHolder<BoundType> + bind(final Function<ContainedType, IHolder<BoundType>> binder) { + final IHolder<IHolder<BoundType>> newHolder = held.map(containedHolder -> { return containedHolder.bind(binder); }); @@ -50,15 +50,17 @@ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) { - return (val) -> { + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(final Function<ContainedType, NewType> func) { + return val -> { return new Lazy<>(func.apply(val)); }; } @Override - public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) { - final IHolder<IHolder<MappedType>> newHolder = held.map((containedHolder) -> { + public <MappedType> IHolder<MappedType> + map(final Function<ContainedType, MappedType> mapper) { + final IHolder<IHolder<MappedType>> newHolder = held.map(containedHolder -> { return containedHolder.map(mapper); }); @@ -66,8 +68,9 @@ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { } @Override - public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { - held.transform((containedHolder) -> { + public IHolder<ContainedType> + transform(final UnaryOperator<ContainedType> transformer) { + held.transform(containedHolder -> { return containedHolder.transform(transformer); }); @@ -75,8 +78,9 @@ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { - return held.unwrap((containedHolder) -> { + public <UnwrappedType> UnwrappedType + unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { + return held.unwrap(containedHolder -> { return containedHolder.unwrap(unwrapper); }); } diff --git a/src/main/java/bjc/data/internals/WrappedOption.java b/src/main/java/bjc/data/internals/WrappedOption.java index eb4d120..6becc16 100644 --- a/src/main/java/bjc/data/internals/WrappedOption.java +++ b/src/main/java/bjc/data/internals/WrappedOption.java @@ -11,7 +11,7 @@ import bjc.data.Option; * * @author Ben Culkin. * @param <ContainedType> - * The wrapped type. + * The wrapped type. */ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { /* The held value. */ @@ -21,16 +21,15 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { * Create a new wrapped option. * * @param seedValue - * The value to wrap. + * The value to wrap. */ public WrappedOption(final IHolder<ContainedType> seedValue) { held = new Option<>(seedValue); } /* - * The dummy parameter is to ensure the compiler can pick the right - * method, because without this method erases to the same type as the - * public one. + * The dummy parameter is to ensure the compiler can pick the right method, + * because without this method erases to the same type as the public one. */ private WrappedOption(final IHolder<IHolder<ContainedType>> toHold, @SuppressWarnings("unused") final boolean dummy) { @@ -38,10 +37,12 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { } @Override - public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) { - final IHolder<IHolder<BoundType>> newHolder = held.map((containedHolder) -> { + public <BoundType> IHolder<BoundType> + bind(final Function<ContainedType, IHolder<BoundType>> binder) { + final IHolder<IHolder<BoundType>> 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); }); @@ -51,17 +52,20 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) { - return (val) -> { + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(final Function<ContainedType, NewType> func) { + return val -> { return new Option<>(func.apply(val)); }; } @Override - public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) { - final IHolder<IHolder<MappedType>> newHolder = held.map((containedHolder) -> { + public <MappedType> IHolder<MappedType> + map(final Function<ContainedType, MappedType> mapper) { + final IHolder<IHolder<MappedType>> newHolder = held.map(containedHolder -> { return containedHolder.map((containedValue) -> { - if(containedValue == null) return null; + if (containedValue == null) + return null; return mapper.apply(containedValue); }); @@ -71,10 +75,12 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { } @Override - public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { - held.transform((containedHolder) -> { + public IHolder<ContainedType> + transform(final UnaryOperator<ContainedType> transformer) { + held.transform(containedHolder -> { return containedHolder.transform((containedValue) -> { - if(containedValue == null) return null; + if (containedValue == null) + return null; return transformer.apply(containedValue); }); @@ -84,10 +90,12 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { - return held.unwrap((containedHolder) -> { + public <UnwrappedType> UnwrappedType + unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { + return held.unwrap(containedHolder -> { return containedHolder.unwrap((containedValue) -> { - if(containedValue == null) return null; + if (containedValue == null) + return null; return unwrapper.apply(containedValue); }); |
