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.counter;
22 import com.google.common.base.Optional;
23 import javax.annotation.Nullable;
24 import org.sonar.server.computation.task.projectanalysis.formula.Counter;
25 import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
26 import org.sonar.server.computation.task.projectanalysis.period.Period;
27 import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolder;
29 import static org.sonar.server.computation.task.projectanalysis.period.PeriodsHolder.MAX_NUMBER_OF_PERIODS;
32 * Convenience class wrapping a long to compute the value of a MeasureVariation as an long and know it is has ever been
35 * Basically, this class will be used in a {@link Counter} implementation as an array property which can be easily
36 * creating using method {@link #newArray()}.
39 public class LongVariationValue {
40 private boolean set = false;
41 private long value = 0L;
44 * @return the current LongVariationValue so that chained calls on a specific LongVariationValue instance can be done
46 public LongVariationValue increment(long increment) {
47 this.value += increment;
53 * @return the current LongVariationValue so that chained calls on a specific LongVariationValue instance can be done
55 public LongVariationValue increment(@Nullable LongVariationValue value) {
56 if (value != null && value.isSet()) {
57 increment(value.value);
62 public boolean isSet() {
66 public long getValue() {
71 * Creates a new Array of {@link LongVariationValue} of size {@link PeriodsHolder#MAX_NUMBER_OF_PERIODS},
72 * initialized with newly creates {@link LongVariationValue} instances.
74 public static Array newArray() {
78 public static class Array {
79 private final LongVariationValue[] values;
82 this.values = new LongVariationValue[MAX_NUMBER_OF_PERIODS];
83 for (int i = 0; i < MAX_NUMBER_OF_PERIODS; i++) {
84 this.values[i] = new LongVariationValue();
88 public LongVariationValue get(Period period) {
89 return values[period.getIndex() - 1];
93 * @return the current Array, so that chained calls on a specific Array instance can be done
95 public Array increment(Period period, long value) {
96 this.values[period.getIndex() - 1].increment(value);
101 * @return the current Array, so that chained calls on a specific Array instance can be done
103 public Array incrementAll(Array source) {
104 for (int i = 0; i < this.values.length; i++) {
105 if (source.values[i].isSet()) {
106 this.values[i].increment(source.values[i]);
113 * Creates a new MeasureVariations from the current array.
115 public Optional<MeasureVariations> toMeasureVariations() {
117 return Optional.absent();
119 Double[] variations = new Double[values.length];
120 for (int i = 0; i < values.length; i++) {
121 if (values[i].isSet()) {
122 variations[i] = (double) values[i].getValue();
125 return Optional.of(new MeasureVariations(variations));
128 private boolean isAnySet() {
129 for (LongVariationValue variationValue : values) {
130 if (variationValue.isSet()) {