3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info 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.ce.task.projectanalysis.measure;
22 import java.util.Optional;
23 import javax.annotation.Nullable;
24 import org.sonar.ce.task.projectanalysis.metric.Metric;
25 import org.sonar.db.measure.LiveMeasureDto;
27 import static java.util.Objects.requireNonNull;
28 import static java.util.Optional.of;
29 import static org.sonar.ce.task.projectanalysis.measure.Measure.Level.toLevel;
31 public class LiveMeasureDtoToMeasure {
33 public Optional<Measure> toMeasure(@Nullable LiveMeasureDto measureDto, Metric metric) {
34 requireNonNull(metric);
35 if (measureDto == null) {
36 return Optional.empty();
38 Double value = measureDto.getValue();
39 String data = measureDto.getDataAsString();
40 switch (metric.getType().getValueType()) {
42 return toIntegerMeasure(value, data);
44 return toLongMeasure(value, data);
46 return toDoubleMeasure(value, data);
48 return toBooleanMeasure(value, data);
50 return toStringMeasure(data);
52 return toLevelMeasure(data);
54 return toNoValueMeasure();
56 throw new IllegalArgumentException("Unsupported Measure.ValueType " + metric.getType().getValueType());
60 private static Optional<Measure> toIntegerMeasure(@Nullable Double value, @Nullable String data) {
62 return toNoValueMeasure();
64 return of(Measure.newMeasureBuilder().create(value.intValue(), data));
67 private static Optional<Measure> toLongMeasure(@Nullable Double value, @Nullable String data) {
69 return toNoValueMeasure();
71 return of(Measure.newMeasureBuilder().create(value.longValue(), data));
74 private static Optional<Measure> toDoubleMeasure(@Nullable Double value, @Nullable String data) {
76 return toNoValueMeasure();
79 return of(Measure.newMeasureBuilder().create(value, org.sonar.api.measures.Metric.MAX_DECIMAL_SCALE, data));
82 private static Optional<Measure> toBooleanMeasure(@Nullable Double value, @Nullable String data) {
84 return toNoValueMeasure();
86 return of(Measure.newMeasureBuilder().create(Double.compare(value, 1.0D) == 0, data));
89 private static Optional<Measure> toStringMeasure(@Nullable String data) {
91 return toNoValueMeasure();
93 return of(Measure.newMeasureBuilder().create(data));
96 private static Optional<Measure> toLevelMeasure(@Nullable String data) {
98 return toNoValueMeasure();
100 Optional<Measure.Level> level = toLevel(data);
101 if (!level.isPresent()) {
102 return toNoValueMeasure();
104 return of(Measure.newMeasureBuilder().create(level.get()));
107 private static Optional<Measure> toNoValueMeasure() {
108 return of(Measure.newMeasureBuilder().createNoValue());