Class DebounceTimer

java.lang.Object
org.cytoscape.event.DebounceTimer

public class DebounceTimer extends Object
A timer that can be used to ensure that time-consuming tasks, usually triggered by events, do not run too often. It can be used by code that fires events to avoid firing too many events, or by code listening for events to avoid running the handler too often.
Since:
3.8

Cytoscape Backwards Compatibility (API Class): We expect that this class will be used but not inherited by developers using this class. As such, we reserve the right to add methods to the class as part of minor version upgrades. We will not remove methods for any changes other than major version upgrades.

Module: event-api

To use this in your app, include the following dependency in your POM:

<dependency>
    <groupId>org.cytoscape</groupId>
    <artifactId>event-api</artifactId>
</dependency>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new DebounceTimer with the default delay.
    DebounceTimer(int delayMilliseconds)
    Creates a new DebounceTimer with the given delay (in milliseconds).
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    debounce(Object key, Runnable runnable)
    Starts a timer that will run the runnable after a short delay (on a separate thread).
    void
    debounce(Runnable runnable)
    Starts a timer that will run the runnable after a short delay (on a separate thread).
    boolean
     
    void
    Initiates a shutdown that will release resources, calls to debounce() will no longer be accepted.

    Methods inherited from class java.lang.Object

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

    • DEFAULT_DELAY_MILLISECONDS

      public static final int DEFAULT_DELAY_MILLISECONDS
      See Also:
  • Constructor Details

    • DebounceTimer

      public DebounceTimer()
      Creates a new DebounceTimer with the default delay. When this DebounceTimer is no longer needed the shutdown() method should be called to release resources.
    • DebounceTimer

      public DebounceTimer(int delayMilliseconds)
      Creates a new DebounceTimer with the given delay (in milliseconds). When this DebounceTimer is no longer needed the shutdown() method should be called to release resources.
  • Method Details

    • debounce

      public void debounce(Runnable runnable)
      Starts a timer that will run the runnable after a short delay (on a separate thread). If another call to this method occurs before the timer expires then the timer will be reset. When there are multiple quick calls to this method and the time between each call is less than the delay then the runnable will only run when the delay is reached. This method is non-blocking.

      Code example:
       public void handleEvent(RowsSetEvent e) {
           if(e.containsColumn(CyNetwork.SELECTED)) {
               debounceTimer.debounce(() -> updateUI());
           }
       }
       
    • debounce

      public void debounce(Object key, Runnable runnable)
      Starts a timer that will run the runnable after a short delay (on a separate thread). If another call to this method (with the same key) occurs before the timer expires then the timer will be reset. When there are multiple quick calls to this method and the time between each call is less than the delay then the runnable will only run when the delay is reached. This method is non-blocking.

      Code example:
       public void handleEvent(RowsSetEvent e) {
           if(e.containsColumn(CyNetwork.SELECTED)) {
               CyNetworkView networkView = applicationManager.getCurrentNetworkView();
               if(networkView != null) {
                   debounceTimer.debounce(networkView, () -> updateUI(networkView));
               }
           }
       }
       
      Parameters:
      key - A key object to associate with the runnable, each key object will get its own timer.
      Throws:
      RejectedExecutionException - if this timer has been shutdown
      NullPointerException - if key or runnable are null
    • shutdown

      public void shutdown()
      Initiates a shutdown that will release resources, calls to debounce() will no longer be accepted.
    • isShutdown

      public boolean isShutdown()