aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java17
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/measures/MeasureUtils.java27
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureUtilsTest.java17
3 files changed, 56 insertions, 5 deletions
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 b592221a51b..352c9b75bd0 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
@@ -1424,6 +1424,23 @@ public final class CoreMetrics {
/**
* @since 2.14
*/
+ public static final String NEW_VIOLATIONS_WITHOUT_REVIEW_KEY = "new_violations_without_review";
+
+ /**
+ * @since 2.14
+ */
+ public static final Metric NEW_VIOLATIONS_WITHOUT_REVIEW = new Metric.Builder(NEW_VIOLATIONS_WITHOUT_REVIEW_KEY, "New unreviewed violations", Metric.ValueType.INT)
+ .setDescription("New violations that have not been reviewed yet")
+ .setDirection(Metric.DIRECTION_WORST)
+ .setQualitative(true)
+ .setDomain(DOMAIN_REVIEWS)
+ .setBestValue(0.0)
+ .setOptimizedBestValue(true)
+ .create();
+
+ /**
+ * @since 2.14
+ */
public static final String FALSE_POSITIVE_REVIEWS_KEY = "false_positive_reviews";
/**
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeasureUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeasureUtils.java
index 4e44efeb30a..be07a34c7d3 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeasureUtils.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/MeasureUtils.java
@@ -19,10 +19,10 @@
*/
package org.sonar.api.measures;
-import org.apache.commons.lang.StringUtils;
-
import java.util.Collection;
+import org.apache.commons.lang.StringUtils;
+
/**
* An utility class to manipulate measures
*
@@ -76,7 +76,6 @@ public final class MeasureUtils {
return defaultValue;
}
-
public static Double getVariation(Measure measure, int periodIndex) {
return getVariation(measure, periodIndex, null);
}
@@ -161,6 +160,28 @@ public final class MeasureUtils {
return zeroIfNone(zeroIfNone);
}
+ /**
+ * Sums a series of measures for the given variation index
+ *
+ * @param zeroIfNone whether to return 0 or null in case measures is null
+ * @param variationIndex the index of the variation to use
+ * @param measures the series of measures
+ * @return the sum of the variations for the measure series
+ */
+ public static Double sumOnVariation(boolean zeroIfNone, int variationIndex, Collection<Measure> measures) {
+ if (measures == null) {
+ return zeroIfNone(zeroIfNone);
+ }
+ Double sum = 0d;
+ for (Measure measure : measures) {
+ Double var = measure.getVariation(variationIndex);
+ if (var != null) {
+ sum += var;
+ }
+ }
+ return sum;
+ }
+
private static Double zeroIfNone(boolean zeroIfNone) {
return zeroIfNone ? 0d : null;
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureUtilsTest.java
index 1a0c418c1d8..c6b18ad7933 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureUtilsTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureUtilsTest.java
@@ -19,14 +19,16 @@
*/
package org.sonar.api.measures;
-import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
-import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
+import java.util.List;
+
+import org.junit.Test;
public class MeasureUtilsTest {
@@ -111,4 +113,15 @@ public class MeasureUtilsTest {
assertThat(MeasureUtils.getVariationAsLong(new Measure().setVariation2(222.0), 2, 3L), is(222L));
}
+ @Test
+ public void shouldSumOnVariation() throws Exception {
+ Measure measure1 = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(1.0).setVariation2(1.0).setVariation3(3.0);
+ Measure measure2 = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(1.0).setVariation2(2.0).setVariation3(3.0);
+ List<Measure> children = Arrays.asList(measure1, measure2);
+
+ assertThat(MeasureUtils.sumOnVariation(true, 1, children), is(2d));
+ assertThat(MeasureUtils.sumOnVariation(true, 2, children), is(3d));
+ assertThat(MeasureUtils.sumOnVariation(true, 3, children), is(6d));
+ }
+
}