diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data')
13 files changed, 297 insertions, 261 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java b/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java index 1dffbc1..4b84243 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/CircularIterator.java @@ -63,10 +63,10 @@ public class CircularIterator<E> implements Iterator<E> { @Override public E next() { - if(!curr.hasNext()) { - if(doCircle) { + if (!curr.hasNext()) { + if (doCircle) curr = source.iterator(); - } else + else return curElm; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java index 3ccc859..d07fbbe 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java @@ -51,7 +51,7 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { private boolean isLeft; private Either(LeftType left, RightType right) { - if(left == null) { + if (left == null) { rightVal = right; } else { leftVal = left; @@ -63,7 +63,8 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( 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); } @@ -71,9 +72,11 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { - if(leftBinder == null) throw new NullPointerException("Left binder must not be null"); + 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); } @@ -81,9 +84,11 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <BoundRight> IPair<LeftType, BoundRight> bindRight( 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); } @@ -93,15 +98,17 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { IPair<OtherLeft, OtherRight> otherPair, BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { - if(otherPair == null) + if (otherPair == null) throw new NullPointerException("Other pair must not be null"); - else if(leftCombiner == null) + else if (leftCombiner == null) throw new NullPointerException("Left combiner must not be null"); - else if(rightCombiner == null) throw new NullPointerException("Right combiner must not be null"); + else if (rightCombiner == null) + throw new NullPointerException("Right combiner must not be null"); - if(isLeft) return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(leftCombiner.apply(leftVal, otherLeft), null); - }); + if (isLeft) + return otherPair.bind((otherLeft, otherRight) -> { + return new Either<>(leftCombiner.apply(leftVal, otherLeft), null); + }); return otherPair.bind((otherLeft, otherRight) -> { return new Either<>(null, rightCombiner.apply(rightVal, otherRight)); @@ -110,25 +117,30 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <NewLeft> IPair<NewLeft, RightType> mapLeft(Function<LeftType, NewLeft> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + 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(Function<RightType, NewRight> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + 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(BiFunction<LeftType, RightType, MergedType> merger) { - if(merger == null) throw new NullPointerException("Merger must not be null"); + if (merger == null) + throw new NullPointerException("Merger must not be null"); return merger.apply(leftVal, rightVal); } @@ -141,49 +153,41 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { result = prime * result + (isLeft ? 1231 : 1237); result = prime * result + ((leftVal == null) ? 0 : leftVal.hashCode()); result = prime * result + ((rightVal == null) ? 0 : rightVal.hashCode()); - + return result; } @Override public boolean equals(Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Either<?, ?>)) + return false; 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() { - StringBuilder builder = new StringBuilder(); - - builder.append("Either ["); - - if(leftVal != null) { - builder.append("leftVal="); - builder.append(leftVal); - builder.append(", "); - } - - if(rightVal != null) { - builder.append("rightVal="); - builder.append(rightVal); - builder.append(", "); - } - - return builder.toString(); + return String.format("Either [leftVal='%s', rightVal='%s', isLeft=%s]", leftVal, rightVal, isLeft); } -} +}
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java index 999f3da..1d380c8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -37,7 +37,7 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * The action to apply to the value */ public default void doWith(Consumer<? super ContainedType> action) { - transform((value) -> { + transform(value -> { action.accept(value); return value; @@ -47,9 +47,12 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { @Override default <ArgType, ReturnType> Function<Functor<ArgType>, Functor<ReturnType>> fmap( Function<ArgType, ReturnType> func) { - return (argumentFunctor) -> { - if(!(argumentFunctor instanceof IHolder<?>)) throw new IllegalArgumentException( - "This functor only supports mapping over instances of IHolder"); + return argumentFunctor -> { + if (!(argumentFunctor instanceof IHolder<?>)) { + String msg = "This functor only supports mapping over instances of IHolder"; + + throw new IllegalArgumentException(msg); + } IHolder<ArgType> holder = (IHolder<ArgType>) argumentFunctor; @@ -59,7 +62,7 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { @Override public default ContainedType getValue() { - return unwrap((value) -> value); + return unwrap(value -> value); } /** @@ -122,7 +125,7 @@ public interface IHolder<ContainedType> extends Functor<ContainedType> { * @return The holder itself */ public default IHolder<ContainedType> replace(ContainedType newValue) { - return transform((oldValue) -> { + return transform(oldValue -> { return newValue; }); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java index 2dbd141..ae54a88 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -109,9 +109,9 @@ public interface IPair<LeftType, RightType> extends Bifunctor<LeftType, RightTyp } @Override - default <OldLeft, OldRight, NewLeft> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, OldRight>> fmapLeft( + default <OldLeft, OldRight, NewLeft> LeftBifunctorMap<OldLeft, OldRight, NewLeft> fmapLeft( Function<OldLeft, NewLeft> func) { - return (argumentPair) -> { + return argumentPair -> { if(!(argumentPair instanceof IPair<?, ?>)) { String msg = "This function can only be applied to instances of IPair"; @@ -125,10 +125,10 @@ public interface IPair<LeftType, RightType> extends Bifunctor<LeftType, RightTyp } @Override - default <OldLeft, OldRight, NewRight> Function<Bifunctor<OldLeft, OldRight>, Bifunctor<OldLeft, NewRight>> + default <OldLeft, OldRight, NewRight> RightBifunctorMap<OldLeft, OldRight, NewRight> fmapRight(Function<OldRight, NewRight> func) { - return (argumentPair) -> { + return argumentPair -> { if(!(argumentPair instanceof IPair<?, ?>)) { String msg = "This function can only be applied to instances of IPair"; diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java b/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java index 2ccebae..166fe3f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ITree.java @@ -78,7 +78,7 @@ public interface ITree<ContainedType> { * @return A tree, with some nodes expanded into trees. */ default ITree<ContainedType> flatMapTree(Function<ContainedType, ITree<ContainedType>> mapper) { - return topDownTransform((dat) -> TopDownTransformResult.PUSHDOWN, (node) -> { + return topDownTransform(dat -> TopDownTransformResult.PUSHDOWN, node -> { if (node.getChildrenCount() > 0) { ITree<ContainedType> parent = node.transformHead(mapper); @@ -100,7 +100,7 @@ public interface ITree<ContainedType> { * @return The specified child of this tree. */ default ITree<ContainedType> getChild(int childNo) { - return transformChild(childNo, (child) -> child); + return transformChild(childNo, child -> child); } /** @@ -116,7 +116,7 @@ public interface ITree<ContainedType> { * @return The data stored in this node. */ default ContainedType getHead() { - return transformHead((head) -> head); + return transformHead(head -> head); } /** diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java index 72fe68c..77e13cf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java @@ -41,42 +41,37 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { return binder.apply(heldValue); } - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + + result = prime * result + ((heldValue == null) ? 0 : heldValue.hashCode()); + + return result; + } + @Override public boolean equals(Object obj) { if (this == obj) return true; - else if (obj == null) + if (obj == null) + return false; + if (!(obj instanceof Identity)) return false; - else if (getClass() != obj.getClass()) return false; Identity<?> other = (Identity<?>) obj; if (heldValue == null) { - if (other.heldValue != null) return false; - } else if (!heldValue.equals(other.heldValue)) return false; + if (other.heldValue != null) + return false; + } else if (!heldValue.equals(other.heldValue)) + return false; return true; } @Override - public int hashCode() { - final int prime = 31; - - int result = 1; - - int fieldHash = heldValue == null ? 0 : heldValue.hashCode(); - - result = prime * result + fieldHash; - - return result; - } - - @Override public <NewType> Function<ContainedType, IHolder<NewType>> lift(Function<ContainedType, NewType> func) { return (val) -> { return new Identity<>(func.apply(val)); @@ -90,7 +85,7 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { @Override public String toString() { - return "holding[v=" + heldValue + "]"; + return String.format("Identity [heldValue=%s]", heldValue); } @Override diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java index 2eb2cf3..719b11f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java @@ -62,7 +62,8 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { actions.forEach(pendingActions::add); Supplier<ContainedType> supplier = () -> { - if (valueMaterialized) return heldValue; + if (valueMaterialized) + return heldValue; return valueSupplier.get(); }; @@ -74,7 +75,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { @Override public <NewType> Function<ContainedType, IHolder<NewType>> lift(Function<ContainedType, NewType> func) { - return (val) -> { + return val -> { return new Lazy<>(func.apply(val)); }; } @@ -93,16 +94,17 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { } return pendingActions.reduceAux(currVal, UnaryOperator<ContainedType>::apply, - (value) -> mapper.apply(value)); + value -> mapper.apply(value)); }); } @Override public String toString() { if (valueMaterialized) { - if (actions.isEmpty()) return "value[v='" + heldValue + "']"; - - return "value[v='" + heldValue + "'] (has pending transforms)"; + if (actions.isEmpty()) + return String.format("value[v='%s']", heldValue); + else + return String.format("value[v='%s'] (has pending transforms)", heldValue); } return "(unmaterialized)"; @@ -123,7 +125,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { valueMaterialized = true; } - actions.forEach((action) -> { + actions.forEach(action -> { heldValue = action.apply(heldValue); }); @@ -146,25 +148,33 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { @Override public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Lazy<?>)) + return false; 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 (other.heldValue != null) + return false; + } else if (!heldValue.equals(other.heldValue)) + return false; } else { return false; } if (actions == null) { - if (other.actions != null) return false; - } else if (actions.getSize() > 0 || other.actions.getSize() > 0) return false; + if (other.actions != null) + return false; + } else if (actions.getSize() > 0 || other.actions.getSize() > 0) + return false; return true; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java index ea49b4c..70768be 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -71,7 +71,8 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { Supplier<LeftType> leftSupp = () -> { - if(leftMaterialized) return leftValue; + if (leftMaterialized) + return leftValue; return leftSupplier.get(); }; @@ -83,7 +84,8 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> public <BoundRight> IPair<LeftType, BoundRight> bindRight( Function<RightType, IPair<LeftType, BoundRight>> rightBinder) { Supplier<RightType> rightSupp = () -> { - if(rightMaterialized) return rightValue; + if (rightMaterialized) + return rightValue; return rightSupplier.get(); }; @@ -98,15 +100,17 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { return otherPair.bind((otherLeft, otherRight) -> { return bind((leftVal, rightVal) -> { - return new LazyPair<>(leftCombiner.apply(leftVal, otherLeft), - rightCombiner.apply(rightVal, otherRight)); + CombinedLeft left = leftCombiner.apply(leftVal, otherLeft); + CombinedRight right = rightCombiner.apply(rightVal, otherRight); + + return new LazyPair<>(left, right); }); }); } @Override public LeftType getLeft() { - if(!leftMaterialized) { + if (!leftMaterialized) { leftValue = leftSupplier.get(); leftMaterialized = true; @@ -117,7 +121,7 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public RightType getRight() { - if(!rightMaterialized) { + if (!rightMaterialized) { rightValue = rightSupplier.get(); rightMaterialized = true; @@ -129,13 +133,15 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public <NewLeft> IPair<NewLeft, RightType> mapLeft(Function<LeftType, NewLeft> mapper) { Supplier<NewLeft> leftSupp = () -> { - if(leftMaterialized) return mapper.apply(leftValue); + if (leftMaterialized) + return mapper.apply(leftValue); return mapper.apply(leftSupplier.get()); }; Supplier<RightType> rightSupp = () -> { - if(rightMaterialized) return rightValue; + if (rightMaterialized) + return rightValue; return rightSupplier.get(); }; @@ -146,13 +152,15 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public <NewRight> IPair<LeftType, NewRight> mapRight(Function<RightType, NewRight> mapper) { Supplier<LeftType> leftSupp = () -> { - if(leftMaterialized) return leftValue; + if (leftMaterialized) + return leftValue; return leftSupplier.get(); }; Supplier<NewRight> rightSupp = () -> { - if(rightMaterialized) return mapper.apply(rightValue); + if (rightMaterialized) + return mapper.apply(rightValue); return mapper.apply(rightSupplier.get()); }; @@ -162,13 +170,13 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public <MergedType> MergedType merge(BiFunction<LeftType, RightType, MergedType> merger) { - if(!leftMaterialized) { + if (!leftMaterialized) { leftValue = leftSupplier.get(); leftMaterialized = true; } - if(!rightMaterialized) { + if (!rightMaterialized) { rightValue = rightSupplier.get(); rightMaterialized = true; @@ -179,25 +187,22 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public String toString() { - StringBuilder sb = new StringBuilder("pair[l="); + String leftVal; + String rightVal; - if(leftMaterialized) { - sb.append(leftValue.toString()); + if (leftMaterialized) { + leftVal = leftValue.toString(); } else { - sb.append("(un-materialized)"); + leftVal = "(un-materialized)"; } - sb.append(", r="); - - if(rightMaterialized) { - sb.append(rightValue.toString()); + if (rightMaterialized) { + rightVal = rightValue.toString(); } else { - sb.append("(un-materialized)"); + rightVal = "(un-materialized)"; } - sb.append("]"); - - return sb.toString(); + return String.format("pair[l=%s,r=%s]", leftVal, rightVal); } @Override @@ -215,27 +220,36 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public boolean equals(Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof LazyPair<?, ?>)) + return false; 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/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java index f460941..8807312 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/ListHolder.java @@ -28,8 +28,8 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { public ListHolder(ContainedType... values) { heldValues = new FunctionalList<>(); - if(values != null) { - for(ContainedType containedValue : values) { + if (values != null) { + for (ContainedType containedValue : values) { heldValues.add(containedValue); } } @@ -48,7 +48,7 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { @Override public <NewType> Function<ContainedType, IHolder<NewType>> lift(Function<ContainedType, NewType> func) { - return (val) -> { + return val -> { return new ListHolder<>(new FunctionalList<>(func.apply(val))); }; } @@ -74,13 +74,7 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("ListHolder [heldValues="); - builder.append(heldValues); - builder.append("]"); - - return builder.toString(); + return String.format("ListHolder [heldValues=%s]", heldValues); } @Override @@ -95,15 +89,20 @@ public class ListHolder<ContainedType> implements IHolder<ContainedType> { @Override public boolean equals(Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof ListHolder<?>)) + return false; 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/BJC-Utils2/src/main/java/bjc/utils/data/Option.java b/BJC-Utils2/src/main/java/bjc/utils/data/Option.java index fa25b54..718ab6e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Option.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Option.java @@ -26,28 +26,30 @@ public class Option<ContainedType> implements IHolder<ContainedType> { @Override public <BoundType> IHolder<BoundType> bind(Function<ContainedType, IHolder<BoundType>> binder) { - if(held == null) return new Option<>(null); + if (held == null) + return new Option<>(null); return binder.apply(held); } @Override public <NewType> Function<ContainedType, IHolder<NewType>> lift(Function<ContainedType, NewType> func) { - return (val) -> { + return val -> { return new Option<>(func.apply(val)); }; } @Override public <MappedType> IHolder<MappedType> map(Function<ContainedType, MappedType> mapper) { - if(held == null) return new Option<>(null); + if (held == null) + return new Option<>(null); return new Option<>(mapper.apply(held)); } @Override public IHolder<ContainedType> transform(UnaryOperator<ContainedType> transformer) { - if(held != null) { + if (held != null) { held = transformer.apply(held); } @@ -56,24 +58,15 @@ public class Option<ContainedType> implements IHolder<ContainedType> { @Override public <UnwrappedType> UnwrappedType unwrap(Function<ContainedType, UnwrappedType> unwrapper) { - if(held == null) return null; + if (held == null) + return null; return unwrapper.apply(held); } @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("Option ["); - - if(held != null) { - builder.append("held="); - builder.append(held); - } - - builder.append("]"); - return builder.toString(); + return String.format("Option [held='%s']", held); } @Override @@ -88,15 +81,20 @@ public class Option<ContainedType> implements IHolder<ContainedType> { @Override public boolean equals(Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Option<?>)) + return false; 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/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java index 48d3aca..2fc3106 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -43,7 +43,8 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( 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); } @@ -51,7 +52,8 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { - if(leftBinder == null) throw new NullPointerException("Binder must not be null"); + if (leftBinder == null) + throw new NullPointerException("Binder must not be null"); return leftBinder.apply(leftValue); } @@ -59,7 +61,8 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { @Override public <BoundRight> IPair<LeftType, BoundRight> bindRight( Function<RightType, IPair<LeftType, BoundRight>> rightBinder) { - 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); } @@ -70,62 +73,76 @@ public class Pair<LeftType, RightType> implements IPair<LeftType, RightType> { BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { return otherPair.bind((otherLeft, otherRight) -> { - return new Pair<>(leftCombiner.apply(leftValue, otherLeft), - rightCombiner.apply(rightValue, otherRight)); + CombinedLeft left = leftCombiner.apply(leftValue, otherLeft); + CombinedRight right = rightCombiner.apply(rightValue, otherRight); + + return new Pair<>(left, right); }); } @Override public <NewLeft> IPair<NewLeft, RightType> mapLeft(Function<LeftType, NewLeft> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + 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(Function<RightType, NewRight> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); return new Pair<>(leftValue, mapper.apply(rightValue)); } @Override public <MergedType> MergedType merge(BiFunction<LeftType, RightType, MergedType> merger) { - if(merger == null) throw new NullPointerException("Merger must not be null"); + if (merger == null) + throw new NullPointerException("Merger must not be null"); return merger.apply(leftValue, rightValue); } @Override public String toString() { - return "pair[l=" + leftValue.toString() + ", r=" + rightValue.toString() + "]"; + return String.format("Pair [leftValue='%s', rightValue='%s']", leftValue, rightValue); } @Override public int hashCode() { final int prime = 31; int result = 1; + result = prime * result + ((leftValue == null) ? 0 : leftValue.hashCode()); result = prime * result + ((rightValue == null) ? 0 : rightValue.hashCode()); + return result; } @Override public boolean equals(Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(getClass() != obj.getClass()) return false; - + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Pair<?, ?>)) + return false; + 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/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java index 0bf1a93..fde5111 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/SingleSupplier.java @@ -43,9 +43,11 @@ public class SingleSupplier<T> implements Supplier<T> { @Override public T get() { - if(gotten == true) { - IllegalStateException isex = new IllegalStateException("Attempted to get value more than once" - + " from single supplier #" + id + ". Previous instantiation below."); + if (gotten == true) { + String msg = String.format( + "Attempted to retrieve value more than once from single supplier #%d", id); + + IllegalStateException isex = new IllegalStateException(msg); isex.initCause(instSite); @@ -56,7 +58,7 @@ public class SingleSupplier<T> implements Supplier<T> { try { throw new IllegalStateException("Previous instantiation here."); - } catch(IllegalStateException isex) { + } catch (IllegalStateException isex) { instSite = isex; } @@ -65,28 +67,6 @@ public class SingleSupplier<T> implements Supplier<T> { @Override public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("SingleSupplier ["); - - if(source != null) { - builder.append("source="); - builder.append(source); - builder.append(", "); - } - - builder.append("gotten="); - builder.append(gotten); - builder.append(", id="); - builder.append(id); - builder.append(", "); - - if(instSite != null) { - builder.append("instSite="); - builder.append(instSite); - } - - builder.append("]"); - - return builder.toString(); + return String.format("SingleSupplier [source='%s', gotten=%s, id=%s]", source, gotten, id); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java index d8e6e39..0dc96eb 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java @@ -103,12 +103,12 @@ public class Tree<ContainedType> implements ITree<ContainedType> { public void prependChild(ITree<ContainedType> child) { if (hasChildren == false) { hasChildren = true; - + children = new FunctionalList<>(); } - + childCount++; - + children.prepend(child); } @@ -135,7 +135,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } } } - + return -1; } @@ -144,28 +144,31 @@ public class Tree<ContainedType> implements ITree<ContainedType> { if (hasChildren) { switch (linearizationMethod) { case INORDER: - if (childCount != 2) throw new IllegalArgumentException( - "Can only do in-order traversal for binary trees."); - + if (childCount != 2) { + String msg = "Can only do in-order traversal for binary trees."; + + throw new IllegalArgumentException(msg); + } + children.getByIndex(0).traverse(linearizationMethod, action); - + action.accept(data); - + children.getByIndex(1).traverse(linearizationMethod, action); break; case POSTORDER: children.forEach((child) -> child.traverse(linearizationMethod, action)); - + action.accept(data); break; case PREORDER: action.accept(data); - + children.forEach((child) -> child.traverse(linearizationMethod, action)); break; default: break; - + } } else { action.accept(data); @@ -184,8 +187,9 @@ public class Tree<ContainedType> implements ITree<ContainedType> { if (hasChildren) { ITree<ContainedType> flatMappedData = mapper.apply(data); - IList<ITree<ContainedType>> mappedChildren = children.map((child) -> child.flatMapTree(mapper)); - mappedChildren.forEach((child) -> flatMappedData.addChild(child)); + IList<ITree<ContainedType>> mappedChildren = children.map(child -> child.flatMapTree(mapper)); + + mappedChildren.forEach(child -> flatMappedData.addChild(child)); return flatMappedData; } @@ -198,9 +202,9 @@ public class Tree<ContainedType> implements ITree<ContainedType> { if (hasChildren) { Function<IList<NewType>, NewType> nodeTransformer = nodeCollapser.apply(data); - IList<NewType> collapsedChildren = children.map((child) -> { + IList<NewType> collapsedChildren = children.map(child -> { NewType collapsed = child.collapse(leafTransform, nodeCollapser, - (subTreeVal) -> subTreeVal); + subTreeVal -> subTreeVal); return collapsed; }); @@ -223,7 +227,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { builder.append("\n"); if (hasChildren) { - children.forEach((child) -> { + children.forEach(child -> { if (child instanceof Tree<?>) { Tree<ContainedType> kid = (Tree<ContainedType>) child; @@ -243,7 +247,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { public <MappedType> ITree<MappedType> rebuildTree(Function<ContainedType, MappedType> leafTransformer, Function<ContainedType, MappedType> operatorTransformer) { if (hasChildren) { - IList<ITree<MappedType>> mappedChildren = children.map((child) -> { + IList<ITree<MappedType>> mappedChildren = children.map(child -> { return child.rebuildTree(leafTransformer, operatorTransformer); }); @@ -256,7 +260,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { @Override public void selectiveTransform(Predicate<ContainedType> nodePicker, UnaryOperator<ContainedType> transformer) { if (hasChildren) { - children.forEach((child) -> child.selectiveTransform(nodePicker, transformer)); + children.forEach(child -> child.selectiveTransform(nodePicker, transformer)); } else { data = transformer.apply(data); } @@ -272,7 +276,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { ITree<ContainedType> result = new Tree<>(data); if (hasChildren) { - children.forEach((child) -> { + children.forEach(child -> { ITree<ContainedType> kid = child.topDownTransform(transformPicker, transformer); result.addChild(kid); @@ -290,7 +294,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { result = new Tree<>(data); if (hasChildren) { - children.forEach((child) -> { + children.forEach(child -> { ITree<ContainedType> kid = child.topDownTransform(transformPicker, transformer); result.addChild(kid); @@ -303,7 +307,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> { result = new Tree<>(intermediateResult.getHead()); - intermediateResult.doForChildren((child) -> { + intermediateResult.doForChildren(child -> { ITree<ContainedType> kid = child.topDownTransform(transformPicker, transformer); result.addChild(kid); @@ -311,16 +315,20 @@ public class Tree<ContainedType> implements ITree<ContainedType> { return result; default: - throw new IllegalArgumentException("Recieved unknown transform result " + transformResult); + String msg = String.format("Recieved unknown transform result %s", transformResult); + throw new IllegalArgumentException(msg); } } @Override public <TransformedType> TransformedType transformChild(int childNo, Function<ITree<ContainedType>, TransformedType> transformer) { - if (childNo < 0 || childNo > childCount - 1) - throw new IllegalArgumentException("Child index #" + childNo + " is invalid"); + if (childNo < 0 || childNo > childCount - 1) { + String msg = String.format("Child index #%d is invalid", childNo); + + throw new IllegalArgumentException(msg); + } ITree<ContainedType> selectedKid = children.getByIndex(childNo); @@ -347,31 +355,39 @@ public class Tree<ContainedType> implements ITree<ContainedType> { @Override public String toString() { StringBuilder builder = new StringBuilder(); - + internalToString(builder, 1, true); - + builder.deleteCharAt(builder.length() - 1); - + return builder.toString(); } @Override public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Tree<?>)) + return false; Tree<?> other = (Tree<?>) obj; if (data == null) { - if (other.data != null) return false; - } else if (!data.equals(other.data)) return false; + if (other.data != null) + return false; + } else if (!data.equals(other.data)) + return false; - if (childCount != other.childCount) return false; + if (childCount != other.childCount) + return false; if (children == null) { - if (other.children != null) return false; - } else if (!children.equals(other.children)) return false; + if (other.children != null) + return false; + } else if (!children.equals(other.children)) + return false; return true; } |
