1   package com.stateofflow.eclipse.metrics.builder;
2   
3   import org.eclipse.core.runtime.CoreException;
4   import org.eclipse.core.runtime.IStatus;
5   import org.eclipse.core.runtime.Status;
6   import org.eclipse.jdt.core.ICompilationUnit;
7   
8   import com.stateofflow.eclipse.metrics.MetricsPlugin;
9   import com.stateofflow.eclipse.metrics.collator.MetricsCollator;
10  import com.stateofflow.eclipse.metrics.export.Exporter;
11  import com.stateofflow.eclipse.metrics.location.MetricLocation;
12  import com.stateofflow.eclipse.metrics.metric.Metric;
13  import com.stateofflow.eclipse.metrics.metric.MetricId;
14  import com.stateofflow.eclipse.metrics.util.ProgressMonitor;
15  
16  public final class ExportingMetricProcessor implements MetricProcessor {
17      private final Exporter[] exporters;
18      private final MetricsCollator typeMetricsCollator;
19      private final MetricsCollator methodMetricsCollator;
20  
21      public ExportingMetricProcessor(final Exporter[] exporters, final MetricsCollator typeMetricsCollator, final MetricsCollator methodMetricsCollator) {
22          this.exporters = exporters;
23          this.typeMetricsCollator = typeMetricsCollator;
24          this.methodMetricsCollator = methodMetricsCollator;
25      }
26  
27      public boolean hasPostProcessingPhase() {
28          return true;
29      }
30  
31      public void afterMeasuringAllCompilationUnits(final ProgressMonitor monitor) throws CoreException {
32          monitor.beginTask("Exporting accumulated results", exporters.length);
33          for (final Exporter exporter : exporters) {
34              export(monitor, exporter);
35          }
36      }
37  
38      private void export(final ProgressMonitor monitor, final Exporter exporter) throws CoreException {
39          try {
40              exporter.export(typeMetricsCollator, methodMetricsCollator, monitor.newChild(1));
41          } catch (final Exception e) {
42              throw new CoreException(new Status(IStatus.WARNING, MetricsPlugin.PLUGIN_ID, e.getMessage(), e));
43          }
44      }
45  
46      public void noteTypeValue(final MetricId metricKey, final MetricLocation location, final int value) {
47          typeMetricsCollator.addMetric(new Metric(metricKey, location, value));
48      }
49  
50      public void noteMethodValue(final MetricId metricKey, final MetricLocation location, final int value) {
51          methodMetricsCollator.addMetric(new Metric(metricKey, location, value));
52      }
53  
54      public void preCompilationUnit(final ICompilationUnit compilationUnit) throws CoreException {
55          // Nothing to do
56      }
57  }
58