package org.sonar.server.computation.task.projectanalysis.formula;
import com.google.common.base.Optional;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
import org.sonar.server.computation.task.projectanalysis.formula.counter.DoubleValue;
import org.sonar.server.computation.task.projectanalysis.measure.Measure;
*/
public class VariationSumFormula implements Formula<VariationSumFormula.VariationSumCounter> {
private final String metricKey;
- @CheckForNull
- private final Double defaultInputValue;
public VariationSumFormula(String metricKey) {
- this(metricKey, null);
- }
-
- public VariationSumFormula(String metricKey, @Nullable Double defaultInputValue) {
this.metricKey = requireNonNull(metricKey, "Metric key cannot be null");
- this.defaultInputValue = defaultInputValue;
}
@Override
public VariationSumCounter createNewCounter() {
- return new VariationSumCounter(metricKey, defaultInputValue);
+ return new VariationSumCounter(metricKey);
}
@Override
}
public static final class VariationSumCounter implements Counter<VariationSumCounter> {
- @CheckForNull
- private final Double defaultInputValue;
private final DoubleValue doubleValue = new DoubleValue();
private final String metricKey;
- private VariationSumCounter(String metricKey, @Nullable Double defaultInputValue) {
+ private VariationSumCounter(String metricKey) {
this.metricKey = metricKey;
- this.defaultInputValue = defaultInputValue;
}
@Override
public void initialize(CounterInitializationContext context) {
Optional<Measure> measure = context.getMeasure(metricKey);
if (!measure.isPresent() || !measure.get().hasVariation()) {
- initializeWithDefaultInputValue();
return;
}
double variation = measure.get().getVariation();
- if (variation > 0) {
- doubleValue.increment(variation);
- } else if (defaultInputValue != null) {
- doubleValue.increment(defaultInputValue);
- }
+ doubleValue.increment(variation);
}
- private void initializeWithDefaultInputValue() {
- if (defaultInputValue == null) {
- return;
- }
- doubleValue.increment(defaultInputValue);
- }
}
}