Skip to content

Commit 7fe2c9f

Browse files
committed
add orElseThrow
1 parent e78a686 commit 7fe2c9f

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

src/main/java/io/jbock/util/Either.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ public abstract void ifLeftOrElse(
179179
Consumer<? super L> leftAction,
180180
Consumer<? super R> rightAction);
181181

182+
/**
183+
* If this is a Right, returns the RHS value.
184+
* Otherwise throws an exception produced by the exception supplying function.
185+
*
186+
* @param exceptionSupplier exception supplying function
187+
* @param <X> type of the exception
188+
* @return the RHS value, if this is a Right
189+
* @throws X the result of applying {@code exceptionSupplier} to the LHS value, if this is a Left
190+
*/
191+
public abstract <X extends Throwable> R orElseThrow(
192+
Function<? super L, ? extends X> exceptionSupplier) throws X;
193+
182194
/**
183195
* Returns {@code true} if this is a Left, otherwise {@code false}.
184196
*

src/main/java/io/jbock/util/Left.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public void ifLeftOrElse(Consumer<? super L> leftAction, Consumer<? super R> rig
8080
leftAction.accept(value);
8181
}
8282

83+
@Override
84+
public <X extends Throwable> R orElseThrow(Function<? super L, ? extends X> exceptionSupplier) throws X {
85+
throw exceptionSupplier.apply(value);
86+
}
87+
8388
@Override
8489
public String toString() {
8590
return String.format("Left[%s]", value);

src/main/java/io/jbock/util/Right.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public void ifLeftOrElse(Consumer<? super L> leftAction, Consumer<? super R> rig
8080
rightAction.accept(value);
8181
}
8282

83+
@Override
84+
public <X extends Throwable> R orElseThrow(Function<? super L, ? extends X> exceptionSupplier) {
85+
return value;
86+
}
87+
8388
@Override
8489
public String toString() {
8590
return String.format("Right[%s]", value);

src/test/java/io/jbock/util/EitherTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import com.google.common.testing.EqualsTester;
44
import org.junit.jupiter.api.Test;
55

6+
import java.io.IOException;
67
import java.util.Objects;
78
import java.util.Optional;
89

910
import static org.junit.jupiter.api.Assertions.assertEquals;
1011
import static org.junit.jupiter.api.Assertions.assertFalse;
1112
import static org.junit.jupiter.api.Assertions.assertSame;
13+
import static org.junit.jupiter.api.Assertions.assertThrows;
1214
import static org.junit.jupiter.api.Assertions.assertTrue;
1315

1416
class EitherTest {
@@ -106,7 +108,7 @@ void testFilterLeft() {
106108
}
107109

108110
@Test
109-
void testAccept() {
111+
void testIfLeftOrElse() {
110112
String[] output = {"1"};
111113
Either<Integer, Integer> left = Either.left(1);
112114
left.ifLeftOrElse(l -> output[0] = "L", r -> output[0] = "R");
@@ -123,4 +125,13 @@ void testFold() {
123125
Either<String, Integer> right = Either.right(2);
124126
assertEquals("2", right.fold(Objects::toString, Objects::toString));
125127
}
128+
129+
@Test
130+
void testOrElseThrow() {
131+
Either<String, ?> left = Either.left("1");
132+
IOException x = assertThrows(IOException.class, () -> left.orElseThrow(IOException::new));
133+
assertEquals("1", x.getMessage());
134+
Either<String, String> right = Either.right("2");
135+
assertEquals("2", right.orElseThrow(IllegalArgumentException::new));
136+
}
126137
}

0 commit comments

Comments
 (0)