Source Code Cross Referenced for Duration.java in  » Database-ORM » openjpa » org » apache » openjpa » persistence » event » common » apps » 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 » Database ORM » openjpa » org.apache.openjpa.persistence.event.common.apps 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements.  See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership.  The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License.  You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied.  See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.    
018:         */
019:        package org.apache.openjpa.persistence.event.common.apps;
020:
021:        import javax.persistence.Entity;
022:
023:        @Entity
024:        /*
025:
026:         Millisecond (only) accuracy timer.
027:
028:         Java 1.4 supposedly has sun.misc.Perf.
029:
030:         Java 1.5 has System.nanoTime (JSR 166)
031:
032:         */
033:        public class Duration
034:
035:        implements  Cloneable {
036:
037:            private String _name;
038:
039:            private boolean _started;
040:
041:            private boolean _running;
042:
043:            private long _startTime; // millis
044:
045:            private long _stopTime; // millis
046:
047:            // NYI clock time of day at start
048:
049:            public Duration(String name) {
050:
051:                _name = name;
052:
053:                _started = false;
054:
055:                _running = false;
056:            }
057:
058:            public String getName() {
059:
060:                return _name;
061:            }
062:
063:            public synchronized void start() {
064:
065:                if (_started) {
066:
067:                    throw new RuntimeException("Duration was already started.");
068:                }
069:
070:                _startTime = System.currentTimeMillis();
071:
072:                _started = true;
073:
074:                _running = true;
075:            }
076:
077:            public synchronized void stop() {
078:
079:                if (!_started) {
080:
081:                    throw new RuntimeException("Duration was never started.");
082:                }
083:
084:                if (!_running) {
085:
086:                    throw new RuntimeException("Duration was already stopped.");
087:                }
088:
089:                _stopTime = System.currentTimeMillis();
090:
091:                _running = false;
092:            }
093:
094:            protected Object clone()
095:
096:            throws CloneNotSupportedException {
097:
098:                return super .clone();
099:            }
100:
101:            /*
102:
103:                Returns a new Duration object from a currently running timer
104:
105:                as a snapshot of this object.
106:
107:                The returned timer is stopped, while this object continue on.
108:
109:             */
110:
111:            public synchronized Duration getCurrentDuration() {
112:
113:                if (!_started) {
114:
115:                    throw new RuntimeException("Duration was never started.");
116:                }
117:
118:                if (!_running) {
119:
120:                    throw new RuntimeException("Duration is not running.");
121:                }
122:
123:                long now = System.currentTimeMillis();
124:
125:                Duration currentDuration;
126:
127:                try {
128:
129:                    currentDuration = (Duration) this .clone();
130:                } catch (Exception e) {
131:
132:                    currentDuration = new Duration("");
133:                }
134:
135:                currentDuration._stopTime = now;
136:
137:                currentDuration._running = false;
138:
139:                return currentDuration;
140:            }
141:
142:            /* Obtain the duration that this timer has run (in seconds)	*/
143:
144:            public synchronized double getDurationAsSeconds() {
145:
146:                if (!_started) {
147:
148:                    throw new RuntimeException("Duration was never started.");
149:                }
150:
151:                if (_running) {
152:
153:                    // snapshot
154:
155:                    Duration snapshot = getCurrentDuration();
156:
157:                    return (1000.0 * (snapshot._stopTime - snapshot._startTime));
158:                }
159:
160:                // Return a double value. Someday this class may make use of
161:
162:                // higher precision timing services (e.g. java 1.5)
163:
164:                return ((_stopTime - _startTime) / (double) 1000.0);
165:            }
166:
167:            public synchronized boolean isRunning() {
168:
169:                return _running;
170:            }
171:
172:            public synchronized boolean wasStarted() {
173:
174:                return _started;
175:            }
176:
177:            public String toString() {
178:
179:                double time = 0.0;
180:
181:                StringBuffer buf = new StringBuffer(256);
182:
183:                if (wasStarted()) {
184:
185:                    if (isRunning()) {
186:
187:                        Duration snapshot = getCurrentDuration();
188:
189:                        time = snapshot.getDurationAsSeconds();
190:                    } else {
191:
192:                        time = getDurationAsSeconds();
193:                    }
194:
195:                    buf.append("Duration for '" + _name + "' is " + time
196:                            + " (s).");
197:                } else {
198:
199:                    buf.append("Duration for '" + _name +
200:
201:                    "' has not yet been started.");
202:                }
203:
204:                return buf.toString();
205:            }
206:
207:            /* Example usage:
208:
209:             public static void main (String[] args)
210:
211:             throws Exception
212:
213:             {
214:
215:             Duration test = new Duration ("hello, count to 1 million");
216:
217:             System.out.println (test);
218:
219:             test.start ();
220:
221:             for (int i = 0; i < 1000000000; i++)
222:
223:             {
224:
225:             }
226:
227:             test.stop ();
228:
229:             System.out.println (test);
230:
231:             }
232:
233:             */
234:        }
w_w_w___.___j___a_v___a_2_s._c_om__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.