KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > manager > impl > AbstractValueInstrumentSample


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.manager.impl;
21
22 import java.io.PrintWriter JavaDoc;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26
27 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
28 import org.apache.excalibur.instrument.manager.ValueInstrumentListener;
29
30 /**
31  * An AbstractValueInstrumentSample contains all of the functionality common
32  * to all InstrumentSamples which represent a fixed value.
33  *
34  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
35  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:25 $
36  * @since 4.1
37  */

38 abstract class AbstractValueInstrumentSample
39     extends AbstractInstrumentSample
40     implements ValueInstrumentListener
41 {
42     /** The sample value. */
43     protected int m_value;
44     
45     /** The number of times that the value has been changed in this sample period. */
46     protected int m_valueCount;
47     
48     /** Last value set to the sample for use for sample periods where no value is set. */
49     protected int m_lastValue;
50     
51     /*---------------------------------------------------------------
52      * Constructors
53      *-------------------------------------------------------------*/

54     /**
55      * Creates a new AbstractValueInstrumentSample
56      *
57      * @param instrumentProxy The InstrumentProxy which owns the
58      * InstrumentSample.
59      * @param name The name of the new InstrumentSample.
60      * @param interval The sample interval of the new InstrumentSample.
61      * @param size The number of samples to store as history. Assumes that size is at least 1.
62      * @param description The description of the new InstrumentSample.
63      * @param lease The length of the lease in milliseconds.
64      */

65     protected AbstractValueInstrumentSample( InstrumentProxy instrumentProxy,
66                                              String JavaDoc name,
67                                              long interval,
68                                              int size,
69                                              String JavaDoc description,
70                                              long lease )
71     {
72         super( instrumentProxy, name, interval, size, description, lease );
73         
74         // Set the current value to 0 initially.
75
m_value = 0;
76     }
77     
78     /*---------------------------------------------------------------
79      * InstrumentSample Methods
80      *-------------------------------------------------------------*/

81     /**
82      * Returns the Type of the Instrument which can use the sample. This
83      * should be the same for all instances of a class.
84      * <p>
85      * This InstrumentSample returns DefaultInstrumentManager.INSTRUMENT_TYPE_VALUE
86      *
87      * @return The Type of the Instrument which can use the sample.
88      */

89     public final int getInstrumentType()
90     {
91         return DefaultInstrumentManager.INSTRUMENT_TYPE_VALUE;
92     }
93     
94     /**
95      * Obtain the value of the sample. All samples are integers, so the profiled
96      * objects must measure quantity (numbers of items), rate (items/period), time in
97      * milliseconds, etc.
98      * <p>
99      * Should only be called when synchronized.
100      *
101      * @return The sample value.
102      */

103     public int getValueInner()
104     {
105         return m_value;
106     }
107
108     /*---------------------------------------------------------------
109      * AbstractInstrumentSample Methods
110      *-------------------------------------------------------------*/

111     /**
112      * The current sample has already been stored. Reset the current sample
113      * and move on to the next.
114      * <p>
115      * Should only be called when synchronized.
116      *
117      * @param reset True if the next sample should be reset.
118      */

119     protected void advanceToNextSample( boolean reset )
120     {
121         // Reset the value count and set the value to the last known value.
122
if ( reset )
123         {
124             m_lastValue = 0;
125         }
126         m_value = m_lastValue;
127         m_valueCount = 0;
128     }
129
130     /**
131      * Returns the value to use for filling in the buffer when time is skipped.
132      * <p>
133      * Should only be called when synchronized.
134      */

135     protected int getFillValue()
136     {
137         return m_lastValue;
138     }
139     
140     /**
141      * Allow subclasses to add information into the saved state.
142      *
143      * @param out PrintWriter to write to.
144      */

145     protected void writeStateAttributes( PrintWriter JavaDoc out )
146     {
147         super.writeStateAttributes( out );
148         
149         out.print( " value-count=\"" );
150         out.print( m_valueCount );
151         out.print( "\" last-value=\"" );
152         out.print( m_lastValue );
153         out.print( "\"" );
154     }
155     
156     /**
157      * Used to load the state, called from AbstractInstrumentSample.loadState();
158      * <p>
159      * Should only be called when synchronized.
160      *
161      * @param value Current value loaded from the state.
162      * @param state Configuration object to load state from.
163      *
164      * @throws ConfigurationException If there were any problems loading the
165      * state.
166      */

167     protected void loadState( int value, Configuration state )
168         throws ConfigurationException
169     {
170         m_value = value;
171         m_valueCount = state.getAttributeAsInteger( "value-count" );
172         m_lastValue = state.getAttributeAsInteger( "last-value" );
173     }
174     
175     /*---------------------------------------------------------------
176      * ValueInstrumentListener Methods
177      *-------------------------------------------------------------*/

178     /**
179      * Called by a ValueInstrument whenever its value is set.
180      *
181      * @param instrumentName The key of Instrument whose value was set.
182      * @param value Value that was set.
183      * @param time The time that the Instrument was incremented.
184      *
185      * ValueInstrument
186      */

187     public void setValue( String JavaDoc instrumentName, int value, long time )
188     {
189         //System.out.println("AbstractValueInstrumentSample.setValue(" + instrumentName + ", "
190
// + value + ", " + time + ") : " + getName());
191
setValueInner( value, time );
192     }
193     
194     
195     /*---------------------------------------------------------------
196      * Methods
197      *-------------------------------------------------------------*/

198     /**
199      * Sets the current value of the sample.
200      *
201      * @param value New sample value.
202      * @param time Time that the new sample arrives.
203      */

204     protected abstract void setValueInner( int value, long time );
205 }
206
Popular Tags