From 9716b1ac09eb92c4ed001be4350d54b41b953239 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sat, 25 Mar 2017 19:14:18 -0400 Subject: Add static constructors --- .../src/main/java/bjc/utils/data/Either.java | 4 +- .../src/main/java/bjc/utils/data/Identity.java | 33 +++++++++--- 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 implements IPair { * The value to put on the left * @return An either with the left side occupied */ - public static Either fromLeft(LeftType left) { + public static Either left(LeftType left) { return new Either<>(left, null); } @@ -40,7 +40,7 @@ public class Either implements IPair { * The value to put on the right * @return An either with the right side occupied */ - public static Either fromRight(RightType right) { + public static Either 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 implements IHolder { */ @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 implements IHolder { public UnwrappedType unwrap(Function unwrapper) { return unwrapper.apply(heldValue); } + + /** + * Create a new identity container. + * + * @param val + * The contained value. + * + * @return A new identity container. + */ + public static Identity id(ContainedType val) { + return new Identity<>(val); + } + + /** + * Create a new empty identity container. + * + * @return A new empty identity container. + */ + public static Identity 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 implements IHolder { actions.forEach(pendingActions::add); Supplier supplier = () -> { - if(valueMaterialized) return heldValue; + if (valueMaterialized) return heldValue; return valueSupplier.get(); }; @@ -88,7 +88,7 @@ public class Lazy implements IHolder { return new Lazy<>(() -> { ContainedType currVal = heldValue; - if(!valueMaterialized) { + if (!valueMaterialized) { currVal = valueSupplier.get(); } @@ -99,8 +99,8 @@ public class Lazy implements IHolder { @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 implements IHolder { @Override public UnwrappedType unwrap(Function unwrapper) { - if(!valueMaterialized) { + if (!valueMaterialized) { heldValue = valueSupplier.get(); valueMaterialized = true; @@ -146,26 +146,51 @@ public class Lazy implements IHolder { @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 Lazy 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 Lazy lazy(Supplier supp) { + return new Lazy<>(supp); + } } -- cgit v1.2.3