Source Code Cross Referenced for StopWatch.java in  » Report » datavision-1.1.0 » jimm » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » datavision 1.1.0 » jimm.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package jimm.util;
002:
003:        import java.io.PrintWriter;
004:
005:        /**
006:         * Prints time durations; used for development purposes only.
007:         * <p>
008:         * No threads or system resources are harmed in the making of a stop watch. A
009:         * stop watch simply remembers the start time (and elapsed time when paused)
010:         * and prints the total &quot;running&quot; time when either {@link #mark} or
011:         * {@link #stop} is called.
012:         * <p>
013:         * {@link #stop} doesn't really stop anything. You can call stop as many times
014:         * as you like and it will print the time elapsed since start. It would be
015:         * easy to change this behavior, but I haven't needed to.
016:         *
017:         * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
018:         */
019:        public class StopWatch {
020:
021:            protected String name;
022:            protected long t0;
023:            protected long elapsedTime;
024:            protected PrintWriter out;
025:
026:            public StopWatch() {
027:                this (null, null);
028:            }
029:
030:            public StopWatch(String name) {
031:                this (name, null);
032:            }
033:
034:            public StopWatch(PrintWriter out) {
035:                this (null, out);
036:            }
037:
038:            public StopWatch(String name, PrintWriter out) {
039:                this .name = name;
040:                if (out == null)
041:                    this .out = new PrintWriter(System.err);
042:                else
043:                    this .out = out;
044:                elapsedTime = -1L; // So we can tell if we are ever started
045:            }
046:
047:            /**
048:             * Remembers the current time and prints a message.
049:             */
050:            public void start() {
051:                start(true);
052:            }
053:
054:            /**
055:             * Remembers the current time and prints a message if requested.
056:             *
057:             * @param printStarting if <code>true</code> and this stop watch has a
058:             * name, print a message
059:             */
060:            public void start(boolean printStarting) {
061:                if (t0 != 0)
062:                    System.err
063:                            .println("(warning: StopWatch already started; resetting)");
064:                if (printStarting && name != null)
065:                    System.err.println("starting " + name);
066:                elapsedTime = 0;
067:                t0 = System.currentTimeMillis();
068:            }
069:
070:            /**
071:             * Pauses the stop watch.
072:             */
073:            public void pause() {
074:                long now = System.currentTimeMillis();
075:                elapsedTime += now - t0;
076:                t0 = 0;
077:            }
078:
079:            /**
080:             * Resumes the stop watch.
081:             */
082:            public void resume() {
083:                t0 = System.currentTimeMillis();
084:            }
085:
086:            /**
087:             * Prints the current elapsed time without stopping.
088:             */
089:            public void mark() {
090:                stop(null, true);
091:            }
092:
093:            /**
094:             * Prints the current elapsed time without stopping, along with the
095:             * stop watch name if <var>printMark</var> is <code>true</code>.
096:             *
097:             * @param printMark if <code>true</code>, the stop watch name will
098:             * be printed
099:             */
100:            public void mark(boolean printMark) {
101:                stop(null, printMark);
102:            }
103:
104:            /**
105:             * Prints the current elapsed time without stopping, along with the
106:             * stop watch name and <var>msg</var>.
107:             *
108:             * @param msg a message to print
109:             */
110:            public void mark(String msg) {
111:                stop(msg, true);
112:            }
113:
114:            /**
115:             * Prints the current elapsed time without stopping, along with, along with
116:             * the stop watch name if <var>printMark</var> is <code>true</code> and the
117:             * <var>msg</var> if it's not <code>null</code>.
118:             *
119:             * @param msg a message to print
120:             * @param printMark if <code>true</code>, the stop watch name will
121:             * be printed
122:             */
123:            public void mark(String msg, boolean printMark) {
124:                stop(msg, printMark);
125:            }
126:
127:            /**
128:             * Stops the stop watch and prints the name of this stop watch and the current
129:             * elapsed time.
130:             */
131:            public void stop() {
132:                stop(null, true);
133:            }
134:
135:            /**
136:             * Stops the stop watch and prints the name of this stop watch,
137:             * <var>msg</var> if non-<code>null</code>, and the current elapsed time.
138:             *
139:             * @param msg a message to print; may be <code>null</code>
140:             */
141:            public void stop(String msg) {
142:                stop(msg, true);
143:            }
144:
145:            /**
146:             * Prints the current elapsed time, along with the stop watch name if
147:             * <var>printMark</var> is <code>true</code> and the <var>msg</var> if it's
148:             * not <code>null</code>.
149:             *
150:             * @param msg a message to print; may be <code>null</code>
151:             * @param printName if <code>true</code>, the stop watch name will
152:             * be printed
153:             */
154:            public void stop(String msg, boolean printName) {
155:                long now = System.currentTimeMillis();
156:
157:                if (elapsedTime == -1) {
158:                    System.err.println("(StopWatch"
159:                            + (name != null ? (" \"" + name + '"') : "")
160:                            + " was stopped without ever being started)");
161:                    return;
162:                }
163:
164:                long total = elapsedTime;
165:                if (t0 != 0)
166:                    total += now - t0;
167:
168:                String separator = null;
169:                if (printName && name != null) {
170:                    System.err.print(name);
171:                    separator = ": ";
172:                }
173:                if (msg != null) {
174:                    if (separator != null)
175:                        System.err.print(' ');
176:                    System.err.print("(" + msg + ")");
177:                    separator = ": ";
178:                }
179:                if (separator != null)
180:                    System.err.print(separator);
181:
182:                System.err.println("" + (total / 1000.0) + " seconds");
183:            }
184:
185:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.