From f51f6da7319787348c38b875652b5c0e9f88c8aa Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Mon, 13 Apr 2020 18:43:13 -0400 Subject: Cleanup pass Pass to do some cleanups --- src/main/java/bjc/data/LazyPair.java | 117 ++++++++++++++++++++--------------- 1 file changed, 68 insertions(+), 49 deletions(-) (limited to 'src/main/java/bjc/data/LazyPair.java') diff --git a/src/main/java/bjc/data/LazyPair.java b/src/main/java/bjc/data/LazyPair.java index 1633c65..e668cd4 100644 --- a/src/main/java/bjc/data/LazyPair.java +++ b/src/main/java/bjc/data/LazyPair.java @@ -13,10 +13,10 @@ import bjc.data.internals.HalfBoundLazyPair; * @author ben * * @param - * The type on the left side of the pair. + * The type on the left side of the pair. * * @param - * The type on the right side of the pair. + * The type on the right side of the pair. */ public class LazyPair implements IPair { /* The supplier for the left value. */ @@ -37,10 +37,10 @@ public class LazyPair implements IPair * Create a new lazy pair, using the set values. * * @param leftVal - * The value for the left side of the pair. + * The value for the left side of the pair. * * @param rightVal - * The value for the right side of the pair. + * The value for the right side of the pair. */ public LazyPair(final LeftType leftVal, final RightType rightVal) { leftValue = leftVal; @@ -54,12 +54,13 @@ public class LazyPair implements IPair * Create a new lazy pair from the given value sources. * * @param leftSupp - * The source for a value on the left side of the pair. + * The source for a value on the left side of the pair. * * @param rightSupp - * The source for a value on the right side of the pair. + * The source for a value on the right side of the pair. */ - public LazyPair(final Supplier leftSupp, final Supplier rightSupp) { + public LazyPair(final Supplier leftSupp, + final Supplier rightSupp) { /* Use single suppliers to catch double-instantiation bugs. */ leftSupplier = new SingleSupplier<>(leftSupp); rightSupplier = new SingleSupplier<>(rightSupp); @@ -75,10 +76,11 @@ public class LazyPair implements IPair } @Override - public IPair bindLeft( - final Function> leftBinder) { + public IPair + bindLeft(final Function> leftBinder) { final Supplier leftSupp = () -> { - if(leftMaterialized) return leftValue; + if (leftMaterialized) + return leftValue; return leftSupplier.get(); }; @@ -90,7 +92,8 @@ public class LazyPair implements IPair public IPair bindRight( final Function> rightBinder) { final Supplier rightSupp = () -> { - if(rightMaterialized) return rightValue; + if (rightMaterialized) + return rightValue; return rightSupplier.get(); }; @@ -99,23 +102,23 @@ public class LazyPair implements IPair } @Override - public IPair combine( - final IPair otherPair, - final BiFunction leftCombiner, - final BiFunction rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - final CombinedLeft left = leftCombiner.apply(leftVal, otherLeft); - final CombinedRight right = rightCombiner.apply(rightVal, otherRight); - - return new LazyPair<>(left, right); - }); - }); + public + IPair + combine(final IPair otherPair, + final BiFunction leftCombiner, + final BiFunction rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> bind((leftVal, rightVal) -> { + final CombinedLeft left = leftCombiner.apply(leftVal, otherLeft); + final CombinedRight right = rightCombiner.apply(rightVal, otherRight); + + return new LazyPair<>(left, right); + })); } @Override public LeftType getLeft() { - if(!leftMaterialized) { + if (!leftMaterialized) { leftValue = leftSupplier.get(); leftMaterialized = true; @@ -126,7 +129,7 @@ public class LazyPair implements IPair @Override public RightType getRight() { - if(!rightMaterialized) { + if (!rightMaterialized) { rightValue = rightSupplier.get(); rightMaterialized = true; @@ -136,15 +139,18 @@ public class LazyPair implements IPair } @Override - public IPair mapLeft(final Function mapper) { + public IPair + mapLeft(final Function mapper) { final Supplier leftSupp = () -> { - if(leftMaterialized) return mapper.apply(leftValue); + if (leftMaterialized) + return mapper.apply(leftValue); return mapper.apply(leftSupplier.get()); }; final Supplier rightSupp = () -> { - if(rightMaterialized) return rightValue; + if (rightMaterialized) + return rightValue; return rightSupplier.get(); }; @@ -153,15 +159,18 @@ public class LazyPair implements IPair } @Override - public IPair mapRight(final Function mapper) { + public IPair + mapRight(final Function mapper) { final Supplier leftSupp = () -> { - if(leftMaterialized) return leftValue; + if (leftMaterialized) + return leftValue; return leftSupplier.get(); }; final Supplier rightSupp = () -> { - if(rightMaterialized) return mapper.apply(rightValue); + if (rightMaterialized) + return mapper.apply(rightValue); return mapper.apply(rightSupplier.get()); }; @@ -170,14 +179,15 @@ public class LazyPair implements IPair } @Override - public MergedType merge(final BiFunction merger) { - if(!leftMaterialized) { + public MergedType + merge(final BiFunction merger) { + if (!leftMaterialized) { leftValue = leftSupplier.get(); leftMaterialized = true; } - if(!rightMaterialized) { + if (!rightMaterialized) { rightValue = rightSupplier.get(); rightMaterialized = true; @@ -191,13 +201,13 @@ public class LazyPair implements IPair String leftVal; String rightVal; - if(leftMaterialized) { + if (leftMaterialized) { leftVal = leftValue.toString(); } else { leftVal = "(un-materialized)"; } - if(rightMaterialized) { + if (rightMaterialized) { rightVal = rightValue.toString(); } else { rightVal = "(un-materialized)"; @@ -221,26 +231,35 @@ public class LazyPair implements IPair @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof LazyPair)) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof LazyPair)) + return false; final LazyPair other = (LazyPair) obj; - if(leftMaterialized != other.leftMaterialized) return false; + if (leftMaterialized != other.leftMaterialized) + return false; - if(leftMaterialized) { - if(leftValue == null) { - if(other.leftValue != null) return false; - } else if(!leftValue.equals(other.leftValue)) return false; + if (leftMaterialized) { + if (leftValue == null) { + if (other.leftValue != null) + return false; + } else if (!leftValue.equals(other.leftValue)) + return false; } else return false; - if(rightMaterialized != other.rightMaterialized) return false; - if(rightMaterialized) { - if(rightValue == null) { - if(other.rightValue != null) return false; - } else if(!rightValue.equals(other.rightValue)) return false; + if (rightMaterialized != other.rightMaterialized) + return false; + if (rightMaterialized) { + if (rightValue == null) { + if (other.rightValue != null) + return false; + } else if (!rightValue.equals(other.rightValue)) + return false; } else return false; -- cgit v1.2.3