gausssoft.util
Class Stopwatch

java.lang.Object
  |
  +--gausssoft.util.Stopwatch
All Implemented Interfaces:
Cloneable, CloneableObject, Serializable

public class Stopwatch
extends Object
implements Serializable, CloneableObject

The Stopwatch class is a simple class emulating a Stopwatch by providing the same functionality.

When a Stopwatch is serialized, it remains in the same state as when it was an object. If you serialize a running Stopwatch, it will continue running and will show the correct time after deserialization.

All results returned are, by default, in milliseconds. The accuracy of the Stopwatch is dependant upon the accuracy of System.currentTimeMillis(), plus the amount of overhead generated to perform each method.

This object is not thread safe.

Author:
Allan Crooks
See Also:
System.currentTimeMillis(), Serialized Form

Constructor Summary
Stopwatch()
          Constructs a new Stopwatch.
Stopwatch(long time)
          Constructs a new Stopwatch, specifying how much time has already elapsed.
 
Method Summary
 Object clone()
          Creates a copy of this Stopwatch.
 boolean equals(Object obj)
          Compares this object against another object.
 long get()
          Gets the number of milliseconds elapsed so far.
static long now()
          Simple hook into System.currentTimeMillis() to provide the current date in milliseconds.
 long offset(long off)
          Adds a specified amount of milliseconds to the current amount of time elapsed.
 void reset()
          Resets the time elapsed so far to 0.
 boolean running()
          Indicates if the Stopwatch is running or not.
 void set(long set)
          Sets the amount of milliseconds elapsed so far.
 void start()
          Starts the Stopwatch.
 long stop()
          Stops the Stopwatch running.
 String toString()
          Returns a string representation of this Stopwatch.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Stopwatch

public Stopwatch()
Constructs a new Stopwatch.

Stopwatch

public Stopwatch(long time)
Constructs a new Stopwatch, specifying how much time has already elapsed.
Parameters:
time - The number of milliseconds that have already passed.
Throws:
IllegalArgumentException - If time is less than 0.
Method Detail

start

public final void start()
Starts the Stopwatch. If the Stopwatch was stopped previously, and did not have it's counter reset, it will carry on counting from the time indicated on it's counter.

get

public final long get()
Gets the number of milliseconds elapsed so far. This can be executed whether the Stopwatch is running or not.
Returns:
The amount of time elapsed in milliseconds.

stop

public final long stop()
Stops the Stopwatch running. If the Stopwatch is not running, then this has no effect.
Returns:
The amount of time elapsed in milliseconds.

reset

public final void reset()
Resets the time elapsed so far to 0. If the Stopwatch is running, it reset the time elapsed to 0 milliseconds, and then carry on running.

set

public final void set(long set)
Sets the amount of milliseconds elapsed so far. If the Stopwatch is running, it will continue to run, using the new value of milliseconds.
Parameters:
set - The amount of milliseconds that have elapsed.
Throws:
IllegalArgumentException - If set is less than 0.

offset

public final long offset(long off)
Adds a specified amount of milliseconds to the current amount of time elapsed. If the Stopwatch is running, it will continue to run, adding the amount of time specified.

The figure supplied can either be positive or negative. If the value specified pushes the amount of milliseconds elapsed to a negative figure, it will be set to 0.

Parameters:
off - The number of milliseconds to add to the current value of elapsed milliseconds.
Returns:
The new amount of milliseconds that has elapsed.

now

public static long now()
Simple hook into System.currentTimeMillis() to provide the current date in milliseconds.
Returns:
The current date in milliseconds.
See Also:
System.currentTimeMillis()

running

public final boolean running()
Indicates if the Stopwatch is running or not.
Returns:
true if the Stopwatch is currently running.

toString

public String toString()
Returns a string representation of this Stopwatch.

The string returned will be of one of the following forms:

Overrides:
toString in class Object
Returns:
A string representation of this Stopwatch.

equals

public boolean equals(Object obj)
Compares this object against another object.

This method returns true only if the object provided is another Stopwatch object, and it both Stopwatches show the same amount of time that has elapsed.

Overrides:
equals in class Object
Parameters:
obj - The object to compare it to.
Returns:
true if this object is as the one provided.

clone

public Object clone()
Creates a copy of this Stopwatch.
Specified by:
clone in interface CloneableObject
Overrides:
clone in class Object
Returns:
A copy of this Stopwatch.