T - tpublic abstract class Try<T> extends Object
| Constructor and Description |
|---|
Try() |
| Modifier and Type | Method and Description |
|---|---|
static <U> Try<U> |
failure(Exception Exception) |
abstract Optional<Try<T>> |
filter(Predicate<T> predicate)
Applies a filter, where a match returns Success and Failure otherwise.
|
abstract <U> Try<U> |
flatMap(ThrowingFunction<? super T,? extends Try<U>,? extends Exception> mapper)
Applies a function on two values.
|
abstract void |
forEach(ThrowingConsumer<? super T,? extends Exception> consumer)
Accepts a consuming function and applies it to the value if it is a Success.
|
abstract <U> Try<U> |
map(ThrowingFunction<? super T,? extends U,? extends Exception> mapper)
Applies a function on a value of type Success.
|
static <U,V,W> Try<U> |
of(ThrowingBiFunction<V,W,? extends U,? extends Exception> func,
V v,
W w)
Starting point to the Try structure.
|
static <U,V> Try<U> |
of(ThrowingFunction<V,? extends U,? extends Exception> func,
V v)
Starting point to the Try structure.
|
static <U> Try<U> |
of(ThrowingSupplier<U,? extends Exception> supplier)
Starting point to the Try structure.
|
abstract T |
orElse(T defaultValue)
Returns the value of the Success, or a default value of the same type if this is a Failure.
|
abstract T |
orElseGet(Supplier<? extends T> defaultValue)
Returns the value of the Success, or lazily falls back to a supplied value of the same type
|
abstract <E extends Exception> |
orElseRethrow()
Escapes the Try and enters a regular try-catch flow by rethowing the caught exception when a Failure
|
abstract <X extends Exception,Y extends Exception> |
orElseThrow(Function<X,Y> ExceptionMapper)
Escapes the Try and enters a regular try-catch flow
|
abstract Try<T> |
peek(ThrowingConsumer<? super T,? extends Exception> consumer)
Same as forEach but returns the Try for further chaining
|
abstract Try<T> |
peekFailure(Consumer<Failure<T>> consumer)
Does nothing on Success, but accepts a consumer on Failure
|
abstract <U> U |
recover(Function<? super T,? extends U> successFunc,
Function<Exception,? extends U> failureFunc)
Accepts two functions, the first applied if Success - returning the value,
the other executed if Failure, returning a fallback value.
|
static <T> Try<List<T>> |
sequence(List<Try<T>> tries)
Creates one Try from a list of tries containing the same type, or the _first_ failure in the given list
|
abstract <X extends Exception> |
toEither()
Creates an Either where the Left is the Failure and Right is the Success from this Try
|
abstract Optional<T> |
toOptional()
Creates an Optional wrapping the value if Success.
|
public abstract <U> Try<U> map(ThrowingFunction<? super T,? extends U,? extends Exception> mapper)
U - Type of the return value of the functionmapper - Function from T to U with an Exception in the signaturepublic abstract <U> Try<U> flatMap(ThrowingFunction<? super T,? extends Try<U>,? extends Exception> mapper)
U - Type of the value to be wrapped in a Try of the functionmapper - Function from T to Try<U> with an Exception in the signaturepublic abstract Optional<Try<T>> filter(Predicate<T> predicate)
predicate - Predicate function to determine Success or Failurepublic abstract void forEach(ThrowingConsumer<? super T,? extends Exception> consumer)
consumer - Consuming function with an Exception in the signaturepublic abstract Try<T> peek(ThrowingConsumer<? super T,? extends Exception> consumer)
consumer - Consuming function with an Exception in the signaturepublic abstract Try<T> peekFailure(Consumer<Failure<T>> consumer)
consumer - Consuming function with a failurepublic abstract T orElse(T defaultValue)
defaultValue - default fallback valuepublic abstract T orElseGet(Supplier<? extends T> defaultValue)
defaultValue - lazy default supplier of fallback valuepublic abstract <U> U recover(Function<? super T,? extends U> successFunc, Function<Exception,? extends U> failureFunc)
U - Type of the valuesuccessFunc - Function handling the Success casefailureFunc - Function handling the Failure casepublic abstract Optional<T> toOptional()
public abstract <X extends Exception> Either<X,T> toEither()
X - Exceptionpublic abstract <X extends Exception,Y extends Exception> T orElseThrow(Function<X,Y> ExceptionMapper) throws Y extends Exception
X - any ExceptionY - any ExceptionExceptionMapper - Function to transform the Exception if this is a FailureY - any ExceptionY extends Exceptionpublic abstract <E extends Exception> T orElseRethrow() throws E extends Exception
E - any ExceptionE - any ExceptionE extends Exceptionpublic static <U,V> Try<U> of(ThrowingFunction<V,? extends U,? extends Exception> func, V v)
U - Type of the function return valueV - Type of the function argumentfunc - Function to be attempted, e.g. URL::newv - Argument for the functionpublic static <U,V,W> Try<U> of(ThrowingBiFunction<V,W,? extends U,? extends Exception> func, V v, W w)
U - Type of the function return valueV - Type of the first function argumentW - Type of the second function argumentfunc - Function to be attempted, e.g. (a,b) -> a / bv - First argument for the functionw - Second argument for the functionpublic static <U> Try<U> of(ThrowingSupplier<U,? extends Exception> supplier)
U - Type of the supplied object from the supplier functionsupplier - The supplier functionCopyright © 2014–2016. All rights reserved.