Skip to content

Commit

Permalink
Commonize no value exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
2bllw8 committed Sep 9, 2021
1 parent 8e935ec commit a975e63
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
15 changes: 12 additions & 3 deletions lib/src/main/java/exe/bbllw8/either/Either.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
Expand All @@ -17,7 +18,7 @@
*
* <code>null</code> values are not accepted and will throw exceptions
* if used in an Either instance.
*
* <p>
* Construct an instance using one of:
* <ul>
* <li>{@link #left(Object)}: instance with a left value</li>
Expand All @@ -33,15 +34,15 @@ public abstract class Either<L, R> {

/**
* Whether this either has a left value.
*
* <p>
* If this is <code>true</code>, {@link #getRight()}
* must be <code>false</code>.
*/
public abstract boolean isLeft();

/**
* Whether this either has a right value.
*
* <p>
* If this is <code>true</code>, {@link #getLeft()}
* must be <code>false</code>.
*/
Expand Down Expand Up @@ -167,4 +168,12 @@ public static <L, R> Collection<R> rightValues(List<Either<L, R>> list) {
public static <T> T reduce(Either<T, T> either) {
return Objects.requireNonNull(either).either(Function.identity(), Function.identity());
}

protected static NoSuchElementException noLeftValueException() {
return new NoSuchElementException("No left value present");
}

protected static NoSuchElementException noRightValueException() {
return new NoSuchElementException("No right value present");
}
}
7 changes: 3 additions & 4 deletions lib/src/main/java/exe/bbllw8/either/LeftEither.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package exe.bbllw8.either;

import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
Expand Down Expand Up @@ -34,18 +33,18 @@ public boolean isRight() {

@Override
public L getLeft() {
return left.orElseThrow(() -> new NoSuchElementException("No left value present"));
return left.orElseThrow(Either::noLeftValueException);
}

@Override
public R getRight() {
throw new NoSuchElementException("No right value present");
throw noRightValueException();
}

@Override
public <T> T either(Function<? super L, ? extends T> mapLeft,
Function<? super R, ? extends T> mapRight) {
return left.map(mapLeft).orElseThrow(() -> new NoSuchElementException("No left value present"));
return left.map(mapLeft).orElseThrow(Either::noLeftValueException);
}

@Override
Expand Down
7 changes: 3 additions & 4 deletions lib/src/main/java/exe/bbllw8/either/RightEither.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package exe.bbllw8.either;

import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
Expand Down Expand Up @@ -34,18 +33,18 @@ public boolean isRight() {

@Override
public L getLeft() {
throw new NoSuchElementException("No left value present");
throw noLeftValueException();
}

@Override
public R getRight() {
return right.orElseThrow(() -> new NoSuchElementException("No right value present"));
return right.orElseThrow(Either::noRightValueException);
}

@Override
public <T> T either(Function<? super L, ? extends T> mapLeft,
Function<? super R, ? extends T> mapRight) {
return right.map(mapRight).orElseThrow(() -> new NoSuchElementException("No right value present"));
return right.map(mapRight).orElseThrow(Either::noRightValueException);
}

@Override
Expand Down

0 comments on commit a975e63

Please sign in to comment.