1   package com.stateofflow.eclipse.metrics.builder;
2   
3   import org.eclipse.core.runtime.CoreException;
4   import org.eclipse.jdt.core.ICompilationUnit;
5   
6   import com.stateofflow.eclipse.metrics.MetricsPlugin;
7   import com.stateofflow.eclipse.metrics.calculators.Calculator;
8   import com.stateofflow.eclipse.metrics.calculators.MeasurementContext;
9   import com.stateofflow.eclipse.metrics.util.ProgressMonitor;
10  
11  public class CompilationUnitMetricProcessor {
12      private final Calculator[] calculators;
13      private final MetricProcessor processor;
14  
15      public CompilationUnitMetricProcessor(final Calculator[] calculators, final MetricProcessor processor) {
16          this.calculators = calculators;
17          this.processor = processor;
18      }
19  
20      public void process(final ICompilationUnit compilationUnit, final ProgressMonitor monitor) throws CoreException {
21          monitor.beginTask(compilationUnit.getElementName(), 1);
22          processor.preCompilationUnit(compilationUnit);
23          processCalculators(compilationUnit);
24      }
25  
26      private void processCalculators(final ICompilationUnit compilationUnit) throws CoreException {
27          final MeasurementContext context = new MeasurementContext(compilationUnit, processor);
28          for (final Calculator calculator : calculators) {
29              measure(calculator, context);
30          }
31      }
32  
33      private void measure(final Calculator calculator, final MeasurementContext context) throws CoreException {
34          try {
35              calculator.measure(context);
36          } catch (final RuntimeException ex) {
37              MetricsPlugin.log("Problem while applying " + calculator.getClass() + " to " + context.getUnparsedCompilationUnit().getElementName(), ex);
38          }
39      }
40  }
41