From d1f4c61f00d2ab1f22b9aac32f1db77812864bbe Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 19 Aug 2021 11:35:05 +0100 Subject: [PATCH] Result.onSuccess should return Result/ResultVoid (#224) * Version set to 3.2.0 * Result{Void}.onSuccess() now returns a Result/ResultVoid --- pom.xml | 2 +- src/main/java/net/kemitix/mon/result/Err.java | 4 +- .../java/net/kemitix/mon/result/ErrVoid.java | 4 +- .../java/net/kemitix/mon/result/Result.java | 3 +- .../net/kemitix/mon/result/ResultVoid.java | 3 +- .../java/net/kemitix/mon/result/Success.java | 3 +- .../net/kemitix/mon/result/SuccessVoid.java | 3 +- src/test/java/net/kemitix/mon/ResultTest.java | 46 +++++++++++++++++++ 8 files changed, 59 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index b1b9c28..e5857a4 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ mon - 3.1.0 + 3.2.0 Mon Wrapper, TypeAlias, Maybe, Result, Tree, Lazy, Either and Combinators for Java. diff --git a/src/main/java/net/kemitix/mon/result/Err.java b/src/main/java/net/kemitix/mon/result/Err.java index 6082cb8..0f9ec45 100644 --- a/src/main/java/net/kemitix/mon/result/Err.java +++ b/src/main/java/net/kemitix/mon/result/Err.java @@ -101,8 +101,8 @@ class Err implements Result { } @Override - public void onSuccess(final Consumer successConsumer) { - // do nothing + public Result onSuccess(final Consumer successConsumer) { + return this; } @Override diff --git a/src/main/java/net/kemitix/mon/result/ErrVoid.java b/src/main/java/net/kemitix/mon/result/ErrVoid.java index edb3f76..fd2da27 100644 --- a/src/main/java/net/kemitix/mon/result/ErrVoid.java +++ b/src/main/java/net/kemitix/mon/result/ErrVoid.java @@ -39,8 +39,8 @@ public class ErrVoid implements ResultVoid { } @Override - public void onSuccess(final Runnable runnable) { - // do nothing + public ResultVoid onSuccess(final Runnable runnable) { + return this; } @Override diff --git a/src/main/java/net/kemitix/mon/result/Result.java b/src/main/java/net/kemitix/mon/result/Result.java index 20563a2..f1111ed 100644 --- a/src/main/java/net/kemitix/mon/result/Result.java +++ b/src/main/java/net/kemitix/mon/result/Result.java @@ -707,9 +707,10 @@ public interface Result extends BaseResult, ThrowableFunctor * * @param successConsumer the consumer to handle the success + * @return the original Result */ @API(status = STABLE) - void onSuccess(Consumer successConsumer); + Result onSuccess(Consumer successConsumer); /** * A handler for error state, when the error matches the errorClass. diff --git a/src/main/java/net/kemitix/mon/result/ResultVoid.java b/src/main/java/net/kemitix/mon/result/ResultVoid.java index 74c9e43..0bf9259 100644 --- a/src/main/java/net/kemitix/mon/result/ResultVoid.java +++ b/src/main/java/net/kemitix/mon/result/ResultVoid.java @@ -66,9 +66,10 @@ public interface ResultVoid extends BaseResult { * success value. When this is an error, then nothing happens.

* * @param runnable the call if the Result is a success + * @return the original ResultVoid */ @API(status = STABLE) - void onSuccess(Runnable runnable); + ResultVoid onSuccess(Runnable runnable); /** * A handler for error state, when the error matches the errorClass. diff --git a/src/main/java/net/kemitix/mon/result/Success.java b/src/main/java/net/kemitix/mon/result/Success.java index 89a8c03..03d9096 100644 --- a/src/main/java/net/kemitix/mon/result/Success.java +++ b/src/main/java/net/kemitix/mon/result/Success.java @@ -93,8 +93,9 @@ class Success implements Result { } @Override - public void onSuccess(final Consumer successConsumer) { + public Result onSuccess(final Consumer successConsumer) { successConsumer.accept(value); + return this; } @Override diff --git a/src/main/java/net/kemitix/mon/result/SuccessVoid.java b/src/main/java/net/kemitix/mon/result/SuccessVoid.java index 5b70d64..11b9598 100644 --- a/src/main/java/net/kemitix/mon/result/SuccessVoid.java +++ b/src/main/java/net/kemitix/mon/result/SuccessVoid.java @@ -47,8 +47,9 @@ public class SuccessVoid implements ResultVoid { } @Override - public void onSuccess(final Runnable runnable) { + public ResultVoid onSuccess(final Runnable runnable) { runnable.run(); + return this; } @Override diff --git a/src/test/java/net/kemitix/mon/ResultTest.java b/src/test/java/net/kemitix/mon/ResultTest.java index 0b92742..ba4f4c1 100644 --- a/src/test/java/net/kemitix/mon/ResultTest.java +++ b/src/test/java/net/kemitix/mon/ResultTest.java @@ -832,6 +832,52 @@ class ResultTest implements WithAssertions { //then assertThat(capture).hasValue(1); } + + @Test + void error_whenOnSuccess_returnsSelf() { + //given + final Result error = anError(new RuntimeException()); + + //when + final Result result = error.onSuccess(x -> {}); + + //then + assertThat(result).isSameAs(error); + } + + @Test + void success_whenOnSuccess_returnsSelf() { + //given + final Result ok = Result.ok(1); + + //when + final Result result = ok.onSuccess(x -> {}); + + //then + assertThat(result).isSameAs(ok); + } + + @Test void errorVoid_whenOnSuccess_returnsSelf() { + //given + final ResultVoid error = Result.error(new RuntimeException()); + + //when + final ResultVoid result = error.onSuccess(() -> {}); + + //then + assertThat(result).isSameAs(error); + } + + @Test void successVoid_whenOnSuccess_returnsSelf() { + //given + final ResultVoid ok = Result.ok(); + + //when + final ResultVoid result = ok.onSuccess(() -> {}); + + //then + assertThat(result).isSameAs(ok); + } } @Nested