Class SimpleActiveTimer
- java.lang.Object
-
- com.pervasivecode.utils.concurrent.timing.SimpleActiveTimer
-
- All Implemented Interfaces:
ListenableTimer,QueryableTimer,StoppableTimer
public final class SimpleActiveTimer extends Object implements StoppableTimer, ListenableTimer, QueryableTimer
A timer implementation that isstoppable,queryable, andlistenable.This is a "one-shot" timer: it can only be started once, and once it's stopped, it can't be started again.
Instances of SimpleActiveTimer are not safe for use by multiple threads.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.pervasivecode.utils.concurrent.timing.ListenableTimer
ListenableTimer.StateChangeListener
-
-
Constructor Summary
Constructors Constructor Description SimpleActiveTimer(CurrentNanosSource nanoSource)Create an instance that is not started yet.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddTimerStartedListener(ListenableTimer.StateChangeListener timerStartedListener)Add a StateChangeListener to the list of listeners that will be invoked when the timer is started.voidaddTimerStoppedListener(ListenableTimer.StateChangeListener timerStoppedListener)Add a StateChangeListener to the list of listeners that will be invoked when the timer is stopped.static SimpleActiveTimercreateAndStart(CurrentNanosSource nanoSource)Create an instance and start it.Durationelapsed()What is the elapsed time of this timer?booleanequals(Object other)booleanhasBeenStarted()Has the timer been started?inthashCode()booleanisRunning()Is the timer currently running?booleanisStopped()Has the timer been stopped?voidstartTimer()Start the timer.DurationstopTimer()Stop the timer, and obtain the elapsed time since it was started.
-
-
-
Constructor Detail
-
SimpleActiveTimer
public SimpleActiveTimer(CurrentNanosSource nanoSource)
Create an instance that is not started yet.- Parameters:
nanoSource- A source of the current time, represented in nanoseconds.
-
-
Method Detail
-
createAndStart
public static SimpleActiveTimer createAndStart(CurrentNanosSource nanoSource)
Create an instance and start it.- Parameters:
nanoSource- A source of the current time, represented in nanoseconds.- Returns:
- A SimpleActiveTimer instance that has been started.
-
addTimerStartedListener
public void addTimerStartedListener(ListenableTimer.StateChangeListener timerStartedListener)
Description copied from interface:ListenableTimerAdd a StateChangeListener to the list of listeners that will be invoked when the timer is started. If the timer has already been started, calling this method will result in the listener being invoked immediately.- Specified by:
addTimerStartedListenerin interfaceListenableTimer- Parameters:
timerStartedListener- The StateChangeListener to add.
-
addTimerStoppedListener
public void addTimerStoppedListener(ListenableTimer.StateChangeListener timerStoppedListener)
Description copied from interface:ListenableTimerAdd a StateChangeListener to the list of listeners that will be invoked when the timer is stopped. If the timer has already been stopped, calling this method will result in the listener being invoked immediately.- Specified by:
addTimerStoppedListenerin interfaceListenableTimer- Parameters:
timerStoppedListener- The StateChangeListener to add.
-
hasBeenStarted
public boolean hasBeenStarted()
Description copied from interface:QueryableTimerHas the timer been started?- Specified by:
hasBeenStartedin interfaceQueryableTimer- Returns:
- True if the timer has been started.
-
isRunning
public boolean isRunning()
Description copied from interface:QueryableTimerIs the timer currently running?- Specified by:
isRunningin interfaceQueryableTimer- Returns:
- True if the timer is currently running.
-
isStopped
public boolean isStopped()
Description copied from interface:QueryableTimerHas the timer been stopped?- Specified by:
isStoppedin interfaceQueryableTimer- Returns:
- True if the timer has been stopped.
-
startTimer
public void startTimer()
Start the timer.- Throws:
IllegalStateException- if the timer has already been started.
-
elapsed
public Duration elapsed()
Description copied from interface:QueryableTimerWhat is the elapsed time of this timer?If the timer is running, this is the elapsed time since it was started, until the current time. If the timer has been stopped, this is the elapsed time from the moment it was started until the moment it was stopped. (When the timer has been stopped, this value does not change.)
- Specified by:
elapsedin interfaceQueryableTimer- Returns:
- The amount of time that has elapsed since the timer was started.
-
stopTimer
public Duration stopTimer()
Description copied from interface:StoppableTimerStop the timer, and obtain the elapsed time since it was started.- Specified by:
stopTimerin interfaceStoppableTimer- Returns:
- The elapsed time since the timer was started.
-
-