Package com.castlabs.utils

# Class HandlerRunner

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

public final class HandlerRunner extends Object
Helper class to execute `Callable`s on specific `Handler`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `HandlerRunner(Handler handler, long timeoutMs, boolean verifyThread)`
  Creates `HandlerRunner` instance.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `disable(boolean mayInterruptIfRunning)`
  Cancels all posted tasks and do not post all future tasks until `enable()`
  `void`
  `enable()`
  Enables posted tasks execution
  `void`
  `runOn(Runnable runnable)`
  Runs `Runnable` on the `Handler` thread.
  `<T> T`
  `runOn(Callable<T> callable, T valueOnError)`
  Runs `Callable` on the `Handler` thread.
  `<T> T`
  `runOnNullable(Callable<T> callable, T valueOnError)`
  Runs `Callable` on the `Handler` thread.

  ### Methods inherited from class java.lang.Object

  `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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