Skip to content

Commit c87b7dd

Browse files
committed
Adding Try#orThrow
1 parent 76b0642 commit c87b7dd

File tree

2 files changed

+35
-2
lines changed
  • src
    • main/java/com/jnape/palatable/lambda/adt
    • test/java/com/jnape/palatable/lambda/adt

2 files changed

+35
-2
lines changed

src/main/java/com/jnape/palatable/lambda/adt/Try.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ public final T forfeit(Function<? super A, ? extends T> fn) {
9797
return match(id(), fn);
9898
}
9999

100+
/**
101+
* If this is a success value, return it. Otherwise, rethrow the captured failure.
102+
*
103+
* @return possibly the success value
104+
* @throws T the possible failure
105+
*/
106+
public abstract A orThrow() throws T;
107+
100108
/**
101109
* If this is a success, wrap the value in a {@link Maybe#just} and return it. Otherwise, return {@link
102110
* Maybe#nothing()}.
@@ -245,6 +253,11 @@ private Failure(T t) {
245253
this.t = t;
246254
}
247255

256+
@Override
257+
public A orThrow() throws T {
258+
throw t;
259+
}
260+
248261
@Override
249262
public <R> R match(Function<? super T, ? extends R> aFn, Function<? super A, ? extends R> bFn) {
250263
return aFn.apply(t);
@@ -275,6 +288,11 @@ private Success(A a) {
275288
this.a = a;
276289
}
277290

291+
@Override
292+
public A orThrow() throws T {
293+
return a;
294+
}
295+
278296
@Override
279297
public <R> R match(Function<? super T, ? extends R> aFn, Function<? super A, ? extends R> bFn) {
280298
return bFn.apply(a);

src/test/java/com/jnape/palatable/lambda/adt/TryTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.jnape.palatable.traitor.annotations.TestTraits;
44
import com.jnape.palatable.traitor.framework.Subjects;
55
import com.jnape.palatable.traitor.runners.Traits;
6+
import org.junit.Rule;
67
import org.junit.Test;
8+
import org.junit.rules.ExpectedException;
79
import org.junit.runner.RunWith;
810
import testsupport.traits.ApplicativeLaws;
911
import testsupport.traits.FunctorLaws;
@@ -18,8 +20,10 @@
1820
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
1921
import static com.jnape.palatable.lambda.adt.Try.failure;
2022
import static com.jnape.palatable.lambda.adt.Try.success;
23+
import static com.jnape.palatable.lambda.adt.Try.trying;
2124
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
2225
import static com.jnape.palatable.traitor.framework.Subjects.subjects;
26+
import static org.hamcrest.CoreMatchers.equalTo;
2327
import static org.hamcrest.CoreMatchers.instanceOf;
2428
import static org.junit.Assert.assertEquals;
2529
import static org.junit.Assert.assertThat;
@@ -28,6 +32,8 @@
2832
@RunWith(Traits.class)
2933
public class TryTest {
3034

35+
@Rule public ExpectedException thrown = ExpectedException.none();
36+
3137
@TestTraits({FunctorLaws.class, ApplicativeLaws.class, MonadLaws.class, TraversableLaws.class})
3238
public Subjects<Try<Throwable, Integer>> testSubject() {
3339
return subjects(failure(new IllegalStateException()), success(1));
@@ -108,6 +114,15 @@ public void recoverEnsuresSuccess() {
108114
assertEquals((Integer) 1, Try.<RuntimeException, Integer>failure(new IllegalArgumentException()).recover(constantly(1)));
109115
}
110116

117+
@Test
118+
public void orThrow() throws Throwable {
119+
assertEquals((Integer) 1, trying(() -> 1).orThrow());
120+
121+
Throwable expected = new Exception("expected");
122+
thrown.expect(equalTo(expected));
123+
trying(() -> {throw expected;}).orThrow();
124+
}
125+
111126
@Test
112127
public void toMaybe() {
113128
assertEquals(just("foo"), Try.success("foo").toMaybe());
@@ -131,8 +146,8 @@ public void toEitherWithLeftMappingFunction() {
131146
@Test
132147
public void tryingCatchesAnyThrowableThrownDuringEvaluation() {
133148
IllegalStateException expected = new IllegalStateException();
134-
assertEquals(failure(expected), Try.trying(() -> {throw expected;}));
149+
assertEquals(failure(expected), trying(() -> {throw expected;}));
135150

136-
assertEquals(success("foo"), Try.trying(() -> "foo"));
151+
assertEquals(success("foo"), trying(() -> "foo"));
137152
}
138153
}

0 commit comments

Comments
 (0)