1   package com.stateofflow.eclipse.metrics.factory;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   import com.stateofflow.eclipse.metrics.calculators.Calculator;
7   import com.stateofflow.eclipse.metrics.calculators.EfferentCouplingsCalculator;
8   import com.stateofflow.eclipse.metrics.calculators.NumberOfFieldsCalculator;
9   import com.stateofflow.eclipse.metrics.calculators.NumberOfParametersCalculator;
10  import com.stateofflow.eclipse.metrics.calculators.cohesion.LackOfCohesionInMethodsCalculator;
11  import com.stateofflow.eclipse.metrics.calculators.complexity.CyclomaticComplexityCalculator;
12  import com.stateofflow.eclipse.metrics.calculators.levels.NumberOfLevelsCalculator;
13  import com.stateofflow.eclipse.metrics.calculators.linesofcode.LinesOfCodeCalculator;
14  import com.stateofflow.eclipse.metrics.calculators.localsinscope.NumberOfLocalsInScopeCalculator;
15  import com.stateofflow.eclipse.metrics.calculators.statements.NumberOfStatementsCalculator;
16  import com.stateofflow.eclipse.metrics.configuration.MetricsConfiguration;
17  import com.stateofflow.eclipse.metrics.metric.MetricId;
18  
19  final class EnabledCalculatorSetBuilder {
20      private void addEnabled(final MetricsConfiguration configuration, final Set<Calculator> accumulator, final Calculator calculator, final MetricId metricId) {
21          if (configuration.isEnabled(metricId)) {
22              accumulator.add(calculator);
23          }
24      }
25  
26      private void addEnabled(final MetricsConfiguration configuration, final Set<Calculator> accumulator, final Calculator calculator, final MetricId[] metricIds) {
27          for (final MetricId metricId : metricIds) {
28              addEnabled(configuration, accumulator, calculator, metricId);
29          }
30      }
31  
32      public Calculator[] build(final MetricsConfiguration configuration) {
33          final Set<Calculator> accumulator = new HashSet<Calculator>();
34  
35          addEnabled(configuration, accumulator, new CyclomaticComplexityCalculator(configuration), new MetricId[]{CyclomaticComplexityCalculator.METHOD_METRIC_ID, CyclomaticComplexityCalculator.WEIGHTED_METHODS_PER_CLASS_METRIC_ID});
36          addEnabled(configuration, accumulator, new LackOfCohesionInMethodsCalculator(), new MetricId[]{LackOfCohesionInMethodsCalculator.CHIDAMBER_KEMERER_METRIC_ID, LackOfCohesionInMethodsCalculator.HENDERSON_SELLERS_METRIC_ID, LackOfCohesionInMethodsCalculator.TOTAL_CORRELATION_METRIC_ID, LackOfCohesionInMethodsCalculator.PAIRWISE_FIELD_IRRELATION_METRIC_ID});
37          addEnabled(configuration, accumulator, new NumberOfLevelsCalculator(), NumberOfLevelsCalculator.METRIC_ID);
38          addEnabled(configuration, accumulator, new NumberOfParametersCalculator(configuration), NumberOfParametersCalculator.METRIC_ID);
39          addEnabled(configuration, accumulator, new NumberOfStatementsCalculator(), NumberOfStatementsCalculator.METRIC_ID);
40          addEnabled(configuration, accumulator, new NumberOfLocalsInScopeCalculator(), NumberOfLocalsInScopeCalculator.METRIC_ID);
41          addEnabled(configuration, accumulator, new NumberOfFieldsCalculator(), NumberOfFieldsCalculator.METRIC_ID);
42          addEnabled(configuration, accumulator, new EfferentCouplingsCalculator(), EfferentCouplingsCalculator.METRIC_ID);
43          addEnabled(configuration, accumulator, new LinesOfCodeCalculator(configuration), LinesOfCodeCalculator.METRIC_ID);
44  
45          return accumulator.toArray(new Calculator[accumulator.size()]);
46      }
47  }
48