Package com.helger.commons.timing
Class StopWatch
- java.lang.Object
-
- com.helger.commons.timing.StopWatch
-
- All Implemented Interfaces:
IStoppable,Serializable
@NotThreadSafe public class StopWatch extends Object implements IStoppable, Serializable
Simple stop watch based onSystem.nanoTime().- Author:
- Philip Helger
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedStopWatch(boolean bStart)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static StopWatchcreatedStarted()static StopWatchcreatedStopped()protected longgetCurrentNanoTime()DurationgetDuration()longgetMillis()longgetNanos()longgetSeconds()booleanisStarted()booleanisStopped()EChangereset()Reset all saved durations, in case this stop watch is to be used in a loop.voidrestart()Stops, resets and starts the stop watch.static TimeValuerunMeasured(Runnable aRunnable)Run the passed runnable and measure the time.EChangestart()Start the stop watch.EChangestop()Stop the stop watch.DurationstopAndGetDuration()Stop the stop watch and get the duration since the start.longstopAndGetMillis()Stop the stop watch and get the elapsed milliseconds since the start.longstopAndGetNanos()Stop the stop watch and get the elapsed nanoseconds since the start.longstopAndGetSeconds()Stop the stop watch and get the elapsed seconds since the start.StringtoString()
-
-
-
Method Detail
-
reset
@Nonnull public EChange reset()
Reset all saved durations, in case this stop watch is to be used in a loop. Does not change the start/stop state.- Returns:
EChange.
-
getCurrentNanoTime
protected final long getCurrentNanoTime()
- Returns:
- The current time in nano seconds.
-
stop
@Nonnull public EChange stop()
Stop the stop watch.- Specified by:
stopin interfaceIStoppable- Returns:
EChange.CHANGEDif the stop watch was previously running and is now stopped, andEChange.UNCHANGEDif the stop watch was already stopped.
-
restart
public void restart()
Stops, resets and starts the stop watch.
-
isStarted
public boolean isStarted()
- Returns:
trueif the stop watch is currently started (running),falseotherwise.
-
isStopped
public boolean isStopped()
- Returns:
trueif the stop watch is currently stopped (not running),falseotherwise.
-
getNanos
@Nonnegative public long getNanos()
- Returns:
- The elapsed nano seconds (1000 nano seconds = 1 milli second).
-
getMillis
@Nonnegative public long getMillis()
- Returns:
- The elapsed milli seconds.
-
getSeconds
@Nonnegative public long getSeconds()
- Returns:
- The elapsed seconds.
-
stopAndGetNanos
@Nonnegative public long stopAndGetNanos()
Stop the stop watch and get the elapsed nanoseconds since the start. If the stop watch was started and stopped multiple times, the duration is added.- Returns:
- The elapsed nano seconds or 0 if the stop watch was never started.
-
stopAndGetMillis
@Nonnegative public long stopAndGetMillis()
Stop the stop watch and get the elapsed milliseconds since the start. If the stop watch was started and stopped multiple times, the duration is added.- Returns:
- The elapsed milli seconds or 0 if the stop watch was never started.
-
stopAndGetSeconds
@Nonnegative public long stopAndGetSeconds()
Stop the stop watch and get the elapsed seconds since the start. If the stop watch was started and stopped multiple times, the duration is added.- Returns:
- The elapsed seconds or 0 if the stop watch was never started.
-
stopAndGetDuration
@Nonnull public Duration stopAndGetDuration()
Stop the stop watch and get the duration since the start. If the stop watch was started and stopped multiple times, the duration is added.- Returns:
- The elapsed duration or 0 if the stop watch was never started.
-
createdStarted
@Nonnull @ReturnsMutableCopy public static StopWatch createdStarted()
- Returns:
- A new
StopWatchobject that is started. Nevernull.
-
createdStopped
@Nonnull @ReturnsMutableCopy public static StopWatch createdStopped()
- Returns:
- A new
StopWatchobject that is NOT started. Nevernull.
-
-