diff options
| author | bjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu> | 2017-04-07 10:51:31 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@WIT-136XG42.wvu-ad.wvu.edu> | 2017-04-07 10:51:31 -0400 |
| commit | 63d88eb8db1f7a6d5924ec2a8b7f462373d5ac9a (patch) | |
| tree | 4a7c67b23c8e1ecb1b2f992e5dbaf3ebb48dcf6b | |
| parent | 848dc739becfa41193aff9a07c918aed91e5ef79 (diff) | |
Cleanup
41 files changed, 1018 insertions, 698 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; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/AbbrevMap.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/AbbrevMap.java index ebab5f0..433f672 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/AbbrevMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/AbbrevMap.java @@ -62,7 +62,7 @@ public class AbbrevMap { seen = new HashSet<>(); - for(String word : wrds) { + for (String word : wrds) { /* * A word always abbreviates to itself. */ @@ -81,7 +81,7 @@ public class AbbrevMap { public void addWords(String... words) { wrds.addAll(Arrays.asList(words)); - for(String word : words) { + for (String word : words) { /* * A word always abbreviates to itself. */ @@ -98,25 +98,26 @@ public class AbbrevMap { /* * Skip blank words. */ - if(word.equals("")) return; + if (word.equals("")) + return; /* * Handle each possible abbreviation. */ - for(int i = word.length(); i > 0; i--) { + for (int i = word.length(); i > 0; i--) { String subword = word.substring(0, i); - if(seen.contains(subword)) { + if (seen.contains(subword)) { /* * Remove a mapping if its ambiguous and not a * whole word. */ - if(abbrevMap.containsKey(subword) && !wrds.contains(subword)) { + if (abbrevMap.containsKey(subword) && !wrds.contains(subword)) { String oldword = abbrevMap.remove(subword); ambMap.put(subword, oldword); ambMap.put(subword, word); - } else if(!wrds.contains(subword)) { + } else if (!wrds.contains(subword)) { ambMap.put(subword, word); } } else { @@ -139,7 +140,7 @@ public class AbbrevMap { public void removeWords(String... words) { wrds.removeAll(Arrays.asList(words)); - for(String word : words) { + for (String word : words) { intRemoveWord(word); } } @@ -151,24 +152,25 @@ public class AbbrevMap { /* * Skip blank words. */ - if(word.equals("")) return; + if (word.equals("")) + return; /* * Handle each possible abbreviation. */ - for(int i = word.length(); i > 0; i--) { + for (int i = word.length(); i > 0; i--) { String subword = word.substring(0, i); - if(abbrevMap.containsKey(subword)) + if (abbrevMap.containsKey(subword)) abbrevMap.remove(subword); else { ambMap.remove(subword, word); Set<String> possWords = ambMap.get(subword); - if(possWords.size() == 0) { + if (possWords.size() == 0) { seen.remove(subword); - } else if(possWords.size() == 1) { + } else if (possWords.size() == 1) { String newWord = possWords.iterator().next(); abbrevMap.put(subword, newWord); @@ -188,7 +190,7 @@ public class AbbrevMap { * @return All the expansions for the provided abbreviation. */ public String[] deabbrev(String abbrev) { - if(abbrevMap.containsKey(abbrev)) + if (abbrevMap.containsKey(abbrev)) return new String[] { abbrevMap.get(abbrev) }; else return ambMap.get(abbrev).toArray(new String[0]); @@ -200,41 +202,34 @@ public class AbbrevMap { int result = 1; result = prime * result + ((wrds == null) ? 0 : wrds.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 AbbrevMap)) + return false; + AbbrevMap other = (AbbrevMap) obj; - - if(wrds == null) { - if(other.wrds != null) return false; - } else if(!wrds.equals(other.wrds)) return false; - + + if (wrds == null) { + if (other.wrds != null) + return false; + } else if (!wrds.equals(other.wrds)) + return false; + return true; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("AbbrevMap [wrds="); - builder.append(wrds); - builder.append("\n\t, abbrevMap="); - builder.append(abbrevMap); - builder.append("\n\t, seen="); - builder.append(seen); - builder.append("\n\t, ambMap="); - builder.append(ambMap); - builder.append("]"); - - return builder.toString(); + String fmt = "AbbrevMap [wrds=%s, abbrevMap=%s, seen=%s, ambMap=%s]"; + + return String.format(fmt, wrds, abbrevMap, seen, ambMap); } - - } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java index 05f686a..52e3172 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/Directory.java @@ -60,7 +60,8 @@ public interface Directory<K, V> { * exists.
*/
default Directory<K, V> newSubdirectory(K key) {
- if(hasSubdirectory(key)) return null;
+ if (hasSubdirectory(key))
+ return null;
Directory<K, V> dir = new SimpleDirectory<>();
diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/DoubleTape.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/DoubleTape.java index ec24431..8df7aa9 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/DoubleTape.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/DoubleTape.java @@ -159,7 +159,7 @@ public class DoubleTape<T> implements Tape<T> { public boolean left(int amt) { boolean succ = front.left(amt); - if(succ) { + if (succ) { back.right(amt); } @@ -192,7 +192,7 @@ public class DoubleTape<T> implements Tape<T> { public boolean right(int amt) { boolean succ = front.right(amt); - if(succ) { + if (succ) { back.left(amt); } @@ -229,33 +229,32 @@ public class DoubleTape<T> implements Tape<T> { @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 DoubleTape<?>)) + return false; + DoubleTape<?> other = (DoubleTape<?>) obj; - - if(back == null) { - if(other.back != null) return false; - } else if(!back.equals(other.back)) return false; - - if(front == null) { - if(other.front != null) return false; - } else if(!front.equals(other.front)) return false; - + + if (back == null) { + if (other.back != null) + return false; + } else if (!back.equals(other.back)) + return false; + + if (front == null) { + if (other.front != null) + return false; + } else if (!front.equals(other.front)) + return false; + return true; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("DoubleTape [front="); - builder.append(front); - builder.append(", back="); - builder.append(back); - builder.append("]"); - - return builder.toString(); + return String.format("DoubleTape [front=%s, back=%s]", front, back); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/PushdownMap.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/PushdownMap.java index 1e8d2d3..bf72f29 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/PushdownMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/PushdownMap.java @@ -85,7 +85,7 @@ public class PushdownMap<KeyType, ValueType> implements IMap<KeyType, ValueType> @Override public ValueType put(KeyType key, ValueType val) { - if(backing.containsKey(key)) { + if (backing.containsKey(key)) { Stack<ValueType> stk = backing.get(key); ValueType vl = stk.top(); @@ -106,7 +106,7 @@ public class PushdownMap<KeyType, ValueType> implements IMap<KeyType, ValueType> public ValueType remove(KeyType key) { Stack<ValueType> stk = backing.get(key); - if(stk.size() > 1) { + if (stk.size() > 1) { return stk.pop(); } else { return backing.remove(key).top(); @@ -130,27 +130,26 @@ public class PushdownMap<KeyType, ValueType> implements IMap<KeyType, ValueType> @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 PushdownMap<?, ?>)) + return false; PushdownMap<?, ?> other = (PushdownMap<?, ?>) obj; - if(backing == null) { - if(other.backing != null) return false; - } else if(!backing.equals(other.backing)) return false; + if (backing == null) { + if (other.backing != null) + return false; + } else if (!backing.equals(other.backing)) + return false; return true; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("PushdownMap [backing="); - builder.append(backing); - builder.append("]"); - - return builder.toString(); + return String.format("PushdownMap [backing=%s]", backing); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/QueueStack.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/QueueStack.java index 005c90e..ebb9d8c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/QueueStack.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/QueueStack.java @@ -28,14 +28,16 @@ public class QueueStack<T> extends Stack<T> { @Override public T pop() { - if(backing.isEmpty()) throw new StackUnderflowException(); + if (backing.isEmpty()) + throw new StackUnderflowException(); return backing.remove(); } @Override public T top() { - if(backing.isEmpty()) throw new StackUnderflowException(); + if (backing.isEmpty()) + throw new StackUnderflowException(); return backing.peek(); } @@ -50,17 +52,6 @@ public class QueueStack<T> extends Stack<T> { return backing.size() == 0; } - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("QueueStack [backing="); - builder.append(backing); - builder.append("]"); - - return builder.toString(); - } - @SuppressWarnings("unchecked") @Override public T[] toArray() { @@ -68,6 +59,11 @@ public class QueueStack<T> extends Stack<T> { } @Override + public String toString() { + return String.format("QueueStack [backing=%s]", backing); + } + + @Override public int hashCode() { final int prime = 31; int result = 1; @@ -79,15 +75,20 @@ public class QueueStack<T> extends Stack<T> { @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 QueueStack<?>)) + return false; QueueStack<?> other = (QueueStack<?>) obj; - if(backing == null) { - if(other.backing != null) return false; - } else if(!backing.equals(other.backing)) return false; + if (backing == null) { + if (other.backing != null) + return false; + } else if (!backing.equals(other.backing)) + return false; return true; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java index 741b33d..bab64f5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleDirectory.java @@ -71,33 +71,32 @@ public class SimpleDirectory<K, V> implements Directory<K, V> { @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 SimpleDirectory<?, ?>)) + return false; SimpleDirectory<?, ?> other = (SimpleDirectory<?, ?>) obj; - 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; - 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; return true; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("SimpleDirectory [children="); - builder.append(children); - builder.append(", data="); - builder.append(data); - builder.append("]"); - - return builder.toString(); + return String.format("SimpleDirectory [children=%s, data=%s]", children, data); } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleStack.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleStack.java index 1de06a6..0e109aa 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleStack.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/SimpleStack.java @@ -28,14 +28,16 @@ public class SimpleStack<T> extends Stack<T> { @Override public T pop() { - if(backing.isEmpty()) throw new StackUnderflowException(); + if (backing.isEmpty()) + throw new StackUnderflowException(); return backing.pop(); } @Override public T top() { - if(backing.isEmpty()) throw new StackUnderflowException(); + if (backing.isEmpty()) + throw new StackUnderflowException(); return backing.peek(); } @@ -67,27 +69,26 @@ public class SimpleStack<T> extends Stack<T> { @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 SimpleStack<?>)) + return false; SimpleStack<?> other = (SimpleStack<?>) obj; - if(backing == null) { - if(other.backing != null) return false; - } else if(!backing.equals(other.backing)) return false; + if (backing == null) { + if (other.backing != null) + return false; + } else if (!backing.equals(other.backing)) + return false; return true; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("SimpleStack [backing="); - builder.append(backing); - builder.append("]"); - - return builder.toString(); + return String.format("SimpleStack [backing=%s]", backing); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/SingleTape.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/SingleTape.java index 056cab4..253c15f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/SingleTape.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/SingleTape.java @@ -93,7 +93,7 @@ public class SingleTape<T> implements Tape<T> { */ @Override public void insertAfter(T itm) { - if(pos == backing.size() - 1) { + if (pos == backing.size() - 1) { backing.add(itm); } else { backing.add(pos + 1, itm); @@ -111,7 +111,7 @@ public class SingleTape<T> implements Tape<T> { @Override public T remove() { T res = backing.remove(pos); - if(pos != 0) { + if (pos != 0) { pos -= 1; } return res; @@ -158,7 +158,8 @@ public class SingleTape<T> implements Tape<T> { */ @Override public boolean left(int amt) { - if(pos - amt < 0) return false; + if (pos - amt < 0) + return false; pos -= amt; return true; @@ -188,9 +189,9 @@ public class SingleTape<T> implements Tape<T> { */ @Override public boolean right(int amt) { - if(pos + amt >= backing.size() - 1) { - if(autoExtend) { - while(pos + amt >= backing.size() - 1) { + if (pos + amt >= backing.size() - 1) { + if (autoExtend) { + while (pos + amt >= backing.size() - 1) { backing.add(null); } } else @@ -218,31 +219,26 @@ public class SingleTape<T> implements Tape<T> { @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 SingleTape<?>)) + return false; SingleTape<?> other = (SingleTape<?>) obj; - if(backing == null) { - if(other.backing != null) return false; - } else if(!backing.equals(other.backing)) return false; + if (backing == null) { + if (other.backing != null) + return false; + } else if (!backing.equals(other.backing)) + return false; return true; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("SingleTape [backing="); - builder.append(backing); - builder.append(", pos="); - builder.append(pos); - builder.append(", autoExtend="); - builder.append(autoExtend); - builder.append("]"); - - return builder.toString(); + return String.format("SingleTape [backing=%s, pos=%s, autoExtend=%s]", backing, pos, autoExtend); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/SpaghettiStack.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/SpaghettiStack.java index c4c469f..6fc4766 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/SpaghettiStack.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/SpaghettiStack.java @@ -34,14 +34,16 @@ class SpaghettiStack<T> extends Stack<T> { @Override public T pop() { - if(backing.empty()) return parent.pop(); + if (backing.empty()) + return parent.pop(); return backing.pop(); } @Override public T top() { - if(backing.empty()) return parent.top(); + if (backing.empty()) + return parent.top(); return backing.top(); } @@ -56,11 +58,6 @@ class SpaghettiStack<T> extends Stack<T> { return backing.empty() && parent.empty(); } - @Override - public String toString() { - return "[base=" + parent.toString() + ", own=" + backing.toString() + "]"; - } - @SuppressWarnings("unchecked") @Override public T[] toArray() { @@ -80,20 +77,32 @@ class SpaghettiStack<T> extends Stack<T> { @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 SpaghettiStack<?>)) + return false; SpaghettiStack<?> other = (SpaghettiStack<?>) obj; - if(backing == null) { - if(other.backing != null) return false; - } else if(!backing.equals(other.backing)) return false; + if (backing == null) { + if (other.backing != null) + return false; + } else if (!backing.equals(other.backing)) + return false; - if(parent == null) { - if(other.parent != null) return false; - } else if(!parent.equals(other.parent)) return false; + if (parent == null) { + if (other.parent != null) + return false; + } else if (!parent.equals(other.parent)) + return false; return true; } + + @Override + public String toString() { + return String.format("SpaghettiStack [backing=%s, parent=%s]", backing, parent); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java index c112732..217c671 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java @@ -79,7 +79,7 @@ public abstract class Stack<T> { public Stack<T> spaghettify() { return new SpaghettiStack<>(this); } - + /* * Basic combinators */ @@ -91,7 +91,7 @@ public abstract class Stack<T> { * The number of items to drop. */ public void drop(int n) { - for(int i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { pop(); } } @@ -135,12 +135,12 @@ public abstract class Stack<T> { public void multidup(int n, int m) { List<T> lst = new ArrayList<>(n); - for(int i = n; i > 0; i--) { + for (int i = n; i > 0; i--) { lst.set(i - 1, pop()); } - for(int i = 0; i < m; i++) { - for(T elm : lst) { + for (int i = 0; i < m; i++) { + for (T elm : lst) { push(elm); } } @@ -176,17 +176,17 @@ public abstract class Stack<T> { T elm = pop(); - for(int i = n; i > 0; i--) { + for (int i = n; i > 0; i--) { lst.set(i - 1, pop()); } - for(T nelm : lst) { + for (T nelm : lst) { push(nelm); } push(elm); - for(int i = 1; i < m; i++) { - for(T nelm : lst) { + for (int i = 1; i < m; i++) { + for (T nelm : lst) { push(nelm); } } @@ -299,13 +299,13 @@ public abstract class Stack<T> { public void dip(int n, Consumer<Stack<T>> cons) { List<T> elms = new ArrayList<>(n); - for(int i = n; i > 0; i--) { + for (int i = n; i > 0; i--) { elms.set(i - 1, pop()); } cons.accept(this); - for(T elm : elms) { + for (T elm : elms) { push(elm); } } @@ -345,12 +345,12 @@ public abstract class Stack<T> { public void multicleave(int n, List<Consumer<Stack<T>>> conses) { List<T> elms = new ArrayList<>(n); - for(int i = n; i > 0; i--) { + for (int i = n; i > 0; i--) { elms.set(i - 1, pop()); } - for(Consumer<Stack<T>> cons : conses) { - for(T elm : elms) { + for (Consumer<Stack<T>> cons : conses) { + for (T elm : elms) { push(elm); } @@ -379,10 +379,10 @@ public abstract class Stack<T> { public void multispread(int n, List<Consumer<Stack<T>>> conses) { List<List<T>> nelms = new ArrayList<>(conses.size()); - for(int i = conses.size(); i > 0; i--) { + for (int i = conses.size(); i > 0; i--) { List<T> elms = new ArrayList<>(n); - for(int j = n; j > 0; j--) { + for (int j = n; j > 0; j--) { elms.set(j, pop()); } @@ -390,8 +390,8 @@ public abstract class Stack<T> { } int i = 0; - for(List<T> elms : nelms) { - for(T elm : elms) { + for (List<T> elms : nelms) { + for (T elm : elms) { push(elm); } @@ -403,7 +403,8 @@ public abstract class Stack<T> { /** * Apply the actions in cons to corresponding elements from the stack. * - * @param conses The actions to execute. + * @param conses + * The actions to execute. */ public void spread(List<Consumer<Stack<T>>> conses) { multispread(1, conses); @@ -422,7 +423,7 @@ public abstract class Stack<T> { public void multiapply(int n, int m, Consumer<Stack<T>> cons) { List<Consumer<Stack<T>>> conses = new ArrayList<>(m); - for(int i = 0; i < m; i++) { + for (int i = 0; i < m; i++) { conses.add(cons); } @@ -440,7 +441,7 @@ public abstract class Stack<T> { public void apply(int n, Consumer<Stack<T>> cons) { multiapply(1, n, cons); } - + /* * Misc. functions */ diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeChanger.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeChanger.java index 64c0460..10764fe 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeChanger.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeChanger.java @@ -37,7 +37,7 @@ public class TapeChanger<T> implements Tape<T> { tapes.insertBefore(current); - for(Tape<T> tp : others) { + for (Tape<T> tp : others) { tapes.insertAfter(tp); tapes.right(); } @@ -53,7 +53,8 @@ public class TapeChanger<T> implements Tape<T> { */ @Override public T item() { - if(currentTape == null) return null; + if (currentTape == null) + return null; return currentTape.item(); } @@ -66,7 +67,8 @@ public class TapeChanger<T> implements Tape<T> { */ @Override public void item(T itm) { - if(currentTape == null) return; + if (currentTape == null) + return; currentTape.item(itm); } @@ -78,7 +80,8 @@ public class TapeChanger<T> implements Tape<T> { */ @Override public int size() { - if(currentTape == null) return 0; + if (currentTape == null) + return 0; return currentTape.size(); } @@ -91,7 +94,8 @@ public class TapeChanger<T> implements Tape<T> { */ @Override public void insertBefore(T itm) { - if(currentTape == null) return; + if (currentTape == null) + return; currentTape.insertBefore(itm); } @@ -101,7 +105,8 @@ public class TapeChanger<T> implements Tape<T> { */ @Override public void insertAfter(T itm) { - if(currentTape == null) return; + if (currentTape == null) + return; currentTape.insertAfter(itm); } @@ -116,7 +121,8 @@ public class TapeChanger<T> implements Tape<T> { */ @Override public T remove() { - if(currentTape == null) return null; + if (currentTape == null) + return null; return currentTape.remove(); } @@ -126,7 +132,8 @@ public class TapeChanger<T> implements Tape<T> { */ @Override public void first() { - if(currentTape == null) return; + if (currentTape == null) + return; currentTape.first(); } @@ -136,7 +143,8 @@ public class TapeChanger<T> implements Tape<T> { */ @Override public void last() { - if(currentTape == null) return; + if (currentTape == null) + return; currentTape.last(); } @@ -166,7 +174,8 @@ public class TapeChanger<T> implements Tape<T> { */ @Override public boolean left(int amt) { - if(currentTape == null) return false; + if (currentTape == null) + return false; return currentTape.left(amt); } @@ -195,7 +204,8 @@ public class TapeChanger<T> implements Tape<T> { */ @Override public boolean right(int amt) { - if(currentTape == null) return false; + if (currentTape == null) + return false; return currentTape.right(amt); } @@ -209,16 +219,18 @@ public class TapeChanger<T> implements Tape<T> { * If the current tape is not double-sided, does nothing. */ public void flip() { - if(currentTape == null) return; + if (currentTape == null) + return; - if(currentTape.isDoubleSided()) { + if (currentTape.isDoubleSided()) { ((DoubleTape<T>) currentTape).flip(); } } @Override public boolean isDoubleSided() { - if(currentTape == null) return false; + if (currentTape == null) + return false; return currentTape.isDoubleSided(); } @@ -243,7 +255,7 @@ public class TapeChanger<T> implements Tape<T> { public boolean nextTape() { boolean succ = tapes.right(); - if(succ) { + if (succ) { currentTape = tapes.item(); } @@ -261,7 +273,7 @@ public class TapeChanger<T> implements Tape<T> { public boolean prevTape() { boolean succ = tapes.left(); - if(succ) { + if (succ) { currentTape = tapes.item(); } @@ -295,7 +307,8 @@ public class TapeChanger<T> implements Tape<T> { * @return The removed tape. */ public Tape<T> removeTape() { - if(currentTape == null) return null; + if (currentTape == null) + return null; Tape<T> tp = tapes.remove(); currentTape = tapes.item(); @@ -322,19 +335,6 @@ public class TapeChanger<T> implements Tape<T> { } @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("TapeChanger [tapes="); - builder.append(tapes); - builder.append(", currentTape="); - builder.append(currentTape); - builder.append("]"); - - return builder.toString(); - } - - @Override public int hashCode() { final int prime = 31; int result = 1; @@ -345,20 +345,32 @@ public class TapeChanger<T> implements Tape<T> { @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 TapeChanger<?>)) + return false; + TapeChanger<?> other = (TapeChanger<?>) obj; - - if(currentTape == null) { - if(other.currentTape != null) return false; - } else if(!currentTape.equals(other.currentTape)) return false; - - if(tapes == null) { - if(other.tapes != null) return false; - } else if(!tapes.equals(other.tapes)) return false; - + + if (currentTape == null) { + if (other.currentTape != null) + return false; + } else if (!currentTape.equals(other.currentTape)) + return false; + + if (tapes == null) { + if (other.tapes != null) + return false; + } else if (!tapes.equals(other.tapes)) + return false; + return true; } + + @Override + public String toString() { + return String.format("TapeChanger [tapes=%s, currentTape='%s']", tapes, currentTape); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeLibrary.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeLibrary.java index 0bae303..85bea49 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeLibrary.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/TapeLibrary.java @@ -33,7 +33,8 @@ public class TapeLibrary<T> implements Tape<T> { */ @Override public T item() { - if(currentTape == null) return null; + if (currentTape == null) + return null; return currentTape.item(); } @@ -46,7 +47,8 @@ public class TapeLibrary<T> implements Tape<T> { */ @Override public void item(T itm) { - if(currentTape == null) return; + if (currentTape == null) + return; currentTape.item(itm); } @@ -58,7 +60,8 @@ public class TapeLibrary<T> implements Tape<T> { */ @Override public int size() { - if(currentTape == null) return 0; + if (currentTape == null) + return 0; return currentTape.size(); } @@ -71,7 +74,8 @@ public class TapeLibrary<T> implements Tape<T> { */ @Override public void insertBefore(T itm) { - if(currentTape == null) return; + if (currentTape == null) + return; currentTape.insertBefore(itm); } @@ -81,7 +85,8 @@ public class TapeLibrary<T> implements Tape<T> { */ @Override public void insertAfter(T itm) { - if(currentTape == null) return; + if (currentTape == null) + return; currentTape.insertAfter(itm); } @@ -96,7 +101,8 @@ public class TapeLibrary<T> implements Tape<T> { */ @Override public T remove() { - if(currentTape == null) return null; + if (currentTape == null) + return null; return currentTape.remove(); } @@ -106,7 +112,8 @@ public class TapeLibrary<T> implements Tape<T> { */ @Override public void first() { - if(currentTape == null) return; + if (currentTape == null) + return; currentTape.first(); } @@ -116,7 +123,8 @@ public class TapeLibrary<T> implements Tape<T> { */ @Override public void last() { - if(currentTape == null) return; + if (currentTape == null) + return; currentTape.last(); } @@ -146,7 +154,8 @@ public class TapeLibrary<T> implements Tape<T> { */ @Override public boolean left(int amt) { - if(currentTape == null) return false; + if (currentTape == null) + return false; return currentTape.left(amt); } @@ -175,7 +184,8 @@ public class TapeLibrary<T> implements Tape<T> { */ @Override public boolean right(int amt) { - if(currentTape == null) return false; + if (currentTape == null) + return false; return currentTape.right(amt); } @@ -189,16 +199,18 @@ public class TapeLibrary<T> implements Tape<T> { * If the current tape is not double-sided, does nothing. */ public void flip() { - if(currentTape == null) return; + if (currentTape == null) + return; - if(currentTape.isDoubleSided()) { + if (currentTape.isDoubleSided()) { ((DoubleTape<T>) currentTape).flip(); } } @Override public boolean isDoubleSided() { - if(currentTape == null) return false; + if (currentTape == null) + return false; return currentTape.isDoubleSided(); } @@ -224,7 +236,7 @@ public class TapeLibrary<T> implements Tape<T> { * @return Whether or not the next tape was loaded. */ public boolean switchTape(String label) { - if(tapes.containsKey(label)) { + if (tapes.containsKey(label)) { currentTape = tapes.get(label); return true; } @@ -310,33 +322,32 @@ public class TapeLibrary<T> implements Tape<T> { @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 TapeLibrary<?>)) + return false; TapeLibrary<?> other = (TapeLibrary<?>) obj; - if(currentTape == null) { - if(other.currentTape != null) return false; - } else if(!currentTape.equals(other.currentTape)) return false; + if (currentTape == null) { + if (other.currentTape != null) + return false; + } else if (!currentTape.equals(other.currentTape)) + return false; - if(tapes == null) { - if(other.tapes != null) return false; - } else if(!tapes.equals(other.tapes)) return false; + if (tapes == null) { + if (other.tapes != null) + return false; + } else if (!tapes.equals(other.tapes)) + return false; return true; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("TapeLibrary [tapes="); - builder.append(tapes); - builder.append(", currentTape="); - builder.append(currentTape); - builder.append("]"); - - return builder.toString(); + return String.format("TapeLibrary [tapes=%s, currentTape='%s']", tapes, currentTape); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java index 61ec72b..a0d9096 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/UnifiedDirectory.java @@ -40,8 +40,11 @@ public class UnifiedDirectory<K, V> implements Directory<K, V> { @Override public Directory<K, V> putSubdirectory(K key, Directory<K, V> val) { - if(data.containsKey(key)) - throw new IllegalArgumentException("Key " + key + " is already used for data."); + if (data.containsKey(key)) { + String msg = String.format("Key %s is already used for data", key); + + throw new IllegalArgumentException(msg); + } return children.put(key, val); } @@ -58,8 +61,11 @@ public class UnifiedDirectory<K, V> implements Directory<K, V> { @Override public V putKey(K key, V val) { - if(children.containsKey(key)) - throw new IllegalArgumentException("Key " + key + " is already used for sub-directories."); + if (children.containsKey(key)) { + String msg = String.format("Key %s is already used for sub-directories.", key); + + throw new IllegalArgumentException(msg); + } return data.put(key, val); } @@ -75,33 +81,32 @@ public class UnifiedDirectory<K, V> implements Directory<K, V> { @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 UnifiedDirectory<?, ?>)) + return false; UnifiedDirectory<?, ?> other = (UnifiedDirectory<?, ?>) obj; - 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; - 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; return true; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); - - builder.append("UnifiedDirectory [children="); - builder.append(children); - builder.append(", data="); - builder.append(data); - builder.append("]"); - - return builder.toString(); + return String.format("UnifiedDirectory [children=%s, data=%s]", children, data); } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java index 49382bc..caa487c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ExtendedMap.java @@ -23,7 +23,8 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { @Override public boolean containsKey(KeyType key) { - if(store.containsKey(key)) return true; + if (store.containsKey(key)) + return true; return delegate.containsKey(key); } @@ -56,7 +57,8 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { @Override public ValueType get(KeyType key) { - if(store.containsKey(key)) return store.get(key); + if (store.containsKey(key)) + return store.get(key); return delegate.get(key); } @@ -83,8 +85,9 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { @Override public ValueType remove(KeyType key) { - if(!store.containsKey(key)) return delegate.remove(key); - + if (!store.containsKey(key)) + return delegate.remove(key); + return store.remove(key); } @@ -92,4 +95,43 @@ class ExtendedMap<KeyType, ValueType> implements IMap<KeyType, ValueType> { public IList<ValueType> valueList() { return ListUtils.mergeLists(store.valueList(), delegate.valueList()); } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((delegate == null) ? 0 : delegate.hashCode()); + result = prime * result + ((store == null) ? 0 : store.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof ExtendedMap)) + return false; + + ExtendedMap<?, ?> other = (ExtendedMap<?, ?>) obj; + + if (delegate == null) { + if (other.delegate != null) + return false; + } else if (!delegate.equals(other.delegate)) + return false; + if (store == null) { + if (other.store != null) + return false; + } else if (!store.equals(other.store)) + return false; + + return true; + } + + @Override + public String toString() { + return String.format("ExtendedMap [delegate=%s, store=%s]", delegate, store); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java index 1e52d05..7627bdf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java @@ -54,7 +54,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { public FunctionalList(E... items) { wrapped = new ArrayList<>(items.length); - for(E item : items) { + for (E item : items) { wrapped.add(item); } } @@ -78,7 +78,8 @@ public class FunctionalList<E> implements Cloneable, IList<E> { * The list to use as a backing list. */ public FunctionalList(List<E> backing) { - if(backing == null) throw new NullPointerException("Backing list must be non-null"); + if (backing == null) + throw new NullPointerException("Backing list must be non-null"); wrapped = backing; } @@ -90,11 +91,12 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public boolean allMatch(Predicate<E> predicate) { - if(predicate == null) throw new NullPointerException("Predicate must be non-null"); + if (predicate == null) + throw new NullPointerException("Predicate must be non-null"); - for(E item : wrapped) { - if(!predicate.test(item)) // We've found a non-matching - // item + for (E item : wrapped) { + if (!predicate.test(item)) + // We've found a non-matching item return false; } @@ -104,10 +106,12 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public boolean anyMatch(Predicate<E> predicate) { - if(predicate == null) throw new NullPointerException("Predicate must be not null"); + if (predicate == null) + throw new NullPointerException("Predicate must be not null"); - for(E item : wrapped) { - if(predicate.test(item)) // We've found a matching item + for (E item : wrapped) { + if (predicate.test(item)) + // We've found a matching item return true; } @@ -126,7 +130,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { public IList<E> clone() { IList<E> cloned = new FunctionalList<>(); - for(E element : wrapped) { + for (E element : wrapped) { cloned.add(element); } @@ -135,16 +139,18 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public <T, F> IList<F> combineWith(IList<T> rightList, BiFunction<E, T, F> itemCombiner) { - if(rightList == null) + if (rightList == null) throw new NullPointerException("Target combine list must not be null"); - else if(itemCombiner == null) throw new NullPointerException("Combiner must not be null"); + else if (itemCombiner == null) + throw new NullPointerException("Combiner must not be null"); IList<F> returned = new FunctionalList<>(); // Get the iterator for the other list Iterator<T> rightIterator = rightList.toIterable().iterator(); - for(Iterator<E> leftIterator = wrapped.iterator(); leftIterator.hasNext() && rightIterator.hasNext();) { + for (Iterator<E> leftIterator = wrapped.iterator(); leftIterator.hasNext() + && rightIterator.hasNext();) { // Add the transformed items to the result list E leftVal = leftIterator.next(); T rightVal = rightIterator.next(); @@ -163,21 +169,24 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public E first() { - if(wrapped.size() < 1) throw new NoSuchElementException("Attempted to get first element of empty list"); + if (wrapped.size() < 1) + throw new NoSuchElementException("Attempted to get first element of empty list"); return wrapped.get(0); } @Override public <T> IList<T> flatMap(Function<E, IList<T>> expander) { - if(expander == null) throw new NullPointerException("Expander must not be null"); + if (expander == null) + throw new NullPointerException("Expander must not be null"); IList<T> returned = new FunctionalList<>(this.wrapped.size()); forEach(element -> { IList<T> expandedElement = expander.apply(element); - if(expandedElement == null) throw new NullPointerException("Expander returned null list"); + if (expandedElement == null) + throw new NullPointerException("Expander returned null list"); // Add each element to the returned list expandedElement.forEach(returned::add); @@ -188,14 +197,16 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public void forEach(Consumer<? super E> action) { - if(action == null) throw new NullPointerException("Action is null"); + if (action == null) + throw new NullPointerException("Action is null"); wrapped.forEach(action); } @Override public void forEachIndexed(BiConsumer<Integer, E> indexedAction) { - if(indexedAction == null) throw new NullPointerException("Action must not be null"); + if (indexedAction == null) + throw new NullPointerException("Action must not be null"); // This is held b/c ref'd variables must be final/effectively // final @@ -226,12 +237,13 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public IList<E> getMatching(Predicate<E> predicate) { - if(predicate == null) throw new NullPointerException("Predicate must not be null"); + if (predicate == null) + throw new NullPointerException("Predicate must not be null"); IList<E> returned = new FunctionalList<>(); wrapped.forEach((element) -> { - if(predicate.test(element)) { + if (predicate.test(element)) { // The item matches, so add it to the returned // list returned.add(element); @@ -260,7 +272,8 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public <T> IList<T> map(Function<E, T> elementTransformer) { - if(elementTransformer == null) throw new NullPointerException("Transformer must be not null"); + if (elementTransformer == null) + throw new NullPointerException("Transformer must be not null"); IList<T> returned = new FunctionalList<>(this.wrapped.size()); @@ -279,25 +292,28 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public IList<IList<E>> partition(int numberPerPartition) { - if(numberPerPartition < 1 || numberPerPartition > wrapped.size()) - throw new IllegalArgumentException("" + numberPerPartition - + " is an invalid partition size. Must be between 1 and " + wrapped.size()); + if (numberPerPartition < 1 || numberPerPartition > wrapped.size()) { + String fmt = "%s is an invalid partition size. Must be between 1 and %d"; + String msg = String.format(fmt, numberPerPartition, wrapped.size()); + + throw new IllegalArgumentException(msg); + } IList<IList<E>> returned = new FunctionalList<>(); // The current partition being filled IHolder<IList<E>> currentPartition = new Identity<>(new FunctionalList<>()); - this.forEach((element) -> { - if(isPartitionFull(numberPerPartition, currentPartition)) { + this.forEach(element -> { + if (isPartitionFull(numberPerPartition, currentPartition)) { // Add the partition to the list - returned.add(currentPartition.unwrap((partition) -> partition)); + returned.add(currentPartition.unwrap(partition -> partition)); // Start a new partition - currentPartition.transform((partition) -> new FunctionalList<>()); + currentPartition.transform(partition -> new FunctionalList<>()); } else { // Add the element to the current partition - currentPartition.unwrap((partition) -> partition.add(element)); + currentPartition.unwrap(partition -> partition.add(element)); } }); @@ -311,7 +327,8 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public E randItem(Function<Integer, Integer> rnd) { - if(rnd == null) throw new NullPointerException("Random source must not be null"); + if (rnd == null) + throw new NullPointerException("Random source must not be null"); int randomIndex = rnd.apply(wrapped.size()); @@ -321,16 +338,17 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public <T, F> F reduceAux(T initialValue, BiFunction<E, T, T> stateAccumulator, Function<T, F> resultTransformer) { - if(stateAccumulator == null) + if (stateAccumulator == null) throw new NullPointerException("Accumulator must not be null"); - else if(resultTransformer == null) throw new NullPointerException("Transformer must not be null"); + else if (resultTransformer == null) + throw new NullPointerException("Transformer must not be null"); // The current collapsed list IHolder<T> currentState = new Identity<>(initialValue); wrapped.forEach(element -> { // Accumulate a new value into the state - currentState.transform((state) -> stateAccumulator.apply(element, state)); + currentState.transform(state -> stateAccumulator.apply(element, state)); }); // Convert the state to its final value @@ -339,14 +357,15 @@ public class FunctionalList<E> implements Cloneable, IList<E> { @Override public boolean removeIf(Predicate<E> removePredicate) { - if(removePredicate == null) throw new NullPointerException("Predicate must be non-null"); + if (removePredicate == null) + throw new NullPointerException("Predicate must be non-null"); return wrapped.removeIf(removePredicate); } @Override public void removeMatching(E desiredElement) { - removeIf((element) -> element.equals(desiredElement)); + removeIf(element -> element.equals(desiredElement)); } @Override @@ -359,7 +378,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { // Search our internal list int foundIndex = Collections.binarySearch(wrapped, searchKey, comparator); - if(foundIndex >= 0) // We found a matching element + if (foundIndex >= 0) // We found a matching element return wrapped.get(foundIndex); // We didn't find an element @@ -391,19 +410,21 @@ public class FunctionalList<E> implements Cloneable, IList<E> { public String toString() { int lSize = getSize(); - if(lSize == 0) return "()"; + if (lSize == 0) + return "()"; StringBuilder sb = new StringBuilder("("); Iterator<E> itr = toIterable().iterator(); E itm = itr.next(); int i = 0; - if(lSize == 1) return "(" + itm + ")"; + if (lSize == 1) + return "(" + itm + ")"; - for(E item : toIterable()) { + for (E item : toIterable()) { sb.append(item.toString()); - if(i < lSize - 1) { + if (i < lSize - 1) { sb.append(", "); } @@ -411,6 +432,7 @@ public class FunctionalList<E> implements Cloneable, IList<E> { } sb.append(")"); + return sb.toString(); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java index b4e5981..62c39af 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalMap.java @@ -38,7 +38,7 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp public FunctionalMap(IPair<KeyType, ValueType>... entries) { this(); - for(IPair<KeyType, ValueType> entry : entries) { + for (IPair<KeyType, ValueType> entry : entries) { entry.doWith((key, val) -> { wrappedMap.put(key, val); }); @@ -52,7 +52,8 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp * The map to wrap */ public FunctionalMap(Map<KeyType, ValueType> wrap) { - if(wrap == null) throw new NullPointerException("Map to wrap must not be null"); + if (wrap == null) + throw new NullPointerException("Map to wrap must not be null"); wrappedMap = wrap; } @@ -89,10 +90,14 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp @Override public ValueType get(KeyType key) { - if(key == null) throw new NullPointerException("Key must not be null"); + if (key == null) + throw new NullPointerException("Key must not be null"); - if(!wrappedMap.containsKey(key)) - throw new IllegalArgumentException("Key " + key + " is not present in the map"); + if (!wrappedMap.containsKey(key)) { + String msg = String.format("Key %s is not present in the map", key); + + throw new IllegalArgumentException(msg); + } return wrappedMap.get(key); } @@ -106,7 +111,7 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp public IList<KeyType> keyList() { FunctionalList<KeyType> keys = new FunctionalList<>(); - wrappedMap.keySet().forEach((key) -> { + wrappedMap.keySet().forEach(key -> { keys.add(key); }); @@ -115,14 +120,16 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp @Override public <MappedValue> IMap<KeyType, MappedValue> transform(Function<ValueType, MappedValue> transformer) { - if(transformer == null) throw new NullPointerException("Transformer must not be null"); + if (transformer == null) + throw new NullPointerException("Transformer must not be null"); return new TransformedValueMap<>(this, transformer); } @Override public ValueType put(KeyType key, ValueType val) { - if(key == null) throw new NullPointerException("Key must not be null"); + if (key == null) + throw new NullPointerException("Key must not be null"); return wrappedMap.put(key, val); } @@ -141,10 +148,37 @@ public class FunctionalMap<KeyType, ValueType> implements IMap<KeyType, ValueTyp public IList<ValueType> valueList() { FunctionalList<ValueType> values = new FunctionalList<>(); - wrappedMap.values().forEach((value) -> { + wrappedMap.values().forEach(value -> { values.add(value); }); return values; } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((wrappedMap == null) ? 0 : wrappedMap.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof FunctionalMap)) + return false; + + FunctionalMap<?, ?> other = (FunctionalMap<?, ?>) obj; + + if (wrappedMap == null) { + if (other.wrappedMap != null) + return false; + } else if (!wrappedMap.equals(other.wrappedMap)) + return false; + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java index b7e3f30..4723bcd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalStringTokenizer.java @@ -19,7 +19,8 @@ public class FunctionalStringTokenizer { * @return A new tokenizer that splits the provided string on spaces. */ public static FunctionalStringTokenizer fromString(String strang) { - if(strang == null) throw new NullPointerException("String to tokenize must be non-null"); + if (strang == null) + throw new NullPointerException("String to tokenize must be non-null"); return new FunctionalStringTokenizer(new StringTokenizer(strang, " ")); } @@ -36,7 +37,8 @@ public class FunctionalStringTokenizer { * The string to tokenize */ public FunctionalStringTokenizer(String inp) { - if(inp == null) throw new NullPointerException("String to tokenize must be non-null"); + if (inp == null) + throw new NullPointerException("String to tokenize must be non-null"); this.input = new StringTokenizer(inp); } @@ -51,9 +53,10 @@ public class FunctionalStringTokenizer { * The set of separating tokens to use for splitting */ public FunctionalStringTokenizer(String input, String seperators) { - if(input == null) + if (input == null) throw new NullPointerException("String to tokenize must not be null"); - else if(seperators == null) throw new NullPointerException("Tokens to split on must not be null"); + else if (seperators == null) + throw new NullPointerException("Tokens to split on must not be null"); this.input = new StringTokenizer(input, seperators); } @@ -65,7 +68,8 @@ public class FunctionalStringTokenizer { * The non-functional string tokenizer to wrap */ public FunctionalStringTokenizer(StringTokenizer toWrap) { - if(toWrap == null) throw new NullPointerException("Wrapped tokenizer must not be null"); + if (toWrap == null) + throw new NullPointerException("Wrapped tokenizer must not be null"); this.input = toWrap; } @@ -77,9 +81,10 @@ public class FunctionalStringTokenizer { * The action to execute for each token */ public void forEachToken(Consumer<String> action) { - if(action == null) throw new NullPointerException("Action must not be null"); + if (action == null) + throw new NullPointerException("Action must not be null"); - while(input.hasMoreTokens()) { + while (input.hasMoreTokens()) { action.accept(input.nextToken()); } } @@ -110,7 +115,7 @@ public class FunctionalStringTokenizer { * @return The next token from the tokenizer */ public String nextToken() { - if(input.hasMoreTokens()) // Return the next available token + if (input.hasMoreTokens()) // Return the next available token return input.nextToken(); // Return no token @@ -138,7 +143,8 @@ public class FunctionalStringTokenizer { * @return A list containing all of the converted tokens. */ public <E> IList<E> toList(Function<String, E> transformer) { - if(transformer == null) throw new NullPointerException("Transformer must not be null"); + if (transformer == null) + throw new NullPointerException("Transformer must not be null"); IList<E> returned = new FunctionalList<>(); @@ -151,4 +157,9 @@ public class FunctionalStringTokenizer { return returned; } + + @Override + public String toString() { + return String.format("FunctionalStringTokenizer [input=%s]", input); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java index 95f4813..cf60ed6 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java @@ -38,7 +38,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * otherwise */ default boolean addAll(IList<ContainedType> items) { - return items.map(this::add).anyMatch((bl) -> bl == false); + return items.map(this::add).anyMatch(bl -> bl == false); } /** @@ -77,7 +77,8 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { Collector<ContainedType, StateType, ReducedType> collector) { BiConsumer<StateType, ContainedType> accumulator = collector.accumulator(); - return reduceAux(collector.supplier().get(), (value, state) -> { + StateType initial = collector.supplier().get(); + return reduceAux(initial, (value, state) -> { accumulator.accept(state, value); return state; @@ -243,7 +244,7 @@ public interface IList<ContainedType> extends Iterable<ContainedType> { * @return A random item from the list */ default ContainedType randItem() { - return randItem((num) -> (int) (Math.random() * num)); + return randItem(num -> (int) (Math.random() * num)); } /** diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java index 6bb5de6..e58409e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IMap.java @@ -112,7 +112,7 @@ public interface IMap<KeyType, ValueType> { * Delete all the values in the map. */ default void clear() { - keyList().forEach((key) -> remove(key)); + keyList().forEach(key -> remove(key)); } /** diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java index 89ea96d..43bd4d0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TransformedValueMap.java @@ -54,7 +54,7 @@ final class TransformedValueMap<OldKey, OldValue, NewValue> implements IMap<OldK @Override public void forEachValue(Consumer<NewValue> action) { - backing.forEachValue((value) -> { + backing.forEachValue(value -> { action.accept(transformer.apply(value)); }); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java index 060b3f5..e85ae34 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTree.java @@ -39,7 +39,8 @@ public class BinarySearchTree<T> { * The thing to use for comparing elements */ public BinarySearchTree(Comparator<T> cmp) { - if(cmp == null) throw new NullPointerException("Comparator must not be null"); + if (cmp == null) + throw new NullPointerException("Comparator must not be null"); elementCount = 0; comparator = cmp; @@ -54,7 +55,7 @@ public class BinarySearchTree<T> { public void addNode(T element) { elementCount++; - if(root == null) { + if (root == null) { root = new BinarySearchTreeNode<>(element, null, null); } else { root.add(element, comparator); @@ -95,8 +96,8 @@ public class BinarySearchTree<T> { int pivotAdjustment = 0; // Add elements until there aren't any left - while(adjustedPivotInBounds(elements, pivot, pivotAdjustment)) { - if(root == null) { + while (adjustedPivotInBounds(elements, pivot, pivotAdjustment)) { + if (root == null) { // Create a new root element root = new BinarySearchTreeNode<>(elements.getByIndex(pivot), null, null); } else { @@ -112,9 +113,9 @@ public class BinarySearchTree<T> { } // Add any trailing unbalanced elements - if(pivot - pivotAdjustment >= 0) { + if (pivot - pivotAdjustment >= 0) { root.add(elements.getByIndex(pivot - pivotAdjustment), comparator); - } else if(pivot + pivotAdjustment < elements.getSize()) { + } else if (pivot + pivotAdjustment < elements.getSize()) { root.add(elements.getByIndex(pivot + pivotAdjustment), comparator); } } @@ -163,9 +164,10 @@ public class BinarySearchTree<T> { * The function to use until it fails */ public void traverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { - if(linearizationMethod == null) + if (linearizationMethod == null) throw new NullPointerException("Linearization method must not be null"); - else if(traversalPredicate == null) throw new NullPointerException("Predicate must not be nulls"); + else if (traversalPredicate == null) + throw new NullPointerException("Predicate must not be nulls"); root.forEach(linearizationMethod, traversalPredicate); } @@ -188,4 +190,40 @@ public class BinarySearchTree<T> { // Add the nodes to the tree in the order they were inserted nodes.forEach(node -> addNode(node)); } + + @Override + public String toString() { + return String.format("BinarySearchTree [elementCount=%s, root='%s']", elementCount, root); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + elementCount; + result = prime * result + ((root == null) ? 0 : root.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof BinarySearchTree<?>)) + return false; + + BinarySearchTree<?> other = (BinarySearchTree<?>) obj; + + if (elementCount != other.elementCount) + return false; + if (root == null) { + if (other.root != null) + return false; + } else if (!root.equals(other.root)) + return false; + + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java index 2696577..fe30dad 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeLeaf.java @@ -41,7 +41,8 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { @Override public <E> E collapse(Function<T, E> leafTransformer, BiFunction<E, E, E> branchCollapser) { - if(leafTransformer == null) throw new NullPointerException("Transformer must not be null"); + if (leafTransformer == null) + throw new NullPointerException("Transformer must not be null"); return leafTransformer.apply(data); } @@ -58,16 +59,17 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { @Override public void delete(T element, Comparator<T> comparator) { - if(data.equals(element)) { + if (data.equals(element)) { isDeleted = true; } } @Override public boolean directedWalk(DirectedWalkFunction<T> treeWalker) { - if(treeWalker == null) throw new NullPointerException("Tree walker must not be null"); + if (treeWalker == null) + throw new NullPointerException("Tree walker must not be null"); - switch(treeWalker.walk(data)) { + switch (treeWalker.walk(data)) { case SUCCESS: return true; // We don't have any children to care about @@ -81,8 +83,45 @@ public class BinarySearchTreeLeaf<T> implements ITreePart<T> { @Override public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { - if(traversalPredicate == null) throw new NullPointerException("Predicate must not be null"); + if (traversalPredicate == null) + throw new NullPointerException("Predicate must not be null"); return traversalPredicate.test(data); } + + @Override + public String toString() { + return String.format("BinarySearchTreeLeaf [data='%s', isDeleted=%s]", data, isDeleted); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((data == null) ? 0 : data.hashCode()); + result = prime * result + (isDeleted ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof BinarySearchTreeLeaf<?>)) + return false; + + BinarySearchTreeLeaf<?> other = (BinarySearchTreeLeaf<?>) obj; + + if (data == null) { + if (other.data != null) + return false; + } else if (!data.equals(other.data)) + return false; + if (isDeleted != other.isDeleted) + return false; + + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java index 4fe9de3..527f221 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/bst/BinarySearchTreeNode.java @@ -47,24 +47,25 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public void add(T element, Comparator<T> comparator) { - if(comparator == null) throw new NullPointerException("Comparator must not be null"); + if (comparator == null) + throw new NullPointerException("Comparator must not be null"); - switch(comparator.compare(data, element)) { + switch (comparator.compare(data, element)) { case -1: - if(left == null) { + if (left == null) { left = new BinarySearchTreeNode<>(element, null, null); } else { left.add(element, comparator); } break; case 0: - if(isDeleted) { + if (isDeleted) { isDeleted = false; } else throw new IllegalArgumentException("Can't add duplicate values"); break; case 1: - if(right == null) { + if (right == null) { right = new BinarySearchTreeNode<>(element, null, null); } else { right.add(element, comparator); @@ -77,15 +78,15 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public <E> E collapse(Function<T, E> nodeCollapser, BiFunction<E, E, E> branchCollapser) { - if(nodeCollapser == null || branchCollapser == null) + if (nodeCollapser == null || branchCollapser == null) throw new NullPointerException("Collapser must not be null"); E collapsedNode = nodeCollapser.apply(data); - if(left != null) { + if (left != null) { E collapsedLeftBranch = left.collapse(nodeCollapser, branchCollapser); - if(right != null) { + if (right != null) { E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser); E collapsedBranches = branchCollapser.apply(collapsedLeftBranch, collapsedRightBranch); @@ -96,7 +97,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { return branchCollapser.apply(collapsedNode, collapsedLeftBranch); } - if(right != null) { + if (right != null) { E collapsedRightBranch = right.collapse(nodeCollapser, branchCollapser); return branchCollapser.apply(collapsedNode, collapsedRightBranch); @@ -107,10 +108,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public boolean contains(T element, Comparator<T> comparator) { - if(comparator == null) throw new NullPointerException("Comparator must not be null"); + if (comparator == null) + throw new NullPointerException("Comparator must not be null"); return directedWalk(currentElement -> { - switch(comparator.compare(element, currentElement)) { + switch (comparator.compare(element, currentElement)) { case -1: return LEFT; case 0: @@ -125,10 +127,11 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public void delete(T element, Comparator<T> comparator) { - if(comparator == null) throw new NullPointerException("Comparator must not be null"); + if (comparator == null) + throw new NullPointerException("Comparator must not be null"); directedWalk(currentElement -> { - switch(comparator.compare(data, element)) { + switch (comparator.compare(data, element)) { case -1: return left == null ? FAILURE : LEFT; case 0: @@ -144,9 +147,10 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public boolean directedWalk(DirectedWalkFunction<T> treeWalker) { - if(treeWalker == null) throw new NullPointerException("Walker must not be null"); + if (treeWalker == null) + throw new NullPointerException("Walker must not be null"); - switch(treeWalker.walk(data)) { + switch (treeWalker.walk(data)) { case SUCCESS: return true; case LEFT: @@ -162,11 +166,12 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { @Override public boolean forEach(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { - if(linearizationMethod == null) + if (linearizationMethod == null) throw new NullPointerException("Linearization method must not be null"); - else if(traversalPredicate == null) throw new NullPointerException("Predicate must not be null"); + else if (traversalPredicate == null) + throw new NullPointerException("Predicate must not be null"); - switch(linearizationMethod) { + switch (linearizationMethod) { case PREORDER: return preorderTraverse(linearizationMethod, traversalPredicate); case INORDER: @@ -180,33 +185,42 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { } private boolean inorderTraverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { - if(!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false; + if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) + return false; - if(!traverseElement(traversalPredicate)) return false; + if (!traverseElement(traversalPredicate)) + return false; - if(!traverseRightBranch(linearizationMethod, traversalPredicate)) return false; + if (!traverseRightBranch(linearizationMethod, traversalPredicate)) + return false; return true; } private boolean postorderTraverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { - if(!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false; + if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) + return false; - if(!traverseRightBranch(linearizationMethod, traversalPredicate)) return false; + if (!traverseRightBranch(linearizationMethod, traversalPredicate)) + return false; - if(!traverseElement(traversalPredicate)) return false; + if (!traverseElement(traversalPredicate)) + return false; return true; } private boolean preorderTraverse(TreeLinearizationMethod linearizationMethod, Predicate<T> traversalPredicate) { - if(!traverseElement(traversalPredicate)) return false; + if (!traverseElement(traversalPredicate)) + return false; - if(!traverseLeftBranch(linearizationMethod, traversalPredicate)) return false; + if (!traverseLeftBranch(linearizationMethod, traversalPredicate)) + return false; - if(!traverseRightBranch(linearizationMethod, traversalPredicate)) return false; + if (!traverseRightBranch(linearizationMethod, traversalPredicate)) + return false; return true; } @@ -214,7 +228,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { private boolean traverseElement(Predicate<T> traversalPredicate) { boolean nodeSuccesfullyTraversed; - if(isDeleted) { + if (isDeleted) { nodeSuccesfullyTraversed = true; } else { nodeSuccesfullyTraversed = traversalPredicate.test(data); @@ -227,7 +241,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { Predicate<T> traversalPredicate) { boolean leftSuccesfullyTraversed; - if(left == null) { + if (left == null) { leftSuccesfullyTraversed = true; } else { leftSuccesfullyTraversed = left.forEach(linearizationMethod, traversalPredicate); @@ -240,7 +254,7 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { Predicate<T> traversalPredicate) { boolean rightSuccesfullyTraversed; - if(right == null) { + if (right == null) { rightSuccesfullyTraversed = true; } else { rightSuccesfullyTraversed = right.forEach(linearizationMethod, traversalPredicate); @@ -248,4 +262,44 @@ public class BinarySearchTreeNode<T> extends BinarySearchTreeLeaf<T> { return rightSuccesfullyTraversed; } + + @Override + public String toString() { + return String.format("BinarySearchTreeNode [left='%s', right='%s']", left, right); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((left == null) ? 0 : left.hashCode()); + result = prime * result + ((right == null) ? 0 : right.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (!(obj instanceof BinarySearchTreeNode<?>)) + return false; + + BinarySearchTreeNode<?> other = (BinarySearchTreeNode<?>) obj; + + if (left == null) { + if (other.left != null) + return false; + } else if (!left.equals(other.left)) + return false; + + if (right == null) { + if (other.right != null) + return false; + } else if (!right.equals(other.right)) + return false; + + return true; + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java index 7e7b003..ef259d5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/CompoundCollector.java @@ -35,7 +35,7 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final BiConsumer<AuxType2, InitialType> secondAccumulator = second.accumulator(); return (state, value) -> { - state.doWith((statePair) -> { + state.doWith(statePair -> { statePair.doWith((left, right) -> { firstAccumulator.accept(left, value); secondAccumulator.accept(right, value); @@ -55,8 +55,8 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final BinaryOperator<AuxType2> secondCombiner = second.combiner(); return (leftState, rightState) -> { - return leftState.unwrap((leftPair) -> { - return rightState.transform((rightPair) -> { + return leftState.unwrap(leftPair -> { + return rightState.transform(rightPair -> { return leftPair.combine(rightPair, firstCombiner, secondCombiner); }); }); @@ -65,10 +65,13 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final @Override public Function<IHolder<IPair<AuxType1, AuxType2>>, IPair<FinalType1, FinalType2>> finisher() { - return (state) -> { - return state.unwrap((pair) -> { + return state -> { + return state.unwrap(pair -> { return pair.bind((left, right) -> { - return new Pair<>(first.finisher().apply(left), second.finisher().apply(right)); + FinalType1 finalLeft = first.finisher().apply(left); + FinalType2 finalRight = second.finisher().apply(right); + + return new Pair<>(finalLeft, finalRight); }); }); }; @@ -77,7 +80,10 @@ final class CompoundCollector<InitialType, AuxType1, AuxType2, FinalType1, Final @Override public Supplier<IHolder<IPair<AuxType1, AuxType2>>> supplier() { return () -> { - return new Identity<>(new Pair<>(first.supplier().get(), second.supplier().get())); + AuxType1 initialLeft = first.supplier().get(); + AuxType2 initialRight = second.supplier().get(); + + return new Identity<>(new Pair<>(initialLeft, initialRight)); }; } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java index cb15d40..4c0abaf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FuncUtils.java @@ -27,7 +27,7 @@ public class FuncUtils { * function */ public static <A, B, C> Function<A, Function<B, C>> curry2(BiFunction<A, B, C> func) { - return (arg1) -> (arg2) -> { + return arg1 -> arg2 -> { return func.apply(arg1, arg2); }; } @@ -41,7 +41,7 @@ public class FuncUtils { * The action to perform */ public static void doTimes(int nTimes, Consumer<Integer> cons) { - for(int i = 0; i < nTimes; i++) { + for (int i = 0; i < nTimes; i++) { cons.accept(i); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java index c806d50..8c3e1eb 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/FunctionalFileVisitor.java @@ -19,14 +19,16 @@ final class FunctionalFileVisitor extends SimpleFileVisitor<Path> { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - if(predicate.test(dir, attrs)) return FileVisitResult.CONTINUE; + if (predicate.test(dir, attrs)) + return FileVisitResult.CONTINUE; return FileVisitResult.SKIP_SUBTREE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if(action.test(file, attrs)) return FileVisitResult.CONTINUE; + if (action.test(file, attrs)) + return FileVisitResult.CONTINUE; return FileVisitResult.TERMINATE; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java index f736dcb..a65f83a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/GroupPartIteration.java @@ -40,7 +40,7 @@ final class GroupPartIteration<E> implements Consumer<E> { public void accept(E value) { boolean shouldStartPartition = numberInCurrentPartition >= numberPerPartition; - if(shouldStartPartition) { + if (shouldStartPartition) { returnedList.add(currentPartition); currentPartition = new FunctionalList<>(); @@ -50,7 +50,7 @@ final class GroupPartIteration<E> implements Consumer<E> { boolean shouldReject = numberInCurrentPartition + currentElementCount >= numberPerPartition; - if(shouldReject) { + if (shouldReject) { rejectedItems.add(value); } else { currentPartition.add(value); diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index 791598f..27666dd 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -29,7 +29,8 @@ public class ListUtils { * @return The collapsed string of tokens */ public static String collapseTokens(IList<String> input) { - if(input == null) throw new NullPointerException("Input must not be null"); + if (input == null) + throw new NullPointerException("Input must not be null"); return collapseTokens(input, ""); } @@ -45,22 +46,23 @@ public class ListUtils { * @return The collapsed string of tokens */ public static String collapseTokens(IList<String> input, String seperator) { - if(input == null) + if (input == null) throw new NullPointerException("Input must not be null"); - else if(seperator == null) throw new NullPointerException("Seperator must not be null"); + else if (seperator == null) + throw new NullPointerException("Seperator must not be null"); - if(input.getSize() < 1) + if (input.getSize() < 1) return ""; - else if(input.getSize() == 1) + else if (input.getSize() == 1) return input.first(); else { StringBuilder state = new StringBuilder(); int i = 1; - for(String itm : input.toIterable()) { + for (String itm : input.toIterable()) { state.append(itm); - if(i != input.getSize()) { + if (i != input.getSize()) { state.append(seperator); } @@ -111,15 +113,15 @@ public class ListUtils { Iterator<E> itr = list.toIterable().iterator(); E element = null; - for(int index = 0; itr.hasNext(); element = itr.next()) { - int winningChance = number - selected.getSize(); + for (int index = 0; itr.hasNext(); element = itr.next()) { // n - m + int winningChance = number - selected.getSize(); - int totalChance = total - (index - 1); // N - t + int totalChance = total - (index - 1); // Probability of selecting the t+1'th element - if(NumberUtils.isProbable(winningChance, totalChance, rng)) { + if (NumberUtils.isProbable(winningChance, totalChance, rng)) { selected.add(element); } } @@ -145,7 +147,7 @@ public class ListUtils { public static <E> IList<E> drawWithReplacement(IList<E> list, int number, Function<Integer, Integer> rng) { IList<E> selected = new FunctionalList<>(new ArrayList<>(number)); - for(int i = 0; i < number; i++) { + for (int i = 0; i < number; i++) { selected.add(list.randItem(rng)); } @@ -171,13 +173,16 @@ public class ListUtils { */ public static <E> IList<IList<E>> groupPartition(IList<E> input, Function<E, Integer> counter, int partitionSize) { - if(input == null) + if (input == null) throw new NullPointerException("Input list must not be null"); - else if(counter == null) + else if (counter == null) throw new NullPointerException("Counter must not be null"); - else if(partitionSize < 1 || partitionSize > input.getSize()) - throw new IllegalArgumentException("" + partitionSize + " is not a valid" - + " partition size. Must be between 1 and " + input.getSize()); + else if (partitionSize < 1 || partitionSize > input.getSize()) { + String fmt = "%d is not a valid partition size. Must be between 1 and %d"; + String msg = String.format(fmt, partitionSize, input.getSize()); + + throw new IllegalArgumentException(msg); + } /* * List that holds our results @@ -194,11 +199,12 @@ public class ListUtils { /* * Run up to a certain number of passes */ - for(int numberOfIterations = 0; numberOfIterations < MAX_NTRIESPART + for (int numberOfIterations = 0; numberOfIterations < MAX_NTRIESPART && !rejected.isEmpty(); numberOfIterations++) { input.forEach(it); - if(rejected.isEmpty()) // Nothing was rejected, so we're + if (rejected.isEmpty()) // Nothing was rejected, so + // we're // done return returned; } @@ -223,8 +229,8 @@ public class ListUtils { public static <E> IList<E> mergeLists(IList<E>... lists) { IList<E> returned = new FunctionalList<>(); - for(IList<E> list : lists) { - for(E itm : list.toIterable()) { + for (IList<E> list : lists) { + for (E itm : list.toIterable()) { returned.add(itm); } } @@ -254,22 +260,22 @@ public class ListUtils { IList<E> returned = new FunctionalList<>(); - for(E itm : list.toIterable()) { + for (E itm : list.toIterable()) { count += counter.apply(itm); returned.add(itm); } - if(count % size != 0) { + if (count % size != 0) { // We need to pad int needed = count % size; int threshold = 0; - while(needed > 0 && threshold <= MAX_NTRIESPART) { + while (needed > 0 && threshold <= MAX_NTRIESPART) { E val = padder.get(); int newCount = counter.apply(val); - if(newCount <= needed) { + if (newCount <= needed) { returned.add(val); threshold = 0; @@ -280,7 +286,7 @@ public class ListUtils { } } - if(threshold > MAX_NTRIESPART) + if (threshold > MAX_NTRIESPART) throw new IllegalArgumentException("Heuristic (more than " + MAX_NTRIESPART + " iterations of attempting to pad) detected unpaddable list "); } |
