summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-04-05 18:45:01 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-04-05 18:46:17 +0200
commit35a24b6c1969f3c6e234f5a29fce8455df9df122 (patch)
treec9fb52dc55b43ec4bb5c0cb413e2f6d2d3cb3246
parent8235e7b3c8c64095c7bae824f783d6ea3f1ab018 (diff)
downloadsonarqube-35a24b6c1969f3c6e234f5a29fce8455df9df122.tar.gz
sonarqube-35a24b6c1969f3c6e234f5a29fce8455df9df122.zip
SONAR-3393 add a factory method to AverageFormula
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java27
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java8
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java16
3 files changed, 29 insertions, 22 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java
index 861c67daf0b..a22824e533d 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/AverageFormula.java
@@ -20,16 +20,16 @@
package org.sonar.api.measures;
+import org.sonar.api.resources.ResourceUtils;
+
import java.util.Arrays;
import java.util.List;
-import org.sonar.api.resources.ResourceUtils;
-
/**
* Formula used to compute an average for a given metric A, which is the result of the sum of measures of this metric (A) divided by another metric (B).
- *
+ * <p/>
* For example: to compute the metric "complexity by file", the main metric (A) is "complexity" and the other metric (B) is "file".
- *
+ *
* @since 2.15
*/
public class AverageFormula implements Formula {
@@ -38,17 +38,24 @@ public class AverageFormula implements Formula {
private Metric byMetric;
/**
- * Creates a new {@link AverageFormula} class.
- *
- * @param mainMetric The metric on which average should be calculated (ex.: "complexity")
- * @param byMetric The metric used to divide the main metric to compute average (ex.: "file" for "complexity by file")
+ * This method should be private but it kep package-protected because of AverageComplexityFormula.
*/
- protected AverageFormula(Metric mainMetric, Metric byMetric) {
+ AverageFormula(Metric mainMetric, Metric byMetric) {
this.mainMetric = mainMetric;
this.byMetric = byMetric;
}
/**
+ * Creates a new {@link AverageFormula} class.
+ *
+ * @param main The metric on which average should be calculated (ex.: "complexity")
+ * @param by The metric used to divide the main metric to compute average (ex.: "file" for "complexity by file")
+ */
+ public static AverageFormula create(Metric main, Metric by) {
+ return new AverageFormula(main, by);
+ }
+
+ /**
* {@inheritDoc}
*/
public List<Metric> dependsUponMetrics() {
@@ -63,7 +70,7 @@ public class AverageFormula implements Formula {
return null;
}
- Measure result = null;
+ Measure result;
if (ResourceUtils.isFile(context.getResource())) {
result = calculateForFile(data, context);
} else {
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java
index ff1bf1c4d4f..c8a1af87734 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java
@@ -288,7 +288,7 @@ public final class CoreMetrics {
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(true)
.setDomain(DOMAIN_COMPLEXITY)
- .setFormula(new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.CLASSES))
+ .setFormula(AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.CLASSES))
.create();
public static final String FUNCTION_COMPLEXITY_KEY = "function_complexity";
@@ -297,7 +297,7 @@ public final class CoreMetrics {
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(true)
.setDomain(DOMAIN_COMPLEXITY)
- .setFormula(new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS))
+ .setFormula(AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS))
.create();
public static final String FILE_COMPLEXITY_KEY = "file_complexity";
@@ -306,7 +306,7 @@ public final class CoreMetrics {
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(true)
.setDomain(DOMAIN_COMPLEXITY)
- .setFormula(new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.FILES))
+ .setFormula(AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FILES))
.create();
/**
@@ -324,7 +324,7 @@ public final class CoreMetrics {
.setDirection(Metric.DIRECTION_WORST)
.setQualitative(true)
.setDomain(DOMAIN_COMPLEXITY)
- .setFormula(new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.PARAGRAPHS))
+ .setFormula(AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.PARAGRAPHS))
.create();
/**
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java
index 9ae9ff98add..1e05b4264ee 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/AverageFormulaTest.java
@@ -48,7 +48,7 @@ public class AverageFormulaTest {
@Test
public void testDependsUponMetrics() throws Exception {
- AverageFormula formula = new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS);
+ AverageFormula formula = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS);
assertThat(formula.dependsUponMetrics(), hasItems(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS));
}
@@ -67,7 +67,7 @@ public class AverageFormulaTest {
when(data.getChildren()).thenReturn(childrenData);
- Measure measure = new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
+ Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
assertThat(measure.getValue(), is(2.0));
}
@@ -75,7 +75,7 @@ public class AverageFormulaTest {
@Test
public void shouldNotComputeIfNotTargetMetric() {
when(data.getMeasure(CoreMetrics.FUNCTION_COMPLEXITY)).thenReturn(new Measure(CoreMetrics.FUNCTION_COMPLEXITY, 2.0));
- Measure measure = new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
+ Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
assertNull(measure);
}
@@ -83,7 +83,7 @@ public class AverageFormulaTest {
public void testWhenNoChildrenMesaures() {
List<FormulaData> childrenData = Lists.newArrayList();
when(data.getChildren()).thenReturn(childrenData);
- Measure measure = new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
+ Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
assertNull(measure);
}
@@ -95,7 +95,7 @@ public class AverageFormulaTest {
when(data1.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 43.0));
when(data.getChildren()).thenReturn(childrenData);
- Measure measure = new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
+ Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
assertNull(measure);
}
@@ -108,7 +108,7 @@ public class AverageFormulaTest {
when(data1.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(new Measure(CoreMetrics.COMPLEXITY, 43.0));
when(data.getChildren()).thenReturn(childrenData);
- Measure measure = new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
+ Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
assertNull(measure);
}
@@ -128,7 +128,7 @@ public class AverageFormulaTest {
when(data.getChildren()).thenReturn(childrenData);
- Measure measure = new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
+ Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
assertThat(measure.getValue(), is(2.5));
}
@@ -139,7 +139,7 @@ public class AverageFormulaTest {
when(data.getMeasure(CoreMetrics.FUNCTIONS)).thenReturn(new Measure(CoreMetrics.FUNCTIONS, 20.0));
when(context.getResource()).thenReturn(new JavaFile("foo"));
- Measure measure = new AverageFormula(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
+ Measure measure = AverageFormula.create(CoreMetrics.COMPLEXITY, CoreMetrics.FUNCTIONS).calculate(data, context);
assertThat(measure.getValue(), is(3.0));
}
}