KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 /**
30  * A InstrumentSample which stores the mean value set during the sample
31  * period.
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  */

35 class MeanValueInstrumentSample
36     extends AbstractValueInstrumentSample
37 {
38     /** Total of all values seen during the sample period. */
39     private long m_valueTotal;
40     
41     /*---------------------------------------------------------------
42      * Constructors
43      *-------------------------------------------------------------*/

44     /**
45      * Creates a new MeanValueInstrumentSample
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     MeanValueInstrumentSample( 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     
65     /*---------------------------------------------------------------
66      * InstrumentSample Methods
67      *-------------------------------------------------------------*/

68     /**
69      * Returns the type of the Instrument Sample.
70      *
71      * @return The type of the Instrument Sample.
72      */

73     public int getType()
74     {
75         return DefaultInstrumentManager.INSTRUMENT_SAMPLE_TYPE_MEAN;
76     }
77     
78     /*---------------------------------------------------------------
79      * AbstractInstrumentSample Methods
80      *-------------------------------------------------------------*/

81     /**
82      * The current sample has already been stored. Reset the current sample
83      * and move on to the next.
84      * <p>
85      * Should only be called when synchronized.
86      *
87      * @param reset True if the next sample should be reset.
88      */

89     protected void advanceToNextSample( boolean reset )
90     {
91         super.advanceToNextSample( reset );
92         
93         m_valueTotal = 0;
94     }
95     
96     /**
97      * Allow subclasses to add information into the saved state.
98      *
99      * @param out PrintWriter to write to.
100      */

101     protected void writeStateAttributes( PrintWriter JavaDoc out )
102     {
103         super.writeStateAttributes( out );
104         
105         out.print( " value-total=\"" );
106         out.print( m_valueTotal );
107         out.print( "\"" );
108     }
109     
110     /**
111      * Used to load the state, called from AbstractInstrumentSample.loadState();
112      * <p>
113      * Should only be called when synchronized.
114      *
115      * @param value Current value loaded from the state.
116      * @param state Configuration object to load state from.
117      *
118      * @throws ConfigurationException If there were any problems loading the
119      * state.
120      */

121     protected void loadState( int value, Configuration state )
122         throws ConfigurationException
123     {
124         super.loadState( value, state );
125         
126         m_valueTotal = state.getAttributeAsLong( "value-total" );
127     }
128     
129     /*---------------------------------------------------------------
130      * AbstractValueInstrumentSample Methods
131      *-------------------------------------------------------------*/

132     /**
133      * Sets the current value of the sample. The value will be set as the
134      * mean of the new value and other values seen during the sample period.
135      *
136      * @param value New sample value.
137      * @param time Time that the new sample arrives.
138      */

139     protected void setValueInner( int value, long time )
140     {
141         int sampleValue;
142         long sampleTime;
143         
144         synchronized(this)
145         {
146             update( time, false );
147             
148             // Always store the last value to use for samples where a value is not set.
149
m_lastValue = value;
150             
151             if ( m_valueCount > 0 )
152             {
153                 // Additional sample
154
m_valueCount++;
155                 m_valueTotal += value;
156                 m_value = (int)(m_valueTotal / m_valueCount);
157             }
158             else
159             {
160                 // First value of this sample.
161
m_valueCount = 1;
162                 m_valueTotal = m_value = value;
163             }
164             sampleValue = m_value;
165             sampleTime = m_time;
166         }
167         
168         updateListeners( sampleValue, sampleTime );
169     }
170 }
171
Popular Tags