Class Try<V extends @Nullable Object>
java.lang.Object
org.junit.platform.commons.function.Try<V>
@API(status=MAINTAINED,
since="1.4")
public abstract class Try<V extends @Nullable Object>
extends Object
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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceTry.Transformer<S extends @Nullable Object, T extends @Nullable Object>A transformer for values of typeSto typeT. -
Method Summary
Modifier and TypeMethodDescriptionabstract <U> Try<U> If thisTryis a success, apply the supplied function to its value and return the resultingTry; if thisTryis a failure, do nothing.abstract <U> Try<U> andThenTry(Try.Transformer<V, U> transformer) If thisTryis a success, apply the supplied transformer to its value and return a new successful or failedTrydepending on the transformer's outcome; if thisTryis a failure, do nothing.Call the suppliedCallableand return a successfulTrythat contains the returned value or, in case an exception was thrown, a failedTrythat contains the exception.static <V> Try<V> Convert the supplied exception into a failedTry.abstract Vget()If thisTryis a success, get the contained value; if thisTryis a failure, throw the contained exception.final @NonNull VIf thisTryis a success, get the contained value; if thisTryis a failure, throw the contained exception.getNonNullOrThrow(Function<@Nullable Exception, E> exceptionTransformer) getOrThrow(Function<? super Exception, E> exceptionTransformer) If thisTryis a failure, call the suppliedConsumerwith the contained exception; otherwise, do nothing.If thisTryis a success, call the suppliedConsumerwith the contained value; otherwise, do nothing.If thisTryis a failure, call the supplied supplier and return the resultingTry; if thisTryis a success, do nothing.If thisTryis a failure, call the supplied action and return a new successful or failedTrydepending on the action's outcome; if thisTryis a success, do nothing.success(V value) Convert the supplied value into a succeededTry.If thisTryis a failure, return an emptyOptional; if thisTryis a success, wrap the contained value usingOptional.ofNullable(Object).
-
Method Details
-
call
Call the suppliedCallableand return a successfulTrythat contains the returned value or, in case an exception was thrown, a failedTrythat contains the exception.- Parameters:
action- the action to try; must not benull- Returns:
- a succeeded or failed
Trydepending on the outcome of the supplied action; nevernull - See Also:
-
success
-
failure
-
andThenTry
If thisTryis a success, apply the supplied transformer to its value and return a new successful or failedTrydepending on the transformer's outcome; if thisTryis a failure, do nothing.- Parameters:
transformer- the transformer to try; must not benull- Returns:
- a succeeded or failed
Try; nevernull
-
andThen
If thisTryis a success, apply the supplied function to its value and return the resultingTry; if thisTryis a failure, do nothing.- Parameters:
function- the function to apply; must not benull- Returns:
- a succeeded or failed
Try; nevernull
-
orElseTry
If thisTryis a failure, call the supplied action and return a new successful or failedTrydepending on the action's outcome; if thisTryis a success, do nothing.- Parameters:
action- the action to try; must not benull- Returns:
- a succeeded or failed
Try; nevernull
-
orElse
-
get
-
getNonNull
If thisTryis a success, get the contained value; if thisTryis a failure, throw the contained exception.- Returns:
- the contained value, if available
- Throws:
Exception- if thisTryis a failure or the contained value isnull- Since:
- 6.0
-
getOrThrow
public abstract <E extends Exception> V getOrThrow(Function<? super Exception, E> exceptionTransformer) throws E If thisTryis a success, get the contained value; if thisTryis a failure, call the suppliedFunctionwith 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 thisTryis a failure
-
getNonNullOrThrow
@API(status=EXPERIMENTAL, since="6.0") public final <E extends Exception> @NonNull V getNonNullOrThrow(Function<@Nullable Exception, E> exceptionTransformer) throws E If thisTryis a success, get the contained value; if thisTryis a failure, call the suppliedFunctionwith 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 and not
null - Throws:
E- if thisTryis a failure or the contained value isnull
-
ifSuccess
If thisTryis a success, call the suppliedConsumerwith 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
Tryfor method chaining
-
ifFailure
If thisTryis a failure, call the suppliedConsumerwith 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
Tryfor method chaining
-
toOptional
If thisTryis a failure, return an emptyOptional; if thisTryis a success, wrap the contained value usingOptional.ofNullable(Object).- Returns:
- an
Optional; nevernullbut potentially empty
-