From 47193e64808cebb610d82555308124eff5961232 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 23 Jun 2018 18:14:19 +0100 Subject: [PATCH] Add Result.map() --- src/main/java/net/kemitix/mon/result/Err.java | 5 ++++ .../java/net/kemitix/mon/result/Result.java | 12 +++++++- .../java/net/kemitix/mon/result/Success.java | 5 ++++ src/test/java/net/kemitix/mon/ResultTest.java | 29 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/kemitix/mon/result/Err.java b/src/main/java/net/kemitix/mon/result/Err.java index e454ade..a94318d 100644 --- a/src/main/java/net/kemitix/mon/result/Err.java +++ b/src/main/java/net/kemitix/mon/result/Err.java @@ -51,6 +51,11 @@ class Err implements Result { return Result.error(error); } + @Override + public Result map(final Function f) { + return Result.error(error); + } + @Override public void match(final Consumer onSuccess, final Consumer onError) { onError.accept(error); diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index 30f446b..3bf2a81 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -73,12 +73,22 @@ public interface Result { * Returns a new Result consisting of the result of applying the function to the contents of the Result. * * @param f the mapping function the produces a Result - * @param the type of the result of the mapping function + * @param the type of the value withing the Result of the mapping function * * @return a Result */ Result flatMap(Function> f); + /** + * Applies the functions to the value of a successful result, while doing nothing with an error. + * + * @param f the mapping function to produce the new value + * @param the type of the result of the mapping function + * + * @return a Result + */ + Result map(Function f); + /** * Matches the Result, either success or error, and supplies the appropriate Consumer with the value or error. * diff --git a/src/main/java/net/kemitix/mon/result/Success.java b/src/main/java/net/kemitix/mon/result/Success.java index 413d7c5..414c487 100644 --- a/src/main/java/net/kemitix/mon/result/Success.java +++ b/src/main/java/net/kemitix/mon/result/Success.java @@ -51,6 +51,11 @@ class Success implements Result { return f.apply(value); } + @Override + public Result map(final Function f) { + return Result.ok(f.apply(value)); + } + @Override public void match(final Consumer onSuccess, final Consumer onError) { onSuccess.accept(value); diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index 82eeebc..4de77e3 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -105,6 +105,35 @@ public class ResultTest implements WithAssertions { assertThat(flatMap.isError()).isTrue(); } + @Test + public void success_whenMap_isSuccess() { + //given + final Result okResult = Result.ok(1); + //when + final Result result = okResult.map(value -> String.valueOf(value)); + //then + assertThat(result.isOkay()).isTrue(); + result.match( + success -> assertThat(success).isEqualTo("1"), + error -> fail("not an error") + ); + } + + @Test + public void error_whenMap_isError() { + //given + final RuntimeException exception = new RuntimeException(); + final Result errorResult = Result.error(exception); + //when + final Result result = errorResult.map(value -> String.valueOf(value)); + //then + assertThat(result.isError()).isTrue(); + result.match( + success -> fail("not an success"), + error -> assertThat(error).isSameAs(exception) + ); + } + @Test public void useCase_whenOkay_thenReturnSuccess() { //given