summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/data/Lazy.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/bjc/data/Lazy.java')
-rw-r--r--src/main/java/bjc/data/Lazy.java96
1 files changed, 53 insertions, 43 deletions
diff --git a/src/main/java/bjc/data/Lazy.java b/src/main/java/bjc/data/Lazy.java
index a425232..b237935 100644
--- a/src/main/java/bjc/data/Lazy.java
+++ b/src/main/java/bjc/data/Lazy.java
@@ -6,7 +6,7 @@ import java.util.function.UnaryOperator;
import bjc.data.internals.BoundLazy;
import bjc.funcdata.FunctionalList;
-import bjc.funcdata.IList;
+import bjc.funcdata.ListEx;
/**
* A holder that holds a means to create a value, but doesn't actually compute
@@ -17,7 +17,7 @@ import bjc.funcdata.IList;
* @param <ContainedType>
* The type of the value being held.
*/
-public class Lazy<ContainedType> implements IHolder<ContainedType> {
+public class Lazy<ContainedType> implements Holder<ContainedType> {
/* The supplier of the type. */
private Supplier<ContainedType> valueSupplier;
/* The actual type value. */
@@ -26,7 +26,7 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
private boolean valueMaterialized;
/* The list of pending actions on the value. */
- private IList<UnaryOperator<ContainedType>> actions = new FunctionalList<>();
+ private ListEx<UnaryOperator<ContainedType>> actions = new FunctionalList<>();
/**
* Create a new lazy value from the specified seed value.
@@ -54,41 +54,44 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
/* Create a new value from a supplier and a list of actions. */
private Lazy(final Supplier<ContainedType> supp,
- final IList<UnaryOperator<ContainedType>> pendingActions) {
+ final ListEx<UnaryOperator<ContainedType>> pendingActions) {
valueSupplier = supp;
actions = pendingActions;
}
@Override
- public <BoundType> IHolder<BoundType>
- bind(final Function<ContainedType, IHolder<BoundType>> binder) {
- final IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
+ public <BoundType> Holder<BoundType>
+ bind(final Function<ContainedType, Holder<BoundType>> binder) {
+ final ListEx<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
- actions.forEach(pendingActions::add);
+ for (UnaryOperator<ContainedType> action : actions) {
+ pendingActions.add(action);
+ }
+
final Supplier<ContainedType> supplier = () -> {
- if (valueMaterialized)
- return heldValue;
-
- return valueSupplier.get();
+ if (valueMaterialized) return heldValue;
+ else return valueSupplier.get();
};
return new BoundLazy<>(() -> new Lazy<>(supplier, pendingActions), binder);
}
@Override
- public <NewType> Function<ContainedType, IHolder<NewType>>
+ public <NewType> Function<ContainedType, Holder<NewType>>
lift(final Function<ContainedType, NewType> func) {
return val -> new Lazy<>(func.apply(val));
}
@Override
- public <MappedType> IHolder<MappedType>
+ public <MappedType> Holder<MappedType>
map(final Function<ContainedType, MappedType> mapper) {
- final IList<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
-
- actions.forEach(pendingActions::add);
+ final ListEx<UnaryOperator<ContainedType>> pendingActions = new FunctionalList<>();
+
+ for (UnaryOperator<ContainedType> action : actions) {
+ pendingActions.add(action);
+ }
return new Lazy<>(() -> {
ContainedType currVal = heldValue;
@@ -97,7 +100,8 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
currVal = valueSupplier.get();
}
- return pendingActions.reduceAux(currVal, UnaryOperator<ContainedType>::apply,
+ return pendingActions.reduceAux(currVal,
+ UnaryOperator<ContainedType>::apply,
value -> mapper.apply(value));
});
}
@@ -107,16 +111,22 @@ 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 %d pending transforms)",
+ heldValue, actions.getSize());
}
-
- return String.format("value[v='%s'] (has pending transforms)", heldValue);
}
- return "(unmaterialized)";
+ if (actions.isEmpty()) {
+ return"(unmaterialized)";
+ } else {
+ return String.format("(unmaterialized; has %d pending transforms",
+ actions.getSize());
+ }
}
@Override
- public IHolder<ContainedType>
+ public Holder<ContainedType>
transform(final UnaryOperator<ContainedType> transformer) {
actions.add(transformer);
@@ -132,9 +142,9 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
valueMaterialized = true;
}
- actions.forEach(action -> {
- heldValue = action.apply(heldValue);
- });
+ for (UnaryOperator<ContainedType> action : actions) {
+ heldValue = action.apply(heldValue);
+ }
actions = new FunctionalList<>();
@@ -155,12 +165,9 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
@Override
public boolean equals(final Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (!(obj instanceof Lazy<?>))
- return false;
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (!(obj instanceof Lazy<?>)) return false;
final Lazy<?> other = (Lazy<?>) obj;
@@ -169,27 +176,29 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
if (valueMaterialized) {
if (heldValue == null) {
- if (other.heldValue != null)
- return false;
- } else if (!heldValue.equals(other.heldValue))
+ if (other.heldValue != null) return false;
+ } else if (!heldValue.equals(other.heldValue)) {
return false;
- } else
+ }
+ } else {
return false;
+ }
if (actions == null) {
- if (other.actions != null)
- return false;
- } else if (actions.getSize() > 0 || other.actions.getSize() > 0)
+ 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.
+ *
+ * @param <ContainedType> The type of the contained value.
+ *
+ * @param val The value for the lazy container.
*
* @return A new lazy container holding that value.
*/
@@ -200,8 +209,9 @@ public class Lazy<ContainedType> implements IHolder<ContainedType> {
/**
* Create a new lazy container with a suspended value.
*
- * @param supp
- * The suspended value for the lazy container.
+ * @param <ContainedType> The type of the contained value.
+ *
+ * @param supp The suspended value for the lazy container.
*
* @return A new lazy container that will un-suspend the value when necessary.
*/