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 java.util.Arrays;
23 import java.util.Optional;
24 import javax.annotation.Nullable;
25 import org.sonar.server.computation.task.projectanalysis.formula.Counter;
26 import org.sonar.server.computation.task.projectanalysis.measure.MeasureVariations;
27 import org.sonar.server.computation.task.projectanalysis.period.Period;
28 import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolder;
29 import org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating;
31 import static org.sonar.server.computation.task.projectanalysis.period.PeriodsHolder.MAX_NUMBER_OF_PERIODS;
34 * Convenience class wrapping a rating.
35 * It also allows to compute the value of a MeasureVariation as a rating, and add a check to know if it is has ever been set.
37 * Basically, this class will be used in a {@link Counter} implementation as an array property which can be easily
38 * created using method {@link #newArray()}.
41 public class RatingVariationValue {
42 private boolean set = false;
43 private Rating value = Rating.A;
46 * @return the current RatingVariationValue so that chained calls on a specific RatingVariationValue instance can be done
48 public RatingVariationValue increment(Rating rating) {
49 if (value.compareTo(rating) > 0) {
57 * @return the current RatingVariationValue so that chained calls on a specific RatingVariationValue instance can be done
59 public RatingVariationValue increment(@Nullable RatingVariationValue value) {
60 if (value != null && value.isSet()) {
61 increment(value.value);
66 public boolean isSet() {
70 public Rating getValue() {
75 * Creates a new Array of {@link RatingVariationValue} of size {@link PeriodsHolder#MAX_NUMBER_OF_PERIODS},
76 * initialized with newly creates {@link RatingVariationValue} instances.
78 public static Array newArray() {
82 public static class Array {
83 private final RatingVariationValue[] values;
86 this.values = new RatingVariationValue[MAX_NUMBER_OF_PERIODS];
87 for (int i = 0; i < MAX_NUMBER_OF_PERIODS; i++) {
88 this.values[i] = new RatingVariationValue();
92 public RatingVariationValue get(Period period) {
93 return values[period.getIndex() - 1];
97 * @return the current Array, so that chained calls on a specific Array instance can be done
99 public Array increment(Period period, Rating value) {
100 this.values[period.getIndex() - 1].increment(value);
105 * @return the current Array, so that chained calls on a specific Array instance can be done
107 public Array incrementAll(Array source) {
108 for (int i = 0; i < this.values.length; i++) {
109 if (source.values[i].isSet()) {
110 this.values[i].increment(source.values[i]);
117 * Creates a new MeasureVariations from the current array.
119 public Optional<MeasureVariations> toMeasureVariations() {
121 return Optional.empty();
123 Double[] variations = new Double[values.length];
124 for (int i = 0; i < values.length; i++) {
125 if (values[i].isSet()) {
126 variations[i] = (double) values[i].getValue().getIndex();
129 return Optional.of(new MeasureVariations(variations));
132 private boolean isAnySet() {
133 return Arrays.stream(values).anyMatch(RatingVariationValue::isSet);