jodd.datetime
Class JStopWatch

java.lang.Object
  extended by jodd.datetime.JStopWatch

public class JStopWatch
extends java.lang.Object

Nice thread-aware stopwatch that supports time spans, cumulative times and laps. Useful for all kind of profiling, time measurements etc.


Field Summary
protected  java.util.List<long[]> laps
          List of all laps.
protected  java.lang.String name
          Optional stopwatch name.
protected  boolean running
          Running flag.
protected  long spanTime
          Last elapsed time.
protected  long startTime
          Last start time.
protected  long stopTime
          Last stop time.
protected  long totalTime
          Cumulative elapsed time.
 
Constructor Summary
JStopWatch()
          Starts the stopwatch.
JStopWatch(boolean putInThread)
           
JStopWatch(java.lang.String name)
          Starts the named stopwatch.
JStopWatch(java.lang.String name, boolean assignToCurrentThread)
          Starts the stopwatch.
 
Method Summary
protected  void assignToCurrentThread()
          Puts current stopwatch in current thread.
 long elapsed()
          Returns total elapsed time from the start() in ms.
static java.lang.String formatTimeSpan(long millis)
          Formats time spans.
 long[] getLapTimes(int index)
          Returns lap times for 1-based lap index.
 java.lang.String getName()
          Returns stopwatch name.
static JStopWatch getThreadStopWatch()
          Returns thread-assigned stop watch.
 boolean isRunning()
          Returns true if stopwatch is running.
 long lap()
          Marks a lap and returns its length.
protected  long lap(long lap)
           
 long restart()
          Restarts the stopwatch.
 long span()
          Stops the stopwatch and returns total time span for last start-stop sequence.
 long start()
          Starts the stopwatch.
 long stop()
          Stops the stopwatch if running.
 java.lang.String toString()
          Returns total elapsed time as formatted string from the last start.
 long total()
          Stops the stopwatch and returns total cumulative time in ms.
 int totalLaps()
          Returns the total number of laps up to this moment.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

protected java.lang.String name
Optional stopwatch name.


startTime

protected long startTime
Last start time.


stopTime

protected long stopTime
Last stop time.


spanTime

protected long spanTime
Last elapsed time.


totalTime

protected long totalTime
Cumulative elapsed time.


running

protected boolean running
Running flag.


laps

protected java.util.List<long[]> laps
List of all laps. Contains long arrays in following format:

Constructor Detail

JStopWatch

public JStopWatch()
Starts the stopwatch.


JStopWatch

public JStopWatch(boolean putInThread)

JStopWatch

public JStopWatch(java.lang.String name)
Starts the named stopwatch.


JStopWatch

public JStopWatch(java.lang.String name,
                  boolean assignToCurrentThread)
Starts the stopwatch.

Method Detail

assignToCurrentThread

protected void assignToCurrentThread()
Puts current stopwatch in current thread.


getThreadStopWatch

public static JStopWatch getThreadStopWatch()
Returns thread-assigned stop watch.


getName

public java.lang.String getName()
Returns stopwatch name.


isRunning

public boolean isRunning()
Returns true if stopwatch is running.


start

public long start()
Starts the stopwatch. stop() must be called prior to restart. Returns starting time in milliseconds.


restart

public long restart()
Restarts the stopwatch.


stop

public long stop()
Stops the stopwatch if running. Returns span time. If laps are used, marks the last lap.


elapsed

public long elapsed()
Returns total elapsed time from the start() in ms.


total

public long total()
Stops the stopwatch and returns total cumulative time in ms.


span

public long span()
Stops the stopwatch and returns total time span for last start-stop sequence.


lap

public long lap()
Marks a lap and returns its length. May be called only while stop watch is running.


lap

protected long lap(long lap)

totalLaps

public int totalLaps()
Returns the total number of laps up to this moment.


getLapTimes

public long[] getLapTimes(int index)
Returns lap times for 1-based lap index. Returns null if laps are not used or if index is invalid.


toString

public java.lang.String toString()
Returns total elapsed time as formatted string from the last start.

Overrides:
toString in class java.lang.Object

formatTimeSpan

public static java.lang.String formatTimeSpan(long millis)
Formats time spans.



Copyright © 2003-2012 Jodd Team