Skip to content

Commit

Permalink
add orElseThrow
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Jul 13, 2021
1 parent e78a686 commit 7fe2c9f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/main/java/io/jbock/util/Either.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ public abstract void ifLeftOrElse(
Consumer<? super L> leftAction,
Consumer<? super R> rightAction);

/**
* If this is a Right, returns the RHS value.
* Otherwise throws an exception produced by the exception supplying function.
*
* @param exceptionSupplier exception supplying function
* @param <X> type of the exception
* @return the RHS value, if this is a Right
* @throws X the result of applying {@code exceptionSupplier} to the LHS value, if this is a Left
*/
public abstract <X extends Throwable> R orElseThrow(
Function<? super L, ? extends X> exceptionSupplier) throws X;

/**
* Returns {@code true} if this is a Left, otherwise {@code false}.
*
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/jbock/util/Left.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public void ifLeftOrElse(Consumer<? super L> leftAction, Consumer<? super R> rig
leftAction.accept(value);
}

@Override
public <X extends Throwable> R orElseThrow(Function<? super L, ? extends X> exceptionSupplier) throws X {
throw exceptionSupplier.apply(value);
}

@Override
public String toString() {
return String.format("Left[%s]", value);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/jbock/util/Right.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public void ifLeftOrElse(Consumer<? super L> leftAction, Consumer<? super R> rig
rightAction.accept(value);
}

@Override
public <X extends Throwable> R orElseThrow(Function<? super L, ? extends X> exceptionSupplier) {
return value;
}

@Override
public String toString() {
return String.format("Right[%s]", value);
Expand Down
13 changes: 12 additions & 1 deletion src/test/java/io/jbock/util/EitherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.google.common.testing.EqualsTester;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Objects;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class EitherTest {
Expand Down Expand Up @@ -106,7 +108,7 @@ void testFilterLeft() {
}

@Test
void testAccept() {
void testIfLeftOrElse() {
String[] output = {"1"};
Either<Integer, Integer> left = Either.left(1);
left.ifLeftOrElse(l -> output[0] = "L", r -> output[0] = "R");
Expand All @@ -123,4 +125,13 @@ void testFold() {
Either<String, Integer> right = Either.right(2);
assertEquals("2", right.fold(Objects::toString, Objects::toString));
}

@Test
void testOrElseThrow() {
Either<String, ?> left = Either.left("1");
IOException x = assertThrows(IOException.class, () -> left.orElseThrow(IOException::new));
assertEquals("1", x.getMessage());
Either<String, String> right = Either.right("2");
assertEquals("2", right.orElseThrow(IllegalArgumentException::new));
}
}

0 comments on commit 7fe2c9f

Please sign in to comment.