Class IntervalChecker

java.lang.Object
de.arstwo.twotil.IntervalChecker

public class IntervalChecker extends Object
A utility class designed for managing and checking time intervals in a thread-safe manner.

This class simplifies the boilerplate code necessary to handle interval checking.

Usage example:


   IntervalChecker everyMinute = IntervalChecker.every(1, ChronoUnit.MINUTES);
   if (everyMinute.updateIfDue()) {
     // Do the task...
   }
   // alternatively:
   everyMinute.executeIfDue(myTask::run);
 
 

Timer checks are performed atomically to ensure that if multiple threads attempt to reset the timer, only one will succeed and trigger due task execution.

  • Method Details

    • every

      public static IntervalChecker every(long amount, TemporalUnit unit)
      Creates a new IntervalChecker with the specified time interval. The returned timer is marked as "due".
      Parameters:
      amount - The amount of the specified unit.
      unit - The time unit, usually ChronoUnit.
      Returns:
      An IntervalChecker set to the specified time interval.
      See Also:
    • every

      public static IntervalChecker every(Duration duration)
      Creates a new IntervalChecker with the specified time interval. The returned timer is marked as "due".
      Parameters:
      duration - interval between actions.
      Returns:
      An IntervalChecker set to the specified time interval.
      See Also:
    • update

      public boolean update()
      Checks the interval and atomically resets the timer if the time has expired. Will not execute any set tasks.
      Returns:
      true if the time had expired, otherwise false.
    • executeTasks

      public boolean executeTasks(Runnable onDue, Runnable onNotDue)
      If the timer is due it is reset and ifDue is executed, otherwise ifNotDue is executed without a timer reset.
      Parameters:
      onDue - task to execute if due, or null.
      onNotDue - task to execute if not due, or null.
      Returns:
      true if the timer was due, otherwise false.
    • execute

      public boolean execute()
      Checks (and possibly resets) the timer, then performs the previously set tasks (if any) accordingly.
      Returns:
      true if the interval had expired, otherwise false.
    • whenDue

      public IntervalChecker whenDue(Runnable task)
      Specifies a fixed task to run on execute.. checks if the timer is due.
      Parameters:
      task - any runnable.
      Returns:
      this, for convenience.
    • whenNotDue

      public IntervalChecker whenNotDue(Runnable task)
      Specifies a fixed task to run on execute... checks if the timer is not due.
      Parameters:
      task - any runnable.
      Returns:
      this, for convenience.
    • executeIfDue

      public boolean executeIfDue()
      Executes the previously set task if the interval has expired. In that case, also resets the timer. Will not run any not-due task.
      Returns:
      true if the interval had expired, otherwise false.
    • executeTaskIfDue

      public boolean executeTaskIfDue(Runnable task)
      Executes the specified operation if the interval has expired. In that case, also resets the timer. Will not run any not-due task.
      Parameters:
      task - The task to be performed.
      Returns:
      true if the interval had expired, otherwise false.
    • executeIfNotDue

      public boolean executeIfNotDue()
      Executes the previously set task if the interval has not expired. Will not run any due task.
      Returns:
      true if the interval had expired, otherwise false.
    • executeTaskIfNotDue

      public boolean executeTaskIfNotDue(Runnable task)
      Checks the timer and executes the given task if not expired, otherwise execute the due task (if set) and resets the timer. Will not run any due task.
      Parameters:
      task - The task to be performed if not due.
      Returns:
      true if the interval had expired, otherwise false.
    • isDue

      public boolean isDue()
      Checks whether or not this timer is due right now without excecuting any tasks.
      Returns:
      true if it is due right now, otherwise false.
    • restartTimer

      public IntervalChecker restartTimer()
      Forces a new time interval without calling any tasks.
      Returns:
      this, for convenience.
    • forceDue

      public IntervalChecker forceDue()
      Forces the timer to indicate that the interval time has expired at the next check.
      Returns:
      this, for convenience.