Result.onSuccess should return Result/ResultVoid (#224)

* Version set to 3.2.0

* Result{Void}.onSuccess() now returns a Result<T>/ResultVoid
This commit is contained in:
Paul Campbell 2021-08-19 11:35:05 +01:00 committed by GitHub
parent 03b288da4c
commit d1f4c61f00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 9 deletions

View file

@ -11,7 +11,7 @@
</parent>
<artifactId>mon</artifactId>
<version>3.1.0</version>
<version>3.2.0</version>
<name>Mon</name>
<description>Wrapper, TypeAlias, Maybe, Result, Tree, Lazy, Either and Combinators for Java.

View file

@ -101,8 +101,8 @@ class Err<T> implements Result<T> {
}
@Override
public void onSuccess(final Consumer<T> successConsumer) {
// do nothing
public Result<T> onSuccess(final Consumer<T> successConsumer) {
return this;
}
@Override

View file

@ -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

View file

@ -707,9 +707,10 @@ public interface Result<T> extends BaseResult, ThrowableFunctor<T, ThrowableFunc
* success value. When this is an error, then nothing happens.</p>
*
* @param successConsumer the consumer to handle the success
* @return the original Result
*/
@API(status = STABLE)
void onSuccess(Consumer<T> successConsumer);
Result<T> onSuccess(Consumer<T> successConsumer);
/**
* A handler for error state, when the error matches the errorClass.

View file

@ -66,9 +66,10 @@ public interface ResultVoid extends BaseResult {
* success value. When this is an error, then nothing happens.</p>
*
* @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.

View file

@ -93,8 +93,9 @@ class Success<T> implements Result<T> {
}
@Override
public void onSuccess(final Consumer<T> successConsumer) {
public Result<T> onSuccess(final Consumer<T> successConsumer) {
successConsumer.accept(value);
return this;
}
@Override

View file

@ -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

View file

@ -832,6 +832,52 @@ class ResultTest implements WithAssertions {
//then
assertThat(capture).hasValue(1);
}
@Test
void error_whenOnSuccess_returnsSelf() {
//given
final Result<Integer> error = anError(new RuntimeException());
//when
final Result<Integer> result = error.onSuccess(x -> {});
//then
assertThat(result).isSameAs(error);
}
@Test
void success_whenOnSuccess_returnsSelf() {
//given
final Result<Integer> ok = Result.ok(1);
//when
final Result<Integer> 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