diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-04-10 16:40:33 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-04-10 16:40:33 -0400 |
| commit | 889fac2bdf993dc86f64a8893c0260fdcf848acb (patch) | |
| tree | 99ed08552efa86fdc5fdf4ddb8720d10e599fafe /BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java | |
| parent | 1656b02144446aeedebb3d1179e07ed99c01861c (diff) | |
Cleanup
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java | 105 |
1 files changed, 43 insertions, 62 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java index 70768be..5cb85f3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java @@ -1,12 +1,12 @@ package bjc.utils.data; -import bjc.utils.data.internals.BoundLazyPair; -import bjc.utils.data.internals.HalfBoundLazyPair; - import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; +import bjc.utils.data.internals.BoundLazyPair; +import bjc.utils.data.internals.HalfBoundLazyPair; + /** * A lazy implementation of a pair * @@ -36,7 +36,7 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> * @param rightVal * The value for the right side of the pair */ - public LazyPair(LeftType leftVal, RightType rightVal) { + public LazyPair(final LeftType leftVal, final RightType rightVal) { leftValue = leftVal; rightValue = rightVal; @@ -52,7 +52,7 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> * @param rightSupp * The source for a value on the right side of the pair */ - public LazyPair(Supplier<LeftType> leftSupp, Supplier<RightType> rightSupp) { + public LazyPair(final Supplier<LeftType> leftSupp, final Supplier<RightType> rightSupp) { // Use single suppliers to catch double-instantiation bugs leftSupplier = new SingleSupplier<>(leftSupp); rightSupplier = new SingleSupplier<>(rightSupp); @@ -63,16 +63,15 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( - BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) { + final BiFunction<LeftType, RightType, IPair<BoundLeft, BoundRight>> binder) { return new BoundLazyPair<>(leftSupplier, rightSupplier, binder); } @Override public <BoundLeft> IPair<BoundLeft, RightType> bindLeft( - Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { - Supplier<LeftType> leftSupp = () -> { - if (leftMaterialized) - return leftValue; + final Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) { + final Supplier<LeftType> leftSupp = () -> { + if (leftMaterialized) return leftValue; return leftSupplier.get(); }; @@ -82,10 +81,9 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public <BoundRight> IPair<LeftType, BoundRight> bindRight( - Function<RightType, IPair<LeftType, BoundRight>> rightBinder) { - Supplier<RightType> rightSupp = () -> { - if (rightMaterialized) - return rightValue; + final Function<RightType, IPair<LeftType, BoundRight>> rightBinder) { + final Supplier<RightType> rightSupp = () -> { + if (rightMaterialized) return rightValue; return rightSupplier.get(); }; @@ -95,13 +93,13 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> @Override public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - IPair<OtherLeft, OtherRight> otherPair, - BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, - BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { + final IPair<OtherLeft, OtherRight> otherPair, + final BiFunction<LeftType, OtherLeft, CombinedLeft> leftCombiner, + final BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) { return otherPair.bind((otherLeft, otherRight) -> { return bind((leftVal, rightVal) -> { - CombinedLeft left = leftCombiner.apply(leftVal, otherLeft); - CombinedRight right = rightCombiner.apply(rightVal, otherRight); + final CombinedLeft left = leftCombiner.apply(leftVal, otherLeft); + final CombinedRight right = rightCombiner.apply(rightVal, otherRight); return new LazyPair<>(left, right); }); @@ -131,17 +129,15 @@ public class LazyPair<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); + public <NewLeft> IPair<NewLeft, RightType> mapLeft(final Function<LeftType, NewLeft> mapper) { + final Supplier<NewLeft> leftSupp = () -> { + if (leftMaterialized) return mapper.apply(leftValue); return mapper.apply(leftSupplier.get()); }; - Supplier<RightType> rightSupp = () -> { - if (rightMaterialized) - return rightValue; + final Supplier<RightType> rightSupp = () -> { + if (rightMaterialized) return rightValue; return rightSupplier.get(); }; @@ -150,17 +146,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; + public <NewRight> IPair<LeftType, NewRight> mapRight(final Function<RightType, NewRight> mapper) { + final Supplier<LeftType> leftSupp = () -> { + if (leftMaterialized) return leftValue; return leftSupplier.get(); }; - Supplier<NewRight> rightSupp = () -> { - if (rightMaterialized) - return mapper.apply(rightValue); + final Supplier<NewRight> rightSupp = () -> { + if (rightMaterialized) return mapper.apply(rightValue); return mapper.apply(rightSupplier.get()); }; @@ -169,7 +163,7 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> } @Override - public <MergedType> MergedType merge(BiFunction<LeftType, RightType, MergedType> merger) { + public <MergedType> MergedType merge(final BiFunction<LeftType, RightType, MergedType> merger) { if (!leftMaterialized) { leftValue = leftSupplier.get(); @@ -211,48 +205,35 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> int result = 1; result = prime * result + (leftMaterialized ? 1231 : 1237); - result = prime * result + ((leftValue == null) ? 0 : leftValue.hashCode()); + result = prime * result + (leftValue == null ? 0 : leftValue.hashCode()); result = prime * result + (rightMaterialized ? 1231 : 1237); - result = prime * result + ((rightValue == null) ? 0 : rightValue.hashCode()); + result = prime * result + (rightValue == null ? 0 : rightValue.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof LazyPair<?, ?>)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof LazyPair<?, ?>)) return false; - LazyPair<?, ?> other = (LazyPair<?, ?>) obj; + final LazyPair<?, ?> other = (LazyPair<?, ?>) obj; - if (leftMaterialized != other.leftMaterialized) - return false; + if (leftMaterialized != other.leftMaterialized) return false; if (leftMaterialized) { if (leftValue == null) { - if (other.leftValue != null) - return false; - } else if (!leftValue.equals(other.leftValue)) - return false; - } else { - return false; - } + if (other.leftValue != null) return false; + } else if (!leftValue.equals(other.leftValue)) return false; + } else return false; - if (rightMaterialized != other.rightMaterialized) - return false; + if (rightMaterialized != other.rightMaterialized) return false; if (rightMaterialized) { if (rightValue == null) { - if (other.rightValue != null) - return false; - } else if (!rightValue.equals(other.rightValue)) - return false; - } else { - return false; - } + if (other.rightValue != null) return false; + } else if (!rightValue.equals(other.rightValue)) return false; + } else return false; return true; } |
