net.jinx.time
Class StopWatch

java.lang.Object
  extended by net.jinx.time.StopWatch

public class StopWatch
extends java.lang.Object

StopWatch allows you to determine how much time has passed in a given period of time, using the most precise clock available. For Java 1.5 or greater, this will use the nanosecond timer. For Java 1.4 or lower, this will use the millisecond timer. For the official Sun JVM on 1.4+, this will use the sun.misc.Perf class (performance profiling class) as a nanosecond timer.

You also have the option of constructing your own time source to plug into a StopWatch.

API Stability: Stable.


Constructor Summary
StopWatch()
          Constructs a real-time StopWatch with one of the default time sources; the most precise time source available is used.
StopWatch(boolean useNano)
          Constructor that allows choosing whether or not nanosecond timing is used if available.
StopWatch(TimeSource source)
          Constructor for using a custom time source.
 
Method Summary
 TimeSource getSource()
          Provides the TimeSource for this StopWatch.
 boolean isNano()
          Determines whether or not this StopWatch is backed by a nanosecond timer or a millisecond timer.
 boolean isRealTime()
          Returns true if this StopWatch is backed by a real-time source.
static void main(java.lang.String[] args)
          Main entry point for the StopWatch test program.
 long millisPassed()
          Provides the number of milliseconds that have passed since the last call to reset().
 long nanoPassed()
          Provides the number of nanoseconds that have passed since the last call to reset().
 void pause()
          Pauses this StopWatch.
 void reset()
          Resets this StopWatch.
 void resume()
          Resumes this StopWatch.
 double timePassed()
          Provides the amount of time that has passed since the last call to reset().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StopWatch

public StopWatch()
Constructs a real-time StopWatch with one of the default time sources; the most precise time source available is used. This is the same as using StopWatch(true).


StopWatch

public StopWatch(TimeSource source)
Constructor for using a custom time source. Reset() is called to give the StopWatch a valid start time.

Parameters:
source - the TimeSource that will drive this StopWatch.
Throws:
java.lang.NullPointerException - if source is null.
java.lang.IllegalArgumentException - if source doesn't implement either of NanoTimeSource or MillisTimeSource.

StopWatch

public StopWatch(boolean useNano)
Constructor that allows choosing whether or not nanosecond timing is used if available. This uses one of the default time sources. Reset() is called to give the StopWatch a valid start time.

Parameters:
useNano - if true and a nanosecond timer is available, it will be used. Otherwise, the millisecond timer will be used.
Method Detail

getSource

public TimeSource getSource()
Provides the TimeSource for this StopWatch. This can be handy if you wish to share a single TimeSource between several StopWatch instances.

Returns:
the TimeSource.

reset

public void reset()
Resets this StopWatch. Calls to timePassed() return values based on how much time has passed since this method was last called.


pause

public void pause()
Pauses this StopWatch. Time will not accumulate on it until reset() or resume() are called.


resume

public void resume()
Resumes this StopWatch.


timePassed

public double timePassed()
Provides the amount of time that has passed since the last call to reset().

Returns:
a double representing the time in seconds since the last reset.

millisPassed

public long millisPassed()
Provides the number of milliseconds that have passed since the last call to reset().

Returns:
a long representing the time in milliseconds since the last reset.

nanoPassed

public long nanoPassed()
Provides the number of nanoseconds that have passed since the last call to reset().

Returns:
a long representing the time in nanoseconds since the last reset.

isRealTime

public boolean isRealTime()
Returns true if this StopWatch is backed by a real-time source. False otherwise.

Returns:
true or false.

isNano

public boolean isNano()
Determines whether or not this StopWatch is backed by a nanosecond timer or a millisecond timer.

Returns:
true if the timer is nanosecond based; false if it's millisecond based.

main

public static void main(java.lang.String[] args)
Main entry point for the StopWatch test program. Just runs a few tests and displays the results through standard out.