S
- The type of a successF
- The type of a failurepublic abstract class Result<S,F> extends Object implements Serializable
The interface for Result is minimal: many common sample operations are implemented with static methods on Introducers, Transformers, and Resolvers. These can be composed upon a Result by passing them as arguments to then().
Modifier and Type | Method and Description |
---|---|
abstract <R,R1 extends R,R2 extends R> |
either(Function<S,R1> onSuccess,
Function<F,R2> onFailure)
Takes two functions, the first of which is executed in the case that this
Result is a Success, the second of which is executed in the case that it
is a Failure, on the wrapped value in either case.
|
static <S,F> Result<S,F> |
failure(F error)
Creates a new Failure
|
static <S,F> Result<S,F> |
failure(F error,
Class<S> successType)
Creates a new Failure, taking the success type for contexts where it can't be inferred.
|
static <S,F> Result<S,F> |
success(S value)
Creates a new Success
|
static <S,F> Result<S,F> |
success(S value,
Class<F> failureType)
Creates a new Success, taking the failure type for contexts where it can't be inferred.
|
<T,T2 extends T> |
then(Function<Result<S,F>,T2> biMapper)
Applies a function to this Result.
|
public static <S,F> Result<S,F> success(S value)
public static <S,F> Result<S,F> success(S value, Class<F> failureType)
public static <S,F> Result<S,F> failure(F error)
public static <S,F> Result<S,F> failure(F error, Class<S> successType)
public abstract <R,R1 extends R,R2 extends R> R either(Function<S,R1> onSuccess, Function<F,R2> onFailure)
R
- the type of the end resultonSuccess
- the function to process the success value, if this is a SuccessonFailure
- the function to process the failure value, if this is a Failurepublic <T,T2 extends T> T then(Function<Result<S,F>,T2> biMapper)
Result<Shop, String> shop;
Result<Hat, String> hat = map(shop, Shop::purchaseHat);
We can write:
Result<Shop, String> shop;
Result<Hat, String> hat = shop.then(map(Shop::purchaseHat));
The advantage of this is that it composes more nicely: instead of this:
Result<Town, String> town;
Result<Hat, String> hat = map(map(shop, Town::findHatShop), Shop::purchaseHat);
We can write: *
Result<Town, String> town;
Result<Hat, String> hat = town.then(map(Town::findHatShop)
.then(map(Shop::purchaseHat));
Copyright © 2018. All rights reserved.