summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java84
1 files changed, 36 insertions, 48 deletions
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 719b11f..ca41b62 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/data/Lazy.java
@@ -1,13 +1,13 @@
package bjc.utils.data;
-import bjc.utils.data.internals.BoundLazy;
-import bjc.utils.funcdata.FunctionalList;
-import bjc.utils.funcdata.IList;
-
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
+import bjc.utils.data.internals.BoundLazy;
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.IList;
+
/**
* A holder that holds a means to create a value, but doesn't actually compute
* the value until it's needed
@@ -31,7 +31,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
* @param value
* The seed value to use
*/
- public Lazy(ContainedType value) {
+ public Lazy(final ContainedType value) {
heldValue = value;
valueMaterialized = true;
@@ -43,27 +43,26 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
* @param supp
* The source of a value to use
*/
- public Lazy(Supplier<ContainedType> supp) {
+ public Lazy(final Supplier<ContainedType> supp) {
valueSupplier = new SingleSupplier<>(supp);
valueMaterialized = false;
}
- private Lazy(Supplier<ContainedType> supp, IList<UnaryOperator<ContainedType>> pendingActions) {
+ private Lazy(final Supplier<ContainedType> supp, final IList<UnaryOperator<ContainedType>> pendingActions) {
valueSupplier = supp;
actions = pendingActions;
}
@Override
- public <BoundType> IHolder<BoundType> bind(Function<ContainedType, IHolder<BoundType>> binder) {
- IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
+ public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) {
+ final IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
actions.forEach(pendingActions::add);
- Supplier<ContainedType> supplier = () -> {
- if (valueMaterialized)
- return heldValue;
+ final Supplier<ContainedType> supplier = () -> {
+ if (valueMaterialized) return heldValue;
return valueSupplier.get();
};
@@ -74,15 +73,15 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
}
@Override
- public <NewType> Function<ContainedType, IHolder<NewType>> lift(Function<ContainedType, NewType> func) {
+ public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) {
return val -> {
return new Lazy<>(func.apply(val));
};
}
@Override
- public <MappedType> IHolder<MappedType> map(Function<ContainedType, MappedType> mapper) {
- IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
+ public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) {
+ final IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
actions.forEach(pendingActions::add);
@@ -103,22 +102,21 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
if (valueMaterialized) {
if (actions.isEmpty())
return String.format("value[v='%s']", heldValue);
- else
- return String.format("value[v='%s'] (has pending transforms)", heldValue);
+ else return String.format("value[v='%s'] (has pending transforms)", heldValue);
}
return "(unmaterialized)";
}
@Override
- public IHolder<ContainedType> transform(UnaryOperator<ContainedType> transformer) {
+ public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) {
actions.add(transformer);
return this;
}
@Override
- public <UnwrappedType> UnwrappedType unwrap(Function<ContainedType, UnwrappedType> unwrapper) {
+ public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) {
if (!valueMaterialized) {
heldValue = valueSupplier.get();
@@ -139,68 +137,58 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
final int prime = 31;
int result = 1;
- result = prime * result + ((actions == null) ? 0 : actions.hashCode());
- result = prime * result + ((heldValue == null) ? 0 : heldValue.hashCode());
+ result = prime * result + (actions == null ? 0 : actions.hashCode());
+ result = prime * result + (heldValue == null ? 0 : heldValue.hashCode());
result = prime * result + (valueMaterialized ? 1231 : 1237);
return result;
}
@Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (!(obj instanceof Lazy<?>))
- return false;
+ public boolean equals(final Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (!(obj instanceof Lazy<?>)) return false;
- Lazy<?> other = (Lazy<?>) obj;
+ final Lazy<?> other = (Lazy<?>) obj;
- if (valueMaterialized != other.valueMaterialized)
- 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 (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 (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) {
+ public static <ContainedType> Lazy<ContainedType> lazy(final 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) {
+ public static <ContainedType> Lazy<ContainedType> lazy(final Supplier<ContainedType> supp) {
return new Lazy<>(supp);
}
}