01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.jorphan.timer;
20:
21: /**
22: * @author <a href="mailto:jeremy_a@bigfoot.com">Jeremy Arnold</a>
23: * @version $Revision: 493784 $
24: */
25: public abstract class AbstractTimer implements ITimer, ITimerConstants {
26: /** Used to keep track of timer state. */
27: private int m_state;
28:
29: /** Timing data. */
30: private double m_data;
31:
32: /*
33: * (non-Javadoc)
34: *
35: * @see org.apache.jorphan.timer.ITimer#start()
36: */
37: public void start() {
38: if (m_state != STATE_READY) {
39: throw new IllegalStateException(this
40: + ": start() must be called from READY state, "
41: + "current state is " + STATE_NAMES[m_state]);
42: }
43:
44: m_state = STATE_STARTED;
45: m_data = getCurrentTime();
46: }
47:
48: /*
49: * (non-Javadoc)
50: *
51: * @see org.apache.jorphan.timer.ITimer#stop()
52: */
53: public void stop() {
54: // Latch stop time in a local var before doing anything else
55: final double data = getCurrentTime();
56:
57: if (m_state != STATE_STARTED) {
58: throw new IllegalStateException(this
59: + ": stop() must be called from STARTED state, "
60: + "current state is " + STATE_NAMES[m_state]);
61: }
62:
63: m_data = data - m_data;
64: m_state = STATE_STOPPED;
65: }
66:
67: /*
68: * (non-Javadoc)
69: *
70: * @see org.apache.jorphan.timer.ITimer#getDuration()
71: */
72: public double getDuration() {
73: if (m_state != STATE_STOPPED) {
74: throw new IllegalStateException(
75: this
76: + ": getDuration() must be called from STOPPED state, "
77: + "current state is "
78: + STATE_NAMES[m_state]);
79: }
80: return m_data;
81: }
82:
83: /*
84: * (non-Javadoc)
85: *
86: * @see org.apache.jorphan.timer.ITimer#reset()
87: */
88: public void reset() {
89: m_state = STATE_READY;
90: }
91:
92: protected abstract double getCurrentTime();
93: }
|