Class Try<V>

java.lang.Object
org.dataloader.Try<V>

@PublicApi public class Try<V> extends Object
Try is class that allows you to hold the result of computation or the throwable it produced. This class is useful in BatchLoaders 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 Type
    Method
    Description
    static <V> Try<V>
    This returns a Try that has always failed with a consistent exception.
    static <V> Try<V>
    failed(Throwable throwable)
    Creates a Try that has failed with the provided throwable
    <U> Try<U>
    flatMap(Function<? super V,Try<U>> mapper)
    Flats maps the Try into another Try type
    get()
     
     
    boolean
     
    boolean
     
    <U> Try<U>
    map(Function<? super V,U> mapper)
    Maps the Try into another Try with a different type
    orElse(V other)
    Returns the successful value of the Try or other if it failed
    orElseGet(Supplier<V> otherSupplier)
    Returns the successful value of the Try or the supplied other if it failed
    recover(Function<Throwable,V> recoverFunction)
    Called the recover function of the Try failed otherwise returns this if it was successful.
    void
    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
    Converts the Try into an Optional where unsuccessful tries are empty
     
    static <V> Try<V>
    tryCall(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> 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 not
    static <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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class 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(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(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> 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
      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> 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 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:
      UnsupportedOperationException - if the Try is in fact in the unsuccessful state
    • getThrowable

      public Throwable 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

      public <U> Try<U> map(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(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 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(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 Throwable
      Rethrows the underlying throwable inside the unsuccessful Try
      Throws:
      Throwable - if the Try was in fact throwable
      UnsupportedOperationException - if the try was in fact a successful Try
    • recover

      public Try<V> recover(Function<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