From 3c60da27357be44af3ab8774a32758dddd366e86 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 9 Jul 2018 19:33:48 +0100 Subject: [PATCH] * Add `Result.maybeThen(Result>, Function)` --- CHANGELOG | 5 +- .../java/net/kemitix/mon/result/Result.java | 13 ++++ src/test/java/net/kemitix/mon/ResultTest.java | 67 +++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 843d704..7851cc2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,8 +4,9 @@ CHANGELOG 0.10.0 ------ -* Add `andThen(Function)` -* Add `thenWith(Function)` +* Add `Result.andThen(Function)` +* Add `Result.thenWith(Function)` +* Add `Result.maybeThen(Result>, Function)` 0.9.0 ----- diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index 45259e6..9b82836 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -120,6 +120,19 @@ public interface Result extends Functor> { .flatMap(value -> Result.ok(Maybe.maybe(value))); } + /** + * Applies the function to the contents of a Maybe within the Result. + * + * @param maybeResult the Result that may contain a value + * @param f the function to apply to the value + * @param the type of the original Result + * @param the type of the updated Result + * @return a new Maybe within a Result + */ + static Result> maybeThen(Result> maybeResult, Function, Result>> f) { + return maybeResult.flatMap(f); + } + /** * Checks of the Result is an error. * diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index ba39c77..8956906 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -604,6 +604,73 @@ public class ResultTest implements WithAssertions { assertThat(result).isSameAs(error); } + @Test + public void okayJust_whenMaybeThen_whereOkayJust_thenIsOkayJust() { + //given + final Result> okJust = Result.ok(Maybe.just(1)); + //when + final Result> result = Result.maybeThen(okJust, mv -> Result.ok(Maybe.just(2))); + //then + result.match( + success -> assertThat(success.toOptional()).contains(2), + error -> fail("Not an error") + ); + } + + @Test + public void okayJust_whenMaybeThen_whereOkayNothing_thenIsOkayNothing() { + //given + final Result> okJust = Result.ok(Maybe.just(1)); + //when + final Result> result = Result.maybeThen(okJust, v -> Result.ok(Maybe.nothing())); + //then + result.match( + success -> assertThat(success.toOptional()).isEmpty(), + error -> fail("Not an error") + ); + } + + @Test + public void okayJust_whenMaybeThen_whereError_thenIsError() { + //given + final Result> okJust = Result.ok(Maybe.just(1)); + final RuntimeException exception = new RuntimeException(); + //when + final Result> result = Result.maybeThen(okJust, v -> Result.error(exception)); + //then + result.match( + success -> fail("Not a success"), + error -> assertThat(error).isSameAs(exception) + ); + } + + @Test + public void okayNothing_whenMaybeThen_thenDoNotApply() { + //given + final Result> okNothing = Result.ok(Maybe.nothing()); + //when + final Result> result = Result.maybeThen(okNothing, v -> Result.ok(Maybe.just(2))); + //then + okNothing.match( + success -> assertThat(success.toOptional()).isEmpty(), + error -> fail("Not an error") + ); + } + + @Test + public void error_whenMaybeThen_thenDoNotApply() { + //given + final RuntimeException exception = new RuntimeException(); + final Result> maybeResult = Result.error(exception); + //when + final Result> result = Result.maybeThen(maybeResult, v -> Result.ok(Maybe.just(2))); + //then + maybeResult.match( + success -> fail("Not a success"), + error -> assertThat(error).isSameAs(exception) + ); + } + @RequiredArgsConstructor private static class UseCase {