KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > lib > StopWatch


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.lib;
33
34 /**
35  * Provides the programatic analog of a physical stop watch. <p>
36  *
37  * The watch can be started, stopped and zeroed and can be queried for
38  * elapsed running time. The watch accumulates elapsed time over starts
39  * and stops such that only the time actually spent running is recorded.
40  * If the watch is zeroed, then the accumulated time is discarded and
41  * the watch starts again with zero acumulated time. <p>
42  *
43  * @author boucherb@users
44  * @version 1.7.2
45  * @since 1.7.2
46  */

47 public class StopWatch {
48
49     /**
50      * The last time this object made the transition
51      * from stopped to running state, as reported
52      * by System.currentTimeMillis().
53      */

54     private long startTime;
55     private long lastStart;
56
57     /**
58      * The accumulated running time of this object since
59      * it was last zeroed.
60      */

61     private long total;
62
63     /** Flags if this object is started or stopped. */
64     boolean running = false;
65
66     /** Creates, zeros, and starts a new StopWatch */
67     public StopWatch() {
68         this(true);
69     }
70
71     /** Creates, zeros, and starts a new StopWatch */
72     public StopWatch(boolean start) {
73
74         if (start) {
75             start();
76         }
77     }
78
79     /**
80      * Retrieves the accumulated time this object has spent running since
81      * it was last zeroed.
82      * @return the accumulated time this object has spent running since
83      * it was last zeroed.
84      */

85     public long elapsedTime() {
86
87         if (running) {
88             return total + System.currentTimeMillis() - startTime;
89         } else {
90             return total;
91         }
92     }
93
94     /**
95      * Retrieves the accumulated time this object has spent running since
96      * it was last started.
97      * @return the accumulated time this object has spent running since
98      * it was last started.
99      */

100     public long currentElapsedTime() {
101
102         if (running) {
103             return System.currentTimeMillis() - startTime;
104         } else {
105             return 0;
106         }
107     }
108
109     /** Zeros accumulated running time and restarts this object. */
110     public void zero() {
111
112         total = 0;
113
114         start();
115     }
116
117     /**
118      * Ensures that this object is in the running state. If this object is not
119      * running, then the call has the effect of setting the <code>startTime</code>
120      * attribute to the current value of System.currentTimeMillis() and setting
121      * the <code>running</code> attribute to <code>true</code>.
122      */

123     public void start() {
124         startTime = System.currentTimeMillis();
125         running = true;
126     }
127
128     /**
129      * Ensures that this object is in the stopped state. If this object is
130      * in the running state, then this has the effect of adding to the
131      * <code>total</code> attribute the elapsed time since the last transition
132      * from stopped to running state and sets the <code>running</code> attribute
133      * to false. If this object is not in the running state, this call has no
134      * effect.
135      */

136     public void stop() {
137
138         if (running) {
139             total += System.currentTimeMillis() - startTime;
140             running = false;
141         }
142     }
143
144     public void mark() {
145         stop();
146         start();
147     }
148
149     /**
150      * Retrieves prefix + " in " + elapsedTime() + " ms."
151      * @param prefix The string to use as a prefix
152      * @return prefix + " in " + elapsedTime() + " ms."
153      */

154     public String JavaDoc elapsedTimeToMessage(String JavaDoc prefix) {
155         return prefix + " in " + elapsedTime() + " ms.";
156     }
157
158     /**
159      * Retrieves prefix + " in " + elapsedTime() + " ms."
160      * @param prefix The string to use as a prefix
161      * @return prefix + " in " + elapsedTime() + " ms."
162      */

163     public String JavaDoc currentElapsedTimeToMessage(String JavaDoc prefix) {
164         return prefix + " in " + currentElapsedTime() + " ms.";
165     }
166
167     /**
168      * Retrieves the internal state of this object, as a String.
169      *
170      * The retreived value is:
171      *
172      * <pre>
173      * super.toString() +
174      * "[running=" +
175      * running +
176      * ", startTime=" +
177      * startTime +
178      * ", total=" +
179      * total + "]";
180      * </pre>
181      * @return the state of this object, as a String
182      */

183     public String JavaDoc toString() {
184         return super.toString() + "[running=" + running + ", startTime="
185                + startTime + ", total=" + total + "]";
186     }
187 }
188
Popular Tags