From 8c1d9bf46953dfde3ab5b4ade5a80e4dcaa9d10d Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Mon, 28 Feb 2011 18:03:52 +0100 Subject: [PATCH] API: add methods MeasureUtils.getVariation(...) and MeasureUtils.getVariationAsLong(...) --- .../org/sonar/api/measures/MeasureUtils.java | 40 +++++++++++++++++-- .../sonar/api/measures/MeasureUtilsTest.java | 25 +++++++++++- 2 files changed, 60 insertions(+), 5 deletions(-) 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 aca0c1ca364..f0f892a5325 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 @@ -57,9 +57,9 @@ public final class MeasureUtils { /** * Get the value of a measure, or alternatively a default value * - * @param measure the measure + * @param measure the measure * @param defaultValue the default value - * @return defaultValue if measure is null or has no values. + * @return defaultValue if measure is null or has no values. */ public static Double getValue(Measure measure, Double defaultValue) { @@ -69,6 +69,38 @@ public final class MeasureUtils { return defaultValue; } + public static Long getValueAsLong(Measure measure, Long defaultValue) { + if (MeasureUtils.hasValue(measure)) { + return measure.getValue().longValue(); + } + return defaultValue; + } + + + public static Double getVariation(Measure measure, int periodIndex) { + return getVariation(measure, periodIndex, null); + } + + public static Double getVariation(Measure measure, int periodIndex, Double defaultValue) { + Double result = null; + if (measure != null) { + result = measure.getVariation(periodIndex); + } + return (result != null ? result : defaultValue); + } + + public static Long getVariationAsLong(Measure measure, int periodIndex) { + return getVariationAsLong(measure, periodIndex, null); + } + + public static Long getVariationAsLong(Measure measure, int periodIndex, Long defaultValue) { + Double result = null; + if (measure != null) { + result = measure.getVariation(periodIndex); + } + return result == null ? defaultValue : Long.valueOf(result.longValue()); + } + /** * Tests if a measure has a value * @@ -93,7 +125,7 @@ public final class MeasureUtils { * Sums a series of measures * * @param zeroIfNone whether to return 0 or null in case measures is null - * @param measures the series of measures + * @param measures the series of measures * @return the sum of the measure series */ public static Double sum(boolean zeroIfNone, Collection measures) { @@ -107,7 +139,7 @@ public final class MeasureUtils { * Sums a series of measures * * @param zeroIfNone whether to return 0 or null in case measures is null - * @param measures the series of measures + * @param measures the series of measures * @return the sum of the measure series */ public static Double sum(boolean zeroIfNone, Measure... measures) { 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 81faf56e385..1e1d33e42eb 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 @@ -80,7 +80,7 @@ public class MeasureUtilsTest { } @Test - public void haveValues() { + public void shouldHaveValues() { assertThat(MeasureUtils.haveValues(), is(false)); assertThat(MeasureUtils.haveValues(null, null), is(false)); assertThat(MeasureUtils.haveValues(new Measure(CoreMetrics.CLASSES, (Double) null)), is(false)); @@ -88,4 +88,27 @@ public class MeasureUtilsTest { assertThat(MeasureUtils.haveValues(new Measure(CoreMetrics.CLASSES, 3.2), new Measure(CoreMetrics.COMPLEXITY, "foo")), is(false)); assertThat(MeasureUtils.haveValues(new Measure(CoreMetrics.CLASSES, 3.2), new Measure(CoreMetrics.COMPLEXITY, 2.5)), is(true)); } + + @Test + public void shouldGetVariation() { + assertThat(MeasureUtils.getVariation(null, 2, 3.14), is(3.14)); + assertThat(MeasureUtils.getVariation(null, 2), nullValue()); + + assertThat(MeasureUtils.getVariation(new Measure(), 2, 3.14), is(3.14)); + assertThat(MeasureUtils.getVariation(new Measure(), 2), nullValue()); + + assertThat(MeasureUtils.getVariation(new Measure().setVariation2(1.618), 2, 3.14), is(1.618)); + } + + @Test + public void shouldGetVariationAsLong() { + assertThat(MeasureUtils.getVariationAsLong(null, 2, 3L), is(3L)); + assertThat(MeasureUtils.getVariationAsLong(null, 2), nullValue()); + + assertThat(MeasureUtils.getVariationAsLong(new Measure(), 2, 3L), is(3L)); + assertThat(MeasureUtils.getVariationAsLong(new Measure(), 2), nullValue()); + + assertThat(MeasureUtils.getVariationAsLong(new Measure().setVariation2(222.0), 2, 3L), is(222L)); + } + } -- 2.39.5