Package javascalautils
Interface Try<T>
-
- Type Parameters:
T
- The type of the value represented by this Try
- All Superinterfaces:
java.lang.Iterable<T>
public interface Try<T> extends java.lang.Iterable<T>
The Try type represents a computation that may either result in an exception, or return a successfully computed value.
Typical use case is situations where parallel computation takes place resulting in more than one response where it's possible that one or more computations fail.
Though it might not be desirable to raise an exception for failed computations hence the Try acts a place holder for a response that is either failed or successful.
Instances of Try, are either an instance ofSuccess
orFailure
.
Example of usage:
Try<SomeData> getSomeData(SomeInput input) { try { // readUserFromDB throws if no user exists SomeData data = readUserFromDB(input); return new Success<>(data); } catch (SomeException ex) { return new Failure<>(ex); } }
checked function
provided to theapply(ThrowableFunction0)
method.
Try<Integer> resultSuccess = Try.apply(() -> 9 / 3); // results in Success(3) Try<Integer> resultFailure = Try.apply(() -> 9 / 0); // results in Failure(ArithmeticException)
apply(ThrowableFunction0)
method.
Try<SomeData> getSomeData(SomeInput input) { // readUserFromDB throws if no user exists Try.apply(() -> readUserFromDB(input)); }
Try(ThrowableFunction0)
.
This however requires you to statically import the proper methods from thecompanion class
related to Try.import static javascalautils.TryCompanion.Try; Try<SomeData> getSomeData(SomeInput input) { // readUserFromDB throws if no user exists Try(() -> readUserFromDB(input)); }
- Since:
- 1.0
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <T> Try<T>
apply(ThrowableFunction0<T> function)
Creates an instance of Try wrapping the result of the provided function.static <T> Try<T>
apply(T value)
Creates an instance of Try.default Option<T>
asOption()
Try<java.lang.Throwable>
failed()
Completes this Try with an exception wrapped in aSuccess
.Try<T>
filter(java.util.function.Predicate<T> filter)
<R> Try<R>
flatMap(ThrowableFunction1<T,Try<R>> function)
T
get()
T
getOrElse(java.util.function.Supplier<T> supplier)
default boolean
isFailure()
boolean
isSuccess()
java.util.Iterator<T>
iterator()
<R> Try<R>
map(ThrowableFunction1<T,R> function)
Try<T>
orElse(java.util.function.Supplier<Try<T>> supplier)
default T
orNull()
Returns the value if it is aSuccess
, elsenull
.Try<T>
recover(ThrowableFunction1<java.lang.Throwable,T> function)
Try<T>
recoverWith(ThrowableFunction1<java.lang.Throwable,Try<T>> function)
java.util.stream.Stream<T>
stream()
-
-
-
Method Detail
-
apply
static <T> Try<T> apply(T value)
Creates an instance of Try.
If anull
or non-throwable value is provided thenSuccess
is returned containing the value, elseFailure
containing the provided throwable.- Type Parameters:
T
- The type for the Try- Parameters:
value
- The value for this to create a Try- Returns:
- The Try instance
- Since:
- 1.0
-
apply
static <T> Try<T> apply(ThrowableFunction0<T> function)
Creates an instance of Try wrapping the result of the provided function.
If the function results in a value thenSuccess
with the value is returned.
In case the function raises an exception thenFailure
is returned containing that exception.
Ifnull
is provided as argument then theapply(Object)
is invoked.
Example simple division by zero results in an exception.Try<Integer> resultSuccess = Try.apply(() -> 9 / 3); // results in Success(3) Try<Integer> resultFailure = Try.apply(() -> 9 / 0); // results in Failure(ArithmeticException)
- Type Parameters:
T
- The type for the Try- Parameters:
function
- The function to render either the value T or raise an exception.- Returns:
- The resulting Try instance wrapping what the function resulted in
- Since:
- 1.3
-
isFailure
default boolean isFailure()
- Returns:
- If the Try is a
Failure
- Since:
- 1.0
-
isSuccess
boolean isSuccess()
- Returns:
- If the Try is a
Success
- Since:
- 1.0
-
getOrElse
T getOrElse(java.util.function.Supplier<T> supplier)
- Parameters:
supplier
- The supplier to return the value in case of aFailure
- Returns:
- The value from the Try or the supplier
- Since:
- 1.0
-
orNull
default T orNull()
Returns the value if it is aSuccess
, elsenull
.- Returns:
- The value of the Try or
null
. - Since:
- 1.0
-
orElse
Try<T> orElse(java.util.function.Supplier<Try<T>> supplier)
- Parameters:
supplier
- The supplier to return the value in case of aFailure
- Returns:
- This try or the value from the supplier
-
get
T get() throws java.lang.Throwable
-
failed
Try<java.lang.Throwable> failed()
-
filter
Try<T> filter(java.util.function.Predicate<T> filter)
Applies the predicate to the value of theTry
and either returns the Try if the predicate matched or aFailure
.
One of the three outcomes are applicable:- Parameters:
filter
- The filter to apply- Returns:
- The try matching the filter, either this if matching or a
Failure
in case no match - Since:
- 1.4
-
iterator
java.util.Iterator<T> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<T>
- Returns:
- The iterator for the Try
- Since:
- 1.0
-
map
<R> Try<R> map(ThrowableFunction1<T,R> function)
Maps the given function to the value from thisSuccess
or returns this if this is aFailure
.
This allows for mapping aTry
containing some type to some completely different type.
The example converts aTry
of type String to Integer.Try<String> t = ... Try<Integer> t2 = t.map(v -> v.length);
- Type Parameters:
R
- The type for the return value from the function- Parameters:
function
- The function to use- Returns:
- The Option containing the mapped value
- Since:
- 1.0
-
flatMap
<R> Try<R> flatMap(ThrowableFunction1<T,Try<R>> function)
- Type Parameters:
R
- The type for the return value from the function- Parameters:
function
- The function to use- Returns:
- The Option containing the mapped value
- Since:
- 1.2
-
recover
Try<T> recover(ThrowableFunction1<java.lang.Throwable,T> function)
Creates a newTry
that in case thisTry
is aFailure
will apply the function to recover to aSuccess
.
Should this be aSuccess
then this is returned, i.e. no new instance is created.
This is a kind ofmap(ThrowableFunction1)
for failures only.
E.g.
In case of t being successful then that value is passed on to recovered, in case of failure then the recover function kicks in and returns the message from the throwable.Try<String> t = ... Try<String> recovered = t.recover(t -> t.getMessage());
Success(3)
Try<Integer> t = Try(() -> 9/3).recover(t -> 0)
Whilst this statement will beSuccess(0)
as it is division-by-zeroTry<Integer> t = Try(() -> 9/0).recover(t -> 0)
- Parameters:
function
- The function to apply in case of aFailure
- Returns:
- The recovered Try
- Since:
- 1.4
-
recoverWith
Try<T> recoverWith(ThrowableFunction1<java.lang.Throwable,Try<T>> function)
Creates a newTry
that in case thisTry
is aFailure
will apply the function to recover theTry
rendered by the function.
Should this be aSuccess
the value is propagated as-is.
This is a kind ofmap(ThrowableFunction1)
for failures only.
E.g.Try<String> t = ... Try<String> recovered = t.recover(t -> new Success<>(t.getMessage()));
Success
with message from the throwable.- Parameters:
function
- The function to apply in case of aFailure
- Returns:
- The recovered Try
- Since:
- 1.4
-
stream
java.util.stream.Stream<T> stream()
- Returns:
- The stream for the Try
- Since:
- 1.0
-
asOption
default Option<T> asOption()
Returns thisTry
as anOption
.
If it is aSuccess
then the value is wrapped inSome
elseNone
is returned.
Should theSuccess
contain anull
value the result will beNone
asnull
values are per definition none/nothing.- Returns:
- The
Option
representing this Try - Since:
- 1.0
-
-