public abstract class Try<T> extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
Try.FunctionExecutor<R,P>
Delayed function executor.
|
| Modifier and Type | Method and Description |
|---|---|
static <T> Try<T> |
execute(Callable<T> code)
Executes code supplied as
Callable. |
static <P,T> Try.FunctionExecutor<T,P> |
execute(Func<T,P> func)
Executes unary function
|
static <T> Try<T> |
failure(Throwable fail)
Creates a failed Try instance.
|
Option<T> |
get()
Retrieves result of computation from Try wrapped in
Option. |
abstract Throwable |
getFailure() |
abstract T |
getValue() |
abstract boolean |
isSuccessful() |
Try<T> |
recover(Callable<T> recoverCode)
Tries to recover from failure with provided
Callable. |
static <T> Try<T> |
success(T value)
Creates a successful Try instance.
|
<S> Try<S> |
then(Func<S,T> mapper)
Allows chaining of several operations depending on result of the previous one.
|
public static <T> Try<T> execute(Callable<T> code)
Callable.T - type of resulting valuecode - Callable to executeTry.Success if execution was successful or Try.Failure if
execution has thrown the exception.public static <T> Try<T> success(T value)
T - Type of the valuevalue - Value of the Trypublic static <T> Try<T> failure(Throwable fail)
T - Type of the possible resultfail - Throwable of the failurepublic static <P,T> Try.FunctionExecutor<T,P> execute(Func<T,P> func)
Executes unary function
Usage:
Try.execute(function).with(42);
P - type of parameterT - type of resultfunc - function to be executedpublic abstract boolean isSuccessful()
true if execution was successfulpublic abstract T getValue()
IllegalStateException - if execution was not successful. Use isSuccessful() to check execution status.public abstract Throwable getFailure()
IllegalStateException - if execution was successful. Use isSuccessful() to check execution status.public <S> Try<S> then(Func<S,T> mapper)
Allows chaining of several operations depending on result of the previous one. Hides complexity of error-handling and allows developer to focus on happy path.
Usage example:
Try<Integer> result = Try.execute(DeepThought).with("What is the answer to the ultimate question?").then(interpretAnswer);
This code will execute function DeepThough which accepts string as a parameter and then pass its result to function interpretAnswer, which
returns int.
If both of the functions execute successfully then result will be Try.Success containing an integer,
if any of the functions fail with exception then result will be Try.Failure containing exception. This
code will never crash.
S - type of returning value of mapper functionmapper - function which accepts value from this object.public Try<T> recover(Callable<T> recoverCode)
Callable.
If original Try.execute() call was successful, no recovery is performedrecoverCode - Callable to call in the event of failureTry.Success or Try.Failurepublic Option<T> get()
Option.
If execution was successful it will return it, otherwise Option.absent() will be returned.Copyright © 2018. All rights reserved.