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.timer;
18  
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Set;
22  
23  import net.sf.sensor.SensorStatistics;
24  
25  /***
26   * Thread-safe, refreshable snapshot view to the internally managed TimerData
27   * representing the timing values as they were when the {@link #refresh()}
28   * method was last called.
29   * 
30   * @author Age Mooy
31   */
32  public class TimerStatistics implements SensorStatistics {
33  
34    // ==========================================================================
35    // The nested TimerData
36    // ==========================================================================
37    
38    /*** 
39     * The nested TimerData instance that serves as the source of all statistics
40     * reported by this instance.
41     *  */
42    private final TimerData timerData;
43    
44    // ==========================================================================
45    // Fields
46    // ==========================================================================
47    
48    /*** The number of hits at the time of the last call to {@link #refresh()}. */
49    private long numberOfHits = 0L;
50    
51    /*** The total time at the time of the last call to {@link #refresh()}. */
52    private long totalTime = 0L;
53    
54    /*** The average time at the time of the last call to {@link #refresh()}. */
55    private long averageTime = 0L;
56    
57    /*** The minimum time at the time of the last call to {@link #refresh()}. */
58    private long minimumTime = 0L;
59    
60    /*** The maximum time at the time of the last call to {@link #refresh()}. */
61    private long maximumTime = 0L;
62  
63    /*** The standard deviation (in ms) at the time of the last call to {@link #refresh()}. */
64    private long standardDeviation = 0L;
65    
66    /*** The sum of the squares of all measurements at the time of the last call to {@link #refresh()}. */
67    private long sumOfSquaresOfTimes = 0L;
68    
69    /*** The time (in ms since 01-01-1979 00:00 UTC) of the first update at the time of the last call to {@link #refresh()}. */
70    private long timeOfFirstUpdate = 0L;
71    
72    /*** The time (in ms since 01-01-1979 00:00 UTC) of the last update at the time of the last call to {@link #refresh()}. */
73    private long timeOfLastUpdate = 0L;
74    
75    
76    /*** Child instances. */
77    private List children;
78    
79    
80    // ==========================================================================
81    // Construction
82    // ==========================================================================
83  
84    /***
85     * Package private constructor since instances of this class can only be
86     * constructed by instances of {@link TimerData}.
87     * @param timerData the {@link TimerData} serving as the data source for this instance.
88     */
89    TimerStatistics(TimerData timerData) {
90      this.timerData = timerData;
91    }
92    
93    
94    // ==========================================================================
95    // SensorStatistics API
96    // ==========================================================================
97  
98    /***
99     * @see net.sf.sensor.SensorStatistics#getId()
100    */
101   public String getId() {
102     return timerData.getId();
103   }
104 
105   /***
106    * @see net.sf.sensor.SensorStatistics#getLabels()
107    */
108   public Set getLabels() {
109     return timerData.getLabels();
110   }
111 
112   /***
113    * Refreshes this instances and all its children.
114    * 
115    * @see net.sf.sensor.SensorStatistics#refresh()
116    */
117   public void refresh() {
118     // We tell our TimerData that we want to be refreshed.
119     // The TimerData will then synchronize on itself and
120     // call our refreshFromData() method with all pertinent data.
121     timerData.refreshStatistics();
122   }
123   
124   /***
125    * @see net.sf.sensor.SensorStatistics#reset()
126    */
127   public void reset() {
128     timerData.reset();
129   }
130   
131   
132   // ==========================================================================
133   // Refreshing of statistics
134   // ==========================================================================
135   
136   /***
137    * Callback method called from {@link TimerData#refreshStatistics(TimerStatistics)}
138    * to update all statistics. Package private because it should only be called from
139    * the associated {@link TimerData}.
140    * 
141    * @param numberOfHits the new number of hits.
142    * @param totalTime the new totalTime.
143    * @param averageTime the new averageTime.
144    * @param standardDeviation The new standardDeviation. 
145    * @param minimumTime the new minimumTime.
146    * @param maximumTime the new maximumTime.
147    * @param timeOfFirstUpdate The new timeOfFirstUpdate.
148    * @param timeOfLastUpdate The new timeOfLastUpdate.
149    * @param sumOfSquaresOfTimes The new sumOfSquaresOfTimes.
150    * @param children The new List<TimerStatistics> of children.
151    */
152   void refreshFromData(long numberOfHits, 
153                        long totalTime, 
154                        long averageTime,
155                        long standardDeviation,
156                        long minimumTime, 
157                        long maximumTime,
158                        long timeOfFirstUpdate, 
159                        long timeOfLastUpdate, 
160                        long sumOfSquaresOfTimes,
161                        List children) {
162     this.numberOfHits        = numberOfHits;
163     this.totalTime           = totalTime;
164     this.averageTime         = averageTime;
165     this.standardDeviation   = standardDeviation;
166     this.minimumTime         = minimumTime;
167     this.maximumTime         = maximumTime;
168     this.timeOfFirstUpdate   = timeOfFirstUpdate;  
169     this.timeOfLastUpdate    = timeOfLastUpdate; 
170     this.sumOfSquaresOfTimes = sumOfSquaresOfTimes;
171     this.children            = children;
172   }
173   
174     
175   // ==========================================================================
176   // Tree API
177   // ==========================================================================
178   
179   /***
180    * Returns true if this instance has one or more child instances.
181    * @return true if this instance has one or more child instances.
182    */
183   public boolean hasChildren() {
184     return children != null && children.size() > 0;
185   }
186   
187   /***
188    * @return a List<TimerStatistics> of all the child instances.
189    */
190   public List getChildren() {  
191     return (children != null) ? children : Collections.EMPTY_LIST;
192   }
193   
194   
195   // ==========================================================================
196   // Statistics API (Getters)
197   // ==========================================================================
198   
199   /***
200    * Returns the number of hits counted until the last call to {@link #refresh()}.
201    * @return the number of hits counted until the last call to {@link #refresh()}.
202    */
203   public long getNumberOfHits() {
204     return numberOfHits;
205   }
206   
207   /***
208    * Returns the total time (in milliseconds) measured until the last call to {@link #refresh()}.
209    * @return the total time (in milliseconds) measured until the last call to {@link #refresh()}.
210    */
211   public long getTotalTime() {
212     return totalTime;
213   }
214   
215   /***
216    * Returns the average time (in milliseconds) measured until the last call to {@link #refresh()}.
217    * @return the average time (in milliseconds) measured until the last call to {@link #refresh()}.
218    */
219   public long getAverageTime() {
220     return averageTime;
221   }
222   
223   /***
224    * Returns the standard deviation (in milliseconds) measured until the last call to {@link #refresh()}.
225    * @return the standard deviation (in milliseconds) measured until the last call to {@link #refresh()}.
226    */
227   public long getStandardDeviation() {
228     return standardDeviation;
229   }
230   
231   /***
232    * Returns the sumOfSquaresOfTimes.
233    * @return Returns the sumOfSquaresOfTimes.
234    */
235   public long getSumOfSquaresOfTimes() {
236     return sumOfSquaresOfTimes;
237   }
238   
239   /***
240    * Returns the minimum time (in milliseconds) measured until the last call to {@link #refresh()}.
241    * @return the minimum time (in milliseconds) measured until the last call to {@link #refresh()}.
242    */
243   public long getMinimumTime() {
244     return minimumTime;
245   }
246   
247   /***
248    * Returns the maximum time (in milliseconds) measured until the last call to {@link #refresh()}.
249    * @return the maximum time (in milliseconds) measured until the last call to {@link #refresh()}.
250    */
251   public long getMaximumTime() {
252     return maximumTime;
253   }
254 
255   /***
256    * Returns the timeOfFirstUpdate in millseconds since the usual 01-01-1970 00:00 UTC.
257    * @return the timeOfFirstUpdate in millseconds since the usual 01-01-1970 00:00 UTC.
258    */
259   public long getTimeOfFirstUpdate() {
260     return timeOfFirstUpdate;
261   }
262   
263   /***
264    * Returns the timeOfLastUpdate in millseconds since the usual 01-01-1970 00:00 UTC.
265    * @return the timeOfLastUpdate in millseconds since the usual 01-01-1970 00:00 UTC.
266    */
267   public long getTimeOfLastUpdate() {
268     return timeOfLastUpdate;
269   }
270   
271   
272   // ==========================================================================
273   // Standard Object method overrides and simple debugging methods
274   // ==========================================================================
275   
276   /***
277    * @see java.lang.Object#toString()
278    */
279   public String toString() {
280     return timerData.toString();
281   }
282   
283   /***
284    * For testing correct tree-structure behaviour. 
285    * @return a String representing the tree path from the root node to this node,
286    *         formatted like this: "/root/path/to/this/instance" where the path
287    *         entries are the ids of the tree nodes.
288    */
289   String getTreePath() {
290     if (timerData.hasParent()) {
291       return timerData.getParent().getTreePath() + "/" + getId();
292     } else {
293       return "/" + getId();
294     }
295   }
296 }