public class Timer extends java.lang.Object implements ErrorHandlerCallback
Support for basic timer profiling.
This is very similar to XPTIMER in X4WTOOLS. It allows the user to create timers which measure interval and total time. The timers can be started, restarted, stopped and checked at various intervals. Options are also provided to control the debug output.
Available Options: OPTION_DATE: Show date stamp OPTION_TIME: Show time stamp OPTION_INTERVAL: Show interval time (in milliseconds) OPTION_TOTAL: Show total time (in milliseconds) OPTION_NAME: Show timer name OPTION_ALL: Turn on all options OPTION_DATE: Show date stamp options OPTION_DEFAULT: Show default options (interval, total, name) It is also possible to set the indent level to make output easier to read.
Modifier and Type | Field and Description |
---|---|
static int |
INDENT_SPACES |
static int |
OPTION_ALL
Show all options.
|
static int |
OPTION_DATE
Show date stamp.
|
static int |
OPTION_DATE_TIME
Show date stamp options.
|
static int |
OPTION_DEFAULT
Show default options.
|
static int |
OPTION_INTERVAL
Show interval time (in milliseconds).
|
static int |
OPTION_NAME
Show timer name.
|
static int |
OPTION_TIME
Show time stamp.
|
static int |
OPTION_TOTAL
Show total time (in milliseconds).
|
static java.lang.String |
TIMER_NOT_STARTED |
Constructor and Description |
---|
Timer()
Default constructor for Timer class.
Example: Timer timer = new Timer (); |
Timer(java.lang.String strName)
Constructor for Timer class.
Example: Timer timer = new Timer ("My Timer!"); |
Timer(java.lang.String strName,
long lOptions)
Constructor for Timer class.
Example: Timer timer = new Timer ("My Timer!", OPTION_DEFAULT); |
Modifier and Type | Method and Description |
---|---|
void |
addErrorHandler(ErrorHandler errorHandler)
Adds an
ErrorHandler object to this
DefaultBuilderContext object. |
java.lang.String |
formatDateTime(java.util.Date date,
int nStyle,
java.util.Locale locale)
Formats the time/date based on timer formatting options.
Example: formatTimeDate (date); |
protected boolean |
formatOutput(java.lang.String strState,
long lInterval,
java.lang.String strDescription,
java.util.Date date)
Formats timer output string.
Example: formatOutput ("This is my starting timer!"); |
ErrorHandler |
getErrorHandler()
Retrieves an error handler.
|
int |
getIndentLevel()
Retrieves the output indent level.
Example: int nIndentLevel = getIndentLevel(); |
int |
getIndentSpaces()
Retrieves the number of spaces per indent.
Example: int nIndentSpaces = getIndentSpaces(); |
java.util.Date |
getLastDate()
Retrieves the last timer date.
Example: Date date = getLastDate(); |
java.lang.String |
getName()
Retrieves the timer name.
Example: String strName = getName(); |
long |
getOptions()
Retrieves the formatting options bitmask.
Example: long lOptions = getOptions(); |
java.io.PrintWriter |
getPrintWriter()
Retrieves the
PrintWriter used to output timer results.Example: PrintWriter printWriter = getPrintWriter(); |
long |
getTotal()
Retrieves the total time in milleseconds since the timer was started.
Example: long lTotal = getTotal(); |
boolean |
interval(java.lang.String strDescription)
Determines timer interval since the last timer event.
Example: interval ("My timer!"); |
boolean |
isEnabled()
Specifies whether timer is enabled.
Example: boolean bIsEnabled = isEnabled(); |
boolean |
isStarted()
Determines if timer has been started.
Example: boolean bStarted = isStarted(); |
static void |
main(java.lang.String[] argv)
Timer |
void |
removeErrorHandler()
Overrides a previously set
ErrorHandler object in this
DefaultBuilderContext object with a default one. |
boolean |
restart(java.lang.String strDescription)
Restarts a stopped timer and retains the total time.
Example: restart ("My timer!"); |
void |
setEnabled(boolean bEnabled)
Retrieves whether timer is enabled.
Example: setEnabled (true); |
void |
setIndentLevel(int nIndentLevel)
Specifies the output indent level.
Example: setIndentLevel (3); |
void |
setIndentSpaces(int nIndentSpaces)
Specifies the number of spaces per indent.
Example: setIndentSpaces (5); |
protected void |
setLastDate(java.util.Date date)
Specifies the last timer date.
Example: setLastDate (date); |
void |
setName(java.lang.String strName)
Specifies the timer name.
Example: setName (strName); |
void |
setOptions(long lOptions)
Specifies the output formatting options bitmask.
Example: setOptions (OPTION_TIME_STAMP); |
void |
setPrintWriter(java.io.PrintWriter printWriter)
Specifies the
PrintWriter used to output timer results.Defaults to System.out Example: setPrintWriter (printWriter); |
protected void |
setStarted(boolean bStarted)
Determines if timer has been started.
Example: setStarted (true); |
protected void |
setTotal(long lTime)
Specifies the total time since timer was started.
Example: setTotal(lTime); |
boolean |
start(java.lang.String strDescription)
Starts the timer.
Example: start ("My timer!"); |
boolean |
stop(java.lang.String strDescription)
Stops the timer and outputs final interval.
Example: stop ("My timer!"); |
protected long |
updateTimer()
Updates the timer, returning interval.
Example: updateTimer (); |
public static final int OPTION_DATE
public static final int OPTION_TIME
public static final int OPTION_INTERVAL
public static final int OPTION_TOTAL
public static final int OPTION_NAME
public static final int OPTION_ALL
OPTION_DATE
,
OPTION_TIME
,
OPTION_INTERVAL
,
OPTION_TOTAL
,
OPTION_NAME
,
Constant Field Valuespublic static final int OPTION_DATE_TIME
OPTION_DATE
,
OPTION_TIME
,
Constant Field Valuespublic static final int OPTION_DEFAULT
OPTION_INTERVAL
,
OPTION_TOTAL
,
OPTION_NAME
,
Constant Field Valuespublic static final int INDENT_SPACES
public static final java.lang.String TIMER_NOT_STARTED
public Timer()
Default constructor for Timer class.
Example: Timer timer = new Timer ();
public Timer(java.lang.String strName)
Constructor for Timer class.
Example: Timer timer = new Timer ("My Timer!");
strName
- A String
which represents the timer name.public Timer(java.lang.String strName, long lOptions)
Constructor for Timer class.
Example: Timer timer = new Timer ("My Timer!", OPTION_DEFAULT);
strName
- A String
which represents the timer name.lOptions
- A long
which represents the output
formatting options bitmaskOPTION_DATE
,
OPTION_TIME
,
OPTION_INTERVAL
,
OPTION_TOTAL
,
OPTION_NAME
,
OPTION_ALL
,
OPTION_DATE
,
OPTION_DEFAULT
public static void main(java.lang.String[] argv)
Timer testing code.
public java.io.PrintWriter getPrintWriter()
Retrieves thePrintWriter
used to output timer results.
Example: PrintWriter printWriter = getPrintWriter();
PrintWriter
which determines where to output timer
results.public void setPrintWriter(java.io.PrintWriter printWriter)
Specifies thePrintWriter
used to output timer results.
Defaults toSystem.out
Example: setPrintWriter (printWriter);
printWriter
- A PrintWriter
which is used to output timer
results.public java.lang.String getName()
Retrieves the timer name.
Example: String strName = getName();
String
which represents the timer name.public void setName(java.lang.String strName)
Specifies the timer name.
Example: setName (strName);
strName
- A String
which represents the timer name.public boolean isEnabled()
Specifies whether timer is enabled.
Example: boolean bIsEnabled = isEnabled();
boolean
which is true
when the timer is
enabled and false
otherwise.public void setEnabled(boolean bEnabled)
Retrieves whether timer is enabled.
Example: setEnabled (true);
bEnabled
- A boolean
which is true
when the
timer is enabled and false
otherwise.public int getIndentLevel()
Retrieves the output indent level.
Example: int nIndentLevel = getIndentLevel();
int
which represents the output indent level.public void setIndentLevel(int nIndentLevel)
Specifies the output indent level.
Example: setIndentLevel (3);
nIndent
- A int
which presents the output indent level.public int getIndentSpaces()
Retrieves the number of spaces per indent.
Example: int nIndentSpaces = getIndentSpaces();
int which represents the number of spaces per indent.
public void setIndentSpaces(int nIndentSpaces)
Specifies the number of spaces per indent.
Example: setIndentSpaces (5);
nIndentSpaces
- A int
which represents the number of spaces
per indent.public long getOptions()
Retrieves the formatting options bitmask.
Example: long lOptions = getOptions();
long
which represents the output formatting options.OPTION_DATE
,
OPTION_TIME
,
OPTION_INTERVAL
,
OPTION_TOTAL
,
OPTION_NAME
,
OPTION_ALL
,
OPTION_DATE
,
OPTION_DEFAULT
public void setOptions(long lOptions)
Specifies the output formatting options bitmask.
Example: setOptions (OPTION_TIME_STAMP);
lOptions
- A long
which represents the output format options
bitmask.OPTION_DATE
,
OPTION_TIME
,
OPTION_INTERVAL
,
OPTION_TOTAL
,
OPTION_NAME
,
OPTION_ALL
,
OPTION_DATE
,
OPTION_DEFAULT
public java.util.Date getLastDate()
Retrieves the last timer date.
Example: Date date = getLastDate();
Date
representing the last timer date.public long getTotal()
Retrieves the total time in milleseconds since the timer was started.
Example: long lTotal = getTotal();
long
which represents the number of milliseconds since
the timer was started.public boolean isStarted()
Determines if timer has been started.
Example: boolean bStarted = isStarted();
boolean
which is true
if the timer has
been started and false
otherwise.public boolean start(java.lang.String strDescription)
Starts the timer.
Example: start ("My timer!");
strDescription
- A String
which represents the description
to associate with the timer.boolean
which is true
if successful and
false
otherwise.public boolean interval(java.lang.String strDescription)
Determines timer interval since the last timer event.
Example: interval ("My timer!");
strDescription
- A String
which presents the description
to associate with this event.boolean
which is true
if successful and
false
otherwise.public boolean stop(java.lang.String strDescription)
Stops the timer and outputs final interval.
Example: stop ("My timer!");
strDescription
- A String
which presents the description
to associate with this event.boolean
which is true
if successful and
false
otherwise.public boolean restart(java.lang.String strDescription)
Restarts a stopped timer and retains the total time.
Example: restart ("My timer!");
strDescription
- A String
which presents the description
to associate with this event.boolean
which is true
if successful and
false
otherwise.Exception
- if timer has not been started.public java.lang.String formatDateTime(java.util.Date date, int nStyle, java.util.Locale locale)
Formats the time/date based on timer formatting options.
Example: formatTimeDate (date);
date
- A Date
to format.nStyle
- A int
which represents the Date
style.locale
- A Locale
which represents the Date
locale.String
which represents the formatted date string.DateFormat
public void addErrorHandler(ErrorHandler errorHandler)
ErrorHandler
object to this
DefaultBuilderContext
object.
The ErrorHandler
object will be
called when the visual component traps an error from other parts
of the system.addErrorHandler
in interface ErrorHandlerCallback
errorHandler
- The ErrorHandler
object.public void removeErrorHandler()
ErrorHandler
object in this
DefaultBuilderContext
object with a default one.removeErrorHandler
in interface ErrorHandlerCallback
public ErrorHandler getErrorHandler()
ErrorHandler
representing the error handler.protected boolean formatOutput(java.lang.String strState, long lInterval, java.lang.String strDescription, java.util.Date date)
Formats timer output string.
Example: formatOutput ("This is my starting timer!");
strState
- A String
which represents the timer state.lInterval
- A long
which represents the timer interval.strDescription
- A String
which represents the description.date
- A Date
which represents the date to display.boolean
which is true
if successful and
false
otherwise.protected void setTotal(long lTime)
Specifies the total time since timer was started.
Example: setTotal(lTime);
lTime
- A long
which represents the total time since timer
was started.protected void setStarted(boolean bStarted)
Determines if timer has been started.
Example: setStarted (true);
bStarted
- A boolean
which is true
if the
timer has been started and false
otherwise.protected void setLastDate(java.util.Date date)
Specifies the last timer date.
Example: setLastDate (date);
date
- A Date
which represents the last date.protected long updateTimer()
Updates the timer, returning interval.
Example: updateTimer ();
long
which represents the timer interval.Exception
- if timer has not been started.