org.sormula.operation.monitor
Class ElapsedTime

java.lang.Object
  extended by org.sormula.operation.monitor.ElapsedTime

public class ElapsedTime
extends java.lang.Object

Records elapsed time in nano seconds. A count, getCount(), is incremented for each start/stop so that an average elapsed time can be calculated, getAverageTime().

Since:
1.5
Author:
Jeff Miller

Constructor Summary
ElapsedTime(java.lang.String name)
          Constructs for name and no other totals.
ElapsedTime(java.lang.String name, ElapsedTime total, ElapsedTime parent)
          Constructs for name, total, and parent.
 
Method Summary
protected  void add(long elpasedNanos)
          Adds nanoseconds to this time and adds to total and parent if they are not null.
 void cancel()
          Cancels any time recorded since start() and time paused with pause().
protected  java.lang.String format(long nanoseconds)
          Formats the elapsed time using the current time format.
 long getAverageTime()
           
 int getCount()
           
 java.lang.String getFormattedAverageTime()
           
 java.lang.String getFormattedTime()
           
 java.lang.String getName()
           
 ElapsedTime getParent()
           
 long getTime()
           
 java.lang.String getTimeFormat()
          Gets the format string to use by getFormattedTime() and getFormattedAverageTime().
 ElapsedTime getTotal()
           
 boolean isFirstIgnored()
           
 boolean isIgnoreFirst()
           
 void pause()
          Stops recording time until resume() is invoked.
 void resume()
          Starts recording the after pause.
 void setIgnoreFirst(boolean ignoreFirst)
          Sets what to do with the first time recorded.
 void setTimeFormat(java.lang.String timeFormat)
          Sets the format string to use when formatting time.
 void start()
          Starts recording eplased time.
 void stop()
          Stops recording elapsed time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ElapsedTime

public ElapsedTime(java.lang.String name)
Constructs for name and no other totals.

Parameters:
name - name to display in log file

ElapsedTime

public ElapsedTime(java.lang.String name,
                   ElapsedTime total,
                   ElapsedTime parent)
Constructs for name, total, and parent.

Parameters:
name - name to display in log file
total - elapsed times are summed into this total; null for none
parent - elapsed times are summed into this parent total; null for none
Method Detail

getName

public java.lang.String getName()
Returns:
name supplied in constructor

getTotal

public ElapsedTime getTotal()
Returns:
total time supplied in constructor

getParent

public ElapsedTime getParent()
Returns:
parent time supplied in constructor

start

public void start()
Starts recording eplased time.


stop

public void stop()
Stops recording elapsed time.


cancel

public void cancel()
Cancels any time recorded since start() and time paused with pause().


pause

public void pause()
Stops recording time until resume() is invoked. Zero or more pause/resume pairs may be invoked.


resume

public void resume()
Starts recording the after pause.


add

protected void add(long elpasedNanos)
Adds nanoseconds to this time and adds to total and parent if they are not null. Increments the count by 1.

Parameters:
elpasedNanos - nanoseconds to add; positive to add or negative to subtract

getTime

public long getTime()
Returns:
elapsed nanoseconds

getAverageTime

public long getAverageTime()
Returns:
average elapsed nanoseconds if count > 0; otherwise 0

getCount

public int getCount()
Returns:
number of times that stop() or add(long) have been invoked

isIgnoreFirst

public boolean isIgnoreFirst()
Returns:
true if first time is ignored; false if all times are recorded

setIgnoreFirst

public void setIgnoreFirst(boolean ignoreFirst)
Sets what to do with the first time recorded. Ignoring first time value (the first time that stop() is used) is a rough way to exclude class loading times which may skew the average time for all occurances.

Parameters:
ignoreFirst - true to ignore the first time; false to record all times

isFirstIgnored

public boolean isFirstIgnored()
Returns:
if the first time has occurred and was ignored

getTimeFormat

public java.lang.String getTimeFormat()
Gets the format string to use by getFormattedTime() and getFormattedAverageTime().

Returns:
String.format(String, Object...) format to use

setTimeFormat

public void setTimeFormat(java.lang.String timeFormat)
Sets the format string to use when formatting time. Default is %02d:%02d:%02d.%09d. Values formatted are hours, minutes, seconds, and nanoseconds in that order. String.format(String, Object...) is used to format.

Parameters:
timeFormat - String.format(String, Object...) format to use

getFormattedTime

public java.lang.String getFormattedTime()
Returns:
elapsed time formatted as a String

getFormattedAverageTime

public java.lang.String getFormattedAverageTime()
Returns:
average elapsed time formatted as a String

format

protected java.lang.String format(long nanoseconds)
Formats the elapsed time using the current time format.

Parameters:
nanoseconds - time in nanoseconds
Returns:
nanoseconds formatted into String