From 62a61213cc664a6704dd38bd6dfa2bd1ea97f774 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 23 Jun 2018 18:22:05 +0100 Subject: [PATCH] Added Result.maybe() --- src/main/java/net/kemitix/mon/result/Err.java | 7 +++ .../java/net/kemitix/mon/result/Result.java | 10 +++ .../java/net/kemitix/mon/result/Success.java | 10 +++ src/test/java/net/kemitix/mon/ResultTest.java | 61 +++++++++++++++++++ 4 files changed, 88 insertions(+) diff --git a/src/main/java/net/kemitix/mon/result/Err.java b/src/main/java/net/kemitix/mon/result/Err.java index a94318d..76431ac 100644 --- a/src/main/java/net/kemitix/mon/result/Err.java +++ b/src/main/java/net/kemitix/mon/result/Err.java @@ -22,9 +22,11 @@ package net.kemitix.mon.result; import lombok.RequiredArgsConstructor; +import net.kemitix.mon.maybe.Maybe; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Predicate; /** * An Error Result. @@ -60,4 +62,9 @@ class Err implements Result { public void match(final Consumer onSuccess, final Consumer onError) { onError.accept(error); } + + @Override + public Result> maybe(final Predicate predicate) { + return Result.error(error); + } } diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index 3bf2a81..37775a9 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -21,8 +21,11 @@ package net.kemitix.mon.result; +import net.kemitix.mon.maybe.Maybe; + import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Predicate; /** * An Either type for holding a result or an error (exception). @@ -97,4 +100,11 @@ public interface Result { */ void match(Consumer onSuccess, Consumer onError); + /** + * Wraps the value within the Result in a Maybe, either a Just if the predicate is true, or Nothing. + * + * @param predicate the test to decide + * @return a Result containing a Maybe that may or may not contain a value + */ + Result> maybe(Predicate predicate); } diff --git a/src/main/java/net/kemitix/mon/result/Success.java b/src/main/java/net/kemitix/mon/result/Success.java index 414c487..80dbea5 100644 --- a/src/main/java/net/kemitix/mon/result/Success.java +++ b/src/main/java/net/kemitix/mon/result/Success.java @@ -22,9 +22,11 @@ package net.kemitix.mon.result; import lombok.RequiredArgsConstructor; +import net.kemitix.mon.maybe.Maybe; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Predicate; /** * A Successful Result. @@ -60,4 +62,12 @@ class Success implements Result { public void match(final Consumer onSuccess, final Consumer onError) { onSuccess.accept(value); } + + @Override + public Result> maybe(final Predicate predicate) { + if (predicate.test(value)) { + return Result.ok(Maybe.just(value)); + } + return Result.ok(Maybe.nothing()); + } } diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index 4de77e3..1bbb9e8 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -1,10 +1,13 @@ package net.kemitix.mon; import lombok.RequiredArgsConstructor; +import net.kemitix.mon.maybe.Maybe; import net.kemitix.mon.result.Result; import org.assertj.core.api.WithAssertions; import org.junit.Test; +import java.util.function.Predicate; + public class ResultTest implements WithAssertions { @Test @@ -134,6 +137,64 @@ public class ResultTest implements WithAssertions { ); } + @Test + public void successMaybe_whenPasses_isSuccessJust() { + //given + final Result okResult = Result.ok(1); + //when + final Result> maybeResult = okResult.maybe(value -> value >= 0); + //then + assertThat(maybeResult.isOkay()).isTrue(); + maybeResult.match( + success -> assertThat(success.toOptional()).contains(1), + error -> fail("not an error") + ); + } + + @Test + public void successMaybe_whenFails_isSuccessNothing() { + //given + final Result okResult = Result.ok(1); + //when + final Result> maybeResult = okResult.maybe(value -> value >= 4); + //then + assertThat(maybeResult.isOkay()).isTrue(); + maybeResult.match( + success -> assertThat(success.toOptional()).isEmpty(), + error -> fail("not an error") + ); + } + + @Test + public void errorMaybe_whenPasses_isError() { + //given + final RuntimeException exception = new RuntimeException(); + final Result errorResult = Result.error(exception); + //when + final Result> maybeResult = errorResult.maybe(value -> value >= 0); + //then + assertThat(maybeResult.isError()).isTrue(); + maybeResult.match( + success -> fail("not a success"), + error -> assertThat(error).isSameAs(exception) + ); + } + + @Test + public void errorMaybe_whenFails_isError() { + //given + final RuntimeException exception = new RuntimeException(); + final Result errorResult = Result.error(exception); + //when + final Result> maybeResult = errorResult.maybe(value -> value >= 4); + //then + assertThat(maybeResult.isError()).isTrue(); + maybeResult.match( + success -> fail("not a success"), + error -> assertThat(error).isSameAs(exception) + ); + } + @Test public void useCase_whenOkay_thenReturnSuccess() { //given