1   package com.stateofflow.eclipse.metrics.collator;
2   
3   import java.util.Arrays;
4   import java.util.Comparator;
5   import java.util.HashMap;
6   import java.util.Iterator;
7   import java.util.Map;
8   import java.util.SortedMap;
9   import java.util.TreeMap;
10  
11  import com.stateofflow.eclipse.metrics.configuration.MetricPresentations;
12  import com.stateofflow.eclipse.metrics.location.MetricLocation;
13  import com.stateofflow.eclipse.metrics.metric.Metric;
14  import com.stateofflow.eclipse.metrics.metric.MetricId;
15  
16  public final class MetricsCollator {
17      private final Map<MetricLocation, Map<MetricId, Metric>> metricLocationToMetricsMap = new HashMap<MetricLocation, Map<MetricId, Metric>>();
18      private final Map<MetricId, Integer> metricIdsToAggregationSteps;
19      private final MetricPresentations presentations;
20  
21      public MetricsCollator(final MetricPresentations presentations) {
22          this.presentations = presentations;
23          metricIdsToAggregationSteps = new HashMap<MetricId, Integer>();
24      }
25  
26      public void addMetric(final MetricLocation location, final Metric metric) {
27          final Map<MetricId, Metric> metricsMap = findOrCreateMetricsMap(location);
28          metricsMap.put(metric.getId(), metric);
29  
30          if (!metricIdsToAggregationSteps.containsKey(metric.getId())) {
31              metricIdsToAggregationSteps.put(metric.getId(), new Integer(metric.getAggregationSteps()));
32          }
33      }
34  
35      public void addMetric(final Metric metric) {
36          addMetric(metric.getLocation(), metric);
37      }
38  
39      public MetricId[] getMetricIds() {
40          return metricIdsToAggregationSteps.keySet().toArray(new MetricId[metricIdsToAggregationSteps.size()]);
41      }
42  
43      public int getAggregationSteps(final MetricId id) {
44          return metricIdsToAggregationSteps.get(id).intValue();
45      }
46  
47      public Metric getMetric(final MetricLocation location, final MetricId metricId) {
48          return getMetricsMap(location).get(metricId);
49      }
50  
51      public boolean hasMetric(final MetricLocation location, final MetricId metricId) {
52          final Map<MetricId, Metric> map = getMetricsMap(location);
53          if (map == null) {
54              return false;
55          }
56  
57          return map.containsKey(metricId);
58      }
59  
60      public void forEach(final Comparator<MetricLocation> sortOrder, final MetricLocation.Closure closure) {
61          final SortedMap<MetricLocation, Map<MetricId, Metric>> map = new TreeMap<MetricLocation, Map<MetricId, Metric>>(sortOrder);
62          map.putAll(metricLocationToMetricsMap);
63          final Iterator<MetricLocation> iter = map.keySet().iterator();
64          while (iter.hasNext()) {
65              closure.execute(iter.next());
66          }
67      }
68  
69      public void forEach(final MetricLocation.Closure closure) {
70          final Iterator<MetricLocation> iter = metricLocationToMetricsMap.keySet().iterator();
71          while (iter.hasNext()) {
72              closure.execute(iter.next());
73          }
74      }
75  
76      public int size() {
77          return metricLocationToMetricsMap.size();
78      }
79  
80      private Map<MetricId, Metric> getMetricsMap(final MetricLocation location) {
81          return metricLocationToMetricsMap.get(location);
82      }
83  
84      private Map<MetricId, Metric> findOrCreateMetricsMap(final MetricLocation location) {
85          if (metricLocationToMetricsMap.containsKey(location)) {
86              return getMetricsMap(location);
87          }
88  
89          final Map<MetricId, Metric> map = new HashMap<MetricId, Metric>();
90          metricLocationToMetricsMap.put(location, map);
91          return map;
92      }
93  
94      public int getNumberOfMetrics() {
95          return metricIdsToAggregationSteps.size();
96      }
97  
98      public MetricId[] getSortedMetricIds() {
99          final MetricId[] ids = getMetricIds();
100         Arrays.sort(ids, new CollatedMetricIdComparator(this, presentations));
101         return ids;
102     }
103 }
104