3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.formula;
22 import com.google.common.base.Optional;
23 import com.google.common.base.Predicate;
24 import javax.annotation.CheckForNull;
25 import javax.annotation.Nullable;
26 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
27 import org.sonar.server.computation.task.projectanalysis.formula.counter.DoubleVariationValue;
28 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
29 import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
30 import org.sonar.server.computation.task.projectanalysis.period.Period;
32 import static com.google.common.collect.FluentIterable.from;
33 import static java.util.Objects.requireNonNull;
34 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
37 * A Formula which aggregates variations of a specific metric by simply making the sums of its variations. It supports
38 * make the sum of only specific periods.
40 public class VariationSumFormula implements Formula<VariationSumFormula.VariationSumCounter> {
41 private final String metricKey;
42 private final Predicate<Period> supportedPeriods;
44 private final Double defaultInputValue;
46 public VariationSumFormula(String metricKey, Predicate<Period> supportedPeriods) {
47 this(metricKey, supportedPeriods, null);
50 public VariationSumFormula(String metricKey, Predicate<Period> supportedPeriods, @Nullable Double defaultInputValue) {
51 this.metricKey = requireNonNull(metricKey, "Metric key cannot be null");
52 this.supportedPeriods = requireNonNull(supportedPeriods, "Period predicate cannot be null");
53 this.defaultInputValue = defaultInputValue;
57 public VariationSumCounter createNewCounter() {
58 return new VariationSumCounter(metricKey, supportedPeriods, defaultInputValue);
62 public Optional<Measure> createMeasure(VariationSumCounter counter, CreateMeasureContext context) {
63 if (!CrawlerDepthLimit.LEAVES.isDeeperThan(context.getComponent().getType())) {
64 return Optional.absent();
66 MeasureVariations.Builder variations = createAndPopulateBuilder(counter.array, context);
67 if (variations.isEmpty()) {
68 return Optional.absent();
70 return Optional.of(newMeasureBuilder().setVariations(variations.build()).createNoValue());
73 private MeasureVariations.Builder createAndPopulateBuilder(DoubleVariationValue.Array array, CreateMeasureContext context) {
74 MeasureVariations.Builder builder = MeasureVariations.newMeasureVariationsBuilder();
75 for (Period period : from(context.getPeriods()).filter(supportedPeriods)) {
76 DoubleVariationValue elements = array.get(period);
77 if (elements.isSet()) {
78 builder.setVariation(period, elements.getValue());
85 public String[] getOutputMetricKeys() {
86 return new String[] {metricKey};
89 public static final class VariationSumCounter implements Counter<VariationSumCounter> {
91 private final Double defaultInputValue;
92 private final DoubleVariationValue.Array array = DoubleVariationValue.newArray();
93 private final String metricKey;
94 private final Predicate<Period> supportedPeriods;
96 private VariationSumCounter(String metricKey, Predicate<Period> supportedPeriods, @Nullable Double defaultInputValue) {
97 this.metricKey = metricKey;
98 this.supportedPeriods = supportedPeriods;
99 this.defaultInputValue = defaultInputValue;
103 public void aggregate(VariationSumCounter counter) {
104 array.incrementAll(counter.array);
108 public void initialize(CounterInitializationContext context) {
109 Optional<Measure> measure = context.getMeasure(metricKey);
110 if (!measure.isPresent() || !measure.get().hasVariations()) {
111 initializeWithDefaultInputValue(context);
114 MeasureVariations variations = measure.get().getVariations();
115 for (Period period : from(context.getPeriods()).filter(supportedPeriods)) {
116 if (variations.hasVariation(period.getIndex())) {
117 double variation = variations.getVariation(period.getIndex());
119 array.increment(period, variation);
120 } else if (defaultInputValue != null) {
121 array.increment(period, defaultInputValue);
127 private void initializeWithDefaultInputValue(CounterInitializationContext context) {
128 if (defaultInputValue == null) {
131 for (Period period : from(context.getPeriods()).filter(supportedPeriods)) {
132 array.increment(period, defaultInputValue);