diff options
author | David Gageot <david@gageot.net> | 2012-07-18 08:53:57 +0200 |
---|---|---|
committer | David Gageot <david@gageot.net> | 2012-07-18 08:54:35 +0200 |
commit | fdc4b664a5d841ec80e15c0867d697dc02f2f4cb (patch) | |
tree | acb2667e5ca2a995ec5795a2496839789138cb5a /sonar-plugin-api | |
parent | 7cd84ab05c0610c9faea163be591a64c61958cae (diff) | |
download | sonarqube-fdc4b664a5d841ec80e15c0867d697dc02f2f4cb.tar.gz sonarqube-fdc4b664a5d841ec80e15c0867d697dc02f2f4cb.zip |
Simplify code
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java | 30 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureTest.java | 13 |
2 files changed, 42 insertions, 1 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java index 44008b1b2be..554b9d5ea9b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Measure.java @@ -21,6 +21,7 @@ package org.sonar.api.measures; import com.google.common.annotations.Beta; import org.apache.commons.lang.builder.ReflectionToStringBuilder; +import org.apache.commons.lang.math.NumberUtils; import org.sonar.api.qualitymodel.Characteristic; import java.math.BigDecimal; @@ -609,6 +610,35 @@ public class Measure { return this; } + /** + * @since 3.2 + */ + public boolean isBestValue() { + return metric.isOptimizedBestValue() == Boolean.TRUE + && metric.getBestValue() != null + && (value == null || NumberUtils.compare(metric.getBestValue(), value) == 0) + && allNull(id, alertStatus, description, tendency, url, data) + && isZeroVariation(variation1, variation2, variation3, variation4, variation5); + } + + private static boolean isZeroVariation(Double... variations) { + for (Double variation : variations) { + if (!((variation == null) || NumberUtils.compare(variation.doubleValue(), 0.0) == 0)) { + return false; + } + } + return true; + } + + private static boolean allNull(Object... values) { + for (Object value : values) { + if (null != value) { + return false; + } + } + return true; + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureTest.java index d14685681ba..1bb8ba4fd6e 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MeasureTest.java @@ -58,7 +58,7 @@ public class MeasureTest { @Test public void persistenceModeIsDatabaseForBigDataMeasures() { Measure bigDataMeasure = new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "long data") - .setPersistenceMode(PersistenceMode.DATABASE); + .setPersistenceMode(PersistenceMode.DATABASE); assertThat(bigDataMeasure.getPersistenceMode()).isEqualTo(PersistenceMode.DATABASE); } @@ -194,4 +194,15 @@ public class MeasureTest { assertThat(measure.hasData()).isFalse(); assertThat(measure.getData()).isNull(); } + + @Test + public void null_value_and_null_variations_should_be_considered_as_best_value() { + assertThat(new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(0.0).isBestValue()).isTrue(); + assertThat(new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(1.0).isBestValue()).isFalse(); + assertThat(new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation2(1.0).isBestValue()).isFalse(); + assertThat(new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation3(1.0).isBestValue()).isFalse(); + assertThat(new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation4(1.0).isBestValue()).isFalse(); + assertThat(new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation5(1.0).isBestValue()).isFalse(); + } + } |