From 8a8b457c98e207d809a7616e73eb59bfe197a7a5 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 31 Mar 2016 11:43:21 -0400 Subject: More code maintenance --- .../main/java/bjc/utils/data/lazy/LazyHolder.java | 64 +++++++++++----------- 1 file changed, 33 insertions(+), 31 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 ddf3cfc..e74ce91 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 @@ -23,26 +23,26 @@ public class LazyHolder implements IHolder { private final class LazyHolderSupplier implements Supplier { private FunctionalList> pendingActions; - private Function f; + private Function pendingTransform; public LazyHolderSupplier(FunctionalList> actons, - Function f) { + Function transform) { // Resolve latent bug I just realized. After a map, adding new // actions to the original holder could've resulted in changes // to all unactualized mapped values from that holder pendingActions = actons.clone(); - this.f = f; + this.pendingTransform = transform; } @Override public NewT get() { - if (held == null) { - return pendingActions.reduceAux(heldSrc.get(), - Function::apply, f::apply); + if (heldValue == null) { + return pendingActions.reduceAux(heldSource.get(), + Function::apply, pendingTransform::apply); } else { - return pendingActions.reduceAux(held, - Function::apply, f::apply); + return pendingActions.reduceAux(heldValue, + Function::apply, pendingTransform::apply); } } } @@ -50,76 +50,78 @@ public class LazyHolder implements IHolder { /** * List of queued actions to be performed on realized values */ - private FunctionalList> actions; + private FunctionalList> actions = + new FunctionalList<>(); /** * The value internally held by this lazy holder */ - private T held; + private T heldValue; /** * The source for a value held by this lazy holder */ - private Supplier heldSrc; + private Supplier heldSource; /** * Create a new lazy holder with the given supplier * - * @param src + * @param source * The supplier for a value when it is neededs */ - public LazyHolder(Supplier src) { - heldSrc = src; + public LazyHolder(Supplier source) { + heldSource = source; - held = null; + heldValue = null; } /** * Create a new lazy holder with the given value * - * @param val + * @param value * The value held in the holder */ - public LazyHolder(T val) { - held = val; + public LazyHolder(T value) { + heldValue = value; } @Override - public void doWith(Consumer f) { - transform((val) -> { + public void doWith(Consumer action) { + transform((value) -> { // Do the action with the value - f.accept(val); + action.accept(value); // Return the untransformed value - return val; + return value; }); } @Override - public IHolder map(Function f) { + public IHolder map(Function transform) { // Don't actually map until we need to - return new LazyHolder<>(new LazyHolderSupplier<>(actions, f)); + return new LazyHolder<>( + new LazyHolderSupplier<>(actions, transform)); } @Override - public IHolder transform(Function f) { + public IHolder transform(Function transform) { // Queue the transform until we need to apply it - actions.add(f); + actions.add(transform); return this; } @Override - public E unwrap(Function f) { + public E unwrap(Function unwrapper) { // Actualize ourselves - if (held == null) { - held = heldSrc.get(); + if (heldValue == null) { + heldValue = heldSource.get(); } // Apply all pending transforms - actions.forEach((act) -> held = act.apply(held)); + actions.forEach((action) -> heldValue = action.apply(heldValue)); - return f.apply(held); + return unwrapper.apply(heldValue); } } -- cgit v1.2.3