diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-25 19:14:18 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-03-25 19:14:18 -0400 |
| commit | 9716b1ac09eb92c4ed001be4350d54b41b953239 (patch) | |
| tree | 606e56d2375e22464e956f89bb44af38cba008c7 /BJC-Utils2/src | |
| parent | 42990231fee502552b769b9af4c04ac0dcaeb195 (diff) | |
Add static constructors
Diffstat (limited to 'BJC-Utils2/src')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/Either.java | 4 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/Identity.java | 33 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java | 61 |
3 files changed, 72 insertions, 26 deletions
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 334beef..3ccc859 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java @@ -25,7 +25,7 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { * The value to put on the left * @return An either with the left side occupied */ - public static <LeftType, RightType> Either<LeftType, RightType> fromLeft(LeftType left) { + public static <LeftType, RightType> Either<LeftType, RightType> left(LeftType left) { return new Either<>(left, null); } @@ -40,7 +40,7 @@ public class Either<LeftType, RightType> implements IPair<LeftType, RightType> { * The value to put on the right * @return An either with the right side occupied */ - public static <LeftType, RightType> Either<LeftType, RightType> fromRight(RightType right) { + public static <LeftType, RightType> Either<LeftType, RightType> right(RightType right) { return new Either<>(null, right); } 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 c356e5a..72fe68c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Identity.java @@ -48,17 +48,17 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { */ @Override public boolean equals(Object obj) { - if(this == obj) + if (this == obj) return true; - else if(obj == null) + else if (obj == null) return false; - else if(getClass() != obj.getClass()) 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 (heldValue == null) { + if (other.heldValue != null) return false; + } else if (!heldValue.equals(other.heldValue)) return false; return true; } @@ -104,4 +104,25 @@ public class Identity<ContainedType> implements IHolder<ContainedType> { public <UnwrappedType> UnwrappedType unwrap(Function<ContainedType, UnwrappedType> unwrapper) { return unwrapper.apply(heldValue); } + + /** + * Create a new identity container. + * + * @param val + * The contained value. + * + * @return A new identity container. + */ + public static <ContainedType> Identity<ContainedType> id(ContainedType val) { + return new Identity<>(val); + } + + /** + * Create a new empty identity container. + * + * @return A new empty identity container. + */ + public static <ContainedType> Identity<ContainedType> id() { + return new Identity<>(); + } }
\ No newline at end of file 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 7cf16fd..2eb2cf3 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,7 @@ 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(); }; @@ -88,7 +88,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { return new Lazy<>(() -> { ContainedType currVal = heldValue; - if(!valueMaterialized) { + if (!valueMaterialized) { currVal = valueSupplier.get(); } @@ -99,8 +99,8 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { @Override public String toString() { - if(valueMaterialized) { - if(actions.isEmpty()) return "value[v='" + heldValue + "']"; + if (valueMaterialized) { + if (actions.isEmpty()) return "value[v='" + heldValue + "']"; return "value[v='" + heldValue + "'] (has pending transforms)"; } @@ -117,7 +117,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> { @Override public <UnwrappedType> UnwrappedType unwrap(Function<ContainedType, UnwrappedType> unwrapper) { - if(!valueMaterialized) { + if (!valueMaterialized) { heldValue = valueSupplier.get(); valueMaterialized = true; @@ -146,26 +146,51 @@ 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 (getClass() != obj.getClass()) return false; Lazy<?> other = (Lazy<?>) obj; - - 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 (valueMaterialized != other.valueMaterialized) return false; + + if (valueMaterialized) { + if (heldValue == null) { + 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 (actions == null) { + if (other.actions != null) return false; + } else if (actions.getSize() > 0 || other.actions.getSize() > 0) return false; return true; } + + /** + * Create a new lazy container with an already present value. + * + * @param val + * The value for the lazy container. + * + * @return A new lazy container holding that value. + */ + public static <ContainedType> Lazy<ContainedType> lazy(ContainedType val) { + return new Lazy<>(val); + } + + /** + * Create a new lazy container with a suspended value. + * + * @param supp + * The suspended value for the lazy container. + * + * @return A new lazy container that will un-suspend the value when + * necessary. + */ + public static <ContainedType> Lazy<ContainedType> lazy(Supplier<ContainedType> supp) { + return new Lazy<>(supp); + } } |
