summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/data/LazyPair.java
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc/utils/data/LazyPair.java')
-rw-r--r--base/src/main/java/bjc/utils/data/LazyPair.java88
1 files changed, 45 insertions, 43 deletions
diff --git a/base/src/main/java/bjc/utils/data/LazyPair.java b/base/src/main/java/bjc/utils/data/LazyPair.java
index 548a09e..0500486 100644
--- a/base/src/main/java/bjc/utils/data/LazyPair.java
+++ b/base/src/main/java/bjc/utils/data/LazyPair.java
@@ -13,40 +13,40 @@ import bjc.utils.data.internals.HalfBoundLazyPair;
* @author ben
*
* @param <LeftType>
- * The type on the left side of the pair.
+ * The type on the left side of the pair.
*
* @param <RightType>
- * The type on the right side of the pair.
+ * The type on the right side of the pair.
*/
public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType> {
/* The supplier for the left value. */
- private Supplier<LeftType> leftSupplier;
+ private Supplier<LeftType> leftSupplier;
/* The left value. */
private LeftType leftValue;
/* Whether the left value has been created. */
- private boolean leftMaterialized;
+ private boolean leftMaterialized;
/* The supplier for the right value. */
private Supplier<RightType> rightSupplier;
/* The right value. */
private RightType rightValue;
/* Whether the right value has been created. */
- private boolean rightMaterialized;
+ private boolean rightMaterialized;
/**
* 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;
+ leftValue = leftVal;
rightValue = rightVal;
- leftMaterialized = true;
+ leftMaterialized = true;
rightMaterialized = true;
}
@@ -54,17 +54,17 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
* 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<LeftType> leftSupp, final Supplier<RightType> rightSupp) {
/* Use single suppliers to catch double-instantiation bugs. */
- leftSupplier = new SingleSupplier<>(leftSupp);
+ leftSupplier = new SingleSupplier<>(leftSupp);
rightSupplier = new SingleSupplier<>(rightSupp);
- leftMaterialized = false;
+ leftMaterialized = false;
rightMaterialized = false;
}
@@ -78,7 +78,7 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
public <BoundLeft> IPair<BoundLeft, RightType> bindLeft(
final Function<LeftType, IPair<BoundLeft, RightType>> leftBinder) {
final Supplier<LeftType> leftSupp = () -> {
- if (leftMaterialized) return leftValue;
+ if(leftMaterialized) return leftValue;
return leftSupplier.get();
};
@@ -90,7 +90,7 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
public <BoundRight> IPair<LeftType, BoundRight> bindRight(
final Function<RightType, IPair<LeftType, BoundRight>> rightBinder) {
final Supplier<RightType> rightSupp = () -> {
- if (rightMaterialized) return rightValue;
+ if(rightMaterialized) return rightValue;
return rightSupplier.get();
};
@@ -105,7 +105,7 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
final BiFunction<RightType, OtherRight, CombinedRight> rightCombiner) {
return otherPair.bind((otherLeft, otherRight) -> {
return bind((leftVal, rightVal) -> {
- final CombinedLeft left = leftCombiner.apply(leftVal, otherLeft);
+ final CombinedLeft left = leftCombiner.apply(leftVal, otherLeft);
final CombinedRight right = rightCombiner.apply(rightVal, otherRight);
return new LazyPair<>(left, right);
@@ -115,7 +115,7 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
@Override
public LeftType getLeft() {
- if (!leftMaterialized) {
+ if(!leftMaterialized) {
leftValue = leftSupplier.get();
leftMaterialized = true;
@@ -126,7 +126,7 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
@Override
public RightType getRight() {
- if (!rightMaterialized) {
+ if(!rightMaterialized) {
rightValue = rightSupplier.get();
rightMaterialized = true;
@@ -138,13 +138,13 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
@Override
public <NewLeft> IPair<NewLeft, RightType> mapLeft(final Function<LeftType, NewLeft> mapper) {
final Supplier<NewLeft> leftSupp = () -> {
- if (leftMaterialized) return mapper.apply(leftValue);
+ if(leftMaterialized) return mapper.apply(leftValue);
return mapper.apply(leftSupplier.get());
};
final Supplier<RightType> rightSupp = () -> {
- if (rightMaterialized) return rightValue;
+ if(rightMaterialized) return rightValue;
return rightSupplier.get();
};
@@ -155,13 +155,13 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
@Override
public <NewRight> IPair<LeftType, NewRight> mapRight(final Function<RightType, NewRight> mapper) {
final Supplier<LeftType> leftSupp = () -> {
- if (leftMaterialized) return leftValue;
+ if(leftMaterialized) return leftValue;
return leftSupplier.get();
};
final Supplier<NewRight> rightSupp = () -> {
- if (rightMaterialized) return mapper.apply(rightValue);
+ if(rightMaterialized) return mapper.apply(rightValue);
return mapper.apply(rightSupplier.get());
};
@@ -171,13 +171,13 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
@Override
public <MergedType> MergedType merge(final BiFunction<LeftType, RightType, MergedType> merger) {
- if (!leftMaterialized) {
+ if(!leftMaterialized) {
leftValue = leftSupplier.get();
leftMaterialized = true;
}
- if (!rightMaterialized) {
+ if(!rightMaterialized) {
rightValue = rightSupplier.get();
rightMaterialized = true;
@@ -191,13 +191,13 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
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 +221,28 @@ public class LazyPair<LeftType, RightType> implements IPair<LeftType, RightType>
@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) {
- 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;
- } else 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(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;
return true;
}