Add Result.toMaybe() and Result.invert()

This commit is contained in:
Paul Campbell 2018-06-24 22:36:02 +01:00
parent 8ec3069d1d
commit f0be463028
2 changed files with 90 additions and 0 deletions

View file

@ -34,6 +34,7 @@ import java.util.function.Supplier;
* @param <T> the type of the result when a success * @param <T> the type of the result when a success
* @author Paul Campbell (pcampbell@kemitix.net) * @author Paul Campbell (pcampbell@kemitix.net)
*/ */
@SuppressWarnings("methodcount")
public interface Result<T> { public interface Result<T> {
/** /**
@ -71,6 +72,35 @@ public interface Result<T> {
return new Success<>(value); return new Success<>(value);
} }
/**
* Creates a Result from the Maybe, where the Result will be an error if the Maybe is Nothing.
*
* @param result the Result the might contain the value of the Result
* @param <T> the type of the Maybe and the Result
* @return a Result containing the value of the Maybe when it is a Just, or the error when it is Nothing
*/
@SuppressWarnings("illegalcatch")
static <T> Maybe<T> toMaybe(final Result<T> result) {
try {
return Maybe.just(result.orElseThrow());
} catch (final Throwable throwable) {
return Maybe.nothing();
}
}
/**
* Swaps the inner Result of a Maybe, so that a Result is on the outside.
* @param maybeResult the Maybe the contains a Result
* @param <T> the type of the value that may be in the Result
* @return a Result containing a Maybe, the value in the Maybe was the value in a successful Result within the
* 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.
*/
static <T> Result<Maybe<T>> invert(final Maybe<Result<T>> maybeResult) {
return maybeResult.orElseGet(() -> Result.ok(null))
.flatMap(value -> Result.ok(Maybe.maybe(value)));
}
/** /**
* Checks of the Result is an error. * Checks of the Result is an error.
* *

View file

@ -222,6 +222,26 @@ public class ResultTest implements WithAssertions {
); );
} }
@Test
public void success_toMaybe_isJust() {
//given
final Result<Integer> ok = Result.ok(1);
//when
final Maybe<Integer> maybe = Result.toMaybe(ok);
//then
assertThat(maybe.toOptional()).contains(1);
}
@Test
public void error_toMaybe_isNothing() {
//given
final Result<Object> error = Result.error(new RuntimeException());
//when
final Maybe<Object> maybe = Result.toMaybe(error);
//then
assertThat(maybe.toOptional()).isEmpty();
}
@Test @Test
public void success_whenOrElseThrow_isValue() throws Throwable { public void success_whenOrElseThrow_isValue() throws Throwable {
//given //given
@ -242,6 +262,46 @@ public class ResultTest implements WithAssertions {
.isSameAs(exception); .isSameAs(exception);
} }
@Test
public void JustSuccess_invert_thenSuccessJust() {
//given
final Maybe<Result<Integer>> justSuccess = Maybe.just(Result.ok(1));
//when
final Result<Maybe<Integer>> result = Result.invert(justSuccess);
//then
result.match(
success -> assertThat(success.toOptional()).contains(1),
error -> fail("Not an error")
);
}
@Test
public void JustError_invert_thenError() {
//given
final RuntimeException exception = new RuntimeException();
final Maybe<Result<Object>> justError = Maybe.just(Result.error(exception));
//when
final Result<Maybe<Object>> result = Result.invert(justError);
//then
result.match(
success -> fail("Not a success"),
error -> assertThat(error).isSameAs(exception)
);
}
@Test
public void Nothing_invert_thenSuccessNothing() {
//given
final Maybe<Result<Integer>> nothing = Maybe.nothing();
//when
final Result<Maybe<Integer>> result = Result.invert(nothing);
//then
result.match(
success -> assertThat(success.toOptional()).isEmpty(),
error -> fail("Not an error")
);
}
@Test @Test
public void useCase_whenOkay_thenReturnSuccess() { public void useCase_whenOkay_thenReturnSuccess() {
//given //given