public static class Try.Failure<T> extends Try<T>
Try
'S failure case. Don't try to
instantiate this class directly. To instantiate this class when you don't
know if the operation will fail, use Try.fromFallible(ThrowableSupplier)
. To instantiate this class from an
exception, use Try.fail(Exception)
.Try.Failure<T>, Try.Success<T>
Modifier and Type | Method and Description |
---|---|
Try<T> |
filter(java.util.function.Predicate<T> predicate)
Returns a
Try instance with a value, if that value matches the
predicate. |
<S> Try<S> |
flatMap(ThrowableFunction<? super T,Try<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. |
<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 . |
T |
get()
Returns a
Success instance'S value, or a Failure
instance'S exception. |
java.lang.Exception |
getException()
Returns the current
Failure instance'S exception. |
T |
getUnchecked()
Returns a
Success instance'S value, or a Failure
instance'S exception wrapped in a RuntimeException . |
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. |
void |
ifSuccess(java.util.function.Consumer<T> consumer)
Calls the provided consumer if the current
Try instance is a
Success ; otherwise nothing occurs. |
boolean |
isFailure()
Returns
true if the current Try instance is a Failure ; otherwise returns false . |
boolean |
isSuccess()
Returns
true if the current Try instance is a Success ; otherwise returns false . |
<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. |
<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. |
<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. |
T |
orElse(T other)
Returns the
Success instance'S value, if the current Try
instance is a Success object. |
T |
orElseGet(java.util.function.Supplier<? extends T> supplier)
Returns the
Success instance'S value, if the current Try
instance is a Success object. |
<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. |
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. |
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. |
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 . |
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 . |
fail, fromFallible, fromFallibleWithResources, fromOptional, mapOptional, mapOptional, success
public Try<T> filter(java.util.function.Predicate<T> predicate)
Try
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.public <S> Try<S> flatMap(ThrowableFunction<? super T,Try<S>> throwableFunction)
Try
Success
instance'S value, if the current Try
instance is a
Success
; otherwise returns the Failure
instance.
This method is similar to Try.map(ThrowableFunction)
, but the
mapping function'S result is already a Try
, and if invoked,
flatMap
doesn't wrap it in an additional Try
.
public <S> S fold(java.util.function.Function<java.lang.Exception,S> failureFunction, ThrowableFunction<T,S> successFunction)
Try
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
.
public T get() throws java.lang.Exception
Try
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
.public java.lang.Exception getException()
Failure
instance'S exception.public T getUnchecked()
Try
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
.getUnchecked
in class Try<T>
Success
instance'S value; otherwise the Failure
instance'S exception wrapped in a RuntimeException
public void ifFailure(java.util.function.Consumer<java.lang.Exception> consumer)
Try
Try
instance is a
Failure
; otherwise nothing occurs.public void ifSuccess(java.util.function.Consumer<T> consumer)
Try
Try
instance is a
Success
; otherwise nothing occurs.public boolean isFailure()
Try
true
if the current Try
instance is a Failure
; otherwise returns false
.public boolean isSuccess()
Try
true
if the current Try
instance is a Success
; otherwise returns false
.public <S> Try<S> map(ThrowableFunction<? super T,? extends S> throwableFunction)
Try
Success
instance'S value, if the current Try
instance is a
Success
; otherwise returns the Failure
instance.
This function is similar to Try.flatMap(ThrowableFunction)
, but the
mapping function'S result isn't a Try
, and if invoked, map
wraps it in Try
.
public <S extends java.lang.Exception> Try<T> mapFail(java.util.function.Function<java.lang.Exception,S> function)
Try
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.public <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
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.mapFailMatching
in class Try<T>
supplier
- the supplierTry
with the exception from the supplier; the Success
object otherwisepublic T orElse(T other)
Try
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.public T orElseGet(java.util.function.Supplier<? extends T> supplier)
Try
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()
.public <S extends java.lang.Throwable> T orElseThrow(java.util.function.Supplier<? extends S> supplier) throws S extends java.lang.Throwable
Try
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()
.orElseThrow
in class Try<T>
supplier
- the supplierSuccess
instance'S valueS
- if the current Try
instance was a Failure
objectS extends java.lang.Throwable
public T recover(java.util.function.Function<? super java.lang.Exception,T> function)
Try
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.public Try<T> recoverWith(ThrowableFunction<? super java.lang.Exception,Try<T>> throwableFunction)
Try
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.recoverWith
in class Try<T>
throwableFunction
- the functionTry
instance, if the current Try
instance
is a Failure
object; the current Success
object
otherwisepublic java.util.Optional<T> toOptional()
Try
Try
instance as an Optional
containing the value if it's a Success
or Optional#empty()
if it's a Failure
.toOptional
in class Try<T>
Optional
containing the value if it's a Success
; Optional#empty()
otherwisepublic void voidFold(java.util.function.Consumer<java.lang.Exception> failureConsumer, ThrowableConsumer<T> successConsumer)
Try
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
.