@PublicApi public class Try<V> extends java.lang.Object
BatchLoader
s so you can mix a batch of calls where some of
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.Modifier and Type | Method and Description |
---|---|
static <V> Try<V> |
alwaysFailed()
This returns a Try that has always failed with an consistent exception.
|
static <V> Try<V> |
failed(java.lang.Throwable throwable)
Creates a Try that has failed with the provided throwable
|
<U> Try<U> |
flatMap(java.util.function.Function<? super V,Try<U>> mapper)
Flats maps the Try into another Try type
|
V |
get() |
java.lang.Throwable |
getThrowable() |
boolean |
isFailure() |
boolean |
isSuccess() |
<U> Try<U> |
map(java.util.function.Function<? super V,U> mapper)
Maps the Try into another Try with a different type
|
V |
orElse(V other)
Returns the successful value of the Try or other if it failed
|
V |
orElseGet(java.util.function.Supplier<V> otherSupplier)
Returns the successful value of the Try or the supplied other if it failed
|
Try<V> |
recover(java.util.function.Function<java.lang.Throwable,V> recoverFunction)
Called the recover function of the Try failed otherwise returns this if it was successful.
|
void |
reThrow()
Rethrows the underlying throwable inside the unsuccessful Try
|
static <V> Try<V> |
succeeded(V value)
Creates a Try that has succeeded with the provided value
|
java.util.Optional<V> |
toOptional()
Converts the Try into an Optional where unsuccessful tries are empty
|
java.lang.String |
toString() |
static <V> Try<V> |
tryCall(java.util.concurrent.Callable<V> callable)
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
|
static <V> java.util.concurrent.CompletableFuture<Try<V>> |
tryFuture(java.util.concurrent.CompletionStage<V> completionStage)
Creates a CompletableFuture that, when it completes, will capture into a Try whether the given completionStage
was successful or not
|
static <V> java.util.concurrent.CompletionStage<Try<V>> |
tryStage(java.util.concurrent.CompletionStage<V> completionStage)
Creates a CompletionStage that, when it completes, will capture into a Try whether the given completionStage
was successful or not
|
public java.lang.String toString()
toString
in class java.lang.Object
public static <V> Try<V> succeeded(V value)
V
- the value typevalue
- the successful valuepublic static <V> Try<V> failed(java.lang.Throwable throwable)
V
- the value typethrowable
- the failed throwablepublic static <V> Try<V> alwaysFailed()
V
- the type of valuepublic static <V> Try<V> tryCall(java.util.concurrent.Callable<V> callable)
V
- the value typecallable
- the code to callpublic static <V> java.util.concurrent.CompletionStage<Try<V>> tryStage(java.util.concurrent.CompletionStage<V> completionStage)
V
- the value typecompletionStage
- the completion stage that will completepublic static <V> java.util.concurrent.CompletableFuture<Try<V>> tryFuture(java.util.concurrent.CompletionStage<V> completionStage)
V
- the value typecompletionStage
- the completion stage that will completepublic V get()
java.lang.UnsupportedOperationException
- if the Try is in fact in the unsuccessful statepublic java.lang.Throwable getThrowable()
java.lang.UnsupportedOperationException
- if the Try is in fact in the successful statepublic boolean isSuccess()
public boolean isFailure()
public <U> Try<U> map(java.util.function.Function<? super V,U> mapper)
U
- the target typemapper
- the function to map the current Try to a new Trypublic <U> Try<U> flatMap(java.util.function.Function<? super V,Try<U>> mapper)
U
- the target typemapper
- the flat map functionpublic java.util.Optional<V> toOptional()
public V orElse(V other)
other
- the other value if the Try failedpublic V orElseGet(java.util.function.Supplier<V> otherSupplier)
otherSupplier
- the other value supplied if the Try failedpublic void reThrow() throws java.lang.Throwable
java.lang.Throwable
- if the Try was in fact throwablejava.lang.UnsupportedOperationException
- if the try was in fact a successful Trypublic Try<V> recover(java.util.function.Function<java.lang.Throwable,V> recoverFunction)
recoverFunction
- the function to recover from a throwable into a new value