org.jscience.util
Class Stopwatch

java.lang.Object
  extended by org.jscience.util.Stopwatch
All Implemented Interfaces:
java.io.Serializable

public class Stopwatch
extends java.lang.Object
implements java.io.Serializable

Stopwatch is a convenient implementation to bench just about anything. Todo: provide support for internationalization

See Also:
Serialized Form

Constructor Summary
Stopwatch()
          initializes a running Stopwatch starting now
Stopwatch(boolean isRunning)
          If the boolean parameter is true, this call is equivalent to the default constructor; if the parameter is false, the StopWach will be initialized but is halted with no elapsed time so far.
Stopwatch(java.util.Date time)
          This constructor starts a Stopwatch starting at the given point in time.
Stopwatch(long milliseconds)
          This constructor starts a Stopwatch starting at the current time minus the milliseconds given.
Stopwatch(long milliseconds, boolean isRunning)
           
Stopwatch(Stopwatch timer)
          This constructor generates a new Stopwatch synchronized with the given timer
 
Method Summary
 java.lang.String display()
          displays the elapsed time as timeAsStringShort(elapsed())
 long elapsed()
          returns the elapsed time in milliseconds
 java.lang.String elapsedAsString()
          returns a string representing the elapsed time with - if applicable - days, hours, minutes and seconds (down to the milliseconds).
protected  long getEnd()
           
protected  long getHalted()
           
 java.util.Date getStartDate()
          returns the initial start time of the Stopwatch.
 long getStartTime()
          returns the time this Stopwatch was constructed or when any of the reset() or restart() methods were called last
 boolean isPaused()
          returns true if Stopwatch is paused.
 boolean isRunning()
          returns true if Stopwatch is running.
 void pause()
          pause() stops the timer and maintains the elapsed time; it does nothing if the timer is already paused.
 void reset()
          reset() will set the start to the current time and set the halted time to zero.
 void reset(long milliseconds)
          sets the given time as elapsed time; run/pause status remains and halted time is reset
 void restart()
          restart() reinitializes the timer equivalent to it just being construced with the default constructor (time is running).
 void restart(long milliseconds)
          restart() reinitializes the timer with the passed milliseconds interpreted as already elapsed time.
 void resume()
          resume() will reactivate a suspended timer (suspended through either stop() or pause()).
 void start()
          start() maintains the beginning time and sets the elapsed time to zero (through halted time); time will be running.
 long stop()
          stop() returns the elapsed time and also performs a reset().
 void synchronize(Stopwatch timer)
          synchronize() will make this Stopwatch equivalent to the passed timer
static java.lang.String timeAsString(long milliSecs)
          takes milliseconds and converts them into a String using all relevant time measures up to days
static java.lang.String timeAsStringShort(long milliSecs)
          takes milliseconds and converts them into a short String.
 java.lang.String toString()
          returns the elapsed time in a convenient format including elapsedAsString() and tells whether the Stopwatch is currently running or not
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Stopwatch

public Stopwatch()
initializes a running Stopwatch starting now


Stopwatch

public Stopwatch(long milliseconds)
This constructor starts a Stopwatch starting at the current time minus the milliseconds given. I.e. a negative value would mean that the StopWach counts towards zero until zero is reached and then continues to count forward.


Stopwatch

public Stopwatch(java.util.Date time)
This constructor starts a Stopwatch starting at the given point in time. I.e. a time in the future would mean that the StopWach counts towards zero until zero is reached and then continues to count forward.


Stopwatch

public Stopwatch(Stopwatch timer)
This constructor generates a new Stopwatch synchronized with the given timer


Stopwatch

public Stopwatch(boolean isRunning)
If the boolean parameter is true, this call is equivalent to the default constructor; if the parameter is false, the StopWach will be initialized but is halted with no elapsed time so far.


Stopwatch

public Stopwatch(long milliseconds,
                 boolean isRunning)
Method Detail

synchronize

public void synchronize(Stopwatch timer)
synchronize() will make this Stopwatch equivalent to the passed timer


getStartTime

public long getStartTime()
returns the time this Stopwatch was constructed or when any of the reset() or restart() methods were called last


getStartDate

public java.util.Date getStartDate()
returns the initial start time of the Stopwatch. This time may have been changed through synchronize() or restart().


isPaused

public boolean isPaused()
returns true if Stopwatch is paused. isPaused() <==> !isRunning()


isRunning

public boolean isRunning()
returns true if Stopwatch is running. isPaused() <==> !isRunning()


pause

public void pause()
pause() stops the timer and maintains the elapsed time; it does nothing if the timer is already paused.


resume

public void resume()
resume() will reactivate a suspended timer (suspended through either stop() or pause()). The beginning time as well as the suspended time is maintained, so that the elapsed time remains accurate. If the StopWach is currently running, the function doesn't do anything.


start

public void start()
start() maintains the beginning time and sets the elapsed time to zero (through halted time); time will be running.


stop

public long stop()
stop() returns the elapsed time and also performs a reset().


reset

public void reset()
reset() will set the start to the current time and set the halted time to zero. The running status (paused or not) of the timer is maintained.


reset

public void reset(long milliseconds)
sets the given time as elapsed time; run/pause status remains and halted time is reset


restart

public void restart()
restart() reinitializes the timer equivalent to it just being construced with the default constructor (time is running).


restart

public void restart(long milliseconds)
restart() reinitializes the timer with the passed milliseconds interpreted as already elapsed time.


elapsed

public long elapsed()
returns the elapsed time in milliseconds


elapsedAsString

public java.lang.String elapsedAsString()
returns a string representing the elapsed time with - if applicable - days, hours, minutes and seconds (down to the milliseconds).


display

public java.lang.String display()
displays the elapsed time as timeAsStringShort(elapsed())

See Also:
timeAsStringShort(long)

timeAsString

public static java.lang.String timeAsString(long milliSecs)
takes milliseconds and converts them into a String using all relevant time measures up to days


timeAsStringShort

public static java.lang.String timeAsStringShort(long milliSecs)
takes milliseconds and converts them into a short String. The format is
h:mm:ss
.


toString

public java.lang.String toString()
returns the elapsed time in a convenient format including elapsedAsString() and tells whether the Stopwatch is currently running or not

Overrides:
toString in class java.lang.Object

getHalted

protected long getHalted()

getEnd

protected long getEnd()