KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > base > StopWatch


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.base;
17
18 /**
19  * Simple StopWatch class for timing stuff.
20  * <p>
21  * Usage to measure an operation: <code>
22  * StopWatch timer = new StopWatch();
23  * <Time consuming operation>
24  * System.out.println( timer ); // outputs the time between the creation of Timer and "now"
25  * </code>
26  * <p>
27  * Usage to use the StopWatch in different methods and classes: <code>
28  * public void test()
29  * {
30  * StopWatch.instance().start();
31  * <time consuming operation>
32  * doOther();
33  * }
34  * public void doOther()
35  * {
36  * <time consuming operation>
37  * System.out.println( StopWatch.instance() ); // outputs the time between the instance().start() and "now"
38  * }
39  * </code>
40  *
41  * @author redsolo
42  */

43 public class StopWatch {
44     private static StopWatch instance = null;
45
46     private long startTime = 0;
47
48     private long stopTime = -1;
49
50     /**
51          * Creates a new instance of StopWatch Starts the timing from the time
52          * the object was created
53          */

54     public StopWatch() {
55     start();
56     }
57
58     /**
59          * Returns a StopWatch instance. This can be used to measure the time
60          * between different methods/classes.
61          *
62          * @return a static StopWatch instance
63          */

64     public static StopWatch instance() {
65     if (instance == null) {
66         instance = new StopWatch();
67     }
68
69     return instance;
70     }
71
72     /**
73          * Starts the watch. Resets the start time and resets the stop time as
74          * well.
75          */

76     public final void start() {
77     startTime = System.currentTimeMillis();
78     stopTime = -1;
79     }
80
81     /**
82          * Stops the watch.
83          *
84          * @return the time passed since the StopWatch was started.
85          */

86     public final long stop() {
87     stopTime = System.currentTimeMillis();
88
89     return (stopTime - startTime);
90     }
91
92     /**
93          * Gets the time (ms) elapsed from the start() method was run until now
94          * OR the stop() method was run. If stop() is executed then this method
95          * will return the same all the time, BUT if the stop() hasnt been
96          * executed this method returns the time (ms) elapsed from the latest
97          * start()
98          *
99          * @return the time since the StopWatch was started; or if it has been
100          * stopped, the time between start() and stop()
101          */

102     public long getTiming() {
103     long time;
104
105     if (stopTime == -1) {
106         time = (System.currentTimeMillis() - startTime);
107     } else {
108         time = (stopTime - startTime);
109     }
110
111     return time;
112     }
113
114     /**
115          * Returns the time elapsed from the start() until now, OR until stop()
116          * was executed
117          *
118          * @return the time (ms) as a string
119          */

120     @Override JavaDoc
121     public String JavaDoc toString() {
122     return String.valueOf(getTiming()) + " ms"; //$NON-NLS-1$
123
}
124 }
125
Popular Tags