From 169c7404bef2838d86c49f721e0f6e7e06938db1 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sun, 8 Nov 2020 19:49:41 -0500 Subject: Do some cleanup of things --- src/main/java/bjc/data/Either.java | 80 +++++++++++++++----------------------- 1 file changed, 31 insertions(+), 49 deletions(-) (limited to 'src/main/java/bjc/data/Either.java') diff --git a/src/main/java/bjc/data/Either.java b/src/main/java/bjc/data/Either.java index 55518d8..0ac3181 100644 --- a/src/main/java/bjc/data/Either.java +++ b/src/main/java/bjc/data/Either.java @@ -75,8 +75,7 @@ public class Either implements IPair { @Override public IPair bind( final BiFunction> binder) { - if (binder == null) - throw new NullPointerException("Binder must not be null"); + if (binder == null) throw new NullPointerException("Binder must not be null"); return binder.apply(leftVal, rightVal); } @@ -84,25 +83,19 @@ public class Either implements IPair { @Override public IPair bindLeft(final Function> leftBinder) { - if (leftBinder == null) - throw new NullPointerException("Left binder must not be null"); + if (leftBinder == null) throw new NullPointerException("Left binder must not be null"); - if (isLeft) - return leftBinder.apply(leftVal); - - return new Either<>(null, rightVal); + if (isLeft) return leftBinder.apply(leftVal); + else return new Either<>(null, rightVal); } @Override public IPair bindRight( final Function> rightBinder) { - if (rightBinder == null) - throw new NullPointerException("Right binder must not be null"); - - if (isLeft) - return new Either<>(leftVal, null); + if (rightBinder == null) throw new NullPointerException("Right binder must not be null"); - return rightBinder.apply(rightVal); + if (isLeft) return new Either<>(leftVal, null); + else return rightBinder.apply(rightVal); } @Override @@ -126,44 +119,37 @@ public class Either implements IPair { return new Either<>(cLeft, null); }); + } else { + return otherPair.bind((otherLeft, otherRight) -> { + CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); + + return new Either<>(null, cRight); + }); } - - return otherPair.bind((otherLeft, otherRight) -> { - CombinedRight cRight = rightCombiner.apply(rightVal, otherRight); - - return new Either<>(null, cRight); - }); } @Override public IPair mapLeft(final Function mapper) { - if (mapper == null) - throw new NullPointerException("Mapper must not be null"); - - if (isLeft) - return new Either<>(mapper.apply(leftVal), null); + if (mapper == null) throw new NullPointerException("Mapper must not be null"); - return new Either<>(null, rightVal); + if (isLeft) return new Either<>(mapper.apply(leftVal), null); + else return new Either<>(null, rightVal); } @Override public IPair mapRight(final Function mapper) { - if (mapper == null) - throw new NullPointerException("Mapper must not be null"); + if (mapper == null) throw new NullPointerException("Mapper must not be null"); - if (isLeft) - return new Either<>(leftVal, null); - - return new Either<>(null, mapper.apply(rightVal)); + if (isLeft) return new Either<>(leftVal, null); + else return new Either<>(null, mapper.apply(rightVal)); } @Override public MergedType merge(final BiFunction merger) { - if (merger == null) - throw new NullPointerException("Merger must not be null"); + if (merger == null) throw new NullPointerException("Merger must not be null"); return merger.apply(leftVal, rightVal); } @@ -182,30 +168,26 @@ public class Either implements IPair { @Override public boolean equals(final Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof Either)) - return false; + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof Either)) return false; final Either other = (Either) obj; - if (isLeft != other.isLeft) - return false; + if (isLeft != other.isLeft) return false; if (leftVal == null) { - if (other.leftVal != null) - return false; - } else if (!leftVal.equals(other.leftVal)) + if (other.leftVal != null) return false; + } else if (!leftVal.equals(other.leftVal)) { return false; + } if (rightVal == null) { - if (other.rightVal != null) - return false; - } else if (!rightVal.equals(other.rightVal)) + if (other.rightVal != null) return false; + } else if (!rightVal.equals(other.rightVal)) { return false; - + } + return true; } -- cgit v1.2.3