Class Try<V>
- java.lang.Object
-
- org.junit.platform.commons.function.Try<V>
-
@API(status=MAINTAINED, since="1.4") public abstract class Try<V> 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
Optionalwhen callers might want to report the exception via logging or by wrapping it in another exception at a later point in time, e.g. viagetOrThrow(Function).Moreover, it makes it particularly convenient to attach follow-up actions should the
Tryhave been successful (cf.andThen(java.util.function.Function<V, org.junit.platform.commons.function.Try<U>>)andandThenTry(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>>)andorElseTry(java.util.concurrent.Callable<V>)).- Since:
- 1.4
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceTry.Transformer<S,T>A transformer for values of typeSto typeT.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract <U> Try<U>andThen(Function<V,Try<U>> function)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.static <V> Try<V>call(Callable<V> action)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>failure(Exception cause)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.abstract <E extends Exception>
VgetOrThrow(Function<? super Exception,E> exceptionTransformer)abstract Try<V>ifFailure(Consumer<Exception> causeConsumer)If thisTryis a failure, call the suppliedConsumerwith the contained exception; otherwise, do nothing.abstract Try<V>ifSuccess(Consumer<V> valueConsumer)If thisTryis a success, call the suppliedConsumerwith the contained value; otherwise, do nothing.abstract Try<V>orElse(Supplier<Try<V>> supplier)If thisTryis a failure, call the supplied supplier and return the resultingTry; if thisTryis a success, do nothing.abstract Try<V>orElseTry(Callable<V> action)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.static <V> Try<V>success(V value)Convert the supplied value into a succeededTry.abstract Optional<V>toOptional()If thisTryis a failure, return an emptyOptional; if thisTryis a success, wrap the contained value usingOptional.ofNullable(Object).
-
-
-
Method Detail
-
call
public static <V> Try<V> call(Callable<V> action)
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(Object),failure(Exception)
-
success
public static <V> Try<V> success(V value)
Convert the supplied value into a succeededTry.- Parameters:
value- the value to wrap; potentiallynull- Returns:
- a succeeded
Trythat contains the supplied value; nevernull
-
failure
public static <V> Try<V> failure(Exception cause)
Convert the supplied exception into a failedTry.- Parameters:
cause- the exception to wrap; must not benull- Returns:
- a failed
Trythat contains the supplied value; nevernull
-
andThenTry
public 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.- Parameters:
transformer- the transformer to try; must not benull- Returns:
- a succeeded or failed
Try; nevernull
-
andThen
public abstract <U> Try<U> andThen(Function<V,Try<U>> function)
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
public abstract Try<V> orElseTry(Callable<V> action)
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
public abstract Try<V> orElse(Supplier<Try<V>> supplier)
If thisTryis a failure, call the supplied supplier and return the resultingTry; if thisTryis a success, do nothing.- Parameters:
supplier- the supplier to call; must not benull- Returns:
- a succeeded or failed
Try; nevernull
-
get
public abstract V get() throws Exception
If thisTryis a success, get the contained value; if thisTryis a failure, throw the contained exception.- Returns:
- the contained value, if available; potentially
null - Throws:
Exception- if thisTryis a failure
-
getOrThrow
public abstract <E extends Exception> V getOrThrow(Function<? super Exception,E> exceptionTransformer) throws E extends Exception
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 failureE extends Exception
-
ifSuccess
public abstract Try<V> ifSuccess(Consumer<V> valueConsumer)
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
public abstract Try<V> ifFailure(Consumer<Exception> causeConsumer)
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
public abstract Optional<V> 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
-
-