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  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  /***
25   * Main entry point for working with Meters.
26   * 
27   * 
28   * @author Age Mooy
29   */
30  public class MeterRuntime {
31  
32    // ==========================================================================
33    // Fields
34    // ==========================================================================
35    
36    /***
37     * The map holding all active meters. 
38     */
39    private Map meterDatas = new HashMap();
40    
41    
42    // ==========================================================================
43    // Meter API
44    // ==========================================================================
45    
46    /***
47     * Returns a new {@link Meter} for the specified label.
48     * @param label the label for the {@link Meter}.
49     * @return the {@link Meter} that matches the specified label.
50     */
51    public Meter getMeter(String label) {
52      return getOrCreateMeterData(label).createMeter();
53    }
54    
55    /***
56     * Returns an unmodifiable Collection<MeterStatistics> of all registered meters.
57     * @return an unmodifiable Collection<MeterStatistics> of all registered meters.
58     */
59    public Collection getMeterStatistics() {
60      return new UnmodifiableMeterStatisticsCollectionView(meterDatas.values());
61    }
62    
63    /***
64     * Resets all active counters to zero.
65     * This method <b>does not delete</b> any counters.
66     */
67    public synchronized void reset() {
68      for (Iterator i = meterDatas.values().iterator(); i.hasNext();) {
69        ((MeterData) i.next()).reset();
70      }
71    }
72    
73    
74    // ==========================================================================
75    // Implementation details
76    // ==========================================================================
77    
78    /***
79     * Retrieves the instance of MeterData matching the specified label from the
80     * counters Map and creates/add it if it doesn't exist yet.
81     * @param label the label that should match an instance of MeterData.
82     * @return the counter matching the specified label.
83     */
84    synchronized MeterData getOrCreateMeterData(String label) {
85      MeterData meterData = (MeterData) meterDatas.get(label);
86      
87      if (meterData == null) {
88        meterData = new MeterData(label);
89        
90        meterDatas.put(label, meterData);
91      }
92      
93      return meterData;
94    }
95    
96    /***
97     * Simple wrapper class that transforms a Collection&lt;MeterData&gt;
98     * into an unmodifiable Collection&lt;{@link MeterStatistics}&gt; without
99     * copying the orignal data or creating new objects/references.
100    * The {@link Iterator} returned by {@link #iterator()} will perform
101    * the translation from MeterData to {@link MeterStatistics} by calling
102    * the MeterData.getStatistics() method when {@link Iterator#next()} is
103    * called. All operations that can change data in the underlying collection
104    * will throw an {@link UnsupportedOperationException}. All other methods
105    * will be passed through to the delegate. 
106    * 
107    * @author Age Mooy
108    */
109   static class UnmodifiableMeterStatisticsCollectionView implements Collection {
110 
111     /*** The Collection&lt;MeterData&gt; to delegate to. */
112     Collection dataCollection;
113 
114     /***
115      * Constructor that wraps the specified Collection&lt;MeterData&gt;.
116      * @param dataCollection a Collection&lt;MeterData&gt;.
117      */
118     UnmodifiableMeterStatisticsCollectionView(Collection dataCollection) {
119       this.dataCollection = dataCollection;
120     }
121 
122     /***
123      * The returned {@link Iterator} will transform the MessageData instances
124      * contained in the delegate Collection&lt;MeterData&gt; into instances of 
125      * {@link MeterStatistics} by calling the MeterData.getStatistics() method
126      * on them.
127      * @see java.util.Collection#iterator()
128      */
129     public Iterator iterator() {
130       return new Iterator() {
131         Iterator i = dataCollection.iterator();
132 
133         public boolean hasNext() {
134           return i.hasNext();
135         }
136 
137         public Object next() {
138           return ((MeterData) i.next()).getStatistics();
139         }
140 
141         public void remove() {
142           throw new UnsupportedOperationException();
143         }
144       };
145     }
146     
147     /***
148      * @see java.util.Collection#size()
149      */
150     public int size() {
151       return dataCollection.size();
152     }
153 
154     /***
155      * @see java.util.Collection#isEmpty()
156      */
157     public boolean isEmpty() {
158       return dataCollection.isEmpty();
159     }
160 
161     /***
162      * @see java.util.Collection#contains(java.lang.Object)
163      */
164     public boolean contains(Object o) {
165       return dataCollection.contains(o);
166     }
167 
168     /***
169      * @see java.util.Collection#containsAll(java.util.Collection)
170      */
171     public boolean containsAll(Collection coll) {
172       return dataCollection.containsAll(coll);
173     }
174     
175     /***
176      * @see java.util.Collection#toArray()
177      */
178     public Object[] toArray() {
179       return dataCollection.toArray();
180     }
181 
182     /***
183      * @see java.util.Collection#toArray(java.lang.Object[])
184      */
185     public Object[] toArray(Object[] a) {
186       return dataCollection.toArray(a);
187     }
188 
189     /***
190      * @see java.lang.Object#toString()
191      */
192     public String toString() {
193       return dataCollection.toString();
194     }
195 
196     /***
197      * @see java.util.Collection#add(java.lang.Object)
198      */
199     public boolean add(Object o) {
200       throw new UnsupportedOperationException();
201     }
202 
203     /***
204      * @see java.util.Collection#addAll(java.util.Collection)
205      */
206     public boolean addAll(Collection coll) {
207       throw new UnsupportedOperationException();
208     }
209 
210     /***
211      * @see java.util.Collection#remove(java.lang.Object)
212      */
213     public boolean remove(Object o) {
214       throw new UnsupportedOperationException();
215     }
216     
217     /***
218      * @see java.util.Collection#removeAll(java.util.Collection)
219      */
220     public boolean removeAll(Collection coll) {
221       throw new UnsupportedOperationException();
222     }
223 
224     /***
225      * @see java.util.Collection#retainAll(java.util.Collection)
226      */
227     public boolean retainAll(Collection coll) {
228       throw new UnsupportedOperationException();
229     }
230 
231     /***
232      * @see java.util.Collection#clear()
233      */
234     public void clear() {
235       throw new UnsupportedOperationException();
236     }
237   }
238   
239   
240 }