org.apache.commons.lang.time

Class StopWatch


public class StopWatch
extends java.lang.Object

StopWatch provides a convenient API for timings.

The methods do not protect against inappropriate calls. Thus you can call stop before start, resume before suspend or unsplit before split. The results are indeterminate in these cases.

To start the watch, call start(). At this point you can:

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.

Version:
$Id: StopWatch.java,v 1.6 2003/08/18 02:22:25 bayard Exp $
Authors:
Henri Yandell
Stephen Colebourne
Since:
2.0

Constructor Summary

StopWatch()
Constructor.

Method Summary

long
getTime()
Get the time on the stopwatch.
void
reset()
Reset 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
toString()
Gets a summary of the time that the stopwatch recorded as a string.
void
unsplit()
Remove a split.

Constructor Details

StopWatch

public StopWatch()
Constructor.

Method Details

getTime

public long getTime()
Get the time on the stopwatch.

This is either the time between start and latest split, between start and stop, or the time between the start and the moment this method is called.

Returns:
the time in milliseconds

reset

public void reset()
Reset the stopwatch.

This method clears the internal values to allow the object to be reused.


resume

public void resume()
Resume 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.


split

public void split()

start

public void start()
Start the stopwatch.

This method starts a new timing session, clearing any previous values.


stop

public void stop()
Stop the stopwatch.

This method ends a new timing session, allowing the time to be retrieved.


suspend

public void suspend()
Suspend 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.


toString

public String toString()
Gets a summary of the time that the stopwatch recorded as a string.

The format used is ISO8601-like, hours:minutes:seconds.milliseconds.

Returns:
the time as a String

unsplit

public void unsplit()
Remove a split.

This method clears the stop time. The start time is unaffected, enabling timing from the original start point to continue.


Copyright © 2001-2003 - Apache Software Foundation