From 1c8bc7132d980c1ff2dbd6b9af579c3b2fd8c63e Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 3 Apr 2016 19:22:48 -0400 Subject: General code refactoring and maintenance --- .../main/java/bjc/utils/data/lazy/LazyHolder.java | 57 ++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java index e74ce91..61a5956 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java @@ -19,7 +19,7 @@ import bjc.utils.funcdata.FunctionalList; * @param * The type of the data being held */ -public class LazyHolder implements IHolder { +public class LazyHolder implements IHolder, ILazy { private final class LazyHolderSupplier implements Supplier { private FunctionalList> pendingActions; @@ -70,8 +70,11 @@ public class LazyHolder implements IHolder { * The supplier for a value when it is neededs */ public LazyHolder(Supplier source) { - heldSource = source; + if (source == null) { + throw new NullPointerException("Source must be non-null"); + } + heldSource = source; heldValue = null; } @@ -87,6 +90,10 @@ public class LazyHolder implements IHolder { @Override public void doWith(Consumer action) { + if (action == null) { + throw new NullPointerException("Action must be non-null"); + } + transform((value) -> { // Do the action with the value action.accept(value); @@ -98,6 +105,10 @@ public class LazyHolder implements IHolder { @Override public IHolder map(Function transform) { + if (transform == null) { + throw new NullPointerException("Transform must be non-null"); + } + // Don't actually map until we need to return new LazyHolder<>( new LazyHolderSupplier<>(actions, transform)); @@ -105,6 +116,10 @@ public class LazyHolder implements IHolder { @Override public IHolder transform(Function transform) { + if (transform == null) { + throw new NullPointerException("Transform must be non-null"); + } + // Queue the transform until we need to apply it actions.add(transform); @@ -113,6 +128,10 @@ public class LazyHolder implements IHolder { @Override public E unwrap(Function unwrapper) { + if (unwrapper == null) { + throw new NullPointerException("Unwrapper must be null"); + } + // Actualize ourselves if (heldValue == null) { heldValue = heldSource.get(); @@ -124,4 +143,36 @@ public class LazyHolder implements IHolder { return unwrapper.apply(heldValue); } -} + @Override + public boolean isMaterialized() { + if (heldSource != null) { + // We're materialized if a value exists + return heldValue == null; + } else { + // We're materialized by default + return true; + } + } + + @Override + public boolean hasPendingActions() { + return actions.isEmpty(); + } + + @Override + public void materialize() { + // Only materialize if we haven't already + if (!isMaterialized()) { + heldValue = heldSource.get(); + } + } + + @Override + public void applyPendingActions() { + materialize(); + + actions.forEach((action) -> { + heldValue = action.apply(heldValue); + }); + } +} \ No newline at end of file -- cgit v1.2.3