@API(status=MAINTAINED,
since="1.4")
public abstract class Try<V>
extends java.lang.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 |
---|---|
private static class |
Try.Failure<V> |
private static class |
Try.Success<V> |
static interface |
Try.Transformer<S,T>
A transformer for values of type
S to type T . |
Modifier | Constructor and Description |
---|---|
private |
Try() |
Modifier and Type | Method and Description |
---|---|
abstract <U> Try<U> |
andThen(java.util.function.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(java.util.concurrent.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. |
private static <T> T |
checkNotNull(T input,
java.lang.String title) |
static <V> Try<V> |
failure(java.lang.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 java.lang.Exception> |
getOrThrow(java.util.function.Function<? super java.lang.Exception,E> exceptionTransformer)
If this
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 . |
abstract Try<V> |
ifFailure(java.util.function.Consumer<java.lang.Exception> causeConsumer)
If this
Try is a failure, call the supplied Consumer with
the contained exception; otherwise, do nothing. |
abstract Try<V> |
ifSuccess(java.util.function.Consumer<V> valueConsumer)
If this
Try is a success, call the supplied Consumer with
the contained value; otherwise, do nothing. |
private static <V> Try<V> |
of(java.util.concurrent.Callable<Try<V>> action) |
abstract Try<V> |
orElse(java.util.function.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(java.util.concurrent.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 java.util.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(java.util.concurrent.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 null
Try
depending on the outcome of the
supplied action; never null
success(Object)
,
failure(Exception)
public static <V> Try<V> success(V value)
Try
.value
- the value to wrap; potentially null
Try
that contains the supplied value; never
null
public static <V> Try<V> failure(java.lang.Exception cause)
Try
.cause
- the exception to wrap; must not be null
Try
that contains the supplied value; never
null
private static <T> T checkNotNull(T input, java.lang.String title)
public 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 null
Try
; never null
public abstract <U> Try<U> andThen(java.util.function.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 null
Try
; never null
public abstract Try<V> orElseTry(java.util.concurrent.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 null
Try
; never null
public abstract Try<V> orElse(java.util.function.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 null
Try
; never null
public abstract V get() throws java.lang.Exception
Try
is a success, get the contained value; if this
Try
is a failure, throw the contained exception.null
java.lang.Exception
- if this Try
is a failurepublic abstract <E extends java.lang.Exception> V getOrThrow(java.util.function.Function<? super java.lang.Exception,E> exceptionTransformer) throws E extends java.lang.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 null
E
- if this Try
is a failureE extends java.lang.Exception
public abstract Try<V> ifSuccess(java.util.function.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 null
Try
for method chainingpublic abstract Try<V> ifFailure(java.util.function.Consumer<java.lang.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 null
Try
for method chainingpublic abstract java.util.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
empty