@API(status=MAINTAINED,
since="1.4")
public abstract class Try<V>
extends Object
Instances of this class should be returned by methods instead of
Optional when callers might want to report the exception via logging
or by wrapping it in another exception at a later point in time, e.g. via
getOrThrow(Function).
Moreover, it makes it particularly convenient to attach follow-up actions
should the Try have been successful (cf. andThen(java.util.function.Function<V, org.junit.platform.commons.function.Try<U>>) and
andThenTry(org.junit.platform.commons.function.Try.Transformer<V, U>)) or fallback actions should it not have been (cf.
orElse(java.util.function.Supplier<org.junit.platform.commons.function.Try<V>>) and orElseTry(java.util.concurrent.Callable<V>)).
| Modifier and Type | Class and Description |
|---|---|
static interface |
Try.Transformer<S,T>
A transformer for values of type
S to type T. |
| Modifier and Type | Method and Description |
|---|---|
abstract <U> Try<U> |
andThen(Function<V,Try<U>> function)
If this
Try is a success, apply the supplied function to its
value and return the resulting Try; if this Try is a
failure, do nothing. |
abstract <U> Try<U> |
andThenTry(Try.Transformer<V,U> transformer)
If this
Try is a success, apply the supplied transformer to its
value and return a new successful or failed Try depending on the
transformer's outcome; if this Try is a failure, do nothing. |
static <V> Try<V> |
call(Callable<V> action)
Call the supplied
Callable and return a successful Try
that contains the returned value or, in case an exception was thrown, a
failed Try that contains the exception. |
static <V> Try<V> |
failure(Exception cause)
Convert the supplied exception into a failed
Try. |
abstract V |
get()
If this
Try is a success, get the contained value; if this
Try is a failure, throw the contained exception. |
abstract <E extends Exception> |
getOrThrow(Function<? super Exception,E> exceptionTransformer)
|
abstract Try<V> |
ifFailure(Consumer<Exception> causeConsumer)
If this
Try is a failure, call the supplied Consumer with
the contained exception; otherwise, do nothing. |
abstract Try<V> |
ifSuccess(Consumer<V> valueConsumer)
If this
Try is a success, call the supplied Consumer with
the contained value; otherwise, do nothing. |
abstract Try<V> |
orElse(Supplier<Try<V>> supplier)
If this
Try is a failure, call the supplied supplier and return
the resulting Try; if this Try is a success, do nothing. |
abstract Try<V> |
orElseTry(Callable<V> action)
If this
Try is a failure, call the supplied action and return a
new successful or failed Try depending on the action's outcome;
if this Try is a success, do nothing. |
static <V> Try<V> |
success(V value)
Convert the supplied value into a succeeded
Try. |
abstract Optional<V> |
toOptional()
If this
Try is a failure, return an empty Optional; if
this Try is a success, wrap the contained value using
Optional.ofNullable(Object). |
public static <V> Try<V> call(Callable<V> action)
Callable and return a successful Try
that contains the returned value or, in case an exception was thrown, a
failed Try that contains the exception.action - the action to try; must not be nullTry depending on the outcome of the
supplied action; never nullsuccess(Object),
failure(Exception)public static <V> Try<V> success(V value)
Try.value - the value to wrap; potentially nullTry that contains the supplied value; never
nullpublic static <V> Try<V> failure(Exception cause)
Try.cause - the exception to wrap; must not be nullTry that contains the supplied value; never
nullpublic abstract <U> Try<U> andThenTry(Try.Transformer<V,U> transformer)
Try is a success, apply the supplied transformer to its
value and return a new successful or failed Try depending on the
transformer's outcome; if this Try is a failure, do nothing.transformer - the transformer to try; must not be nullTry; never nullpublic abstract <U> Try<U> andThen(Function<V,Try<U>> function)
Try is a success, apply the supplied function to its
value and return the resulting Try; if this Try is a
failure, do nothing.function - the function to apply; must not be nullTry; never nullpublic abstract Try<V> orElseTry(Callable<V> action)
Try is a failure, call the supplied action and return a
new successful or failed Try depending on the action's outcome;
if this Try is a success, do nothing.action - the action to try; must not be nullTry; never nullpublic abstract Try<V> orElse(Supplier<Try<V>> supplier)
Try is a failure, call the supplied supplier and return
the resulting Try; if this Try is a success, do nothing.supplier - the supplier to call; must not be nullTry; never nullpublic abstract V get() throws Exception
Try is a success, get the contained value; if this
Try is a failure, throw the contained exception.nullException - if this Try is a failurepublic abstract <E extends Exception> V getOrThrow(Function<? super Exception,E> exceptionTransformer) throws E extends Exception
Try is a success, get the contained value; if this
Try is a failure, call the supplied Function with the
contained exception and throw the resulting Exception.exceptionTransformer - the transformer to be called with the
contained exception, if available; must not be nullE - if this Try is a failureE extends Exceptionpublic abstract Try<V> ifSuccess(Consumer<V> valueConsumer)
Try is a success, call the supplied Consumer with
the contained value; otherwise, do nothing.valueConsumer - the consumer to be called with the contained value,
if available; must not be nullTry for method chainingpublic abstract Try<V> ifFailure(Consumer<Exception> causeConsumer)
Try is a failure, call the supplied Consumer with
the contained exception; otherwise, do nothing.causeConsumer - the consumer to be called with the contained
exception, if available; must not be nullTry for method chainingpublic abstract Optional<V> toOptional()
Try is a failure, return an empty Optional; if
this Try is a success, wrap the contained value using
Optional.ofNullable(Object).Optional; never null but potentially
emptyCopyright © 2024. All rights reserved.