From 9923bbc880d75dd762e14f2f5b8fc6b37fecd775 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 30 Jun 2018 18:13:29 +0100 Subject: [PATCH] Add `Result.peek(Consumer)` --- .../java/net/kemitix/mon/maybe/Maybe.java | 4 +-- src/main/java/net/kemitix/mon/result/Err.java | 6 +++++ .../java/net/kemitix/mon/result/Result.java | 8 ++++++ .../java/net/kemitix/mon/result/Success.java | 7 ++++++ src/test/java/net/kemitix/mon/ResultTest.java | 25 +++++++++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/kemitix/mon/maybe/Maybe.java b/src/main/java/net/kemitix/mon/maybe/Maybe.java index b4fffd8..02b1ffc 100644 --- a/src/main/java/net/kemitix/mon/maybe/Maybe.java +++ b/src/main/java/net/kemitix/mon/maybe/Maybe.java @@ -136,10 +136,10 @@ public interface Maybe extends Functor> { Maybe filter(Predicate predicate); /** - * Provide the value within the Maybe, if it exists, to the Supplier, and returns the Maybe. + * Provide the value within the Maybe, if it exists, to the Consumer, and returns this Maybe. * * @param consumer the Consumer to the value if present - * @return the Maybe + * @return this Maybe */ Maybe peek(Consumer consumer); } diff --git a/src/main/java/net/kemitix/mon/result/Err.java b/src/main/java/net/kemitix/mon/result/Err.java index e197dea..0e54336 100644 --- a/src/main/java/net/kemitix/mon/result/Err.java +++ b/src/main/java/net/kemitix/mon/result/Err.java @@ -35,6 +35,7 @@ import java.util.function.Predicate; * @param the type of the value in the Result if it has been a success */ @RequiredArgsConstructor +@SuppressWarnings("methodcount") class Err implements Result { private final Throwable error; @@ -74,6 +75,11 @@ class Err implements Result { throw error; } + @Override + public Result peek(final Consumer consumer) { + return this; + } + @Override public boolean equals(final Object other) { return other instanceof Err && Objects.equals(error, ((Err) other).error); diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index 30043e2..4dd6d65 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -169,4 +169,12 @@ public interface Result extends Functor> { */ @SuppressWarnings("illegalthrows") T orElseThrow() throws Throwable; + + /** + * Provide the value within the Result, if it is a success, to the Consumer, and returns this Result. + * + * @param consumer the Consumer to the value if a success + * @return this Result + */ + Result peek(Consumer consumer); } diff --git a/src/main/java/net/kemitix/mon/result/Success.java b/src/main/java/net/kemitix/mon/result/Success.java index ec2aa58..7c880fa 100644 --- a/src/main/java/net/kemitix/mon/result/Success.java +++ b/src/main/java/net/kemitix/mon/result/Success.java @@ -35,6 +35,7 @@ import java.util.function.Predicate; * @param the type of the value in the Result */ @RequiredArgsConstructor +@SuppressWarnings("methodcount") class Success implements Result { private final T value; @@ -77,6 +78,12 @@ class Success implements Result { return value; } + @Override + public Result peek(final Consumer consumer) { + consumer.accept(value); + return this; + } + @Override public boolean equals(final Object other) { return other instanceof Success && Objects.equals(value, ((Success) other).value); diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index e8b24cd..9e5c52c 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -8,6 +8,7 @@ import org.junit.Test; import java.io.IOException; import java.util.concurrent.Callable; +import java.util.concurrent.atomic.AtomicReference; public class ResultTest implements WithAssertions { @@ -427,6 +428,30 @@ public class ResultTest implements WithAssertions { ); } + @Test + public void success_peek_consumes() { + //given + final Result result = Result.ok(1); + final AtomicReference consumed = new AtomicReference<>(0); + //when + final Result peeked = result.peek(consumed::set); + //then + assertThat(consumed).hasValue(1); + assertThat(peeked).isSameAs(result); + } + + @Test + public void error_peek_doesNothing() { + //given + final Result result = Result.error(new RuntimeException()); + final AtomicReference consumed = new AtomicReference<>(0); + //when + final Result peeked = result.peek(consumed::set); + //then + assertThat(consumed).hasValue(0); + assertThat(peeked).isSameAs(result); + } + @RequiredArgsConstructor private static class UseCase {