T
- the possible value type@ConsumerType
public abstract class Try<T>
extends java.lang.Object
Try
type. This class's instances represent the
result of an operation that either succeeds with type T
or fails with
an exception. Only two descendants of this class are allowed: Try.Success
for the success case, and Try.Failure
for the failure case.
Never instantiate this class directly. If you're unsure whether the operation
will succeed, use fromFallible(ThrowableSupplier)
to create an
instance of this class. To create a Failure
instance directly
from an exception, use fail(Exception)
. To create a Success
instance directly from T
, use success(Object)
.Modifier and Type | Class and Description |
---|---|
static class |
Try.Failure<T>
The implementation of
Try 'S failure case. |
static class |
Try.Success<T>
The implementation of
Try 'S success case. |
Modifier and Type | Method and Description |
---|---|
static <T> Try<T> |
fail(java.lang.Exception exception)
Creates a new
Try instance from an exception. |
abstract Try<T> |
filter(java.util.function.Predicate<T> predicate)
Returns a
Try instance with a value, if that value matches the
predicate. |
abstract <S> Try<S> |
flatMap(ThrowableFunction<? super T,Try<S>> function)
Returns the result of applying the mapping function to the
Success instance'S value, if the current Try instance is a
Success ; otherwise returns the Failure instance. |
abstract <S> S |
fold(java.util.function.Function<java.lang.Exception,S> failureFunction,
ThrowableFunction<T,S> successFunction)
Returns the value that results from applying
failureFunction if
this is a Failure , or successFunction if this is a Success . |
static <T> Try<T> |
fromFallible(ThrowableSupplier<T> throwableSupplier)
Creates a new
Try instance by executing a fallible lambda in a
ThrowableSupplier . |
static <T,S extends java.io.Closeable> |
fromFallibleWithResources(ThrowableSupplier<S> throwableSupplier,
ThrowableFunction<S,T> throwableFunction)
Creates a new
Try instance by composing two functions: throwableSupplier and throwableFunction . |
static <T> Try<T> |
fromOptional(ThrowableSupplier<java.util.Optional<T>> throwableSupplier,
java.util.function.Supplier<? extends java.lang.Exception> supplier)
Creates a new
Try instance by executing a fallible lambda that
returns an Optional in a ThrowableSupplier . |
abstract T |
get()
Returns a
Success instance'S value, or a Failure
instance'S exception. |
abstract T |
getUnchecked()
Returns a
Success instance'S value, or a Failure
instance'S exception wrapped in a RuntimeException . |
abstract void |
ifFailure(java.util.function.Consumer<java.lang.Exception> consumer)
Calls the provided consumer if the current
Try instance is a
Failure ; otherwise nothing occurs. |
abstract void |
ifSuccess(java.util.function.Consumer<T> consumer)
Calls the provided consumer if the current
Try instance is a
Success ; otherwise nothing occurs. |
abstract boolean |
isFailure()
Returns
true if the current Try instance is a Failure ; otherwise returns false . |
abstract boolean |
isSuccess()
Returns
true if the current Try instance is a Success ; otherwise returns false . |
abstract <S> Try<S> |
map(ThrowableFunction<? super T,? extends S> throwableFunction)
Returns the result of applying the mapping function to the
Success instance'S value, if the current Try instance is a
Success ; otherwise returns the Failure instance. |
abstract <S extends java.lang.Exception> |
mapFail(java.util.function.Function<java.lang.Exception,S> function)
Returns a
Try instance that contains the result of applying the
mapping function to the current Try instance'S exception, if that
instance is a Failure object. |
abstract <S extends java.lang.Exception,U extends java.lang.Exception> |
mapFailMatching(java.lang.Class<U> exceptionClass,
java.util.function.Supplier<S> supplier)
Returns a
Try instance that contains the exception provided by
the Supplier , if the current Try instance is a Failure object whose exception class matches that of the exceptionClass parameter. |
<S> Try<S> |
mapOptional(ThrowableFunction<? super T,? extends java.util.Optional<S>> throwableFunction)
Returns the result of applying the mapping function to the
Success instance's value and unwrapping the resulting Optional ,
if the current Try instance is a Success and the Optional isn't Optional#empty() ; otherwise returns the Failure . |
<S> Try<S> |
mapOptional(ThrowableFunction<? super T,? extends java.util.Optional<S>> throwableFunction,
java.util.function.Supplier<? extends java.lang.Exception> supplier)
Returns the result of applying the mapping function to the
Success instance's value and unwrapping the resulting Optional ,
if the current Try instance is a Success and the Optional isn't Optional#empty() ; otherwise returns the Failure instance with the provided exception. |
abstract T |
orElse(T other)
Returns the
Success instance'S value, if the current Try
instance is a Success object. |
abstract T |
orElseGet(java.util.function.Supplier<? extends T> supplier)
Returns the
Success instance'S value, if the current Try
instance is a Success object. |
abstract <S extends java.lang.Throwable> |
orElseThrow(java.util.function.Supplier<? extends S> supplier)
Returns the
Success instance'S value, if the current Try
instance is a Success object. |
abstract T |
recover(java.util.function.Function<? super java.lang.Exception,T> function)
Returns the result of applying the function to the
Failure
object'S exception, if the current Try instance is a Failure object. |
abstract Try<T> |
recoverWith(ThrowableFunction<? super java.lang.Exception,Try<T>> throwableFunction)
Returns a new
Try instance extracted from the provided function,
if the current Try instance is a Failure object. |
static <T> Try<T> |
success(T t)
Creates a new
Try instance from an object. |
abstract java.util.Optional<T> |
toOptional()
Returns the current
Try instance as an Optional
containing the value if it's a Success or Optional#empty() if it's a Failure . |
abstract void |
voidFold(java.util.function.Consumer<java.lang.Exception> failureConsumer,
ThrowableConsumer<T> successConsumer)
Applies
failureConsumer if the current Try instance is a
Failure , or successConsumer if it's a Success . |
public static <T> Try<T> fail(java.lang.Exception exception)
Try
instance from an exception. This method creates
the instance as a Failure
object.exception
- the exception to create the Failure
fromTry
instance created from the exceptionpublic static <T> Try<T> fromFallible(ThrowableSupplier<T> throwableSupplier)
Try
instance by executing a fallible lambda in a
ThrowableSupplier
. If this throws an exception, a Failure
instance is created. Otherwise, a Success
instance with the
lambda'S result is created.throwableSupplier
- the throwable supplier that contains the
fallible lambdaFailure
if the throwable supplier throws an exception;
Success
otherwisepublic static <T,S extends java.io.Closeable> Try<T> fromFallibleWithResources(ThrowableSupplier<S> throwableSupplier, ThrowableFunction<S,T> throwableFunction)
Try
instance by composing two functions: throwableSupplier
and throwableFunction
. The result of throwableSupplier
is passed as the only parameter to throwableFunction
. If either function fails, a Failure
instance
is returned. Otherwise, a Success
instance with the results of
throwableFunction
is returned.throwableFunction
- the function to execute with the results of
throwableSupplier
Success
instance if the functions succeed; a Failure
instance otherwisepublic static <T> Try<T> fromOptional(ThrowableSupplier<java.util.Optional<T>> throwableSupplier, java.util.function.Supplier<? extends java.lang.Exception> supplier)
Try
instance by executing a fallible lambda that
returns an Optional
in a ThrowableSupplier
. If this
throws an exception, a Failure
instance is created. If the
returned Optional
is empty, a Failure
containing the
exception supplier's value is returned. Otherwise, a Success
instance with the lambda's Optional
result is created.throwableSupplier
- the throwable supplier that contains the
fallible lambda that returns an Optional
supplier
- the supplier for the exception if the obtained Optional
is Optional#empty()
Failure
if the throwable supplier throws an exception, or
the Optional
is empty; Success
otherwisepublic static <T> Try<T> success(T t)
Try
instance from an object. This method creates
the instance as a Success
object.t
- the object to create the Success
object fromSuccess
objectpublic abstract Try<T> filter(java.util.function.Predicate<T> predicate)
Try
instance with a value, if that value matches the
predicate. Otherwise, this method returns a Try
instance with an
exception that indicates the false
predicate.predicate
- the predicate to apply to a valueTry
instance with a value, if that value matches the
predicate; otherwise a Try
instance with an exception for
a {@code false}
predicatepublic abstract <S> Try<S> flatMap(ThrowableFunction<? super T,Try<S>> function)
Success
instance'S value, if the current Try
instance is a
Success
; otherwise returns the Failure
instance.
This method is similar to map(ThrowableFunction)
, but the
mapping function'S result is already a Try
, and if invoked,
flatMap
doesn't wrap it in an additional Try
.
function
- the mapping functionSuccess
instance'S value; the Failure
instance otherwisepublic abstract <S> S fold(java.util.function.Function<java.lang.Exception,S> failureFunction, ThrowableFunction<T,S> successFunction)
failureFunction
if
this is a Failure
, or successFunction
if this is a Success
.
If successFunction
throws an Exception
, this method
returns the result of applying failureFunction
to the new Exception
.
failureFunction
- the function to apply when this Try
is a
Failure
successFunction
- the function to apply when this Try
is a
Success
public abstract T get() throws java.lang.Exception
Success
instance'S value, or a Failure
instance'S exception. What this method returns therefore depends on
whether the current Try
instance is a Success
or Failure
.Success
instance'S value; otherwise the Failure
instance'S exceptionjava.lang.Exception
- if the operation failedpublic abstract T getUnchecked()
Success
instance'S value, or a Failure
instance'S exception wrapped in a RuntimeException
. What this
method returns therefore depends on whether the current Try
instance is a Success
or Failure
.Success
instance'S value; otherwise the Failure
instance'S exception wrapped in a RuntimeException
public abstract void ifFailure(java.util.function.Consumer<java.lang.Exception> consumer)
Try
instance is a
Failure
; otherwise nothing occurs.consumer
- the consumerpublic abstract void ifSuccess(java.util.function.Consumer<T> consumer)
Try
instance is a
Success
; otherwise nothing occurs.consumer
- the consumerpublic abstract boolean isFailure()
true
if the current Try
instance is a Failure
; otherwise returns false
.true
if the current Try
instance is a Failure
; false
otherwise.public abstract boolean isSuccess()
true
if the current Try
instance is a Success
; otherwise returns false
.true
if the current Try
instance is a Success
; false
otherwise.public abstract <S> Try<S> map(ThrowableFunction<? super T,? extends S> throwableFunction)
Success
instance'S value, if the current Try
instance is a
Success
; otherwise returns the Failure
instance.
This function is similar to flatMap(ThrowableFunction)
, but the
mapping function'S result isn't a Try
, and if invoked, map
wraps it in Try
.
throwableFunction
- the mapping functionSuccess
instance'S value; the Failure
instance otherwisepublic abstract <S extends java.lang.Exception> Try<T> mapFail(java.util.function.Function<java.lang.Exception,S> function)
Try
instance that contains the result of applying the
mapping function to the current Try
instance'S exception, if that
instance is a Failure
object. If the current Try
instance
is a Success
object, this method returns it unmodified.function
- the mapping function to apply to the Failure
instance'S exceptionTry
with the mapping function'S result; the Success
object otherwisepublic abstract <S extends java.lang.Exception,U extends java.lang.Exception> Try<T> mapFailMatching(java.lang.Class<U> exceptionClass, java.util.function.Supplier<S> supplier)
Try
instance that contains the exception provided by
the Supplier
, if the current Try
instance is a Failure
object whose exception class matches that of the exceptionClass
parameter. If the current Try
instance is a
Success
object, this method returns it unmodified.supplier
- the supplierTry
with the exception from the supplier; the Success
object otherwisepublic <S> Try<S> mapOptional(ThrowableFunction<? super T,? extends java.util.Optional<S>> throwableFunction)
Success
instance's value and unwrapping the resulting Optional
,
if the current Try
instance is a Success
and the Optional
isn't Optional#empty()
; otherwise returns the Failure
.throwableFunction
- the mapping functionSuccess
instance's
value doesn't return Optional#empty()
; the Failure
instance otherwisepublic <S> Try<S> mapOptional(ThrowableFunction<? super T,? extends java.util.Optional<S>> throwableFunction, java.util.function.Supplier<? extends java.lang.Exception> supplier)
Success
instance's value and unwrapping the resulting Optional
,
if the current Try
instance is a Success
and the Optional
isn't Optional#empty()
; otherwise returns the Failure
instance with the provided exception.throwableFunction
- the mapping functionsupplier
- the exception's supplier in case the obtained Optional
is Optional#empty()
Success
instance's
value doesn't return Optional#empty()
; the Failure
instance otherwisepublic abstract T orElse(T other)
Success
instance'S value, if the current Try
instance is a Success
object. If the current Try
instance
is a Failure
object, this method returns the other
parameter.other
- the value to return if current Try
instance is a
Failure
objectSuccess
instance'S value, or other
public abstract T orElseGet(java.util.function.Supplier<? extends T> supplier)
Success
instance'S value, if the current Try
instance is a Success
object. If the current Try
instance
is a Failure
object, this method returns the result of invoking
Supplier#get()
.supplier
- the supplierSuccess
instance'S value, or the result of the
supplier'S get
methodpublic abstract <S extends java.lang.Throwable> T orElseThrow(java.util.function.Supplier<? extends S> supplier) throws S extends java.lang.Throwable
Success
instance'S value, if the current Try
instance is a Success
object. If the current Try
instance
is a Failure
object, this method throws the exception that
results from invoking Supplier#get()
.supplier
- the supplierSuccess
instance'S valueS
- if the current Try
instance was a Failure
objectS extends java.lang.Throwable
public abstract T recover(java.util.function.Function<? super java.lang.Exception,T> function)
Failure
object'S exception, if the current Try
instance is a Failure
object. If the current Try
instance is a Success
object, this method returns that object'S value.function
- the functionFailure
; the
Success
object'S value otherwisepublic abstract Try<T> recoverWith(ThrowableFunction<? super java.lang.Exception,Try<T>> throwableFunction)
Try
instance extracted from the provided function,
if the current Try
instance is a Failure
object. If the
current Try
instance is a Success
object, this method
returns that object.throwableFunction
- the functionTry
instance, if the current Try
instance
is a Failure
object; the current Success
object
otherwisepublic abstract java.util.Optional<T> toOptional()
Try
instance as an Optional
containing the value if it's a Success
or Optional#empty()
if it's a Failure
.Optional
containing the value if it's a Success
; Optional#empty()
otherwisepublic abstract void voidFold(java.util.function.Consumer<java.lang.Exception> failureConsumer, ThrowableConsumer<T> successConsumer)
failureConsumer
if the current Try
instance is a
Failure
, or successConsumer
if it's a Success
.
If successConsumer
throws an Exception
, this method
returns the result of applying failureConsumer
to the new Exception
.
failureConsumer
- the consumer to apply when this Try
is a
Failure
successConsumer
- the consumer to apply when this Try
is a
Success