Merge pull request #35 from kemitix/result-maybe-then-static
Add `Result.maybeThen(Result<Maybe<T>>, Function)`
This commit is contained in:
commit
97e08ab4fa
3 changed files with 83 additions and 2 deletions
|
@ -4,8 +4,9 @@ CHANGELOG
|
||||||
0.10.0
|
0.10.0
|
||||||
------
|
------
|
||||||
|
|
||||||
* Add `andThen(Function)`
|
* Add `Result.andThen(Function)`
|
||||||
* Add `thenWith(Function)`
|
* Add `Result.thenWith(Function)`
|
||||||
|
* Add `Result.maybeThen(Result<Maybe<T>>, Function)`
|
||||||
|
|
||||||
0.9.0
|
0.9.0
|
||||||
-----
|
-----
|
||||||
|
|
|
@ -120,6 +120,19 @@ public interface Result<T> extends Functor<T, Result<?>> {
|
||||||
.flatMap(value -> Result.ok(Maybe.maybe(value)));
|
.flatMap(value -> Result.ok(Maybe.maybe(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies the function to the contents of a Maybe within the Result.
|
||||||
|
*
|
||||||
|
* @param maybeResult the Result that may contain a value
|
||||||
|
* @param f the function to apply to the value
|
||||||
|
* @param <T> the type of the original Result
|
||||||
|
* @param <R> the type of the updated Result
|
||||||
|
* @return a new Maybe within a Result
|
||||||
|
*/
|
||||||
|
static <T, R> Result<Maybe<R>> maybeThen(Result<Maybe<T>> maybeResult, Function<Maybe<T>, Result<Maybe<R>>> f) {
|
||||||
|
return maybeResult.flatMap(f);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks of the Result is an error.
|
* Checks of the Result is an error.
|
||||||
*
|
*
|
||||||
|
|
|
@ -604,6 +604,73 @@ public class ResultTest implements WithAssertions {
|
||||||
assertThat(result).isSameAs(error);
|
assertThat(result).isSameAs(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void okayJust_whenMaybeThen_whereOkayJust_thenIsOkayJust() {
|
||||||
|
//given
|
||||||
|
final Result<Maybe<Integer>> okJust = Result.ok(Maybe.just(1));
|
||||||
|
//when
|
||||||
|
final Result<Maybe<Integer>> result = Result.maybeThen(okJust, mv -> Result.ok(Maybe.just(2)));
|
||||||
|
//then
|
||||||
|
result.match(
|
||||||
|
success -> assertThat(success.toOptional()).contains(2),
|
||||||
|
error -> fail("Not an error")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void okayJust_whenMaybeThen_whereOkayNothing_thenIsOkayNothing() {
|
||||||
|
//given
|
||||||
|
final Result<Maybe<Integer>> okJust = Result.ok(Maybe.just(1));
|
||||||
|
//when
|
||||||
|
final Result<Maybe<Integer>> result = Result.maybeThen(okJust, v -> Result.ok(Maybe.nothing()));
|
||||||
|
//then
|
||||||
|
result.match(
|
||||||
|
success -> assertThat(success.toOptional()).isEmpty(),
|
||||||
|
error -> fail("Not an error")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void okayJust_whenMaybeThen_whereError_thenIsError() {
|
||||||
|
//given
|
||||||
|
final Result<Maybe<Integer>> okJust = Result.ok(Maybe.just(1));
|
||||||
|
final RuntimeException exception = new RuntimeException();
|
||||||
|
//when
|
||||||
|
final Result<Maybe<Integer>> result = Result.maybeThen(okJust, v -> Result.error(exception));
|
||||||
|
//then
|
||||||
|
result.match(
|
||||||
|
success -> fail("Not a success"),
|
||||||
|
error -> assertThat(error).isSameAs(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void okayNothing_whenMaybeThen_thenDoNotApply() {
|
||||||
|
//given
|
||||||
|
final Result<Maybe<Integer>> okNothing = Result.ok(Maybe.nothing());
|
||||||
|
//when
|
||||||
|
final Result<Maybe<Integer>> result = Result.maybeThen(okNothing, v -> Result.ok(Maybe.just(2)));
|
||||||
|
//then
|
||||||
|
okNothing.match(
|
||||||
|
success -> assertThat(success.toOptional()).isEmpty(),
|
||||||
|
error -> fail("Not an error")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void error_whenMaybeThen_thenDoNotApply() {
|
||||||
|
//given
|
||||||
|
final RuntimeException exception = new RuntimeException();
|
||||||
|
final Result<Maybe<Integer>> maybeResult = Result.error(exception);
|
||||||
|
//when
|
||||||
|
final Result<Maybe<Integer>> result = Result.maybeThen(maybeResult, v -> Result.ok(Maybe.just(2)));
|
||||||
|
//then
|
||||||
|
maybeResult.match(
|
||||||
|
success -> fail("Not a success"),
|
||||||
|
error -> assertThat(error).isSameAs(exception)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
private static class UseCase {
|
private static class UseCase {
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue