Holger's
Java API

com.antelmann.calendar
Class Stopwatch

java.lang.Object
  extended by com.antelmann.calendar.Stopwatch
All Implemented Interfaces:
Timed, Serializable

public class Stopwatch
extends Object
implements Serializable, Timed

Stopwatch is a convenient implementation to bench just about anything. By default, this class uses the machine's internal clock, but by setting a custom TimeSystem, it can also be clocked by an atomic clock or any other time measurement system.

Author:
Holger Antelmann
See Also:
HourFormat, Serialized Form

Field Summary
 
Fields inherited from interface com.antelmann.calendar.Timed
COMPARATOR, DATE_EXTRACTOR
 
Constructor Summary
Stopwatch()
          initializes a running Stopwatch starting now
Stopwatch(boolean isRunning)
          If the boolean parameter is true, this call is equivalent to the default constructor; if the parameter is false, the StopWach will be initialized but is halted with no elapsed time so far.
Stopwatch(Date time)
          This constructor starts a Stopwatch starting at the given point in time.
Stopwatch(long milliseconds)
          This constructor starts a Stopwatch starting at the current time minus the milliseconds given.
Stopwatch(long milliseconds, boolean isRunning)
           
Stopwatch(long milliseconds, TimeSystem system)
           
Stopwatch(Stopwatch timer)
          This constructor generates a new Stopwatch synchronized with the given timer
 
Method Summary
 String display()
          displays the elapsed time as timeAsStringShort(elapsed())
 long elapsed()
          returns the elapsed time in milliseconds
 String elapsedAsString()
          returns a string representing the elapsed time with - if applicable - days, hours, minutes and seconds (down to the milliseconds).
 String elapsedAsStringShort()
          returns a short string representing the elapsed time with - if applicable - days, hours, minutes and seconds (down to the milliseconds).
protected  long getEnd()
           
protected  long getHalted()
           
 Date getStartDate()
          returns the initial start time of the Stopwatch.
 long getStartTime()
          returns the time this Stopwatch was constructed or when any of the reset() or restart() methods were called last
 long getTime()
           
 TimeSystem getTimeSystem()
          returns the time measurement system in place
static String hourMinuteString(long milliSecs)
          formats a String with hours as the largest entity in the format h:mm
 boolean isPaused()
          returns true if Stopwatch is paused.
 boolean isRunning()
          returns true if Stopwatch is running.
static long parse(String s)
          assumes the format h:m:s; can parse a String created by timeAsStringShort(long)
 void pause()
          pause() stops the timer and maintains the elapsed time; it does nothing if the timer is already paused.
 void reset()
          reset() will set the start to the current time and set the halted time to zero.
 void reset(long milliseconds)
          sets the given time as elapsed time; run/pause status remains and halted time is reset
 void restart()
          restart() reinitializes the timer equivalent to it just being construced with the default constructor (time is running).
 void restart(long milliseconds)
          restart() reinitializes the timer with the passed milliseconds interpreted as already elapsed time.
 void resume()
          resume() will reactivate a suspended timer (suspended through either stop() or pause()).
 void setTimeSystem(TimeSystem system)
          allows to use a customized measurement for time
 void start()
          start() maintains the beginning time and sets the elapsed time to zero (through halted time); time will be running.
 long stop()
          stop() returns the elapsed time and also performs a reset().
 void synchronize(Stopwatch timer)
          synchronize() will make this Stopwatch equivalent to the passed timer
static String timeAsHourString(long milliSecs)
          formats a String with hours as the largest entity and down to the minute
static String timeAsHourStringShort(long milliSecs)
          formats a String with hours as the largest entity in the format h:mm with an 'h' at the end
static String timeAsString(long milliSecs)
           
static String timeAsString(long milliSecs, Locale locale)
          takes milliseconds and converts them into a localized String using all relevant time measures from millisecs to days
static String timeAsStringShort(long milliSecs)
          takes milliseconds and converts them into a short String of the format h:mm:ss.
 String toString()
          returns the elapsed time in a convenient format including elapsedAsString() and tells whether the Stopwatch is currently running or not
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Stopwatch

public Stopwatch()
initializes a running Stopwatch starting now


Stopwatch

public Stopwatch(long milliseconds)
This constructor starts a Stopwatch starting at the current time minus the milliseconds given. I.e. a negative value would mean that the StopWach counts towards zero until zero is reached and then continues to count forward.


Stopwatch

