1   package com.stateofflow.eclipse.metrics.builder;
2   
3   import org.eclipse.core.resources.IResource;
4   import org.eclipse.core.runtime.CoreException;
5   import org.eclipse.jdt.core.ICompilationUnit;
6   
7   import com.stateofflow.eclipse.metrics.MetricsBuilder;
8   import com.stateofflow.eclipse.metrics.configuration.MetricsConfiguration;
9   import com.stateofflow.eclipse.metrics.location.MetricLocation;
10  import com.stateofflow.eclipse.metrics.location.NamedLineNumber;
11  import com.stateofflow.eclipse.metrics.metric.MetricId;
12  import com.stateofflow.eclipse.metrics.util.ProgressMonitor;
13  
14  public final class MarkingMetricProcessor implements MetricProcessor {
15      private ICompilationUnit compilationUnit;
16      private final MarkerFactory factory;
17      private final MetricsConfiguration configuration;
18  
19      public MarkingMetricProcessor(final MetricsConfiguration configuration, final MarkerFactory factory) {
20          this.configuration = configuration;
21          this.factory = factory;
22      }
23  
24      public void preCompilationUnit(final ICompilationUnit compilationUnit) throws CoreException {
25          this.compilationUnit = compilationUnit;
26          compilationUnit.getCorrespondingResource().deleteMarkers(MetricsBuilder.MARKER_ID, true, IResource.DEPTH_ZERO);
27      }
28  
29      public void afterMeasuringAllCompilationUnits(final ProgressMonitor newChild) {
30          // Nothing to do
31      }
32  
33      public boolean hasPostProcessingPhase() {
34          return false;
35      }
36  
37      public void noteMethodValue(final MetricId metricKey, final MetricLocation location, final int value) {
38          createMarkerIfRequired(metricKey, location.getMethodInfo(), value);
39      }
40  
41      public void noteTypeValue(final MetricId metricKey, final MetricLocation location, final int value) {
42          createMarkerIfRequired(metricKey, location.getTypeInfo(), value);
43      }
44  
45      private void createMarkerIfRequired(final MetricId metricKey, final NamedLineNumber lineNumberInfo, final int value) {
46          final int preferredUpperBound = configuration.getUpperBound(metricKey);
47          if (value > preferredUpperBound) {
48              factory.createMarker(compilationUnit, lineNumberInfo, metricKey, value);
49          }
50      }
51  }
52