Class SimpleTimer

java.lang.Object
com.oorian.html.SimpleTimer

public class SimpleTimer extends Object
A simplified timer implementation for scheduling delayed or periodic tasks.

SimpleTimer provides a lightweight wrapper around Java's Timer class, offering both one-time delayed execution and periodic execution with an optional identifier. It supports a listener pattern for notifying registered observers when the timer expires.

Features:

  • One-time delayed execution or periodic interval execution
  • Optional timer identification for managing multiple timers
  • Listener pattern with weak references to prevent memory leaks
  • Template method pattern with protected onExpire() for subclass customization
  • Simple start/stop control

Usage:


 // One-time timer
 SimpleTimer timer = new SimpleTimer("myTimer", 5000);
 timer.addListener(id -> System.out.println("Timer expired: " + id));
 timer.start();

 // Periodic timer
 SimpleTimer periodic = new SimpleTimer("refresh", 1000, 5000);
 periodic.addListener(id -> refreshData());
 periodic.start();
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new SimpleTimer with no configuration.
    SimpleTimer(long delay)
    Creates a new one-shot SimpleTimer with the specified delay.
    SimpleTimer(long delay, long interval)
    Creates a new periodic SimpleTimer with the specified delay and interval.
    Creates a new SimpleTimer with the specified identifier.
    SimpleTimer(String id, long delay)
    Creates a new one-shot SimpleTimer with the specified identifier and delay.
    SimpleTimer(String id, long delay, long interval)
    Creates a new periodic SimpleTimer with the specified identifier, delay, and interval.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a listener to be notified when the timer expires.
    long
    Returns the initial delay before the timer first fires.
    Returns the identifier for this timer.
    long
    Returns the interval between periodic executions.
    boolean
    Checks if this timer is configured for periodic execution.
    protected void
    Template method called when the timer expires.
    void
    Starts the timer with the configured delay and interval.
    void
    Stops the timer and cancels any pending executions.

    Methods inherited from class java.lang.Object

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

    • SimpleTimer

      public SimpleTimer()
      Creates a new SimpleTimer with no configuration.
    • SimpleTimer

      public SimpleTimer(String id)
      Creates a new SimpleTimer with the specified identifier.
      Parameters:
      id - The timer identifier.
    • SimpleTimer

      public SimpleTimer(String id, long delay)
      Creates a new one-shot SimpleTimer with the specified identifier and delay.
      Parameters:
      id - The timer identifier.
      delay - The delay in milliseconds before the timer fires.
    • SimpleTimer

      public SimpleTimer(long delay)
      Creates a new one-shot SimpleTimer with the specified delay.
      Parameters:
      delay - The delay in milliseconds before the timer fires.
    • SimpleTimer

      public SimpleTimer(long delay, long interval)
      Creates a new periodic SimpleTimer with the specified delay and interval.
      Parameters:
      delay - The initial delay in milliseconds before the first execution.
      interval - The interval in milliseconds between subsequent executions.
    • SimpleTimer

      public SimpleTimer(String id, long delay, long interval)
      Creates a new periodic SimpleTimer with the specified identifier, delay, and interval.
      Parameters:
      id - The timer identifier.
      delay - The initial delay in milliseconds before the first execution.
      interval - The interval in milliseconds between subsequent executions.
  • Method Details

    • getId

      public String getId()
      Returns the identifier for this timer.
      Returns:
      The timer ID, or null if no ID was set.
    • isPeriodic

      public boolean isPeriodic()
      Checks if this timer is configured for periodic execution.
      Returns:
      true if the timer repeats at intervals, false for one-time execution.
    • getDelay

      public long getDelay()
      Returns the initial delay before the timer first fires.
      Returns:
      The delay in milliseconds.
    • getInterval

      public long getInterval()
      Returns the interval between periodic executions.
      Returns:
      The interval in milliseconds, or 0 for non-periodic timers.
    • start

      public void start()
      Starts the timer with the configured delay and interval.

      For periodic timers, executes repeatedly at the specified interval. For non-periodic timers, executes once after the delay.

    • stop

      public void stop()
      Stops the timer and cancels any pending executions.

      After calling this method, the timer cannot be restarted.

    • addListener

      public void addListener(TimerListener listener)
      Adds a listener to be notified when the timer expires.

      Listeners are stored as weak references to prevent memory leaks.

      Parameters:
      listener - The TimerListener to be notified on expiration.
    • onExpire

      protected void onExpire()
      Template method called when the timer expires.

      Subclasses can override this method to provide custom behavior. This method is called before notifying listeners.