Class Throttle

java.lang.Object
no.ntnu.principes.util.Throttle

public class Throttle extends Object
Provides throttling and debouncing utilities for controlling execution frequency. Throttling limits function calls to a specified rate, while debouncing delays execution until a quiet period has elapsed.
  • Constructor Details

    • Throttle

      public Throttle()
  • Method Details

    • setDebug

      public static void setDebug(boolean debug)
      Enables or disables debug logging.
      Parameters:
      debug - True to enable logging, false to disable
    • throttle

      public static <T> T throttle(String key, Supplier<T> supplier, long delayMs)
      Throttles a function that returns a value. Executes the function only if the specified delay has passed since last execution.
      Type Parameters:
      T - The return type of the function
      Parameters:
      key - Unique identifier for this throttled operation
      supplier - The function to throttle
      delayMs - Minimum time between executions in milliseconds
      Returns:
      The result of executing the function, or the cached result if throttled
    • throttle

      public static void throttle(String key, Runnable runnable, long delayMs)
      Throttles a function with no return value. Executes the function only if the specified delay has passed since last execution.
      Parameters:
      key - Unique identifier for this throttled operation
      runnable - The function to throttle
      delayMs - Minimum time between executions in milliseconds
    • debounce

      public static void debounce(String key, Runnable runnable, long delayMs)
      Debounces a function execution. Delays execution until a quiet period has elapsed, canceling pending executions. Function is executed on the JavaFX application thread.
      Parameters:
      key - Unique identifier for this debounced operation
      runnable - The function to debounce
      delayMs - Delay in milliseconds before execution
    • clear

      public static void clear(String key)
      Clears all tracking data for a specific key. Cancels any pending debounced tasks for this key.
      Parameters:
      key - The key to clear data for
    • clearAll

      public static void clearAll()
      Clears all throttling and debouncing data. Cancels all pending debounced tasks.
    • shutdown

      public static void shutdown()
      Shuts down the throttling/debouncing system. Clears all data and shuts down the executor service.