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
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. viagetOrThrow(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>>)
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 interface
Try.Transformer<S,T>
A transformer for values of typeS
to 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 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(Callable<V> action)
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>
failure(Exception cause)
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.abstract <E extends Exception>
VgetOrThrow(Function<? super Exception,E> exceptionTransformer)
abstract Try<V>
ifFailure(Consumer<Exception> causeConsumer)
If thisTry
is a failure, call the suppliedConsumer
with the contained exception; otherwise, do nothing.abstract Try<V>
ifSuccess(Consumer<V> valueConsumer)
If thisTry
is a success, call the suppliedConsumer
with the contained value; otherwise, do nothing.abstract Try<V>
orElse(Supplier<Try<V>> supplier)
If thisTry
is a failure, call the supplied supplier and return the resultingTry
; if thisTry
is a success, do nothing.abstract Try<V>
orElseTry(Callable<V> action)
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
.abstract Optional<V>
toOptional()
If thisTry
is a failure, return an emptyOptional
; if thisTry
is a success, wrap the contained value usingOptional.ofNullable(Object)
.
-
-
-
Method Detail
-
call
public static <V> Try<V> call(Callable<V> action)
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(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
Try
that 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
Try
that contains the supplied value; nevernull
-
andThenTry
public 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.- 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 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
public abstract Try<V> orElseTry(Callable<V> action)
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
public abstract Try<V> orElse(Supplier<Try<V>> supplier)
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
public abstract V get() throws Exception
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 E extends Exception
If 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 failureE extends Exception
-
ifSuccess
public abstract Try<V> ifSuccess(Consumer<V> valueConsumer)
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
public abstract Try<V> ifFailure(Consumer<Exception> causeConsumer)
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
public abstract Optional<V> 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
-
-