public Stopwatch(long milliseconds,
                 TimeSystem system)

Stopwatch

public Stopwatch(Date time)
This constructor starts a Stopwatch starting at the given point in time. I.e. a time in the future would mean that the StopWach counts towards zero until zero is reached and then continues to count forward.


Stopwatch

public Stopwatch(Stopwatch timer)
This constructor generates a new Stopwatch synchronized with the given timer


Stopwatch

public Stopwatch(boolean isRunning)
If the boolean parameter is true, this call is equivalent to the default constructor; if the parameter is false, the StopWach will be initialized but is halted with no elapsed time so far.


Stopwatch

public Stopwatch(long milliseconds,
                 boolean isRunning)
Method Detail

synchronize

public void synchronize(Stopwatch timer)
synchronize() will make this Stopwatch equivalent to the passed timer


getTimeSystem

public TimeSystem getTimeSystem()
returns the time measurement system in place


setTimeSystem

public void setTimeSystem(TimeSystem system)
allows to use a customized measurement for time


getStartTime

public long getStartTime()
returns the time this Stopwatch was constructed or when any of the reset() or restart() methods were called last


getStartDate

public Date getStartDate()
returns the initial start time of the Stopwatch. This time may have been changed through synchronize() or restart().


isPaused

public boolean isPaused()
returns true if Stopwatch is paused. isPaused() <==> !isRunning()


isRunning

public boolean isRunning()
returns true if Stopwatch is running. isPaused() <==> !isRunning()


pause

public void pause()
pause() stops the timer and maintains the elapsed time; it does nothing if the timer is already paused.


resume

public void resume()
resume() will reactivate a suspended timer (suspended through either stop() or pause()). The beginning time as well as the suspended time is maintained, so that the elapsed time remains accurate. If the StopWach is currently running, the function doesn't do anything.


start

public void start()
start() maintains the beginning time and sets the elapsed time to zero (through halted time); time will be running.


stop

public long stop()
stop() returns the elapsed time and also performs a reset().


reset

public void reset()
reset() will set the start to the current time and set the halted time to zero. The running status (paused or not) of the timer is maintained.


reset

public void reset(long milliseconds)
sets the given time as elapsed time; run/pause status remains and halted time is reset


restart

public void restart()
restart() reinitializes the timer equivalent to it just being construced with the default constructor (time is running).


restart

public void restart(long milliseconds)
restart() reinitializes the timer with the passed milliseconds interpreted as already elapsed time.


elapsed

public long elapsed()
returns the elapsed time in milliseconds


getTime

public long getTime()
Specified by:
getTime in interface Timed

elapsedAsString

public String elapsedAsString()
returns a string representing the elapsed time with - if applicable - days, hours, minutes and seconds (down to the milliseconds).

See Also:
timeAsString(long)

elapsedAsStringShort

public String elapsedAsStringShort()
returns a short string representing the elapsed time with - if applicable - days, hours, minutes and seconds (down to the milliseconds).

See Also:
timeAsStringShort(long)

display

public String display()
displays the elapsed time as timeAsStringShort(elapsed())

See Also:
timeAsStringShort(long)

timeAsString

public static String timeAsString(long milliSecs)

timeAsString

public static String timeAsString(long milliSecs,
                                  Locale locale)
takes milliseconds and converts them into a localized String using all relevant time measures from millisecs to days


timeAsStringShort

public static String timeAsStringShort(long milliSecs)
takes milliseconds and converts them into a short String of the format
h:mm:ss
. As opposed to SimpleDateFormat, this format will also show the full hours even though there may be more than 24.

See Also:
HourFormat

parse

public static long parse(String s)
                  throws FormatException
assumes the format h:m:s; can parse a String created by timeAsStringShort(long)

Throws:
FormatException
See Also:
HourFormat

timeAsHourString

public static String timeAsHourString(long milliSecs)
formats a String with hours as the largest entity and down to the minute


timeAsHourStringShort

public static String timeAsHourStringShort(long milliSecs)
formats a String with hours as the largest entity in the format h:mm with an 'h' at the end


hourMinuteString

public static String hourMinuteString(long milliSecs)
formats a String with hours as the largest entity in the format h:mm


toString

public String toString()
returns the elapsed time in a convenient format including elapsedAsString() and tells whether the Stopwatch is currently running or not

Overrides:
toString in class Object

getHalted

protected long getHalted()

getEnd

protected long getEnd()


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads