public final class Chronometer
extends java.lang.Object
This class implements a virtual digital stopwatch (or chronometer),
providing basic and useful functionalities for time measurement,
such as start()
, stop()
, multiple laps
and total elapsed running time
.
Modifier and Type | Class and Description |
---|---|
static class |
Chronometer.Precision
Defines the method to be utilized by a Chronometer instance in order to
obtain current time.
|
Constructor and Description |
---|
Chronometer()
Default constructor.
|
Chronometer(Chronometer.Precision precision)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
int |
countLaps()
The total count of laps this chronometer is currently keeping track of.
|
java.lang.String |
dumpTimes()
This method will dump the entire content of the chronometer to a string.
|
static java.lang.String |
format(long time)
Converts a time interval to a human-readable text up to the millisecond.
|
static java.lang.String |
format(long time,
Chronometer.Precision precision)
Converts a time interval to a human-readable text up to the specificed precision.
|
long |
getCurrentLap()
Gets the running time elapsed for the current lap,
which excludes any time elapsed while the chronometer was stopped.
|
long |
getElapsedTime()
Total elapsed time during which the chronometer is or was running.
|
long |
getLap(java.lang.String label)
Gets the running time elapsed for the given labeled lap.
|
boolean |
isRunning() |
void |
newLap(java.lang.String label)
Starts of a new labeled lap and also stops the previous one, if any.
|
void |
reset()
Completely resets the chronometer, putting it in stop state.
|
void |
start()
Starts the chronometer.
|
long |
stop()
Completely "freezes" the chronometer, including any current lap.
|
java.lang.String |
toString()
Converts the current elapsed time to a human-readable text up to the chronometer's precision.
|
java.lang.String |
toString(java.lang.String label)
Converts the current elapsed time for a given lap to a human-readable
text up to the chronometer's precision.
|
public Chronometer()
Chronometer(Precision.Milliseconds
.public Chronometer(Chronometer.Precision precision)
precision
- The chronometer's precisionpublic long stop()
Completely "freezes" the chronometer, including any current lap.
Calling start()
after a stop means that the chronometer
will restart exactly from the last stop. In other words, the time
elapsed between these two sequential calls: stop() -> start()
won't count.
getElapsedTime()
public void reset()
public void start()
Starts the chronometer.
Calling start after a stop()
means that the chronometer
will restart exactly from the last stop. In other words, the time
elapsed between these two sequential calls: stop()
-> start()
won't count.
If the chronometer is already running, an IllegalStateException will be thrown.
public boolean isRunning()
public long getElapsedTime()
public int countLaps()
public void newLap(java.lang.String label)
Starts of a new labeled lap and also stops the previous one, if any.
The chronometer has to be running, othersize an IllegalStateException will be thrown.
label
- Non-null label for the new lappublic long getLap(java.lang.String label)
label
- Label of the lap to retrievepublic long getCurrentLap()
public static java.lang.String format(long time)
time
- Elapsed timepublic static java.lang.String format(long time, Chronometer.Precision precision)
time
- Elapsed timejava.lang.NullPointerException
- If given precision is nullpublic java.lang.String dumpTimes()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(java.lang.String label)
label
- The label associated with the desired lap