public class StopWatch
extends java.lang.Object
StopWatch
provides a convenient API for timings.
To start the watch, call start()
or createStarted()
. At this point you can:
split()
the watch to get the time whilst the watch continues in the background. unsplit()
will
remove the effect of the split. At this point, these three options are available again.suspend()
the watch to pause it. resume()
allows the watch to continue. Any time between the
suspend and resume will not be counted in the total. At this point, these three options are available again.stop()
the watch to complete the timing session.
It is intended that the output methods toString()
and getTime()
should only be called after stop,
split or suspend, however a suitable result will be returned at other points.
NOTE: As from v2.1, the methods protect against inappropriate calls. Thus you cannot now call stop before start, resume before suspend or unsplit before split.
1. split(), suspend(), or stop() cannot be invoked twice
2. unsplit() may only be called if the watch has been split()
3. resume() may only be called if the watch has been suspend()
4. start() cannot be called twice without calling reset()
This class is not thread-safe
Modifier and Type | Class and Description |
---|---|
private static class |
StopWatch.SplitState
Enumeration type which indicates the split status of stopwatch.
|
private static class |
StopWatch.State
Enumeration type which indicates the status of stopwatch.
|
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
message
A message for string presentation.
|
private static long |
NANO_2_MILLIS |
private StopWatch.State |
runningState
The current running state of the StopWatch.
|
private StopWatch.SplitState |
splitState
Whether the stopwatch has a split time recorded.
|
private long |
startTimeMillis
The start time in milliseconds - nanoTime is only for elapsed time so we
need to also store the currentTimeMillis to maintain the old
getStartTime API.
|
private long |
startTimeNanos
The start time in nanoseconds.
|
private long |
stopTimeMillis
The end time in milliseconds - nanoTime is only for elapsed time so we
need to also store the currentTimeMillis to maintain the old
getStartTime API.
|
private long |
stopTimeNanos
The stop time in nanoseconds.
|
Constructor and Description |
---|
StopWatch()
Constructor.
|
StopWatch(java.lang.String message)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
static StopWatch |
create()
Creates a stopwatch for convenience.
|
static StopWatch |
createStarted()
Creates a started stopwatch for convenience.
|
java.lang.String |
formatSplitTime()
Returns the time formatted by
DurationFormatUtils.formatDurationHMS(long) . |
java.lang.String |
formatTime()
Returns the split time formatted by
DurationFormatUtils.formatDurationHMS(long) . |
java.lang.String |
getMessage()
Gets the message for string presentation.
|
long |
getNanoTime()
Gets the elapsed time in nanoseconds.
|
long |
getSplitNanoTime()
Gets the split time in nanoseconds.
|
long |
getSplitTime()
Gets the split time on the stopwatch.
|
long |
getStartTime()
Gets the time this stopwatch was started in milliseconds, between the current time and midnight, January 1, 1970
UTC.
|
long |
getStopTime()
Gets the time this stopwatch was stopped in milliseconds, between the current time and midnight, January 1, 1970
UTC.
|
long |
getTime()
Gets the time on the stopwatch.
|
long |
getTime(java.util.concurrent.TimeUnit timeUnit)
Gets the time in the specified TimeUnit.
|
boolean |
isStarted()
Returns whether the StopWatch is started.
|
boolean |
isStopped()
Returns whether StopWatch is stopped.
|
boolean |
isSuspended()
Returns whether the StopWatch is suspended.
|
void |
reset()
Resets the stopwatch.
|
void |
resume()
Resumes the stopwatch after a suspend.
|
void |
split()
Splits the time.
|
void |
start()
Starts the stopwatch.
|
void |
stop()
Stops the stopwatch.
|
void |
suspend()
Suspends the stopwatch for later resumption.
|
java.lang.String |
toSplitString()
Gets a summary of the split time that the stopwatch recorded as a string.
|
java.lang.String |
toString()
Gets a summary of the time that the stopwatch recorded as a string.
|
void |
unsplit()
Removes a split.
|
private static final long NANO_2_MILLIS
private final java.lang.String message
private StopWatch.State runningState
private StopWatch.SplitState splitState
private long startTimeNanos
private long startTimeMillis
private long stopTimeMillis
private long stopTimeNanos
public StopWatch()
Constructor.
public StopWatch(java.lang.String message)
Constructor.
message
- A message for string presentation.public static StopWatch create()
public static StopWatch createStarted()
public java.lang.String formatSplitTime()
DurationFormatUtils.formatDurationHMS(long)
.DurationFormatUtils.formatDurationHMS(long)
.public java.lang.String formatTime()
DurationFormatUtils.formatDurationHMS(long)
.DurationFormatUtils.formatDurationHMS(long)
.public java.lang.String getMessage()
public long getNanoTime()
Gets the elapsed time in nanoseconds.
This is either the time between the start and the moment this method is called, or the amount of time between start and stop.
System.nanoTime()
public long getSplitNanoTime()
Gets the split time in nanoseconds.
This is the time between start and latest split.
java.lang.IllegalStateException
- if the StopWatch has not yet been split.public long getSplitTime()
Gets the split time on the stopwatch.
This is the time between start and latest split.
java.lang.IllegalStateException
- if the StopWatch has not yet been split.public long getStartTime()
java.lang.IllegalStateException
- if this StopWatch has not been startedpublic long getStopTime()
java.lang.IllegalStateException
- if this StopWatch has not been startedpublic long getTime()
Gets the time on the stopwatch.
This is either the time between the start and the moment this method is called, or the amount of time between start and stop.
public long getTime(java.util.concurrent.TimeUnit timeUnit)
Gets the time in the specified TimeUnit.
This is either the time between the start and the moment this method is called, or the amount of time between
start and stop. The resulting time will be expressed in the desired TimeUnit with any remainder rounded down.
For example, if the specified unit is TimeUnit.HOURS
and the stopwatch time is 59 minutes, then the
result returned will be 0
.
timeUnit
- the unit of time, not nullpublic boolean isStarted()
Returns whether the StopWatch is started. A suspended StopWatch is also started watch.
public boolean isStopped()
Returns whether StopWatch is stopped. The stopwatch which's not yet started and explicitly stopped stopwatch is considered as stopped.
public boolean isSuspended()
Returns whether the StopWatch is suspended.
public void reset()
Resets the stopwatch. Stops it if need be.
This method clears the internal values to allow the object to be reused.
public void resume()
Resumes the stopwatch after a suspend.
This method resumes the watch after it was suspended. The watch will not include time between the suspend and resume calls in the total time.
java.lang.IllegalStateException
- if the StopWatch has not been suspended.public void split()
Splits the time.
This method sets the stop time of the watch to allow a time to be extracted. The start time is unaffected,
enabling unsplit()
to continue the timing from the original start point.
java.lang.IllegalStateException
- if the StopWatch is not running.public void start()
Starts the stopwatch.
This method starts a new timing session, clearing any previous values.
java.lang.IllegalStateException
- if the StopWatch is already running.public void stop()
Stops the stopwatch.
This method ends a new timing session, allowing the time to be retrieved.
java.lang.IllegalStateException
- if the StopWatch is not running.public void suspend()
Suspends the stopwatch for later resumption.
This method suspends the watch until it is resumed. The watch will not include time between the suspend and resume calls in the total time.
java.lang.IllegalStateException
- if the StopWatch is not currently running.public java.lang.String toSplitString()
Gets a summary of the split time that the stopwatch recorded as a string.
The format used is ISO 8601-like, [message ]hours:minutes:seconds.milliseconds.
"message "
if the message is set.public java.lang.String toString()
Gets a summary of the time that the stopwatch recorded as a string.
The format used is ISO 8601-like, [message ]hours:minutes:seconds.milliseconds.
toString
in class java.lang.Object
"message "
if the message is set.public void unsplit()
Removes a split.
This method clears the stop time. The start time is unaffected, enabling timing from the original start point to continue.
java.lang.IllegalStateException
- if the StopWatch has not been split.