From 63d88eb8db1f7a6d5924ec2a8b7f462373d5ac9a Mon Sep 17 00:00:00 2001 From: bjculkin Date: Fri, 7 Apr 2017 10:51:31 -0400 Subject: Cleanup --- .../src/main/java/bjc/utils/data/LazyPair.java | 90 +++++++++++++--------- 1 file changed, 52 insertions(+), 38 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/LazyPair.java') 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 implements IPair public IPair bindLeft( Function> leftBinder) { Supplier leftSupp = () -> { - if(leftMaterialized) return leftValue; + if (leftMaterialized) + return leftValue; return leftSupplier.get(); }; @@ -83,7 +84,8 @@ public class LazyPair implements IPair public IPair bindRight( Function> rightBinder) { Supplier rightSupp = () -> { - if(rightMaterialized) return rightValue; + if (rightMaterialized) + return rightValue; return rightSupplier.get(); }; @@ -98,15 +100,17 @@ public class LazyPair implements IPair BiFunction 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 implements IPair @Override public RightType getRight() { - if(!rightMaterialized) { + if (!rightMaterialized) { rightValue = rightSupplier.get(); rightMaterialized = true; @@ -129,13 +133,15 @@ public class LazyPair implements IPair @Override public IPair mapLeft(Function mapper) { Supplier leftSupp = () -> { - if(leftMaterialized) return mapper.apply(leftValue); + if (leftMaterialized) + return mapper.apply(leftValue); return mapper.apply(leftSupplier.get()); }; Supplier rightSupp = () -> { - if(rightMaterialized) return rightValue; + if (rightMaterialized) + return rightValue; return rightSupplier.get(); }; @@ -146,13 +152,15 @@ public class LazyPair implements IPair @Override public IPair mapRight(Function mapper) { Supplier leftSupp = () -> { - if(leftMaterialized) return leftValue; + if (leftMaterialized) + return leftValue; return leftSupplier.get(); }; Supplier 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 implements IPair @Override public MergedType merge(BiFunction 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 implements IPair @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 implements IPair @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; } -- cgit v1.2.3