Class HandlerRunner

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

public final class HandlerRunner extends Object
Helper class to execute Callables on specific Handler
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static boolean
    Set to true to get more logging
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    static void
    Logs all threads data
    boolean
    post(Runnable runnable)
    Posts Runnable to the Handler thread.
    void
    run(Runnable runnable)
    Runs Runnable on the Handler thread and waits for the completion.
    static void
    run(Runnable runnable, Handler handler)
    Runs Runnable on the Handler thread and waits for the completion.
    <T> T
    run(Callable<T> callable, T valueOnError)
    Runs Callable on the Handler thread and waits for the result.
    static <T> T
    run(Callable<T> callable, T valueOnError, Handler handler)
    Runs Callable on the Handler thread and waits for the result.
    <T> T
    runNullable(Callable<T> callable, T valueOnError)
    Runs Callable on the Handler thread and waits for the result.
    static <T> T
    runNullable(Callable<T> callable, T valueOnError, Handler handler)
    Runs Callable on the Handler thread and waits for the result.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEBUG

      public static boolean DEBUG
      Set to true to get more logging
  • Constructor Details

    • HandlerRunner

      public HandlerRunner(@NonNull Handler handler)
      Creates HandlerRunner instance.
      Parameters:
      handler - The handler with the looper to execute the callable on
    • 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

    • run

      @NonNull public <T> T run(@NonNull Callable<T> callable, @NonNull T valueOnError)
      Runs Callable on the Handler thread and waits for the result.
      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.
    • run

      @NonNull public static <T> T run(@NonNull Callable<T> callable, @NonNull T valueOnError, @NonNull Handler handler)
      Runs Callable on the Handler thread and waits for the result.
      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.
      handler - Handler
      Returns:
      The callable result if non-null, or valueOnError otherwise.
    • runNullable

      @Nullable public <T> T runNullable(@NonNull Callable<T> callable, @Nullable T valueOnError)
      Runs Callable on the Handler thread and waits for the result.
      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.
    • runNullable

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

      public boolean post(@NonNull Runnable runnable)
      Posts Runnable to the Handler thread.
      Parameters:
      runnable - Runnable to post
      Returns:
      true if the runnable was posted
    • run

      public void run(@NonNull Runnable runnable)
      Runs Runnable on the Handler thread and waits for the completion.
      Parameters:
      runnable - Runnable to execute
    • run

      public static void run(@NonNull Runnable runnable, @NonNull Handler handler)
      Runs Runnable on the Handler thread and waits for the completion.
      Parameters:
      runnable - Runnable to execute
      handler - Handler
    • dumpAllThreads

      public static void dumpAllThreads(@NonNull String tag)
      Logs all threads data
      Parameters:
      tag - The log tag