T
- The type of the value represented by this Trypublic interface Try<T> extends Iterable<T>
Success
or Failure
. A more elaborate way is to provide aTry<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); } }
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)); }
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 . |
<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()
|
default 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 . |
default java.util.stream.Stream<T> |
stream()
|
forEach, spliterator
static <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()
Failure
boolean isSuccess()
Success
T getOrElse(java.util.function.Supplier<T> supplier)
supplier
- The supplier to return the value in case of a Failure
default 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 Failure
<R> Try<R> map(java.util.function.Function<T,R> function)
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 usedefault java.util.stream.Stream<T> stream()
Success
, or an empty Stream if it is a Failure
. Success
containing a null
value then the stream will also be empty.Copyright © 2015, Peter Nerg Apache License v2.0