diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-03 19:22:48 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-03 19:22:48 -0400 |
| commit | 1c8bc7132d980c1ff2dbd6b9af579c3b2fd8c63e (patch) | |
| tree | a29777f07ebd81fbef61b5ae02f13f1a9d8f65a2 /BJC-Utils2/src/main/java/bjc/utils/data | |
| parent | a023de85aa08c8f2b8b2441c6b14064eabee2775 (diff) | |
General code refactoring and maintenance
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/data')
5 files changed, 191 insertions, 11 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java index 6854440..e042554 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java @@ -46,6 +46,10 @@ public class GenHolder<T> implements IHolder<T> { */ @Override public void doWith(Consumer<T> action) { + if (action == null) { + throw new NullPointerException("Action must be non-null"); + } + action.accept(heldValue); } @@ -56,6 +60,10 @@ public class GenHolder<T> implements IHolder<T> { */ @Override public <NewT> IHolder<NewT> map(Function<T, NewT> transformer) { + if (transformer == null) { + throw new NullPointerException("Transformer must be non-null"); + } + return new GenHolder<>(transformer.apply(heldValue)); } @@ -66,6 +74,10 @@ public class GenHolder<T> implements IHolder<T> { */ @Override public IHolder<T> transform(Function<T, T> transformer) { + if (transformer == null) { + throw new NullPointerException("Transformer must be non-null"); + } + heldValue = transformer.apply(heldValue); return this; @@ -78,11 +90,19 @@ public class GenHolder<T> implements IHolder<T> { */ @Override public <E> E unwrap(Function<T, E> unwrapper) { + if (unwrapper == null) { + throw new NullPointerException("Unwrapper must be null"); + } + return unwrapper.apply(heldValue); } @Override public String toString() { + if (heldValue == null) { + return "(null)"; + } + return heldValue.toString(); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java index b5ad953..97cb195 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -56,6 +56,11 @@ public class Pair<L, R> implements IPair<L, R> { @Override public <L2, R2> IPair<L2, R2> apply(Function<L, L2> leftTransformer, Function<R, R2> rightTransformer) { + if (leftTransformer == null || rightTransformer == null) { + throw new NullPointerException( + "Transformers must be non-null"); + } + return new Pair<>(leftTransformer.apply(leftValue), rightTransformer.apply(rightValue)); } @@ -67,6 +72,10 @@ public class Pair<L, R> implements IPair<L, R> { */ @Override public void doWith(BiConsumer<L, R> action) { + if (action == null) { + throw new NullPointerException("Action must be non-null"); + } + action.accept(leftValue, rightValue); } @@ -77,6 +86,10 @@ public class Pair<L, R> implements IPair<L, R> { */ @Override public <E> E merge(BiFunction<L, R, E> merger) { + if (merger == null) { + throw new NullPointerException("Merger must be non-null"); + } + return merger.apply(leftValue, rightValue); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/ILazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/ILazy.java new file mode 100644 index 0000000..a4fab67 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/ILazy.java @@ -0,0 +1,35 @@ +package bjc.utils.data.lazy; + +/** + * Interface for some maintenance operations on lazy objects + * + * @author ben + * + */ +public interface ILazy { + /** + * Check if this object has been materialized + * + * @return Whether or not this object has been materialized + */ + public boolean isMaterialized(); + + /** + * Check if there are pending actions that need to be applied + * + * @return Whether or not there are pending actions + */ + public boolean hasPendingActions(); + + /** + * Make this object materialize itelf + */ + public void materialize(); + + /** + * Make this object apply any pending objects + * + * As a requirement, will materialize the object if it is not materialized + */ + public void applyPendingActions(); +} 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 <T> * The type of the data being held */ -public class LazyHolder<T> implements IHolder<T> { +public class LazyHolder<T> implements IHolder<T>, ILazy { private final class LazyHolderSupplier<NewT> implements Supplier<NewT> { private FunctionalList<Function<T, T>> pendingActions; @@ -70,8 +70,11 @@ public class LazyHolder<T> implements IHolder<T> { * The supplier for a value when it is neededs */ public LazyHolder(Supplier<T> 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<T> implements IHolder<T> { @Override public void doWith(Consumer<T> 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<T> implements IHolder<T> { @Override public <NewT> IHolder<NewT> map(Function<T, NewT> 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<T> implements IHolder<T> { @Override public IHolder<T> transform(Function<T, T> 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<T> implements IHolder<T> { @Override public <E> E unwrap(Function<T, E> 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<T> implements IHolder<T> { 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 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 3c21b56..e08c8fb 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 @@ -21,11 +21,14 @@ import bjc.utils.data.Pair; * @param <R> * The type of value stored on the right side of the pair */ -public class LazyPair<L, R> implements IPair<L, R> { +public class LazyPair<L, R> implements IPair<L, R>, ILazy { /** * The backing store for this pair */ - protected IHolder<IPair<L, R>> delegatePair; + protected IHolder<IPair<L, R>> delegatePair; + + private boolean materialized = false; + private boolean pendingActions = false; /** * Create a new blank lazy pair @@ -43,6 +46,8 @@ public class LazyPair<L, R> implements IPair<L, R> { * The initial value for the right side of the pair */ public LazyPair(L leftValue, R rightValue) { + materialized = true; + delegatePair = new LazyHolder<>(new Pair<>(leftValue, rightValue)); } @@ -56,6 +61,10 @@ public class LazyPair<L, R> implements IPair<L, R> { */ public LazyPair(Supplier<L> leftValueSource, Supplier<R> rightValueSource) { + if (leftValueSource == null || rightValueSource == null) { + throw new NullPointerException("Sources must be non-null"); + } + delegatePair = new LazyHolder<>(() -> { return new Pair<>(leftValueSource.get(), rightValueSource.get()); @@ -68,7 +77,11 @@ public class LazyPair<L, R> implements IPair<L, R> { * @param delegate * The internal delegate for the pair */ - private LazyPair(IHolder<IPair<L, R>> delegate) { + private LazyPair(IHolder<IPair<L, R>> delegate, boolean mater, + boolean pend) { + materialized = mater; + pendingActions = pend; + delegatePair = delegate; } @@ -81,10 +94,15 @@ public class LazyPair<L, R> implements IPair<L, R> { @Override 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)); + if (leftTransform == null || rightTransform == null) { + throw new NullPointerException("Transforms must be non-null"); + } - return new LazyPair<>(newPair); + IHolder<IPair<L2, R2>> newPair = + delegatePair.map((currentPair) -> currentPair + .apply(leftTransform, rightTransform)); + + return new LazyPair<>(newPair, materialized, true); } /* @@ -94,6 +112,12 @@ public class LazyPair<L, R> implements IPair<L, R> { */ @Override public void doWith(BiConsumer<L, R> action) { + if (action == null) { + throw new NullPointerException("Action must be non-null"); + } + + pendingActions = true; + delegatePair.doWith((currentPair) -> { currentPair.doWith(action); }); @@ -106,6 +130,43 @@ public class LazyPair<L, R> implements IPair<L, R> { */ @Override public <E> E merge(BiFunction<L, R, E> merger) { - return delegatePair.unwrap((currentPair) -> currentPair.merge(merger)); + if (merger == null) { + throw new NullPointerException("Merger must be non-null"); + } + + materialized = true; + pendingActions = false; + + return delegatePair + .unwrap((currentPair) -> currentPair.merge(merger)); + } + + @Override + public boolean isMaterialized() { + return materialized; + } + + @Override + public boolean hasPendingActions() { + return pendingActions; + } + + /* + * Note: Materializing will also apply all currently pending actions + */ + @Override + public void materialize() { + merge((left, right) -> null); + + materialized = true; + pendingActions = false; + } + + @Override + public void applyPendingActions() { + merge((left, right) -> null); + + materialized = true; + pendingActions = false; } -} +}
\ No newline at end of file |
