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 void
addTimerStartedListener(ListenableTimer.StateChangeListener timerStartedListener)
Add a StateChangeListener to the list of listeners that will be invoked when the timer is started.void
addTimerStoppedListener(ListenableTimer.StateChangeListener timerStoppedListener)
Add a StateChangeListener to the list of listeners that will be invoked when the timer is stopped.static SimpleActiveTimer
createAndStart(CurrentNanosSource nanoSource)
Create an instance and start it.Duration
elapsed()
What is the elapsed time of this timer?boolean
equals(Object other)
boolean
hasBeenStarted()
Has the timer been started?int
hashCode()
boolean
isRunning()
Is the timer currently running?boolean
isStopped()
Has the timer been stopped?void
startTimer()
Start the timer.Duration
stopTimer()
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:ListenableTimer
Add 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:
addTimerStartedListener
in interfaceListenableTimer
- Parameters:
timerStartedListener
- The StateChangeListener to add.
-
addTimerStoppedListener
public void addTimerStoppedListener(ListenableTimer.StateChangeListener timerStoppedListener)
Description copied from interface:ListenableTimer
Add 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:
addTimerStoppedListener
in interfaceListenableTimer
- Parameters:
timerStoppedListener
- The StateChangeListener to add.
-
hasBeenStarted
public boolean hasBeenStarted()
Description copied from interface:QueryableTimer
Has the timer been started?- Specified by:
hasBeenStarted
in interfaceQueryableTimer
- Returns:
- True if the timer has been started.
-
isRunning
public boolean isRunning()
Description copied from interface:QueryableTimer
Is the timer currently running?- Specified by:
isRunning
in interfaceQueryableTimer
- Returns:
- True if the timer is currently running.
-
isStopped
public boolean isStopped()
Description copied from interface:QueryableTimer
Has the timer been stopped?- Specified by:
isStopped
in 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:QueryableTimer
What 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:
elapsed
in interfaceQueryableTimer
- Returns:
- The amount of time that has elapsed since the timer was started.
-
stopTimer
public Duration stopTimer()
Description copied from interface:StoppableTimer
Stop the timer, and obtain the elapsed time since it was started.- Specified by:
stopTimer
in interfaceStoppableTimer
- Returns:
- The elapsed time since the timer was started.
-
-