Package org.dataloader
Class Try<V>
- java.lang.Object
-
- org.dataloader.Try<V>
-
@PublicApi public class Try<V> extends java.lang.Object
Try is class that allows you to hold the result of computation or the throwable it produced. This class is useful inBatchLoaders 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() { ... }DataLoaderunderstands the use of Try and will take the exceptional path and complete the value promise with that exception value.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <V> Try<V>alwaysFailed()This returns a Try that has always failed with a 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 typeVget()java.lang.ThrowablegetThrowable()booleanisFailure()booleanisSuccess()<U> Try<U>map(java.util.function.Function<? super V,U> mapper)Maps the Try into another Try with a different typeVorElse(V other)Returns the successful value of the Try or other if it failedVorElseGet(java.util.function.Supplier<V> otherSupplier)Returns the successful value of the Try or the supplied other if it failedTry<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.voidreThrow()Rethrows the underlying throwable inside the unsuccessful Trystatic <V> Try<V>succeeded(V value)Creates a Try that has succeeded with the provided valuejava.util.Optional<V>toOptional()Converts the Try into an Optional where unsuccessful tries are emptyjava.lang.StringtoString()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 thatstatic <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 notstatic <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
-
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
succeeded
public static <V> Try<V> succeeded(V value)
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
public static <V> Try<V> failed(java.lang.Throwable throwable)
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
public static <V> Try<V> 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
public 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- Type Parameters:
V- the value type- Parameters:
callable- the code to call- Returns:
- a Try which is the result of the call
-
tryStage
public 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- 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
public 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- 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
public V get()
- Returns:
- the successful value of this try
- Throws:
java.lang.UnsupportedOperationException- if the Try is in fact in the unsuccessful state
-
getThrowable
public java.lang.Throwable getThrowable()
- Returns:
- the failed throwable of this try
- Throws:
java.lang.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
public <U> Try<U> map(java.util.function.Function<? super V,U> mapper)
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
public <U> Try<U> flatMap(java.util.function.Function<? super V,Try<U>> mapper)
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
public java.util.Optional<V> toOptional()
Converts the Try into an Optional where unsuccessful tries are empty- Returns:
- a new optional
-
orElse
public V orElse(V other)
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
public V orElseGet(java.util.function.Supplier<V> otherSupplier)
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
public void reThrow() throws java.lang.ThrowableRethrows the underlying throwable inside the unsuccessful Try- Throws:
java.lang.Throwable- if the Try was in fact throwablejava.lang.UnsupportedOperationException- if the try was in fact a successful Try
-
recover
public 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.- Parameters:
recoverFunction- the function to recover from a throwable into a new value- Returns:
- a Try of the same type
-
-