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 ofSuccessorFailure.
Example of usage:
A more elaborate way is to provide aTry<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 functionprovided to theapply(ThrowableFunction0)method.
Or let us re-write the first example to use theTry<Integer> resultSuccess = Try.apply(() -> 9 / 3); // results in Success(3) Try<Integer> resultFailure = Try.apply(() -> 9 / 0); // results in Failure(ArithmeticException)
apply(ThrowableFunction0)method.
To make it even more concise we can create Try instances just by usingTry<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 classrelated 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)Tget()TgetOrElse(java.util.function.Supplier<T> supplier)default booleanisFailure()booleanisSuccess()java.util.Iterator<T>iterator()<R> Try<R>map(ThrowableFunction1<T,R> function)Try<T>orElse(java.util.function.Supplier<Try<T>> supplier)default TorNull()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 anullor non-throwable value is provided thenSuccessis returned containing the value, elseFailurecontaining 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 thenSuccesswith the value is returned.
In case the function raises an exception thenFailureis returned containing that exception.
Ifnullis 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 theTryand 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
Failurein case no match - Since:
- 1.4
-
iterator
java.util.Iterator<T> iterator()
- Specified by:
iteratorin 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 thisSuccessor returns this if this is aFailure.
This allows for mapping aTrycontaining some type to some completely different type.
The example converts aTryof 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 newTrythat in case thisTryis aFailurewill apply the function to recover to aSuccess.
Should this be aSuccessthen 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.
This statement will beTry<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 newTrythat in case thisTryis aFailurewill apply the function to recover theTryrendered by the function.
Should this be aSuccessthe value is propagated as-is.
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 aTry<String> t = ... Try<String> recovered = t.recover(t -> new Success<>(t.getMessage()));
Successwith 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 thisTryas anOption.
If it is aSuccessthen the value is wrapped inSomeelseNoneis returned.
Should theSuccesscontain anullvalue the result will beNoneasnullvalues are per definition none/nothing.- Returns:
- The
Optionrepresenting this Try - Since:
- 1.0
-
-