View Javadoc

1   /*
2    * Copyright 2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.sensor.meter;
18  
19  
20  /***
21   * 
22   * 
23   * 
24   * @author Age Mooy
25   */
26  public class MeterStatistics {
27  
28    // ==========================================================================
29    // The nested MeterData
30    // ==========================================================================
31    
32    /*** 
33     * The nested MeterData instance that serves as the source of all statistics
34     * reported by this instance.
35     *  */
36    private final MeterData meterData;
37    
38    // ==========================================================================
39    // Fields
40    // ==========================================================================
41  
42    private long currentValue = 0L;
43    private long sumOfValues = 0L;
44    private long averageValue = 0L;
45    private long minimumValue = 0L;
46    private long maximumValue = 0L;
47    private long standardDeviation = 0L;
48    
49    private long numberOfValueChanges = 0L;
50    private long sumOfSquaresOfValues = 0L;
51    
52    private long timeOfFirstUpdate = 0L;
53    private long timeOfLastUpdate = 0L;
54    
55    
56    // ==========================================================================
57    // Construction
58    // ==========================================================================
59    
60    /***
61     * @param meterData 
62     */
63    public MeterStatistics(MeterData meterData) {
64      this.meterData = meterData;
65    }
66    
67    
68    // ==========================================================================
69    // Basic API
70    // ==========================================================================
71  
72    /***
73     * Returns the String label that uniquely identifies this instance.
74     * @return the String label that uniquely identifies this instance.
75     */
76    public String getLabel() {
77      return meterData.getLabel();
78    }
79  
80    
81    // ==========================================================================
82    // Refreshing and resetting of statistics
83    // ==========================================================================
84    
85    /***
86     * Refreshes this instances and all its children.
87     */
88    public void refresh() {
89      meterData.refreshStatistics();
90    }
91  
92    /***
93     * Callback method called from {@link MeterData#refreshStatistics(MeterStatistics)}
94     * to update all statistics. Package private because it should only be called from
95     * the associated {@link MeterData}.
96     * 
97     * @param currentValue the new value of currentValue.
98     * @param numberOfValueChanges the new value of numberOfValueChanges.
99     * @param sumOfValues the new value of sumOfValues.
100    * @param averageValue the new value of averageValue.
101    * @param standardDeviation the new value of standardDeviation.
102    * @param minimumValue the new value of minimumValue.
103    * @param maximumValue the new value of maximumValue.
104    * @param timeOfFirstUpdate the new value of timeOfFirstUpdate.
105    * @param timeOfLastUpdate the new value of timeOfLastUpdate.
106    * @param sumOfSquaresOfValues the new value of sumOfSquaresOfValues.
107    */
108   public void refreshFromData(long currentValue, 
109                               long numberOfValueChanges, 
110                               long sumOfValues, 
111                               long averageValue, 
112                               long standardDeviation, 
113                               long minimumValue, 
114                               long maximumValue, 
115                               long timeOfFirstUpdate, 
116                               long timeOfLastUpdate, 
117                               long sumOfSquaresOfValues) {
118     this.currentValue = currentValue;
119     this.numberOfValueChanges = numberOfValueChanges;
120     this.sumOfValues = sumOfValues;
121     this.averageValue = averageValue;
122     this.standardDeviation = standardDeviation;
123     this.minimumValue = minimumValue;
124     this.maximumValue = maximumValue;
125     this.timeOfFirstUpdate = timeOfFirstUpdate;
126     this.timeOfLastUpdate = timeOfLastUpdate;
127     this.sumOfSquaresOfValues = sumOfSquaresOfValues;
128   }
129   
130   
131   // ==========================================================================
132   // Statistics API (Getters)
133   // ==========================================================================
134   
135   /***
136    * Returns the currentValue.
137    * @return Returns the currentValue.
138    */
139   public long getCurrentValue() {
140     return currentValue;
141   }
142   
143   /***
144    * Returns the numberOfValueChanges.
145    * @return Returns the numberOfValueChanges.
146    */
147   public long getNumberOfValueChanges() {
148     return numberOfValueChanges;
149   }
150   
151   /***
152    * Returns the sumOfValues.
153    * @return Returns the sumOfValues.
154    */
155   public long getSumOfValues() {
156     return sumOfValues;
157   }
158   
159   /***
160    * Returns the averageValue.
161    * @return Returns the averageValue.
162    */
163   public long getAverageValue() {
164     return averageValue;
165   }
166   
167   /***
168    * Returns the standardDeviation.
169    * @return Returns the standardDeviation.
170    */
171   public long getStandardDeviation() {
172     return standardDeviation;
173   }
174   
175   /***
176    * Returns the sumOfSquaresOfValues.
177    * @return Returns the sumOfSquaresOfValues.
178    */
179   public long getSumOfSquaresOfValues() {
180     return sumOfSquaresOfValues;
181   }
182   
183   /***
184    * Returns the minimumValue.
185    * @return Returns the minimumValue.
186    */
187   public long getMinimumValue() {
188     return minimumValue;
189   }
190   
191   /***
192    * Returns the maximumValue.
193    * @return Returns the maximumValue.
194    */
195   public long getMaximumValue() {
196     return maximumValue;
197   }
198   
199   /***
200    * Returns the timeOfFirstUpdate in millseconds since the usual 01-01-1970 00:00 UTC.
201    * @return the timeOfFirstUpdate in millseconds since the usual 01-01-1970 00:00 UTC.
202    */
203   public long getTimeOfFirstUpdate() {
204     return timeOfFirstUpdate;
205   }
206   
207   /***
208    * Returns the timeOfLastUpdate in millseconds since the usual 01-01-1970 00:00 UTC.
209    * @return the timeOfLastUpdate in millseconds since the usual 01-01-1970 00:00 UTC.
210    */
211   public long getTimeOfLastUpdate() {
212     return timeOfLastUpdate;
213   }
214 
215   
216   // ==========================================================================
217   // Standard Object method overrides and simple debugging methods
218   // ==========================================================================
219   
220   /***
221    * @see java.lang.Object#toString()
222    */
223   public String toString() {
224     return meterData.toString();
225   }
226   
227 }