KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > time > StopWatch


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.time;
17
18 /**
19  * <p><code>StopWatch</code> provides a convenient API for timings.</p>
20  *
21  * <p>To start the watch, call {@link #start()}. At this point you can:</p>
22  * <ul>
23  * <li>{@link #split()} the watch to get the time whilst the watch continues in the
24  * background. {@link #unsplit()} will remove the effect of the split. At this point,
25  * these three options are available again.</li>
26  * <li>{@link #suspend()} the watch to pause it. {@link #resume()} allows the watch
27  * to continue. Any time between the suspend and resume will not be counted in
28  * the total. At this point, these three options are available again.</li>
29  * <li>{@link #stop()} the watch to complete the timing session.</li>
30  * </ul>
31  *
32  * <p>It is intended that the output methods {@link #toString()} and {@link #getTime()}
33  * should only be called after stop, split or suspend, however a suitable result will
34  * be returned at other points.</p>
35  *
36  * <p>NOTE: As from v2.1, the methods protect against inappropriate calls.
37  * Thus you cannot now call stop before start, resume before suspend or
38  * unsplit before split.</p>
39  *
40  * <p>1. split(), suspend(), or stop() cannot be invoked twice<br />
41  * 2. unsplit() may only be called if the watch has been split()<br />
42  * 3. resume() may only be called if the watch has been suspend()<br />
43  * 4. start() cannot be called twice without calling reset()</p>
44  *
45  * @author Henri Yandell
46  * @author Stephen Colebourne
47  * @since 2.0
48  * @version $Id: StopWatch.java 189443 2005-06-07 21:28:07Z scolebourne $
49  */

