es.iac.ing.codeso.gen
Class Stopwatch

java.lang.Object
  extended by es.iac.ing.codeso.gen.Stopwatch

@NotThreadSafe
public class Stopwatch
extends java.lang.Object

Modelled on the stopwatch facility of the typical digital watch this class provides a simple facility for general purpose timing.

The class is intended primarily for instrumenting code to aid in optimisation.

The current implementation makes use of the Java 1.5 System.nanotime method and inherits the same quality of service guarantees.

To avoid synchronisation overhead this class is NOT thread-safe. If synchronisation is required this should be achieved externally.


Nested Class Summary
static class Stopwatch.TimeUnit
          An enumeration which modifies the time units when invoking methods on this class.
 
Field Summary
private  boolean running
          Holds the current state of the stopwatch: running or stopped.
private  long startTime
          A timestamp which keeps track of the instant in which the stopwatch was last started.
private  long stopTime
          A timestamp which keeps track of the instant in which the stopwatch was last stopped.
private  long totalElapsedTimeWhenLastStopped
          Holds the the sum of all the completed time intervals.
 
Constructor Summary
Stopwatch()
          Constructs a new stopwatch
 
Method Summary
 long getElapsedTime(Stopwatch.TimeUnit units)
          Gets the total elapsed time in a time unit specified by the user.
 java.lang.String getElapsedTimeAsFormattedString()
          Gets the total elapsed time as a string.
 long getElapsedTimeInMillisecondsAsLong()
          Gets the total elapsed time in the default time unit, which is milliseconds.
 long getIntervalTime(Stopwatch.TimeUnit units)
          Gets the interval time in a time unit selectable by the user.
private  long getNanosecondTimeStamp()
          Gets the current timestamp in nanoseconds with the best resolution possible on the system.
 void reset()
          Resets the stopwatch setting the interval time and total elapsed time back to zero.
 void start()
          Starts (or restarts) the stopwatch, initiating a new timing interval.
 void stop()
          Stops the stopwatch, ending the current timing interval.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

running

private boolean running
Holds the current state of the stopwatch: running or stopped.


startTime

private long startTime
A timestamp which keeps track of the instant in which the stopwatch was last started.


stopTime

private long stopTime
A timestamp which keeps track of the instant in which the stopwatch was last stopped.


totalElapsedTimeWhenLastStopped

private long totalElapsedTimeWhenLastStopped
Holds the the sum of all the completed time intervals. A time interval is the interval between a start/stop sequence.

Constructor Detail

Stopwatch

public Stopwatch()
Constructs a new stopwatch

Method Detail

start

public void start()
Starts (or restarts) the stopwatch, initiating a new timing interval.

This method should only be invoked when the stopwatch is stopped.

This method does NOT reset the total elapsed time.

Throws:
java.lang.IllegalStateException - if the timer is already running

stop

public void stop()
Stops the stopwatch, ending the current timing interval.

This method should only be invoked when the stopwatch is running.

Throws:
java.lang.IllegalStateException - if the timer is already stopped

reset

public void reset()
Resets the stopwatch setting the interval time and total elapsed time back to zero.

This method can be called at any time, regardless of whether the stopwatch is running or stopped.


getElapsedTimeAsFormattedString

public java.lang.String getElapsedTimeAsFormattedString()
Gets the total elapsed time as a string.

The total elapsed time is is the summation of all the timing intervals that have occurred since the stopwatch was last reset.

The returned string has the format ssss.uuuuuu where s denotes the elapsed time in seconds, and u denotes the fractional part in microseconds.

Returns:
String specifying the elapsed time of the stopwatch

getElapsedTimeInMillisecondsAsLong

public long getElapsedTimeInMillisecondsAsLong()
Gets the total elapsed time in the default time unit, which is milliseconds.

The total elapsed time is is the summation of all the timing intervals since the stopwatch was last reset.

Returns:
long containing the result (which will always be zero or positive)

getElapsedTime

public long getElapsedTime(Stopwatch.TimeUnit units)
Gets the total elapsed time in a time unit specified by the user.

The total elapsed time is is the summation of all the timing intervals since the stopwatch was last reset.

Parameters:
units - specifying the time unit in which the results are to be returned.
Returns:
long containing the result (which will always be zero or positive)

getIntervalTime

public long getIntervalTime(Stopwatch.TimeUnit units)
Gets the interval time in a time unit selectable by the user.

When the stopwatch is running this method returns the elapsed time since the last invocation of the start method. When stopped this method returns the duration of the previous time interval.

Parameters:
units - specifying the time unit in which the results are to be returned.
Returns:
long containing the result (which will always be zero or positive)

getNanosecondTimeStamp

private long getNanosecondTimeStamp()
Gets the current timestamp in nanoseconds with the best resolution possible on the system.

Returns:
long containing the current time relative to some arbitrary reference moment in the past (or future)