/**
* 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 <code>defaultValue</code> if measure is null or has no values.
+ * @return <code>defaultValue</code> if measure is null or has no values.
*/
public static Double getValue(Measure measure, Double defaultValue) {
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
*
* 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<Measure> measures) {
* 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) {
}
@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));
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));
+ }
+
}