From e57493d42d4f9885329eb8be1e8aa66d57fc1e74 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 5 Oct 2015 09:59:29 +0200 Subject: [PATCH] SONAR-6827 now VariationSumFurmula supports always generating measure --- .../formula/VariationSumFormula.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/VariationSumFormula.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/VariationSumFormula.java index cf066b754a6..8ab6fa3bf16 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/VariationSumFormula.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/VariationSumFormula.java @@ -21,6 +21,8 @@ package org.sonar.server.computation.formula; import com.google.common.base.Optional; import com.google.common.base.Predicate; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import org.sonar.server.computation.component.CrawlerDepthLimit; import org.sonar.server.computation.formula.counter.DoubleVariationValue; import org.sonar.server.computation.measure.Measure; @@ -38,15 +40,22 @@ import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; public class VariationSumFormula implements Formula { private final String metricKey; private final Predicate supportedPeriods; + @CheckForNull + private final Double defaultInputValue; public VariationSumFormula(String metricKey, Predicate supportedPeriods) { - this.supportedPeriods = supportedPeriods; + this(metricKey, supportedPeriods, null); + } + + public VariationSumFormula(String metricKey, Predicate supportedPeriods, @Nullable Double defaultInputValue) { this.metricKey = requireNonNull(metricKey, "Metric key cannot be null"); + this.supportedPeriods = requireNonNull(supportedPeriods, "Period predicate can not be null"); + this.defaultInputValue = defaultInputValue; } @Override public VariationSumCounter createNewCounter() { - return new VariationSumCounter(metricKey, supportedPeriods); + return new VariationSumCounter(metricKey, supportedPeriods, defaultInputValue); } @Override @@ -78,13 +87,16 @@ public class VariationSumFormula implements Formula { + @CheckForNull + private final Double defaultInputValue; private final DoubleVariationValue.Array array = DoubleVariationValue.newArray(); private final String metricKey; private final Predicate supportedPeriods; - private VariationSumCounter(String metricKey, Predicate supportedPeriods) { + private VariationSumCounter(String metricKey, Predicate supportedPeriods, @Nullable Double defaultInputValue) { this.metricKey = metricKey; this.supportedPeriods = supportedPeriods; + this.defaultInputValue = defaultInputValue; } @Override @@ -96,6 +108,7 @@ public class VariationSumFormula implements Formula measure = context.getMeasure(metricKey); if (!measure.isPresent() || !measure.get().hasVariations()) { + initializeWithDefaultInputValue(context); return; } MeasureVariations variations = measure.get().getVariations(); @@ -103,10 +116,21 @@ public class VariationSumFormula implements Formula 0) { - array.increment(period, variations.getVariation(period.getIndex())); + array.increment(period, variation); + } else if (defaultInputValue != null) { + array.increment(period, defaultInputValue); } } } } + + private void initializeWithDefaultInputValue(CounterInitializationContext context) { + if (defaultInputValue == null) { + return; + } + for (Period period : from(context.getPeriods()).filter(supportedPeriods)) { + array.increment(period, defaultInputValue); + } + } } } -- 2.39.5