From 946cab444bc301d8a7c756a1bab039558288de89 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Wed, 11 Oct 2017 13:41:07 -0300 Subject: Cleanup work --- base/src/main/java/bjc/utils/data/Either.java | 70 +++++++++++++++++---------- 1 file changed, 45 insertions(+), 25 deletions(-) (limited to 'base/src/main/java/bjc/utils/data/Either.java') diff --git a/base/src/main/java/bjc/utils/data/Either.java b/base/src/main/java/bjc/utils/data/Either.java index 36b3324..2b64feb 100644 --- a/base/src/main/java/bjc/utils/data/Either.java +++ b/base/src/main/java/bjc/utils/data/Either.java @@ -4,52 +4,64 @@ import java.util.function.BiFunction; import java.util.function.Function; /** - * Represents a pair where only one side has a value + * Represents a pair where only one side has a value. * * @author ben + * * @param - * The type that could be on the left + * The type that could be on the left. + * * @param - * The type that could be on the right + * The type that could be on the right. * */ public class Either implements IPair { /** - * Create a new either with the left value occupied + * Create a new either with the left value occupied. * * @param - * The type of the left value + * The type of the left value. + * * @param - * The type of the empty right value + * The type of the empty right value. + * * @param left - * The value to put on the left - * @return An either with the left side occupied + * The value to put on the left. + * + * @return + * An either with the left side occupied. */ public static Either left(final LeftType left) { return new Either<>(left, null); } /** - * Create a new either with the right value occupied + * Create a new either with the right value occupied. * * @param - * The type of the empty left value + * The type of the empty left value. + * * @param - * The type of the right value + * The type of the right value. + * * @param right - * The value to put on the right - * @return An either with the right side occupied + * The value to put on the right. + * + * @return + * An either with the right side occupied. */ public static Either right(final RightType right) { return new Either<>(null, right); } + /* The left value of the either. */ private LeftType leftVal; - + /* The right value of the either. */ private RightType rightVal; - + /* Whether the left value is the one filled out. */ private boolean isLeft; + /* Create a new either with specifed values. */ private Either(final LeftType left, final RightType right) { if (left == null) { rightVal = right; @@ -93,19 +105,27 @@ public class Either implements IPair { final IPair otherPair, final BiFunction leftCombiner, final BiFunction rightCombiner) { - if (otherPair == null) + 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) throw new NullPointerException("Right combiner must not be null"); + } else if (rightCombiner == null) { + throw new NullPointerException("Right combiner must not be null"); + } - if (isLeft) return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(leftCombiner.apply(leftVal, otherLeft), null); - }); + if (isLeft) { + return otherPair.bind((otherLeft, otherRight) -> { + CombinedLeft cLeft = leftCombiner.apply(leftVal, otherLeft); - return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(null, rightCombiner.apply(rightVal, otherRight)); - }); + return new Either<>(cLeft, null); + }); + } else { + return otherPair.bind((otherLeft, otherRight) -> { + CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); + + return new Either<>(null, cRight); + }); + } } @Override @@ -170,4 +190,4 @@ public class Either implements IPair { public String toString() { return String.format("Either [leftVal='%s', rightVal='%s', isLeft=%s]", leftVal, rightVal, isLeft); } -} \ No newline at end of file +} -- cgit v1.2.3