KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > util > StopWatch


1 package jimm.util;
2 import java.io.PrintWriter JavaDoc;
3
4 /**
5  * Prints time durations; used for development purposes only.
6  * <p>
7  * No threads or system resources are harmed in the making of a stop watch. A
8  * stop watch simply remembers the start time (and elapsed time when paused)
9  * and prints the total &quot;running&quot; time when either {@link #mark} or
10  * {@link #stop} is called.
11  * <p>
12  * {@link #stop} doesn't really stop anything. You can call stop as many times
13  * as you like and it will print the time elapsed since start. It would be
14  * easy to change this behavior, but I haven't needed to.
15  *
16  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
17  */

18 public class StopWatch {
19
20 protected String JavaDoc name;
21 protected long t0;
22 protected long elapsedTime;
23 protected PrintWriter JavaDoc out;
24
25 public StopWatch() {
26     this(null, null);
27 }
28
29 public StopWatch(String JavaDoc name) {
30     this(name, null);
31 }
32
33 public StopWatch(PrintWriter JavaDoc out) {
34     this(null, out);
35 }
36
37 public StopWatch(String JavaDoc name, PrintWriter JavaDoc out) {
38     this.name = name;
39     if (out == null)
40     this.out = new PrintWriter JavaDoc(System.err);
41     else
42     this.out = out;
43     elapsedTime = -1L; // So we can tell if we are ever started
44
}
45
46 /**
47  * Remembers the current time and prints a message.
48  */

49 public void start() {
50     start(true);
51 }
52
53 /**
54  * Remembers the current time and prints a message if requested.
55  *
56  * @param printStarting if <code>true</code> and this stop watch has a
57  * name, print a message
58  */

59 public void start(boolean printStarting) {
60     if (t0 != 0)
61     System.err.println("(warning: StopWatch already started; resetting)");
62     if (printStarting && name != null)
63     System.err.println("starting " + name);
64     elapsedTime = 0;
65     t0 = System.currentTimeMillis();
66 }
67
68 /**
69  * Pauses the stop watch.
70  */

71 public void pause() {
72     long now = System.currentTimeMillis();
73     elapsedTime += now - t0;
74     t0 = 0;
75 }
76
77 /**
78  * Resumes the stop watch.
79  */

80 public void resume() {
81     t0 = System.currentTimeMillis();
82 }
83
84 /**
85  * Prints the current elapsed time without stopping.
86  */

87 public void mark() {
88     stop(null, true);
89 }
90
91 /**
92  * Prints the current elapsed time without stopping, along with the
93  * stop watch name if <var>printMark</var> is <code>true</code>.
94  *
95  * @param printMark if <code>true</code>, the stop watch name will
96  * be printed
97  */

98 public void mark(boolean printMark) {
99     stop(null, printMark);
100 }
101
102 /**
103  * Prints the current elapsed time without stopping, along with the
104  * stop watch name and <var>msg</var>.
105  *
106  * @param msg a message to print
107  */

108 public void mark(String JavaDoc msg) {
109     stop(msg, true);
110 }
111
112 /**
113  * Prints the current elapsed time without stopping, along with, along with
114  * the stop watch name if <var>printMark</var> is <code>true</code> and the
115  * <var>msg</var> if it's not <code>null</code>.
116  *
117  * @param msg a message to print
118  * @param printMark if <code>true</code>, the stop watch name will
119  * be printed
120  */

121 public void mark(String JavaDoc msg, boolean printMark) {
122     stop(msg, printMark);
123 }
124
125 /**
126  * Stops the stop watch and prints the name of this stop watch and the current
127  * elapsed time.
128  */

129 public void stop() {
130     stop(null, true);
131 }
132
133 /**
134  * Stops the stop watch and prints the name of this stop watch,
135  * <var>msg</var> if non-<code>null</code>, and the current elapsed time.
136  *
137  * @param msg a message to print; may be <code>null</code>
138  */

139 public void stop(String JavaDoc msg) {
140     stop(msg, true);
141 }
142
143 /**
144  * Prints the current elapsed time, along with the stop watch name if
145  * <var>printMark</var> is <code>true</code> and the <var>msg</var> if it's
146  * not <code>null</code>.
147  *
148  * @param msg a message to print; may be <code>null</code>
149  * @param printName if <code>true</code>, the stop watch name will
150  * be printed
151  */

152 public void stop(String JavaDoc msg, boolean printName) {
153     long now = System.currentTimeMillis();
154
155     if (elapsedTime == -1) {
156     System.err.println("(StopWatch"
157                + (name != null ? (" \"" + name + '"') : "")
158                + " was stopped without ever being started)");
159     return;
160     }
161
162     long total = elapsedTime;
163     if (t0 != 0)
164     total += now - t0;
165
166     String JavaDoc separator = null;
167     if (printName && name != null) {
168     System.err.print(name);
169     separator = ": ";
170     }
171     if (msg != null) {
172     if (separator != null)
173         System.err.print(' ');
174     System.err.print("(" + msg + ")");
175     separator = ": ";
176     }
177     if (separator != null)
178     System.err.print(separator);
179
180     System.err.println("" + (total / 1000.0) + " seconds");
181 }
182
183 }
184
Popular Tags