public final class StopWatch extends Object
StopWatch provides a convenient API for timings.
To start the watch, call start(). 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.
split(), suspend(), or stop() cannot be invoked twiceunsplit() may only be called if the watch has been split()resume() may only be called if the watch has been suspend()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 static String |
DATE_FORMAT
Default format for string representation.
|
private static long |
NANO_2_MILLIS
Factor to convert from nanoseconds to milliseconds.
|
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 |
startTime
The start time.
|
private long |
startTimeMillis
The start time in Millis - nanoTime is only for elapsed time so we need to also store the currentTimeMillis to
maintain the old getStartTime API.
|
private long |
stopTime
The stop time.
|
| Constructor and Description |
|---|
StopWatch() |
| Modifier and Type | Method and Description |
|---|---|
long |
getNanoTime()
Get the time on the stopwatch in nanoseconds.
|
long |
getSplitNanoTime()
Get the split time on the stopwatch in nanoseconds.
|
long |
getSplitTime()
Get the split time on the stopwatch.
|
long |
getStartTime()
Returns the time this stopwatch was started.
|
long |
getTime()
Get the time on the stopwatch.
|
boolean |
isStarted()
The method is used to find out if the StopWatch is started.
|
boolean |
isStopped()
This method is used to find out whether the StopWatch is stopped.
|
boolean |
isSuspended()
This method is used to find out whether the StopWatch is suspended.
|
void |
reset()
Resets the stopwatch.
|
void |
resume()
Resume the stopwatch after a suspend.
|
void |
split()
Split the time.
|
void |
start()
Start the stopwatch.
|
void |
stop()
Stop the stopwatch.
|
void |
suspend()
Suspend the stopwatch for later resumption.
|
String |
toSplitString()
Gets a summary of the split time that the stopwatch recorded as a string.
|
String |
toString()
Gets a summary of the time that the stopwatch recorded as a string.
|
void |
unsplit()
Remove a split.
|
private static final String DATE_FORMAT
private static final long NANO_2_MILLIS
private StopWatch.State runningState
private StopWatch.SplitState splitState
private long startTime
private long startTimeMillis
private long stopTime
public void start()
This method starts a new timing session, clearing any previous values.
CHECKSTYLE:OFFIllegalStateException - if the StopWatch is already running. CHECKSTYLE:ONpublic void stop()
This method ends a new timing session, allowing the time to be retrieved.
Throws IllegalStateException if the StopWatch is not running.
public void reset()
This method clears the internal values to allow the object to be reused.
public void split()
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.
Throws {@link IllegalStateException if the StopWatch is not running.
public void unsplit()
This method clears the stop time. The start time is unaffected, enabling timing from the original start point to continue.
Throws {@link IllegalStateException if the StopWatch has not been split.
public void suspend()
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.
Throws {@link IllegalStateException if the StopWatch is not currently running.
public void resume()
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.
Throws {@link IllegalStateException if the StopWatch has not been suspended.
public long getTime()
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 getNanoTime()
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 getSplitTime()
This is the time between start and latest split.
Throws {@link IllegalStateException if the StopWatch has not yet been split.
public long getSplitNanoTime()
This is the time between start and latest split.
Throws {@link IllegalStateException if the StopWatch has not yet been split.
public long getStartTime()
Throws {@link IllegalStateException if this StopWatch has not been started.
public String toString()
The format used is ISO8601-like, hours:minutes:seconds.milliseconds.
public String toSplitString()
The format used is ISO8601-like, hours:minutes:seconds.milliseconds.
public boolean isStarted()
public boolean isSuspended()
public boolean isStopped()
This method is used to find out whether the StopWatch is stopped. The stopwatch which's not yet started and explicitly stopped stopwatch is considered as stopped.
Copyright © 2012 Sven Strittmatter. All Rights Reserved.