T - The type of the value represented by this Trypublic interface Try<T> extends Iterable<T>
Success or Failure.
Try<SomeData> getSomeData(SomeInput input) {
try {
// readUserFromDB throws if no user exists
SomeData data = readUserFromDB(input);
return new Success<>(data);
} catch (SomeException ex) {
return new Failure<>(ex);
}
}
A more elaborate way is to provide a checked function provided to the apply(ThrowableFunction0) method. Or let us re-write the first example to use theTry<Integer> resultSuccess = Try.apply(() -> 9 / 3); // results in Success(3) Try<Integer> resultFailure = Try.apply(() -> 9 / 0); // results in Failure(ArithmeticException)
apply(ThrowableFunction0) method.
Try<SomeData> getSomeData(SomeInput input) {
// readUserFromDB throws if no user exists
Try.apply(() -> readUserFromDB(input));
}
To make it even more concise we can create Try instances just by using Try(ThrowableFunction0). companion class related to Try.
import static javascalautils.TryCompanion.Try;
Try<SomeData> getSomeData(SomeInput input) {
// readUserFromDB throws if no user exists
Try(() -> readUserFromDB(input));
}
| Modifier and Type | Method and Description |
|---|---|
static <T> Try<T> |
apply(T value)
Creates an instance of Try.
|
static <T> Try<T> |
apply(ThrowableFunction0<T> function)
Creates an instance of Try wrapping the result of the provided function.
|
default Option<T> |
asOption()
|
Try<Throwable> |
failed()
Completes this Try with an exception wrapped in a
Success. |
Try<T> |
filter(java.util.function.Predicate<T> filter)
|
<R> Try<R> |
flatMap(java.util.function.Function<T,Try<R>> function)
|
T |
get()
|
T |
getOrElse(java.util.function.Supplier<T> supplier)
|
default boolean |
isFailure()
|
boolean |
isSuccess()
|
Iterator<T> |
iterator()
|
<R> Try<R> |
map(java.util.function.Function<T,R> function)
|
Try<T> |
orElse(java.util.function.Supplier<Try<T>> supplier)
|
default T |
orNull()
Returns the value if it is a
Success, else null. |
Try<T> |
recover(java.util.function.Function<Throwable,T> function)
|
Try<T> |
recoverWith(java.util.function.Function<Throwable,Try<T>> function)
|
java.util.stream.Stream<T> |
stream()
|
forEach, spliteratorstatic <T> Try<T> apply(T value)
null or non-throwable value is provided then Success is returned containing the value, else Failure containing the
provided throwable.T - The type for the Tryvalue - The value for this to create a Trystatic <T> Try<T> apply(ThrowableFunction0<T> function)
Success with the value is returned. Failure is returned containing that exception. null is provided as argument then the apply(Object) is invoked. Try<Integer> resultSuccess = Try.apply(() -> 9 / 3); // results in Success(3) Try<Integer> resultFailure = Try.apply(() -> 9 / 0); // results in Failure(ArithmeticException)
T - The type for the Tryfunction - The function to render either the value T or raise an exception.default boolean isFailure()
Failureboolean isSuccess()
SuccessT getOrElse(java.util.function.Supplier<T> supplier)
supplier - The supplier to return the value in case of a Failuredefault T orNull()
Success, else null.null.Try<T> orElse(java.util.function.Supplier<Try<T>> supplier)
supplier - The supplier to return the value in case of a FailureTry<T> filter(java.util.function.Predicate<T> filter)
Try and either returns the Try if the predicate matched or a Failure. filter - The filter to applyFailure in case no match<R> Try<R> map(java.util.function.Function<T,R> function)
Success or returns this if this is a Failure. Try containing some type to some completely different type. Try of type String to Integer.Try<String> t = ... Try<Integer> t2 = t.map(v -> v.length);
R - The type for the return value from the functionfunction - The function to use<R> Try<R> flatMap(java.util.function.Function<T,Try<R>> function)
R - The type for the return value from the functionfunction - The function to useTry<T> recover(java.util.function.Function<Throwable,T> function)
Try that in case this Try is a Failure will apply the function to recover to a Success. Success then this is returned, i.e. no new instance is created. map(Function) for failures only.This statement will beTry<String> t = ... Try<String> recovered = t.recover(t -> t.getMessage());
Success(3) Try<Integer> t = Try(() -> 9/3).recover(t -> 0)
Success(0) as it is division-by-zero Try<Integer> t = Try(() -> 9/0).recover(t -> 0)
function - The function to apply in case of a FailureTry<T> recoverWith(java.util.function.Function<Throwable,Try<T>> function)
Try that in case this Try is a Failure will apply the function to recover the Try rendered by the
function. Success the value is propagated as-is. map(Function) for failures only.In case of t being successful then that value is passed on to recovered, in case of failure then the recover function kicks in and returns the aTry<String> t = ... Try<String> recovered = t.recover(t -> new Success<>(t.getMessage()));
Success with message from the throwable.function - The function to apply in case of a Failurejava.util.stream.Stream<T> stream()
default Option<T> asOption()
Try as an Option. Success then the value is wrapped in Some else None is returned. Success contain a null value the result will be None as null values are per definition
none/nothing.Option representing this TryCopyright © 2015, Peter Nerg Apache License v2.0