From dca8e9f586fd595a7995f07788318fb92b8cce79 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 28 Jul 2016 16:44:36 -0400 Subject: Format/Cleanup pass --- .../src/main/java/bjc/utils/data/Either.java | 89 +++++++++++----------- 1 file changed, 44 insertions(+), 45 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/Either.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java index 8787888..9418882 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Either.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Either.java @@ -15,21 +15,6 @@ import java.util.function.Function; */ public class Either implements IPair { - private LeftType leftVal; - private RightType rightVal; - - private boolean isLeft; - - private Either(LeftType left, RightType right) { - if (left == null) { - rightVal = right; - } else { - leftVal = left; - - isLeft = true; - } - } - /** * Create a new either with the left value occupied * @@ -41,11 +26,10 @@ public class Either * The value to put on the left * @return An either with the left side occupied */ - public static Either - fromLeft(LeftType left) { + public static Either fromLeft( + LeftType left) { return new Either<>(left, null); } - /** * Create a new either with the right value occupied * @@ -57,11 +41,27 @@ public class Either * The value to put on the right * @return An either with the right side occupied */ - public static Either - fromRight(RightType right) { + public static Either fromRight( + RightType right) { return new Either<>(null, right); } + private LeftType leftVal; + + private RightType rightVal; + + private boolean isLeft; + + private Either(LeftType left, RightType right) { + if (left == null) { + rightVal = right; + } else { + leftVal = left; + + isLeft = true; + } + } + @Override public IPair bind( BiFunction> binder) { @@ -89,8 +89,26 @@ public class Either } @Override - public IPair - mapLeft(Function mapper) { + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction rightCombiner) { + if (isLeft) { + return otherPair.bind((otherLeft, otherRight) -> { + return new Either<>(leftCombiner.apply(leftVal, otherLeft), + null); + }); + } + + return otherPair.bind((otherLeft, otherRight) -> { + return new Either<>(null, + rightCombiner.apply(rightVal, otherRight)); + }); + } + + @Override + public IPair mapLeft( + Function mapper) { if (isLeft) { return new Either<>(mapper.apply(leftVal), null); } @@ -99,8 +117,8 @@ public class Either } @Override - public IPair - mapRight(Function mapper) { + public IPair mapRight( + Function mapper) { if (isLeft) { return new Either<>(leftVal, null); } @@ -109,27 +127,8 @@ public class Either } @Override - public MergedType - merge(BiFunction merger) { + public MergedType merge( + BiFunction merger) { return merger.apply(leftVal, rightVal); } - - @Override - public - IPair - combine(IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { - if (isLeft) { - return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(leftCombiner.apply(leftVal, otherLeft), - null); - }); - } - - return otherPair.bind((otherLeft, otherRight) -> { - return new Either<>(null, - rightCombiner.apply(rightVal, otherRight)); - }); - } } -- cgit v1.2.3