KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24
25 import org.apache.excalibur.instrument.manager.DefaultInstrumentManager;
26 import org.apache.excalibur.instrument.manager.CounterInstrumentListener;
27
28 /**
29  * A InstrumentSample which stores the number of times that increment has been
30  * called during the sample period.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  */

34 class CounterInstrumentSample
35     extends AbstractInstrumentSample
36     implements CounterInstrumentListener
37 {
38     /** The count. */
39     protected int m_count;
40     
41     /*---------------------------------------------------------------
42      * Constructors
43      *-------------------------------------------------------------*/

44     /**
45      * Creates a new CounterInstrumentSample
46      *
47      * @param instrumentProxy The InstrumentProxy which owns the
48      * InstrumentSample.
49      * @param name The name of the new InstrumentSample.
50      * @param interval The sample interval of the new InstrumentSample.
51      * @param size The number of samples to store as history. Assumes that size is at least 1.
52      * @param description The description of the new InstrumentSample.
53      * @param lease The length of the lease in milliseconds.
54      */

55     CounterInstrumentSample( InstrumentProxy instrumentProxy,
56                              String JavaDoc name,
57                              long interval,
58                              int size,
59                              String JavaDoc description,
60                              long lease )
61     {
62         super( instrumentProxy, name, interval, size, description, lease );
63         
64         // Set the current value to 0 initially.
65
m_count = 0;
66     }
67     
68     /*---------------------------------------------------------------
69      * InstrumentSample Methods
70      *-------------------------------------------------------------*/

71     /**
72      * Returns the type of the Instrument Sample.
73      *
74      * @return The type of the Instrument Sample.
75      */

76     public int getType()
77     {
78         return DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_COUNTER;
79     }
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_COUNTER
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_COUNTER;
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      *
99      * @return The sample value.
100      */

101     public int getValueInner()
102     {
103         return m_count;
104     }
105     
106     /*---------------------------------------------------------------
107      * AbstractInstrumentSample Methods
108      *-------------------------------------------------------------*/

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

117     protected void advanceToNextSample( boolean reset )
118     {
119         // Counts do not propagate, so always reset the count to 0.
120
m_count = 0;
121     }
122
123     /**
124      * Returns the value to use for filling in the buffer when time is skipped.
125      * <p>
126      * Should only be called when synchronized.
127      */

128     protected int getFillValue()
129     {
130         return 0;
131     }
132     
133     /**
134      * Used to load the state, called from AbstractInstrumentSample.loadState();
135      * <p>
136      * Should only be called when synchronized.
137      *
138      * @param value Current value loaded from the state.
139      * @param state Configuration object to load state from.
140      *
141      * @throws ConfigurationException If there were any problems loading the
142      * state.
143      */

144     protected void loadState( int value, Configuration state )
145         throws ConfigurationException
146     {
147         m_count = value;
148     }
149     
150     /*---------------------------------------------------------------
151      * CounterInstrumentListener Methods
152      *-------------------------------------------------------------*/

153     /**
154      * Called by a CounterInstrument whenever its value is incremented.
155      *
156      * @param instrumentName The name of Instrument which was incremented.
157      * @param count A positive integer to increment the counter by.
158      * @param time The time that the Instrument was incremented.
159      */

160     public void increment( String JavaDoc instrumentName, int count, long time )
161     {
162         //System.out.println("CounterInstrumentSample.increment(" + instrumentName + ", " + count + ", " + time + ") : " + getName() );
163
increment( count, time );
164     }
165     
166     /*---------------------------------------------------------------
167      * Methods
168      *-------------------------------------------------------------*/

169     /**
170      * Increments the count.
171      *
172      * @param time Time that the count is incremented.
173      * @param count A positive integer to increment the counter by.
174      */

175     private void increment( int count, long time )
176     {
177         int sampleValue;
178         long sampleTime;
179         
180         synchronized(this)
181         {
182             update( time, false );
183             
184             m_count += count;
185             
186             sampleValue = m_count;
187             sampleTime = m_time;
188         }
189         
190         updateListeners( sampleValue, sampleTime );
191     }
192 }
193
Popular Tags