From f0be46302894db9b1eacd963f29666c02ee5c1b3 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 24 Jun 2018 22:36:02 +0100 Subject: [PATCH] Add Result.toMaybe() and Result.invert() --- .../java/net/kemitix/mon/result/Result.java | 30 ++++++++++ src/test/java/net/kemitix/mon/ResultTest.java | 60 +++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index 018db6a..ea0de64 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -34,6 +34,7 @@ import java.util.function.Supplier; * @param the type of the result when a success * @author Paul Campbell (pcampbell@kemitix.net) */ +@SuppressWarnings("methodcount") public interface Result { /** @@ -71,6 +72,35 @@ public interface Result { return new Success<>(value); } + /** + * Creates a Result from the Maybe, where the Result will be an error if the Maybe is Nothing. + * + * @param result the Result the might contain the value of the Result + * @param the type of the Maybe and the Result + * @return a Result containing the value of the Maybe when it is a Just, or the error when it is Nothing + */ + @SuppressWarnings("illegalcatch") + static Maybe toMaybe(final Result result) { + try { + return Maybe.just(result.orElseThrow()); + } catch (final Throwable throwable) { + return Maybe.nothing(); + } + } + + /** + * Swaps the inner Result of a Maybe, so that a Result is on the outside. + * @param maybeResult the Maybe the contains a Result + * @param the type of the value that may be in the Result + * @return a Result containing a Maybe, the value in the Maybe was the value in a successful Result within the + * original Maybe. If the original Maybe is Nothing, the Result will contain Nothing. If the original Result was an + * error, then the Result will also be an error. + */ + static Result> invert(final Maybe> maybeResult) { + return maybeResult.orElseGet(() -> Result.ok(null)) + .flatMap(value -> Result.ok(Maybe.maybe(value))); + } + /** * 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 df36e63..7c25651 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -222,6 +222,26 @@ public class ResultTest implements WithAssertions { ); } + @Test + public void success_toMaybe_isJust() { + //given + final Result ok = Result.ok(1); + //when + final Maybe maybe = Result.toMaybe(ok); + //then + assertThat(maybe.toOptional()).contains(1); + } + + @Test + public void error_toMaybe_isNothing() { + //given + final Result error = Result.error(new RuntimeException()); + //when + final Maybe maybe = Result.toMaybe(error); + //then + assertThat(maybe.toOptional()).isEmpty(); + } + @Test public void success_whenOrElseThrow_isValue() throws Throwable { //given @@ -242,6 +262,46 @@ public class ResultTest implements WithAssertions { .isSameAs(exception); } + @Test + public void JustSuccess_invert_thenSuccessJust() { + //given + final Maybe> justSuccess = Maybe.just(Result.ok(1)); + //when + final Result> result = Result.invert(justSuccess); + //then + result.match( + success -> assertThat(success.toOptional()).contains(1), + error -> fail("Not an error") + ); + } + + @Test + public void JustError_invert_thenError() { + //given + final RuntimeException exception = new RuntimeException(); + final Maybe> justError = Maybe.just(Result.error(exception)); + //when + final Result> result = Result.invert(justError); + //then + result.match( + success -> fail("Not a success"), + error -> assertThat(error).isSameAs(exception) + ); + } + + @Test + public void Nothing_invert_thenSuccessNothing() { + //given + final Maybe> nothing = Maybe.nothing(); + //when + final Result> result = Result.invert(nothing); + //then + result.match( + success -> assertThat(success.toOptional()).isEmpty(), + error -> fail("Not an error") + ); + } + @Test public void useCase_whenOkay_thenReturnSuccess() { //given