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: }
|