KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > timer > AbstractTimer


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/timer/AbstractTimer.java,v 1.2 2004/02/11 23:57:23 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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: 1.2 $
24  */

25 public abstract class AbstractTimer implements ITimer, ITimerConstants
26 {
27     /** Used to keep track of timer state. */
28     private int m_state;
29     
30     /** Timing data. */
31     private double m_data;
32
33
34     /* (non-Javadoc)
35      * @see org.apache.jorphan.timer.ITimer#start()
36      */

37     public void start()
38     {
39         if (m_state != STATE_READY)
40         {
41             throw new IllegalStateException JavaDoc(
42                 this
43                     + ": start() must be called from READY state, "
44                     + "current state is "
45                     + STATE_NAMES[m_state]);
46         }
47         
48         m_state = STATE_STARTED;
49         m_data = getCurrentTime();
50     }
51
52     /* (non-Javadoc)
53      * @see org.apache.jorphan.timer.ITimer#stop()
54      */

55     public void stop()
56     {
57         // Latch stop time in a local var before doing anything else
58
final double data = getCurrentTime();
59         
60         if (m_state != STATE_STARTED)
61         {
62             throw new IllegalStateException JavaDoc(
63                 this
64                     + ": stop() must be called from STARTED state, "
65                     + "current state is "
66                     + STATE_NAMES[m_state]);
67         }
68         
69         m_data = data - m_data;
70         m_state = STATE_STOPPED;
71     }
72
73     /* (non-Javadoc)
74      * @see org.apache.jorphan.timer.ITimer#getDuration()
75      */

76     public double getDuration()
77     {
78         if (m_state != STATE_STOPPED)
79         {
80             throw new IllegalStateException JavaDoc(
81                 this
82                     + ": getDuration() must be called from STOPPED state, "
83                     + "current state is "
84                     + STATE_NAMES[m_state]);
85         }
86         return m_data;
87     }
88
89     /* (non-Javadoc)
90      * @see org.apache.jorphan.timer.ITimer#reset()
91      */

92     public void reset()
93     {
94         m_state = STATE_READY;
95     }
96     
97     protected abstract double getCurrentTime();
98 }
99
Popular Tags