diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:43:13 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:43:13 -0400 |
| commit | f51f6da7319787348c38b875652b5c0e9f88c8aa (patch) | |
| tree | 943888fc724da2d2dedd89abec99dcbfcc089fd0 /src/main/java/bjc/data/internals | |
| parent | 9052ed6da37af23ea82588d248f409e60a33c6cb (diff) | |
Cleanup pass
Pass to do some cleanups
Diffstat (limited to 'src/main/java/bjc/data/internals')
| -rw-r--r-- | src/main/java/bjc/data/internals/BoundLazy.java | 66 | ||||
| -rw-r--r-- | src/main/java/bjc/data/internals/BoundLazyPair.java | 122 | ||||
| -rw-r--r-- | src/main/java/bjc/data/internals/BoundListHolder.java | 50 | ||||
| -rw-r--r-- | src/main/java/bjc/data/internals/HalfBoundLazyPair.java | 73 | ||||
| -rw-r--r-- | src/main/java/bjc/data/internals/WrappedLazy.java | 38 | ||||
| -rw-r--r-- | src/main/java/bjc/data/internals/WrappedOption.java | 46 |
6 files changed, 223 insertions, 172 deletions
diff --git a/src/main/java/bjc/data/internals/BoundLazy.java b/src/main/java/bjc/data/internals/BoundLazy.java index 728af8e..9c984eb 100644 --- a/src/main/java/bjc/data/internals/BoundLazy.java +++ b/src/main/java/bjc/data/internals/BoundLazy.java @@ -15,7 +15,8 @@ import bjc.funcdata.IList; * @author Ben Culkin */ @SuppressWarnings("javadoc") -public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundContainedType> { +public class BoundLazy<OldType, BoundContainedType> + implements IHolder<BoundContainedType> { /* The old value. */ private final Supplier<IHolder<OldType>> oldSupplier; @@ -29,16 +30,17 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont private boolean holderBound; /* Transformations currently pending on the bound value. */ - private final IList<UnaryOperator<BoundContainedType>> actions = new FunctionalList<>(); + private final IList<UnaryOperator<BoundContainedType>> actions + = new FunctionalList<>(); /** * Create a new bound lazy value. * * @param supp - * The supplier of the old value. + * The supplier of the old value. * * @param binder - * The function to use to bind the old value to the new one. + * The function to use to bind the old value to the new one. */ public BoundLazy(final Supplier<IHolder<OldType>> supp, final Function<OldType, IHolder<BoundContainedType>> binder) { @@ -47,11 +49,14 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont } @Override - public <BoundType> IHolder<BoundType> bind(final Function<BoundContainedType, IHolder<BoundType>> bindr) { - if(bindr == null) throw new NullPointerException("Binder must not be null"); + public <BoundType> IHolder<BoundType> + bind(final Function<BoundContainedType, IHolder<BoundType>> bindr) { + if (bindr == null) + throw new NullPointerException("Binder must not be null"); /* Prepare a list of pending actions. */ - final IList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>(); + final IList<UnaryOperator<BoundContainedType>> pendingActions + = new FunctionalList<>(); actions.forEach(pendingActions::add); /* Create the new supplier of a value. */ @@ -59,35 +64,37 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont IHolder<BoundContainedType> oldHolder = boundHolder; /* Bind the value if it hasn't been bound before. */ - if(!holderBound) { + if (!holderBound) { oldHolder = oldSupplier.get().unwrap(binder); } /* Apply all the pending actions. */ - return pendingActions.reduceAux(oldHolder, (action, state) -> { - return state.transform(action); - }, (value) -> value); + return pendingActions.reduceAux(oldHolder, (action, state) -> state.transform(action), value -> value); }; return new BoundLazy<>(typeSupplier, bindr); } @Override - public <NewType> Function<BoundContainedType, IHolder<NewType>> lift( - final Function<BoundContainedType, NewType> func) { - if(func == null) throw new NullPointerException("Function to lift must not be null"); + public <NewType> Function<BoundContainedType, IHolder<NewType>> + lift(final Function<BoundContainedType, NewType> func) { + if (func == null) + throw new NullPointerException("Function to lift must not be null"); - return (val) -> { + return val -> { return new Lazy<>(func.apply(val)); }; } @Override - public <MappedType> IHolder<MappedType> map(final Function<BoundContainedType, MappedType> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <MappedType> IHolder<MappedType> + map(final Function<BoundContainedType, MappedType> mapper) { + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); /* Prepare a list of pending actions. */ - final IList<UnaryOperator<BoundContainedType>> pendingActions = new FunctionalList<>(); + final IList<UnaryOperator<BoundContainedType>> pendingActions + = new FunctionalList<>(); actions.forEach(pendingActions::add); /* Prepare the new supplier. */ @@ -95,14 +102,12 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont IHolder<BoundContainedType> oldHolder = boundHolder; /* Bound the value if it hasn't been bound. */ - if(!holderBound) { + if (!holderBound) { oldHolder = oldSupplier.get().unwrap(binder); } /* Apply pending actions. */ - return pendingActions.reduceAux(oldHolder.getValue(), (action, state) -> { - return action.apply(state); - }, (value) -> mapper.apply(value)); + return pendingActions.reduceAux(oldHolder.getValue(), (action, state) -> action.apply(state), value -> mapper.apply(value)); }; return new Lazy<>(typeSupplier); @@ -110,14 +115,17 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont @Override public String toString() { - if(holderBound) return boundHolder.toString(); + if (holderBound) + return boundHolder.toString(); return "(unmaterialized)"; } @Override - public IHolder<BoundContainedType> transform(final UnaryOperator<BoundContainedType> transformer) { - if(transformer == null) throw new NullPointerException("Transformer must not be null"); + public IHolder<BoundContainedType> + transform(final UnaryOperator<BoundContainedType> transformer) { + if (transformer == null) + throw new NullPointerException("Transformer must not be null"); actions.add(transformer); @@ -125,10 +133,12 @@ public class BoundLazy<OldType, BoundContainedType> implements IHolder<BoundCont } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<BoundContainedType, UnwrappedType> unwrapper) { - if(unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); + public <UnwrappedType> UnwrappedType + unwrap(final Function<BoundContainedType, UnwrappedType> unwrapper) { + if (unwrapper == null) + throw new NullPointerException("Unwrapper must not be null"); - if(!holderBound) { + if (!holderBound) { boundHolder = oldSupplier.get().unwrap(binder::apply); } diff --git a/src/main/java/bjc/data/internals/BoundLazyPair.java b/src/main/java/bjc/data/internals/BoundLazyPair.java index 105b410..e081c04 100644 --- a/src/main/java/bjc/data/internals/BoundLazyPair.java +++ b/src/main/java/bjc/data/internals/BoundLazyPair.java @@ -15,7 +15,8 @@ import bjc.data.LazyPair; * @author Ben Culkin */ @SuppressWarnings("javadoc") -public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPair<NewLeft, NewRight> { +public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> + implements IPair<NewLeft, NewRight> { /* The supplier of the left value. */ private final Supplier<OldLeft> leftSupplier; /* The supplier of the right value. */ @@ -34,16 +35,17 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai * Create a new bound lazy pair. * * @param leftSupp - * The supplier for the left value. + * The supplier for the left value. * * @param rightSupp - * The supplier for the right value. + * The supplier for the right value. * * @param bindr - * The function to use to bind the left and right into a new - * pair. + * The function to use to bind the left and right into a new + * pair. */ - public BoundLazyPair(final Supplier<OldLeft> leftSupp, final Supplier<OldRight> rightSupp, + public BoundLazyPair(final Supplier<OldLeft> leftSupp, + final Supplier<OldRight> rightSupp, final BiFunction<OldLeft, OldRight, IPair<NewLeft, NewRight>> bindr) { leftSupplier = leftSupp; rightSupplier = rightSupp; @@ -53,54 +55,53 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai @Override public <BoundLeft, BoundRight> IPair<BoundLeft, BoundRight> bind( final BiFunction<NewLeft, NewRight, IPair<BoundLeft, BoundRight>> bindr) { - if(bindr == null) throw new NullPointerException("Binder must not be null"); + if (bindr == null) + throw new NullPointerException("Binder must not be null"); final IHolder<IPair<NewLeft, NewRight>> newPair = new Identity<>(boundPair); final IHolder<Boolean> newPairMade = new Identity<>(pairBound); final Supplier<NewLeft> leftSupp = () -> { - if(!newPairMade.getValue()) { + if (!newPairMade.getValue()) { /* - * If the pair hasn't been bound before, bind - * it. + * If the pair hasn't been bound before, bind it. */ newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); newPairMade.replace(true); } - return newPair.unwrap((pair) -> pair.getLeft()); + return newPair.unwrap(pair -> pair.getLeft()); }; final Supplier<NewRight> rightSupp = () -> { - if(!newPairMade.getValue()) { + if (!newPairMade.getValue()) { /* - * If the pair hasn't been bound before, bind - * it. + * If the pair hasn't been bound before, bind it. */ newPair.replace(binder.apply(leftSupplier.get(), rightSupplier.get())); newPairMade.replace(true); } - return newPair.unwrap((pair) -> pair.getRight()); + return newPair.unwrap(pair -> pair.getRight()); }; return new BoundLazyPair<>(leftSupp, rightSupp, bindr); } @Override - public <BoundLeft> IPair<BoundLeft, NewRight> bindLeft( - final Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) { - if(leftBinder == null) throw new NullPointerException("Left binder must not be null"); + public <BoundLeft> IPair<BoundLeft, NewRight> + bindLeft(final Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) { + if (leftBinder == null) + throw new NullPointerException("Left binder must not be null"); final Supplier<NewLeft> leftSupp = () -> { IPair<NewLeft, NewRight> newPair = boundPair; - if(!pairBound) { + if (!pairBound) { /* - * If the pair hasn't been bound before, bind - * it. + * If the pair hasn't been bound before, bind it. */ newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } @@ -112,17 +113,17 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai } @Override - public <BoundRight> IPair<NewLeft, BoundRight> bindRight( - final Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) { - if(rightBinder == null) throw new NullPointerException("Right binder must not be null"); + public <BoundRight> IPair<NewLeft, BoundRight> + bindRight(final Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) { + if (rightBinder == null) + throw new NullPointerException("Right binder must not be null"); final Supplier<NewRight> rightSupp = () -> { IPair<NewLeft, NewRight> newPair = boundPair; - if(!pairBound) { + if (!pairBound) { /* - * If the pair hasn't been bound before, bind - * it. + * If the pair hasn't been bound before, bind it. */ newPair = binder.apply(leftSupplier.get(), rightSupplier.get()); } @@ -134,35 +135,37 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - final IPair<OtherLeft, OtherRight> otherPair, - final BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, - final BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { - if(otherPair == null) { + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> + IPair<CombinedLeft, CombinedRight> + combine(final IPair<OtherLeft, OtherRight> otherPair, + final BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, + final BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { + if (otherPair == null) { throw new NullPointerException("Other pair must not be null"); - } else if(leftCombiner == null) { + } else if (leftCombiner == null) { throw new NullPointerException("Left combiner must not be null"); - } else if(rightCombiner == null) { + } else if (rightCombiner == null) { throw new NullPointerException("Right combiner must not be null"); } - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); - CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); + return otherPair.bind((otherLeft, otherRight) -> bind((leftVal, rightVal) -> { + CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); + CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); - return new LazyPair<>(cLeft, cRight); - }); - }); + return new LazyPair<>(cLeft, cRight); + })); } @Override - public <NewLeftType> IPair<NewLeftType, NewRight> mapLeft(final Function<NewLeft, NewLeftType> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <NewLeftType> IPair<NewLeftType, NewRight> + mapLeft(final Function<NewLeft, NewLeftType> mapper) { + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); final Supplier<NewLeftType> leftSupp = () -> { - if(!pairBound) { - final NewLeft leftVal = binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); + if (!pairBound) { + final NewLeft leftVal + = binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); return mapper.apply(leftVal); } @@ -171,7 +174,8 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai }; final Supplier<NewRight> rightSupp = () -> { - if(!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getRight(); + if (!pairBound) + return binder.apply(leftSupplier.get(), rightSupplier.get()).getRight(); return boundPair.getRight(); }; @@ -180,19 +184,22 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai } @Override - public <NewRightType> IPair<NewLeft, NewRightType> mapRight(final Function<NewRight, NewRightType> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <NewRightType> IPair<NewLeft, NewRightType> + mapRight(final Function<NewRight, NewRightType> mapper) { + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); final Supplier<NewLeft> leftSupp = () -> { - if(!pairBound) return binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); + if (!pairBound) + return binder.apply(leftSupplier.get(), rightSupplier.get()).getLeft(); return boundPair.getLeft(); }; final Supplier<NewRightType> rightSupp = () -> { - if(!pairBound) { - final NewRight rightVal = binder.apply(leftSupplier.get(), rightSupplier.get()) - .getRight(); + if (!pairBound) { + final NewRight rightVal = binder + .apply(leftSupplier.get(), rightSupplier.get()).getRight(); return mapper.apply(rightVal); } @@ -204,10 +211,12 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai } @Override - public <MergedType> MergedType merge(final BiFunction<NewLeft, NewRight, MergedType> merger) { - if(merger == null) throw new NullPointerException("Merger must not be null"); + public <MergedType> MergedType + merge(final BiFunction<NewLeft, NewRight, MergedType> merger) { + if (merger == null) + throw new NullPointerException("Merger must not be null"); - if(!pairBound) { + if (!pairBound) { /* * If the pair isn't bound yet, bind it. */ @@ -221,7 +230,8 @@ public class BoundLazyPair<OldLeft, OldRight, NewLeft, NewRight> implements IPai @Override public String toString() { - if(pairBound) return boundPair.toString(); + if (pairBound) + return boundPair.toString(); return "(un-materialized)"; } diff --git a/src/main/java/bjc/data/internals/BoundListHolder.java b/src/main/java/bjc/data/internals/BoundListHolder.java index 8f8cab4..1193c8d 100644 --- a/src/main/java/bjc/data/internals/BoundListHolder.java +++ b/src/main/java/bjc/data/internals/BoundListHolder.java @@ -21,48 +21,58 @@ public class BoundListHolder<ContainedType> implements IHolder<ContainedType> { * Create a new list of holders. * * @param toHold - * The list of holders to, well, hold. + * The list of holders to, well, hold. */ public BoundListHolder(final IList<IHolder<ContainedType>> toHold) { heldHolders = toHold; } @Override - public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) { - if(binder == null) throw new NullPointerException("Binder must not be null"); + public <BoundType> IHolder<BoundType> + bind(final Function<ContainedType, IHolder<BoundType>> binder) { + if (binder == null) + throw new NullPointerException("Binder must not be null"); - final IList<IHolder<BoundType>> boundHolders = heldHolders.map((containedHolder) -> { - return containedHolder.bind(binder); - }); + final IList<IHolder<BoundType>> boundHolders + = heldHolders.map(containedHolder -> { + return containedHolder.bind(binder); + }); return new BoundListHolder<>(boundHolders); } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) { - if(func == null) throw new NullPointerException("Function to lift must not be null"); + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(final Function<ContainedType, NewType> func) { + if (func == null) + throw new NullPointerException("Function to lift must not be null"); - return (val) -> { + return val -> { return new ListHolder<>(func.apply(val)); }; } @Override - public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) { - if(mapper == null) throw new NullPointerException("Mapper must not be null"); + public <MappedType> IHolder<MappedType> + map(final Function<ContainedType, MappedType> mapper) { + if (mapper == null) + throw new NullPointerException("Mapper must not be null"); - final IList<IHolder<MappedType>> mappedHolders = heldHolders.map((containedHolder) -> { - return containedHolder.map(mapper); - }); + final IList<IHolder<MappedType>> mappedHolders + = heldHolders.map(containedHolder -> { + return containedHolder.map(mapper); + }); return new BoundListHolder<>(mappedHolders); } @Override - public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { - if(transformer == null) throw new NullPointerException("Transformer must not be null"); + public IHolder<ContainedType> + transform(final UnaryOperator<ContainedType> transformer) { + if (transformer == null) + throw new NullPointerException("Transformer must not be null"); - heldHolders.forEach((containedHolder) -> { + heldHolders.forEach(containedHolder -> { containedHolder.transform(transformer); }); @@ -70,8 +80,10 @@ public class BoundListHolder<ContainedType> implements IHolder<ContainedType> { } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { - if(unwrapper == null) throw new NullPointerException("Unwrapper must not be null"); + public <UnwrappedType> UnwrappedType + unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { + if (unwrapper == null) + throw new NullPointerException("Unwrapper must not be null"); /* * @NOTE Is there another way we could want to do this? diff --git a/src/main/java/bjc/data/internals/HalfBoundLazyPair.java b/src/main/java/bjc/data/internals/HalfBoundLazyPair.java index 4f28012..6bcb6ae 100644 --- a/src/main/java/bjc/data/internals/HalfBoundLazyPair.java +++ b/src/main/java/bjc/data/internals/HalfBoundLazyPair.java @@ -23,7 +23,8 @@ import bjc.data.LazyPair; * @author Ben Culkin */ @SuppressWarnings("javadoc") -public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewLeft, NewRight> { +public class HalfBoundLazyPair<OldType, NewLeft, NewRight> + implements IPair<NewLeft, NewRight> { /* The supplier of the old value. */ private final Supplier<OldType> oldSupplier; @@ -39,10 +40,10 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL * Create a new half-bound lazy pair. * * @param oldSupp - * The supplier of the old value. + * The supplier of the old value. * * @param bindr - * The function to use to create the pair from the old value. + * The function to use to create the pair from the old value. */ public HalfBoundLazyPair(final Supplier<OldType> oldSupp, final Function<OldType, IPair<NewLeft, NewRight>> bindr) { @@ -57,35 +58,35 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL final IHolder<Boolean> newPairMade = new Identity<>(pairBound); final Supplier<NewLeft> leftSupp = () -> { - if(!newPairMade.getValue()) { + if (!newPairMade.getValue()) { /* Bind the pair if it hasn't been bound yet. */ newPair.replace(binder.apply(oldSupplier.get())); newPairMade.replace(true); } - return newPair.unwrap((pair) -> pair.getLeft()); + return newPair.unwrap(pair -> pair.getLeft()); }; final Supplier<NewRight> rightSupp = () -> { - if(!newPairMade.getValue()) { + if (!newPairMade.getValue()) { /* Bind the pair if it hasn't been bound yet. */ newPair.replace(binder.apply(oldSupplier.get())); newPairMade.replace(true); } - return newPair.unwrap((pair) -> pair.getRight()); + return newPair.unwrap(pair -> pair.getRight()); }; return new BoundLazyPair<>(leftSupp, rightSupp, bindr); } @Override - public <BoundLeft> IPair<BoundLeft, NewRight> bindLeft( - final Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) { + public <BoundLeft> IPair<BoundLeft, NewRight> + bindLeft(final Function<NewLeft, IPair<BoundLeft, NewRight>> leftBinder) { final Supplier<NewLeft> leftSupp = () -> { IPair<NewLeft, NewRight> newPair = boundPair; - if(!pairBound) { + if (!pairBound) { newPair = binder.apply(oldSupplier.get()); } @@ -96,12 +97,12 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL } @Override - public <BoundRight> IPair<NewLeft, BoundRight> bindRight( - final Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) { + public <BoundRight> IPair<NewLeft, BoundRight> + bindRight(final Function<NewRight, IPair<NewLeft, BoundRight>> rightBinder) { final Supplier<NewRight> rightSupp = () -> { IPair<NewLeft, NewRight> newPair = boundPair; - if(!pairBound) { + if (!pairBound) { newPair = binder.apply(oldSupplier.get()); } @@ -112,24 +113,25 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL } @Override - public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> IPair<CombinedLeft, CombinedRight> combine( - final IPair<OtherLeft, OtherRight> otherPair, - final BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, - final BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { - return otherPair.bind((otherLeft, otherRight) -> { - return bind((leftVal, rightVal) -> { - CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); - CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); - - return new LazyPair<>(cLeft, cRight); - }); - }); + public <OtherLeft, OtherRight, CombinedLeft, CombinedRight> + IPair<CombinedLeft, CombinedRight> + combine(final IPair<OtherLeft, OtherRight> otherPair, + final BiFunction<NewLeft, OtherLeft, CombinedLeft> leftCombiner, + final BiFunction<NewRight, OtherRight, CombinedRight> rightCombiner) { + return otherPair.bind((otherLeft, otherRight) -> bind((leftVal, rightVal) -> { + CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); + CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); + + return new LazyPair<>(cLeft, cRight); + })); } @Override - public <NewLeftType> IPair<NewLeftType, NewRight> mapLeft(final Function<NewLeft, NewLeftType> mapper) { + public <NewLeftType> IPair<NewLeftType, NewRight> + mapLeft(final Function<NewLeft, NewLeftType> mapper) { final Supplier<NewLeftType> leftSupp = () -> { - if(pairBound) return mapper.apply(boundPair.getLeft()); + if (pairBound) + return mapper.apply(boundPair.getLeft()); final NewLeft leftVal = binder.apply(oldSupplier.get()).getLeft(); @@ -137,7 +139,8 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL }; final Supplier<NewRight> rightSupp = () -> { - if(pairBound) return boundPair.getRight(); + if (pairBound) + return boundPair.getRight(); return binder.apply(oldSupplier.get()).getRight(); }; @@ -146,15 +149,18 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL } @Override - public <NewRightType> IPair<NewLeft, NewRightType> mapRight(final Function<NewRight, NewRightType> mapper) { + public <NewRightType> IPair<NewLeft, NewRightType> + mapRight(final Function<NewRight, NewRightType> mapper) { final Supplier<NewLeft> leftSupp = () -> { - if(pairBound) return boundPair.getLeft(); + if (pairBound) + return boundPair.getLeft(); return binder.apply(oldSupplier.get()).getLeft(); }; final Supplier<NewRightType> rightSupp = () -> { - if(pairBound) return mapper.apply(boundPair.getRight()); + if (pairBound) + return mapper.apply(boundPair.getRight()); final NewRight rightVal = binder.apply(oldSupplier.get()).getRight(); @@ -165,8 +171,9 @@ public class HalfBoundLazyPair<OldType, NewLeft, NewRight> implements IPair<NewL } @Override - public <MergedType> MergedType merge(final BiFunction<NewLeft, NewRight, MergedType> merger) { - if(!pairBound) { + public <MergedType> MergedType + merge(final BiFunction<NewLeft, NewRight, MergedType> merger) { + if (!pairBound) { boundPair = binder.apply(oldSupplier.get()); pairBound = true; diff --git a/src/main/java/bjc/data/internals/WrappedLazy.java b/src/main/java/bjc/data/internals/WrappedLazy.java index 17d2309..cda86fd 100644 --- a/src/main/java/bjc/data/internals/WrappedLazy.java +++ b/src/main/java/bjc/data/internals/WrappedLazy.java @@ -11,7 +11,7 @@ import bjc.data.Lazy; * * @author Ben Culkin * @param <ContainedType> - * The type of the wrapped value. + * The type of the wrapped value. */ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { /* Held value. */ @@ -21,19 +21,18 @@ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { * Create a new wrapped lazy value. * * @param wrappedHolder - * The holder to make lazy. + * The holder to make lazy. */ public WrappedLazy(final IHolder<ContainedType> wrappedHolder) { held = new Lazy<>(wrappedHolder); } /* - * This has an extra parameter, because otherwise it erases to the same - * as the public one. + * This has an extra parameter, because otherwise it erases to the same as the + * public one. * - * This is a case where reified generics would be useful, because then - * the compiler could know which one we meant without the dummy - * parameter. + * This is a case where reified generics would be useful, because then the + * compiler could know which one we meant without the dummy parameter. */ private WrappedLazy(final IHolder<IHolder<ContainedType>> wrappedHolder, @SuppressWarnings("unused") final boolean dummy) { @@ -41,8 +40,9 @@ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { } @Override - public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) { - final IHolder<IHolder<BoundType>> newHolder = held.map((containedHolder) -> { + public <BoundType> IHolder<BoundType> + bind(final Function<ContainedType, IHolder<BoundType>> binder) { + final IHolder<IHolder<BoundType>> newHolder = held.map(containedHolder -> { return containedHolder.bind(binder); }); @@ -50,15 +50,17 @@ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) { - return (val) -> { + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(final Function<ContainedType, NewType> func) { + return val -> { return new Lazy<>(func.apply(val)); }; } @Override - public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) { - final IHolder<IHolder<MappedType>> newHolder = held.map((containedHolder) -> { + public <MappedType> IHolder<MappedType> + map(final Function<ContainedType, MappedType> mapper) { + final IHolder<IHolder<MappedType>> newHolder = held.map(containedHolder -> { return containedHolder.map(mapper); }); @@ -66,8 +68,9 @@ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { } @Override - public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { - held.transform((containedHolder) -> { + public IHolder<ContainedType> + transform(final UnaryOperator<ContainedType> transformer) { + held.transform(containedHolder -> { return containedHolder.transform(transformer); }); @@ -75,8 +78,9 @@ public class WrappedLazy<ContainedType> implements IHolder<ContainedType> { } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { - return held.unwrap((containedHolder) -> { + public <UnwrappedType> UnwrappedType + unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { + return held.unwrap(containedHolder -> { return containedHolder.unwrap(unwrapper); }); } diff --git a/src/main/java/bjc/data/internals/WrappedOption.java b/src/main/java/bjc/data/internals/WrappedOption.java index eb4d120..6becc16 100644 --- a/src/main/java/bjc/data/internals/WrappedOption.java +++ b/src/main/java/bjc/data/internals/WrappedOption.java @@ -11,7 +11,7 @@ import bjc.data.Option; * * @author Ben Culkin. * @param <ContainedType> - * The wrapped type. + * The wrapped type. */ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { /* The held value. */ @@ -21,16 +21,15 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { * Create a new wrapped option. * * @param seedValue - * The value to wrap. + * The value to wrap. */ public WrappedOption(final IHolder<ContainedType> seedValue) { held = new Option<>(seedValue); } /* - * The dummy parameter is to ensure the compiler can pick the right - * method, because without this method erases to the same type as the - * public one. + * The dummy parameter is to ensure the compiler can pick the right method, + * because without this method erases to the same type as the public one. */ private WrappedOption(final IHolder<IHolder<ContainedType>> toHold, @SuppressWarnings("unused") final boolean dummy) { @@ -38,10 +37,12 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { } @Override - public <BoundType> IHolder<BoundType> bind(final Function<ContainedType, IHolder<BoundType>> binder) { - final IHolder<IHolder<BoundType>> newHolder = held.map((containedHolder) -> { + public <BoundType> IHolder<BoundType> + bind(final Function<ContainedType, IHolder<BoundType>> binder) { + final IHolder<IHolder<BoundType>> newHolder = held.map(containedHolder -> { return containedHolder.bind((containedValue) -> { - if(containedValue == null) return new Option<>(null); + if (containedValue == null) + return new Option<>(null); return binder.apply(containedValue); }); @@ -51,17 +52,20 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { } @Override - public <NewType> Function<ContainedType, IHolder<NewType>> lift(final Function<ContainedType, NewType> func) { - return (val) -> { + public <NewType> Function<ContainedType, IHolder<NewType>> + lift(final Function<ContainedType, NewType> func) { + return val -> { return new Option<>(func.apply(val)); }; } @Override - public <MappedType> IHolder<MappedType> map(final Function<ContainedType, MappedType> mapper) { - final IHolder<IHolder<MappedType>> newHolder = held.map((containedHolder) -> { + public <MappedType> IHolder<MappedType> + map(final Function<ContainedType, MappedType> mapper) { + final IHolder<IHolder<MappedType>> newHolder = held.map(containedHolder -> { return containedHolder.map((containedValue) -> { - if(containedValue == null) return null; + if (containedValue == null) + return null; return mapper.apply(containedValue); }); @@ -71,10 +75,12 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { } @Override - public IHolder<ContainedType> transform(final UnaryOperator<ContainedType> transformer) { - held.transform((containedHolder) -> { + public IHolder<ContainedType> + transform(final UnaryOperator<ContainedType> transformer) { + held.transform(containedHolder -> { return containedHolder.transform((containedValue) -> { - if(containedValue == null) return null; + if (containedValue == null) + return null; return transformer.apply(containedValue); }); @@ -84,10 +90,12 @@ public class WrappedOption<ContainedType> implements IHolder<ContainedType> { } @Override - public <UnwrappedType> UnwrappedType unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { - return held.unwrap((containedHolder) -> { + public <UnwrappedType> UnwrappedType + unwrap(final Function<ContainedType, UnwrappedType> unwrapper) { + return held.unwrap(containedHolder -> { return containedHolder.unwrap((containedValue) -> { - if(containedValue == null) return null; + if (containedValue == null) + return null; return unwrapper.apply(containedValue); }); |
