KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > misc > Stopwatch


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.misc;
12
13 import java.util.Map JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 /**
17  * A Stopwatch is used to measure the time elapsed during
18  * an operation. To do this create a stopwatch before the
19  * operation is executed and call stop() when the operation
20  * afterwards. The elapsed time will be printed.
21  *
22  * A Stopwatch can also be used to measure an interval time. To
23  * do this create a stopwatch and call printInterval after
24  * every important interval. The resetInterval method can also
25  * be used to start a new interval.
26  *
27  * A Stopwatch can also be registered for global access. To
28  * do this create a stopwatch and call register. From this point
29  * on a handle to the stopwatch can be retrieved by calling
30  * Stopwatch.getStopwatch("name"). The stopwatch should be
31  * unregistered when no longer needed.
32  */

33 public class Stopwatch {
34     private long startTime;
35     private long lastTime;
36     private String JavaDoc name;
37     private static Map JavaDoc registry;
38 /**
39  * Construct a new Stopwatch and start it.
40  * To reset the watch at a later time just call start() again.
41  */

42 public Stopwatch(String JavaDoc name) {
43     this.name = name;
44     start();
45 }
46 /**
47  * Get a stopwatch from the registry.
48  */

49 static public Stopwatch getStopwatch(String JavaDoc name) {
50     if (registry != null)
51         return (Stopwatch)registry.get(name);
52     else
53         return null;
54 }
55 /**
56  * Print the elapsed time since start(), printInterval(), or
57  * resetInterval() was last called.
58  */

59 public void printInterval(String JavaDoc hint) {
60     long time = System.currentTimeMillis() - lastTime;
61     System.out.println(name + " '" + hint + "' took " + time + " ms");//$NON-NLS-3$//$NON-NLS-2$//$NON-NLS-1$
62
lastTime = System.currentTimeMillis();
63 }
64 /**
65  * Print the current elapsed time.
66  */

67 public void printTime() {
68     long time = System.currentTimeMillis() - startTime;
69     System.out.print(name + " is now " + time + " ms");//$NON-NLS-2$//$NON-NLS-1$
70
}
71 /**
72  * Add this stopwatch to the registry.
73  */

74 public void register() {
75     if (registry == null)
76         registry = new HashMap JavaDoc(2);
77     registry.put(name, this);
78 }
79 /**
80  * Reset the interval timer.
81  */

82 public void resetInterval() {
83     lastTime = System.currentTimeMillis();
84 }
85 /**
86  * Start the watch.
87  */

88 public void start() {
89     startTime = lastTime = System.currentTimeMillis();
90     System.out.println(name + " started");//$NON-NLS-1$
91
}
92 /**
93  * Stop the watch and print the elapsed time.
94  */

95 public void stop() {
96     long time = System.currentTimeMillis() - startTime;
97     System.out.println(name + " finished in " + time + " ms");//$NON-NLS-2$//$NON-NLS-1$
98
}
99 /**
100  * Remove this stopwatch from the registry.
101  */

102 public void unregister() {
103     if (registry != null)
104         registry.remove(name);
105 }
106 }
107
Popular Tags