KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > StopWatch


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 import java.io.Serializable JavaDoc;
25
26 /**
27  * Simulates a stop watch with a <em>lap</em> counter.
28  *
29  * @version <tt>$Revision: 1958 $</tt>
30  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
31  */

32 public class StopWatch
33    implements Serializable JavaDoc, Cloneable JavaDoc
34 {
35    /** Total time */
36    protected long total = 0;
37
38    /** Start time */
39    protected long start = -1;
40
41    /** Stop time */
42    protected long stop = -1;
43
44    /** The <i>lap</i> count */
45    protected int count = 0;
46
47    /** Is the watch started */
48    protected boolean running = false;
49
50    /**
51     * Default constructor.
52     */

53    public StopWatch() {}
54
55    /**
56     * Construct a StopWatch.
57     *
58     * @param running Start the watch
59     */

60    public StopWatch(final boolean running)
61    {
62       if (running) start();
63    }
64
65    /**
66     * Start the watch.
67     *
68     * @param reset True to reset the watch prior to starting.
69     */

70    public void start(final boolean reset)
71    {
72       if (!running) {
73          if (reset) reset();
74          start = System.currentTimeMillis();
75          running = true;
76       }
77    }
78
79    /**
80     * Start the watch.
81     */

82    public void start()
83    {
84       start(false);
85    }
86
87    /**
88     * Stop the watch.
89     *
90     * @return Elapsed time or 0 if the watch was never started.
91     */

92    public long stop()
93    {
94       long lap = 0;
95
96       if (running) {
97          count++;
98          stop = System.currentTimeMillis();
99          lap = stop - start;
100          total += lap;
101          running = false;
102       }
103
104       return lap;
105    }
106
107    /**
108     * Reset the watch.
109     */

110    public void reset()
111    {
112       start = -1;
113       stop = -1;
114       total = 0;
115       count = 0;
116       running = false;
117    }
118
119    /**
120     * Get the <i>lap</i> count.
121     *
122     * @return The <i>lap</i> count.
123     */

124    public int getLapCount()
125    {
126       return count;
127    }
128
129    /**
130     * Get the elapsed <i>lap</i> time since the watch was started.
131     *
132     * @return Elapsed <i>lap</i> time or 0 if the watch was never started
133     */

134    public long getLapTime()
135    {
136       if (start == -1) {
137          return 0;
138       }
139       else if (running) {
140          return System.currentTimeMillis() - start;
141       }
142       else {
143          return stop - start;
144       }
145    }
146
147    /**
148     * Get the average <i>lap</i> time since the watch was started.
149     *
150     * @return Average <i>lap</i> time since the watch was started.
151     */

152    public long getAverageLapTime()
153    {
154       return (count == 0) ? 0 : getLapTime() / getLapCount();
155    }
156
157    /**
158     * Get the elapsed time since the watch was created or last reset.
159     *
160     * @return Elapsed time or 0 if the watch was never started.
161     */

162    public long getTime()
163    {
164       if (start == -1) {
165          return 0;
166       }
167       else if (running) {
168          return total + System.currentTimeMillis() - start;
169       }
170       else {
171          return total;
172       }
173    }
174
175    /**
176     * Check if the watch is running.
177     *
178     * @return True if the watch is running.
179     */

180    public boolean isRunning()
181    {
182       return running;
183    }
184
185    /**
186     * Return a string representation.
187     */

188    public String JavaDoc toString()
189    {
190       StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
191       
192       if (running) {
193          // the watch has not been stopped
194
formatElapsedTime(buff, getTime());
195
196          // add the current lap time too if there is more than one lap
197
if (count >= 1) {
198             buff.append(", count=").append(count);
199             buff.append(", current=");
200             formatElapsedTime(buff, getLapTime());
201          }
202       }
203       else {
204          // the watch has been stopped
205
formatElapsedTime(buff, getTime());
206
207          // add extra info if there is more than one lap
208
if (count > 1) {
209             buff.append(", count=").append(count);
210             buff.append(", average=");
211             formatElapsedTime(buff, getAverageLapTime());
212          }
213       }
214
215       return buff.toString();
216    }
217
218    private void formatElapsedTime(final StringBuffer JavaDoc buff, final long lapsed)
219    {
220       long m = lapsed / 60000;
221       if (m != 0) {
222          buff.append(m).append("m:");
223       }
224
225       long s = (lapsed - 60000 * m) / 1000;
226       if (s != 0) {
227          buff.append(s).append("s:");
228       }
229
230       // ms is always there, even if it was 0 too
231
long ms = (lapsed - 60000 * m - 1000 * s);
232       buff.append(ms).append("ms");
233    }
234    
235    /**
236     * Return a cloned copy of this object.
237     *
238     * @return A cloned copy of this object.
239     */

240    public Object JavaDoc clone()
241    {
242       try {
243          return super.clone();
244       }
245       catch (CloneNotSupportedException JavaDoc e) {
246          throw new InternalError JavaDoc();
247       }
248    }
249
250
251    /////////////////////////////////////////////////////////////////////////
252
// Wrappers //
253
/////////////////////////////////////////////////////////////////////////
254

255    /**
256     * Base wrapper class for other wrappers.
257     */

258    private static class Wrapper
259       extends StopWatch
260    {
261       protected StopWatch watch;
262
263       public Wrapper(final StopWatch watch) {
264          this.watch = watch;
265       }
266
267       public void start(final boolean reset) {
268          watch.start(reset);
269       }
270
271       public void start() {
272          watch.start();
273       }
274
275       public long stop() {
276          return watch.stop();
277       }
278
279       public void reset() {
280          watch.reset();
281       }
282
283       public long getLapTime() {
284          return watch.getLapTime();
285       }
286
287       public long getAverageLapTime() {
288          return watch.getAverageLapTime();
289       }
290
291       public int getLapCount() {
292          return watch.getLapCount();
293       }
294
295       public long getTime() {
296          return watch.getTime();
297       }
298
299       public boolean isRunning() {
300          return watch.isRunning();
301       }
302
303       public String JavaDoc toString() {
304          return watch.toString();
305       }
306    }
307
308    /**
309     * Return a synchronized stop watch.
310     *
311     * @param watch StopWatch to synchronize.
312     * @return Synchronized stop watch.
313     */

314    public static StopWatch makeSynchronized(final StopWatch watch)
315    {
316       return new Wrapper(watch)
317          {
318             public synchronized void start(final boolean reset) {
319                this.watch.start(reset);
320             }
321
322             public synchronized void start() {
323                this.watch.start();
324             }
325
326             public synchronized long stop() {
327                return this.watch.stop();
328             }
329
330             public synchronized void reset() {
331                this.watch.reset();
332             }
333
334             public synchronized long getLapTime() {
335                return this.watch.getLapTime();
336             }
337
338             public synchronized long getAverageLapTime() {
339                return this.watch.getAverageLapTime();
340             }
341
342             public synchronized int getLapCount() {
343                return this.watch.getLapCount();
344             }
345
346             public synchronized long getTime() {
347                return this.watch.getTime();
348             }
349
350             public synchronized boolean isRunning() {
351                return this.watch.isRunning();
352             }
353
354             public synchronized String JavaDoc toString() {
355                return this.watch.toString();
356             }
357          };
358    }
359 }
360
Popular Tags