Add Result.orElseThrow()

This commit is contained in:
Paul Campbell 2018-06-24 08:16:24 +01:00
parent 05aa6fb323
commit e9184f88bf
4 changed files with 41 additions and 3 deletions

View file

@ -67,4 +67,9 @@ class Err<T> implements Result<T> {
public Result<Maybe<T>> maybe(final Predicate<T> predicate) { public Result<Maybe<T>> maybe(final Predicate<T> predicate) {
return Result.error(error); return Result.error(error);
} }
@Override
public T orElseThrow() throws Throwable {
throw error;
}
} }

View file

@ -29,7 +29,7 @@ import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
* An Either type for holding a result or an error (exception). * An Either type for holding a result or an error (Throwable).
* *
* @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)
@ -44,7 +44,7 @@ public interface Result<T> {
* @param <T> the type of the Maybe and 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 * @return a Result containing the value of the Maybe when it is a Just, or the error when it is Nothing
*/ */
static <T> Result<T> fromMaybe(final Maybe<T> maybe, final Supplier<Exception> error) { static <T> Result<T> fromMaybe(final Maybe<T> maybe, final Supplier<Throwable> error) {
return maybe.map(Result::ok) return maybe.map(Result::ok)
.orElseGet(() -> Result.error(error.get())); .orElseGet(() -> Result.error(error.get()));
} }
@ -52,7 +52,7 @@ public interface Result<T> {
/** /**
* Create a Result for an error. * Create a Result for an error.
* *
* @param error the error (exception) * @param error the error (Throwable)
* @param <T> the type had the result been a success * @param <T> the type had the result been a success
* @return an error Result * @return an error Result
*/ */
@ -119,4 +119,12 @@ public interface Result<T> {
*/ */
Result<Maybe<T>> maybe(Predicate<T> predicate); Result<Maybe<T>> maybe(Predicate<T> predicate);
/**
* Extracts the successful value from the result, or throws the error Throwable.
*
* @return the value if a success
* @throws Throwable the result is an error
*/
@SuppressWarnings("illegalthrows")
T orElseThrow() throws Throwable;
} }

View file

@ -70,4 +70,9 @@ class Success<T> implements Result<T> {
} }
return Result.ok(Maybe.nothing()); return Result.ok(Maybe.nothing());
} }
@Override
public T orElseThrow() {
return value;
}
} }

View file

@ -222,6 +222,26 @@ public class ResultTest implements WithAssertions {
); );
} }
@Test
public void success_whenOrElseThrow_isValue() throws Throwable {
//given
final Result<Integer> ok = Result.ok(1);
//when
final Integer value = ok.orElseThrow();
//then
assertThat(value).isEqualTo(1);
}
@Test
public void error_whenOrElseThrow_throws() {
//given
final RuntimeException exception = new RuntimeException();
final Result<Integer> error = Result.error(exception);
//when
assertThatThrownBy(() -> error.orElseThrow())
.isSameAs(exception);
}
@Test @Test
public void useCase_whenOkay_thenReturnSuccess() { public void useCase_whenOkay_thenReturnSuccess() {
//given //given