summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data/lazy
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/lazy')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java64
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java48
2 files changed, 58 insertions, 54 deletions
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<T> implements IHolder<T> {
private final class LazyHolderSupplier<NewT>
implements Supplier<NewT> {
private FunctionalList<Function<T, T>> pendingActions;
- private Function<T, NewT> f;
+ private Function<T, NewT> pendingTransform;
public LazyHolderSupplier(FunctionalList<Function<T, T>> actons,
- Function<T, NewT> f) {
+ Function<T, NewT> 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<T, T>::apply, f::apply);
+ if (heldValue == null) {
+ return pendingActions.reduceAux(heldSource.get(),
+ Function<T, T>::apply, pendingTransform::apply);
} else {
- return pendingActions.reduceAux(held,
- Function<T, T>::apply, f::apply);
+ return pendingActions.reduceAux(heldValue,
+ Function<T, T>::apply, pendingTransform::apply);
}
}
}
@@ -50,76 +50,78 @@ public class LazyHolder<T> implements IHolder<T> {
/**
* List of queued actions to be performed on realized values
*/
- private FunctionalList<Function<T, T>> actions;
+ private FunctionalList<Function<T, T>> 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<T> heldSrc;
+ private Supplier<T> 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<T> src) {
- heldSrc = src;
+ public LazyHolder(Supplier<T> 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<T> f) {
- transform((val) -> {
+ public void doWith(Consumer<T> 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 <NewT> IHolder<NewT> map(Function<T, NewT> f) {
+ public <NewT> IHolder<NewT> map(Function<T, NewT> 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<T> transform(Function<T, T> f) {
+ public IHolder<T> transform(Function<T, T> transform) {
// Queue the transform until we need to apply it
- actions.add(f);
+ actions.add(transform);
return this;
}
@Override
- public <E> E unwrap(Function<T, E> f) {
+ public <E> E unwrap(Function<T, E> 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<L, R> implements IPair<L, R> {
/**
* The backing store for this pair
*/
- protected IHolder<IPair<L, R>> del;
+ protected IHolder<IPair<L, R>> 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<L> leftValSrc, Supplier<R> rightValSrc) {
- del = new LazyHolder<>(() -> {
- return new Pair<>(leftValSrc.get(), rightValSrc.get());
+ public LazyPair(Supplier<L> leftValueSource,
+ Supplier<R> 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<IPair<L, R>> deleg) {
- del = deleg;
+ private LazyPair(IHolder<IPair<L, R>> delegate) {
+ delegatePair = delegate;
}
/*
@@ -77,10 +79,10 @@ public class LazyPair<L, R> implements IPair<L, R> {
* java.util.function.Function)
*/
@Override
- public <L2, R2> IPair<L2, R2> apply(Function<L, L2> lf,
- Function<R, R2> rf) {
- IHolder<IPair<L2, R2>> newPair =
- del.map(par -> par.apply(lf, rf));
+ public <L2, R2> IPair<L2, R2> apply(Function<L, L2> leftTransform,
+ Function<R, R2> rightTransform) {
+ IHolder<IPair<L2, R2>> newPair = delegatePair
+ .map((currentPair) -> currentPair.apply(leftTransform, rightTransform));
return new LazyPair<>(newPair);
}
@@ -91,9 +93,9 @@ public class LazyPair<L, R> implements IPair<L, R> {
* @see bjc.utils.data.IPair#doWith(java.util.function.BiConsumer)
*/
@Override
- public void doWith(BiConsumer<L, R> bc) {
- del.doWith((par) -> {
- par.doWith(bc);
+ public void doWith(BiConsumer<L, R> action) {
+ delegatePair.doWith((currentPair) -> {
+ currentPair.doWith(action);
});
}
@@ -103,7 +105,7 @@ public class LazyPair<L, R> implements IPair<L, R> {
* @see bjc.utils.data.IPair#merge(java.util.function.BiFunction)
*/
@Override
- public <E> E merge(BiFunction<L, R, E> bf) {
- return del.unwrap((par) -> par.merge(bf));
+ public <E> E merge(BiFunction<L, R, E> merger) {
+ return delegatePair.unwrap((currentPair) -> currentPair.merge(merger));
}
}