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:
parent
03b288da4c
commit
d1f4c61f00
8 changed files with 59 additions and 9 deletions
2
pom.xml
2
pom.xml
|
@ -11,7 +11,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>mon</artifactId>
|
<artifactId>mon</artifactId>
|
||||||
<version>3.1.0</version>
|
<version>3.2.0</version>
|
||||||
|
|
||||||
<name>Mon</name>
|
<name>Mon</name>
|
||||||
<description>Wrapper, TypeAlias, Maybe, Result, Tree, Lazy, Either and Combinators for Java.
|
<description>Wrapper, TypeAlias, Maybe, Result, Tree, Lazy, Either and Combinators for Java.
|
||||||
|
|
|
@ -101,8 +101,8 @@ class Err<T> implements Result<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(final Consumer<T> successConsumer) {
|
public Result<T> onSuccess(final Consumer<T> successConsumer) {
|
||||||
// do nothing
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -39,8 +39,8 @@ public class ErrVoid implements ResultVoid {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(final Runnable runnable) {
|
public ResultVoid onSuccess(final Runnable runnable) {
|
||||||
// do nothing
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -707,9 +707,10 @@ public interface Result<T> extends BaseResult, ThrowableFunctor<T, ThrowableFunc
|
||||||
* success value. When this is an error, then nothing happens.</p>
|
* success value. When this is an error, then nothing happens.</p>
|
||||||
*
|
*
|
||||||
* @param successConsumer the consumer to handle the success
|
* @param successConsumer the consumer to handle the success
|
||||||
|
* @return the original Result
|
||||||
*/
|
*/
|
||||||
@API(status = STABLE)
|
@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.
|
* A handler for error state, when the error matches the errorClass.
|
||||||
|
|
|
@ -66,9 +66,10 @@ public interface ResultVoid extends BaseResult {
|
||||||
* success value. When this is an error, then nothing happens.</p>
|
* success value. When this is an error, then nothing happens.</p>
|
||||||
*
|
*
|
||||||
* @param runnable the call if the Result is a success
|
* @param runnable the call if the Result is a success
|
||||||
|
* @return the original ResultVoid
|
||||||
*/
|
*/
|
||||||
@API(status = STABLE)
|
@API(status = STABLE)
|
||||||
void onSuccess(Runnable runnable);
|
ResultVoid onSuccess(Runnable runnable);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A handler for error state, when the error matches the errorClass.
|
* A handler for error state, when the error matches the errorClass.
|
||||||
|
|
|
@ -93,8 +93,9 @@ class Success<T> implements Result<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(final Consumer<T> successConsumer) {
|
public Result<T> onSuccess(final Consumer<T> successConsumer) {
|
||||||
successConsumer.accept(value);
|
successConsumer.accept(value);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -47,8 +47,9 @@ public class SuccessVoid implements ResultVoid {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(final Runnable runnable) {
|
public ResultVoid onSuccess(final Runnable runnable) {
|
||||||
runnable.run();
|
runnable.run();
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -832,6 +832,52 @@ class ResultTest implements WithAssertions {
|
||||||
//then
|
//then
|
||||||
assertThat(capture).hasValue(1);
|
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
|
@Nested
|
||||||
|
|
Loading…
Reference in a new issue