Package org.dataloader
Class Try<V>
java.lang.Object
org.dataloader.Try<V>
Try is class that allows you to hold the result of computation or the throwable it produced.
This class is useful in
BatchLoader
s so you can mix a batch of calls where some
the calls succeeded and some of them failed. You would make your batch loader declaration like :
BatchLoader<K,Try<V> batchLoader = new BatchLoader() { ... }
DataLoader
understands the use of Try and will take the exceptional path and complete
the value promise with that exception value.-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> Try<V>
This returns a Try that has always failed with a consistent exception.static <V> Try<V>
Creates a Try that has failed with the provided throwable<U> Try<U>
Flats maps the Try into another Try typeget()
boolean
boolean
<U> Try<U>
Maps the Try into another Try with a different typeReturns the successful value of the Try or other if it failedReturns the successful value of the Try or the supplied other if it failedCalled the recover function of the Try failed otherwise returns this if it was successful.void
reThrow()
Rethrows the underlying throwable inside the unsuccessful Trystatic <V> Try<V>
succeeded
(V value) Creates a Try that has succeeded with the provided valueConverts the Try into an Optional where unsuccessful tries are emptytoString()
static <V> Try<V>
Calls the callable and if it returns a value, the Try is successful with that value or if throws and exception the Try captures thatstatic <V> CompletableFuture<Try<V>>
tryFuture
(CompletionStage<V> completionStage) Creates a CompletableFuture that, when it completes, will capture into a Try whether the given completionStage was successful or notstatic <V> CompletionStage<Try<V>>
tryStage
(CompletionStage<V> completionStage) Creates a CompletionStage that, when it completes, will capture into a Try whether the given completionStage was successful or not
-
Method Details
-
toString
-
succeeded
Creates a Try that has succeeded with the provided value- Type Parameters:
V
- the value type- Parameters:
value
- the successful value- Returns:
- a successful Try
-
failed
Creates a Try that has failed with the provided throwable- Type Parameters:
V
- the value type- Parameters:
throwable
- the failed throwable- Returns:
- a failed Try
-
alwaysFailed
This returns a Try that has always failed with a consistent exception. Use this when you don't care about the exception but only that the Try failed.- Type Parameters:
V
- the type of value- Returns:
- a Try that has failed
-
tryCall
Calls the callable and if it returns a value, the Try is successful with that value or if throws and exception the Try captures that- Type Parameters:
V
- the value type- Parameters:
callable
- the code to call- Returns:
- a Try which is the result of the call
-
tryStage
Creates a CompletionStage that, when it completes, will capture into a Try whether the given completionStage was successful or not- Type Parameters:
V
- the value type- Parameters:
completionStage
- the completion stage that will complete- Returns:
- a CompletionStage Try which is the result of the call
-
tryFuture
Creates a CompletableFuture that, when it completes, will capture into a Try whether the given completionStage was successful or not- Type Parameters:
V
- the value type- Parameters:
completionStage
- the completion stage that will complete- Returns:
- a CompletableFuture Try which is the result of the call
-
get
- Returns:
- the successful value of this try
- Throws:
UnsupportedOperationException
- if the Try is in fact in the unsuccessful state
-
getThrowable
- Returns:
- the failed throwable of this try
- Throws:
UnsupportedOperationException
- if the Try is in fact in the successful state
-
isSuccess
public boolean isSuccess()- Returns:
- true if this Try succeeded and therefore has a value
-
isFailure
public boolean isFailure()- Returns:
- true if this Try failed and therefore has a throwable
-
map
Maps the Try into another Try with a different type- Type Parameters:
U
- the target type- Parameters:
mapper
- the function to map the current Try to a new Try- Returns:
- the mapped Try
-
flatMap
Flats maps the Try into another Try type- Type Parameters:
U
- the target type- Parameters:
mapper
- the flat map function- Returns:
- a new Try
-
toOptional
Converts the Try into an Optional where unsuccessful tries are empty- Returns:
- a new optional
-
orElse
Returns the successful value of the Try or other if it failed- Parameters:
other
- the other value if the Try failed- Returns:
- the value of the Try or an alternative
-
orElseGet
Returns the successful value of the Try or the supplied other if it failed- Parameters:
otherSupplier
- the other value supplied if the Try failed- Returns:
- the value of the Try or an alternative
-
reThrow
Rethrows the underlying throwable inside the unsuccessful Try- Throws:
Throwable
- if the Try was in fact throwableUnsupportedOperationException
- if the try was in fact a successful Try
-
recover
Called the recover function of the Try failed otherwise returns this if it was successful.- Parameters:
recoverFunction
- the function to recover from a throwable into a new value- Returns:
- a Try of the same type
-