Class HandlerRunner

java.lang.Object
com.castlabs.utils.HandlerRunner

public final class HandlerRunner extends Object
Helper class to execute Callables on specific Handler
  • Constructor Details

    • HandlerRunner

      public HandlerRunner(@NonNull Handler handler, long timeoutMs, boolean verifyThread)
      Creates HandlerRunner instance.
      Parameters:
      handler - The handler with the looper to execute the callable on
      timeoutMs - The timeout for the operation in milliseconds
      verifyThread - true if thread verification is enabled, false otherwise
  • Method Details

    • disable

      public void disable(boolean mayInterruptIfRunning)
      Cancels all posted tasks and do not post all future tasks until enable()
      Parameters:
      mayInterruptIfRunning - Attempts to cancel execution of a running task
    • enable

      public void enable()
      Enables posted tasks execution
    • runOn

      @NonNull public <T> T runOn(@NonNull Callable<T> callable, @NonNull T valueOnError)
      Runs Callable on the Handler thread.
      Type Parameters:
      T - The callable result type.
      Parameters:
      callable - The callable to run.
      valueOnError - The value to return if execution fails or if the callable returns null. Must not be null.
      Returns:
      The callable result if non-null, or valueOnError otherwise.
    • runOnNullable

      @Nullable public <T> T runOnNullable(@NonNull Callable<T> callable, @Nullable T valueOnError)
      Runs Callable on the Handler thread.
      Type Parameters:
      T - The result type returned by the callable.
      Parameters:
      callable - Callable to execute
      valueOnError - Value to return upon failure
      Returns:
      The callable result, or valueOnError if execution fails. May be null.
    • runOn

      public void runOn(@NonNull Runnable runnable)
      Runs Runnable on the Handler thread.
      Parameters:
      runnable - Runnable to execute