50 public class StopWatch {
51
52     // running states
53
private static final int STATE_UNSTARTED = 0;
54     private static final int STATE_RUNNING = 1;
55     private static final int STATE_STOPPED = 2;
56     private static final int STATE_SUSPENDED = 3;
57
58     // split state
59
private static final int STATE_UNSPLIT = 10;
60     private static final int STATE_SPLIT = 11;
61
62     /**
63      * The current running state of the StopWatch.
64      */

65     private int runningState = STATE_UNSTARTED;
66
67     /**
68      * Whether the stopwatch has a split time recorded.
69      */

70     private int splitState = STATE_UNSPLIT;
71
72     /**
73      * The start time.
74      */

75     private long startTime = -1;
76     /**
77      * The stop time.
78      */

79     private long stopTime = -1;
80
81     /**
82      * <p>Constructor.</p>
83      */

84     public StopWatch() {
85     }
86
87     /**
88      * <p>Start the stopwatch.</p>
89      *
90      * <p>This method starts a new timing session, clearing any previous values.</p>
91      *
92      * @throws IllegalStateException if the StopWatch is already running.
93      */

94     public void start() {
95         if(this.runningState == STATE_STOPPED) {
96             throw new IllegalStateException JavaDoc("Stopwatch must be reset before being restarted. ");
97         }
98         if(this.runningState != STATE_UNSTARTED) {
99             throw new IllegalStateException JavaDoc("Stopwatch already started. ");
100         }
101         stopTime = -1;
102         startTime = System.currentTimeMillis();
103         this.runningState = STATE_RUNNING;
104     }
105
106     /**
107      * <p>Stop the stopwatch.</p>
108      *
109      * <p>This method ends a new timing session, allowing the time to be retrieved.</p>
110      *
111      * @throws IllegalStateException if the StopWatch is not running.
112      */

113     public void stop() {
114         if(this.runningState != STATE_RUNNING && this.runningState != STATE_SUSPENDED) {
115             throw new IllegalStateException JavaDoc("Stopwatch is not running. ");
116         }
117         stopTime = System.currentTimeMillis();
118         this.runningState = STATE_STOPPED;
119     }
120
121     /**
122      * <p>Resets the stopwatch. Stops it if need be. </p>
123      *
124      * <p>This method clears the internal values to allow the object to be reused.</p>
125      */

126     public void reset() {
127         this.runningState = STATE_UNSTARTED;
128         this.splitState = STATE_UNSPLIT;
129         startTime = -1;
130         stopTime = -1;
131     }
132
133     /**
134      * <p>Split the time.</p>
135      *
136      * <p>This method sets the stop time of the watch to allow a time to be extracted.
137      * The start time is unaffected, enabling {@link #unsplit()} to continue the
138      * timing from the original start point.</p>
139      *
140      * @throws IllegalStateException if the StopWatch is not running.
141      */

142     public void split() {
143         if(this.runningState != STATE_RUNNING) {
144             throw new IllegalStateException JavaDoc("Stopwatch is not running. ");
145         }
146         stopTime = System.currentTimeMillis();
147         this.splitState = STATE_SPLIT;
148     }
149
150     /**
151      * <p>Remove a split.</p>
152      *
153      * <p>This method clears the stop time. The start time is unaffected, enabling
154      * timing from the original start point to continue.</p>
155      *
156      * @throws IllegalStateException if the StopWatch has not been split.
157      */

158     public void unsplit() {
159         if(this.splitState != STATE_SPLIT) {
160             throw new IllegalStateException JavaDoc("Stopwatch has not been split. ");
161         }
162         stopTime = -1;
163         this.splitState = STATE_UNSPLIT;
164     }
165
166     /**
167      * <p>Suspend the stopwatch for later resumption.</p>
168      *
169      * <p>This method suspends the watch until it is resumed. The watch will not include
170      * time between the suspend and resume calls in the total time.</p>
171      *
172      * @throws IllegalStateException if the StopWatch is not currently running.
173      */

174     public void suspend() {
175         if(this.runningState != STATE_RUNNING) {
176             throw new IllegalStateException JavaDoc("Stopwatch must be running to suspend. ");
177         }
178         stopTime = System.currentTimeMillis();
179         this.runningState = STATE_SUSPENDED;
180     }
181
182     /**
183      * <p>Resume the stopwatch after a suspend.</p>
184      *
185      * <p>This method resumes the watch after it was suspended. The watch will not include
186      * time between the suspend and resume calls in the total time.</p>
187      *
188      * @throws IllegalStateException if the StopWatch has not been suspended.
189      */

190     public void resume() {
191         if(this.runningState != STATE_SUSPENDED) {
192             throw new IllegalStateException JavaDoc("Stopwatch must be suspended to resume. ");
193         }
194         startTime += (System.currentTimeMillis() - stopTime);
195         stopTime = -1;
196         this.runningState = STATE_RUNNING;
197     }
198
199     /**
200      * <p>Get the time on the stopwatch.</p>
201      *
202      * <p>This is either the time between the start and the moment this method
203      * is called, or the amount of time between start and stop.</p>
204      *
205      * @return the time in milliseconds
206      */

207     public long getTime() {
208         if(this.runningState == STATE_STOPPED || this.runningState == STATE_SUSPENDED) {
209             return this.stopTime - this.startTime;
210         } else
211         if(this.runningState == STATE_UNSTARTED) {
212             return 0;
213         } else
214         if(this.runningState == STATE_RUNNING) {
215             return System.currentTimeMillis() - this.startTime;
216         }
217         throw new RuntimeException JavaDoc("Illegal running state has occured. ");
218     }
219
220     /**
221      * <p>Get the split time on the stopwatch.</p>
222      *
223      * <p>This is the time between start and latest split. </p>
224      *
225      * @return the split time in milliseconds
226      *
227      * @throws IllegalStateException if the StopWatch has not yet been split.
228      * @since 2.1
229      */

230     public long getSplitTime() {
231         if(this.splitState != STATE_SPLIT) {
232             throw new IllegalStateException JavaDoc("Stopwatch must be split to get the split time. ");
233         }
234         return this.stopTime - this.startTime;
235     }
236
237     /**
238      * <p>Gets a summary of the time that the stopwatch recorded as a string.</p>
239      *
240      * <p>The format used is ISO8601-like,
241      * <i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.</p>
242      *
243      * @return the time as a String
244      */

245     public String JavaDoc toString() {
246         return DurationFormatUtils.formatDurationHMS(getTime());
247     }
248
249     /**
250      * <p>Gets a summary of the split time that the stopwatch recorded as a string.</p>
251      *
252      * <p>The format used is ISO8601-like,
253      * <i>hours</i>:<i>minutes</i>:<i>seconds</i>.<i>milliseconds</i>.</p>
254      *
255      * @return the split time as a String
256      * @since 2.1
257      */

258     public String JavaDoc toSplitString() {
259         return DurationFormatUtils.formatDurationHMS(getSplitTime());
260     }
261
262 }
263
Popular Tags