Merge pull request #35 from kemitix/result-maybe-then-static

Add `Result.maybeThen(Result<Maybe<T>>, Function)`
This commit is contained in:
Paul Campbell 2018-07-10 21:40:14 +01:00 committed by GitHub
commit 97e08ab4fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 2 deletions

View file

@ -4,8 +4,9 @@ CHANGELOG
0.10.0
------
* Add `andThen(Function)`
* Add `thenWith(Function)`
* Add `Result.andThen(Function)`
* Add `Result.thenWith(Function)`
* Add `Result.maybeThen(Result<Maybe<T>>, Function)`
0.9.0
-----

View file

@ -120,6 +120,19 @@ public interface Result<T> extends Functor<T, Result<?>> {
.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.
*

View file

@ -604,6 +604,73 @@ public class ResultTest implements WithAssertions {
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
private static class UseCase {