1   package com.stateofflow.eclipse.metrics.collator;
2   
3   import com.stateofflow.eclipse.metrics.location.MetricLocation;
4   import com.stateofflow.eclipse.metrics.metric.Metric;
5   import com.stateofflow.eclipse.metrics.metric.MetricId;
6   import com.stateofflow.eclipse.metrics.util.ProgressMonitor;
7   
8   final class MetricAggregator {
9       public void aggregate(final MetricsCollator to, final MetricsCollator from, final ProgressMonitor progressMonitor) {
10          progressMonitor.beginTask("Aggregating metrics", 1);
11  
12          final MetricId[] metricIds = from.getMetricIds();
13          from.forEach(new MetricLocation.Closure() {
14              public void execute(final MetricLocation location) {
15                  aggregateMetrics(to, from, metricIds, location);
16              }
17          });
18      }
19  
20      private void aggregateMetrics(final MetricsCollator to, final MetricsCollator from, final MetricId[] metricIds, final MetricLocation location) {
21          final MetricLocation containingLocation = location.createContainingLocation();
22          for (int i = 0; i < metricIds.length; i++) {
23              if (from.hasMetric(location, metricIds[i])) {
24                  aggregateMetric(to, containingLocation, from.getMetric(location, metricIds[i]));
25              }
26          }
27      }
28  
29      private void aggregateMetric(final MetricsCollator to, final MetricLocation containingLocation, final Metric metric) {
30          if (!to.hasMetric(containingLocation, metric.getId())) {
31              to.addMetric(containingLocation, new Metric(containingLocation, metric));
32          } else {
33              final Metric existingMethodMetric = to.getMetric(containingLocation, metric.getId());
34              if (metric.getValue() > existingMethodMetric.getValue()) {
35                  to.addMetric(containingLocation, new Metric(containingLocation, metric));
36              }
37          }
38      }
39  }
40