Rename Result.invert as swap

This commit is contained in:
Paul Campbell 2018-07-21 22:42:16 +01:00
parent b80650b6ae
commit 4542d82c1a
2 changed files with 7 additions and 7 deletions

View file

@ -123,7 +123,7 @@ public interface Result<T> extends Functor<T, Result<?>> {
* original Maybe. If the original Maybe is Nothing, the Result will contain Nothing. If the original Result was an * original Maybe. If the original Maybe is Nothing, the Result will contain Nothing. If the original Result was an
* error, then the Result will also be an error. * error, then the Result will also be an error.
*/ */
static <T> Result<Maybe<T>> invert(final Maybe<Result<T>> maybeResult) { static <T> Result<Maybe<T>> swap(final Maybe<Result<T>> maybeResult) {
return maybeResult.orElseGet(() -> Result.ok(null)) return maybeResult.orElseGet(() -> Result.ok(null))
.flatMap(value -> Result.ok(Maybe.maybe(value))); .flatMap(value -> Result.ok(Maybe.maybe(value)));
} }

View file

@ -300,7 +300,7 @@ public class ResultTest implements WithAssertions {
//given //given
final Maybe<Result<Integer>> justSuccess = Maybe.just(Result.ok(1)); final Maybe<Result<Integer>> justSuccess = Maybe.just(Result.ok(1));
//when //when
final Result<Maybe<Integer>> result = Result.invert(justSuccess); final Result<Maybe<Integer>> result = Result.swap(justSuccess);
//then //then
result.match( result.match(
success -> assertThat(success.toOptional()).contains(1), success -> assertThat(success.toOptional()).contains(1),
@ -314,7 +314,7 @@ public class ResultTest implements WithAssertions {
final RuntimeException exception = new RuntimeException(); final RuntimeException exception = new RuntimeException();
final Maybe<Result<Object>> justError = Maybe.just(Result.error(exception)); final Maybe<Result<Object>> justError = Maybe.just(Result.error(exception));
//when //when
final Result<Maybe<Object>> result = Result.invert(justError); final Result<Maybe<Object>> result = Result.swap(justError);
//then //then
result.match( result.match(
success -> fail("Not a success"), success -> fail("Not a success"),
@ -327,7 +327,7 @@ public class ResultTest implements WithAssertions {
//given //given
final Maybe<Result<Integer>> nothing = Maybe.nothing(); final Maybe<Result<Integer>> nothing = Maybe.nothing();
//when //when
final Result<Maybe<Integer>> result = Result.invert(nothing); final Result<Maybe<Integer>> result = Result.swap(nothing);
//then //then
result.match( result.match(
success -> assertThat(success.toOptional()).isEmpty(), success -> assertThat(success.toOptional()).isEmpty(),
@ -695,7 +695,7 @@ public class ResultTest implements WithAssertions {
final Result<Integer> result11 = result1.reduce(result10, (a, b) -> a + b); final Result<Integer> result11 = result1.reduce(result10, (a, b) -> a + b);
//then //then
result11.match( result11.match(
success->fail("Not a success"), success -> fail("Not a success"),
error -> assertThat(error).isSameAs(exception) error -> assertThat(error).isSameAs(exception)
); );
} }
@ -710,7 +710,7 @@ public class ResultTest implements WithAssertions {
final Result<Integer> result11 = result1.reduce(result10, (a, b) -> a + b); final Result<Integer> result11 = result1.reduce(result10, (a, b) -> a + b);
//then //then
result11.match( result11.match(
success->fail("Not a success"), success -> fail("Not a success"),
error -> assertThat(error).isSameAs(exception) error -> assertThat(error).isSameAs(exception)
); );
} }
@ -726,7 +726,7 @@ public class ResultTest implements WithAssertions {
final Result<Integer> result11 = result1.reduce(result10, (a, b) -> a + b); final Result<Integer> result11 = result1.reduce(result10, (a, b) -> a + b);
//then //then
result11.match( result11.match(
success->fail("Not a success"), success -> fail("Not a success"),
error -> assertThat(error).isSameAs(exception1) error -> assertThat(error).isSameAs(exception1)
); );
} }