Package com.oorian.html
Class SimpleTimer
java.lang.Object
com.oorian.html.SimpleTimer
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
ConstructorsConstructorDescriptionCreates 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.SimpleTimer(String id) 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 TypeMethodDescriptionvoidaddListener(TimerListener listener) Adds a listener to be notified when the timer expires.longgetDelay()Returns the initial delay before the timer first fires.getId()Returns the identifier for this timer.longReturns the interval between periodic executions.booleanChecks if this timer is configured for periodic execution.protected voidonExpire()Template method called when the timer expires.voidstart()Starts the timer with the configured delay and interval.voidstop()Stops the timer and cancels any pending executions.
-
Constructor Details
-
SimpleTimer
public SimpleTimer()Creates a new SimpleTimer with no configuration. -
SimpleTimer
Creates a new SimpleTimer with the specified identifier.- Parameters:
id- The timer identifier.
-
SimpleTimer
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
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
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:
trueif the timer repeats at intervals,falsefor 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
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.
-