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 +++++++++++----------- .../main/java/bjc/utils/data/lazy/LazyPair.java | 48 ++++++++-------- 2 files changed, 58 insertions(+), 54 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/lazy') 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); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java index 70b6f2f..3c21b56 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java @@ -25,49 +25,51 @@ public class LazyPair implements IPair { /** * The backing store for this pair */ - protected IHolder> del; + protected IHolder> delegatePair; /** * Create a new blank lazy pair */ public LazyPair() { - del = new LazyHolder<>(new Pair<>()); + delegatePair = new LazyHolder<>(new Pair<>()); } /** * Create a new lazy pair with the specified initial values * - * @param leftVal + * @param leftValue * The initial value for the left side of the pair - * @param rightVal + * @param rightValue * The initial value for the right side of the pair */ - public LazyPair(L leftVal, R rightVal) { - del = new LazyHolder<>(new Pair<>(leftVal, rightVal)); + public LazyPair(L leftValue, R rightValue) { + delegatePair = new LazyHolder<>(new Pair<>(leftValue, rightValue)); } /** * Create a new lazy pair with the specified sources for initial values * - * @param leftValSrc + * @param leftValueSource * The function to call for the left initial value - * @param rightValSrc + * @param rightValueSource * The function to call for the right initial value */ - public LazyPair(Supplier leftValSrc, Supplier rightValSrc) { - del = new LazyHolder<>(() -> { - return new Pair<>(leftValSrc.get(), rightValSrc.get()); + public LazyPair(Supplier leftValueSource, + Supplier rightValueSource) { + delegatePair = new LazyHolder<>(() -> { + return new Pair<>(leftValueSource.get(), + rightValueSource.get()); }); } /** * Create a new lazy pair with a specified internal delegate * - * @param deleg + * @param delegate * The internal delegate for the pair */ - private LazyPair(IHolder> deleg) { - del = deleg; + private LazyPair(IHolder> delegate) { + delegatePair = delegate; } /* @@ -77,10 +79,10 @@ public class LazyPair implements IPair { * java.util.function.Function) */ @Override - public IPair apply(Function lf, - Function rf) { - IHolder> newPair = - del.map(par -> par.apply(lf, rf)); + public IPair apply(Function leftTransform, + Function rightTransform) { + IHolder> newPair = delegatePair + .map((currentPair) -> currentPair.apply(leftTransform, rightTransform)); return new LazyPair<>(newPair); } @@ -91,9 +93,9 @@ public class LazyPair implements IPair { * @see bjc.utils.data.IPair#doWith(java.util.function.BiConsumer) */ @Override - public void doWith(BiConsumer bc) { - del.doWith((par) -> { - par.doWith(bc); + public void doWith(BiConsumer action) { + delegatePair.doWith((currentPair) -> { + currentPair.doWith(action); }); } @@ -103,7 +105,7 @@ public class LazyPair implements IPair { * @see bjc.utils.data.IPair#merge(java.util.function.BiFunction) */ @Override - public E merge(BiFunction bf) { - return del.unwrap((par) -> par.merge(bf)); + public E merge(BiFunction merger) { + return delegatePair.unwrap((currentPair) -> currentPair.merge(merger)); } } -- cgit v1.2.3