java.lang.Object
org.junit.platform.commons.function.Try<V>
A container object which may either contain a nullable value in case of
success or an exception in case of failure.
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>)
).
- Since:
- 1.4
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A transformer for values of typeS
to typeT
. -
Method Summary
Modifier and TypeMethodDescriptionabstract <U> Try
<U> If thisTry
is a success, apply the supplied function to its value and return the resultingTry
; if thisTry
is a failure, do nothing.abstract <U> Try
<U> andThenTry
(Try.Transformer<V, U> transformer) If thisTry
is a success, apply the supplied transformer to its value and return a new successful or failedTry
depending on the transformer's outcome; if thisTry
is a failure, do nothing.static <V> Try
<V> Call the suppliedCallable
and return a successfulTry
that contains the returned value or, in case an exception was thrown, a failedTry
that contains the exception.static <V> Try
<V> Convert the supplied exception into a failedTry
.abstract V
get()
If thisTry
is a success, get the contained value; if thisTry
is a failure, throw the contained exception.getOrThrow
(Function<? super Exception, E> exceptionTransformer) If thisTry
is a failure, call the suppliedConsumer
with the contained exception; otherwise, do nothing.If thisTry
is a success, call the suppliedConsumer
with the contained value; otherwise, do nothing.If thisTry
is a failure, call the supplied supplier and return the resultingTry
; if thisTry
is a success, do nothing.If thisTry
is a failure, call the supplied action and return a new successful or failedTry
depending on the action's outcome; if thisTry
is a success, do nothing.static <V> Try
<V> success
(V value) Convert the supplied value into a succeededTry
.If thisTry
is a failure, return an emptyOptional
; if thisTry
is a success, wrap the contained value usingOptional.ofNullable(Object)
.
-
Method Details
-
call
Call the suppliedCallable
and return a successfulTry
that contains the returned value or, in case an exception was thrown, a failedTry
that contains the exception.- Parameters:
action
- the action to try; must not benull
- Returns:
- a succeeded or failed
Try
depending on the outcome of the supplied action; nevernull
- See Also:
-
success
Convert the supplied value into a succeededTry
.- Parameters:
value
- the value to wrap; potentiallynull
- Returns:
- a succeeded
Try
that contains the supplied value; nevernull
-
failure
Convert the supplied exception into a failedTry
.- Parameters:
cause
- the exception to wrap; must not benull
- Returns:
- a failed
Try
that contains the supplied value; nevernull
-
andThenTry
If thisTry
is a success, apply the supplied transformer to its value and return a new successful or failedTry
depending on the transformer's outcome; if thisTry
is a failure, do nothing.- Parameters:
transformer
- the transformer to try; must not benull
- Returns:
- a succeeded or failed
Try
; nevernull
-
andThen
If thisTry
is a success, apply the supplied function to its value and return the resultingTry
; if thisTry
is a failure, do nothing.- Parameters:
function
- the function to apply; must not benull
- Returns:
- a succeeded or failed
Try
; nevernull
-
orElseTry
If thisTry
is a failure, call the supplied action and return a new successful or failedTry
depending on the action's outcome; if thisTry
is a success, do nothing.- Parameters:
action
- the action to try; must not benull
- Returns:
- a succeeded or failed
Try
; nevernull
-
orElse
If thisTry
is a failure, call the supplied supplier and return the resultingTry
; if thisTry
is a success, do nothing.- Parameters:
supplier
- the supplier to call; must not benull
- Returns:
- a succeeded or failed
Try
; nevernull
-
get
If thisTry
is a success, get the contained value; if thisTry
is a failure, throw the contained exception.- Returns:
- the contained value, if available; potentially
null
- Throws:
Exception
- if thisTry
is a failure
-
getOrThrow
public abstract <E extends Exception> V getOrThrow(Function<? super Exception, E> exceptionTransformer) throws EIf thisTry
is a success, get the contained value; if thisTry
is a failure, call the suppliedFunction
with the contained exception and throw the resultingException
.- Parameters:
exceptionTransformer
- the transformer to be called with the contained exception, if available; must not benull
- Returns:
- the contained value, if available
- Throws:
E
- if thisTry
is a failure
-
ifSuccess
If thisTry
is a success, call the suppliedConsumer
with the contained value; otherwise, do nothing.- Parameters:
valueConsumer
- the consumer to be called with the contained value, if available; must not benull
- Returns:
- the same
Try
for method chaining
-
ifFailure
If thisTry
is a failure, call the suppliedConsumer
with the contained exception; otherwise, do nothing.- Parameters:
causeConsumer
- the consumer to be called with the contained exception, if available; must not benull
- Returns:
- the same
Try
for method chaining
-
toOptional
If thisTry
is a failure, return an emptyOptional
; if thisTry
is a success, wrap the contained value usingOptional.ofNullable(Object)
.- Returns:
- an
Optional
; nevernull
but potentially empty
-