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.MeasureDto;
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 MeasureDtoToMeasure {
33 public Optional<Measure> toMeasure(@Nullable MeasureDto measureDto, Metric metric) {
34 requireNonNull(metric);
35 if (measureDto == null) {
36 return Optional.empty();
38 Double value = measureDto.getValue();
39 String data = measureDto.getData();
40 switch (metric.getType().getValueType()) {
42 return toIntegerMeasure(measureDto, value, data);
44 return toLongMeasure(measureDto, value, data);
46 return toDoubleMeasure(measureDto, value, data);
48 return toBooleanMeasure(measureDto, value, data);
50 return toStringMeasure(measureDto, data);
52 return toLevelMeasure(measureDto, data);
54 return toNoValueMeasure(measureDto);
56 throw new IllegalArgumentException("Unsupported Measure.ValueType " + metric.getType().getValueType());
60 private static Optional<Measure> toIntegerMeasure(MeasureDto measureDto, @Nullable Double value, String data) {
62 return toNoValueMeasure(measureDto);
64 return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).create(value.intValue(), data));
67 private static Optional<Measure> toLongMeasure(MeasureDto measureDto, @Nullable Double value, String data) {
69 return toNoValueMeasure(measureDto);
71 return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).create(value.longValue(), data));
74 private static Optional<Measure> toDoubleMeasure(MeasureDto measureDto, @Nullable Double value, String data) {
76 return toNoValueMeasure(measureDto);
79 return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto)
80 .create(value, org.sonar.api.measures.Metric.MAX_DECIMAL_SCALE, data));
83 private static Optional<Measure> toBooleanMeasure(MeasureDto measureDto, @Nullable Double value, String data) {
85 return toNoValueMeasure(measureDto);
87 return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).create(Double.compare(value, 1.0D) == 0, data));
90 private static Optional<Measure> toStringMeasure(MeasureDto measureDto, @Nullable String data) {
92 return toNoValueMeasure(measureDto);
94 return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).create(data));
97 private static Optional<Measure> toLevelMeasure(MeasureDto measureDto, @Nullable String data) {
99 return toNoValueMeasure(measureDto);
101 Optional<Measure.Level> level = toLevel(data);
102 if (!level.isPresent()) {
103 return toNoValueMeasure(measureDto);
105 return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).create(level.get()));
108 private static Optional<Measure> toNoValueMeasure(MeasureDto measureDto) {
109 return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).createNoValue());
112 private static Measure.NewMeasureBuilder setCommonProperties(Measure.NewMeasureBuilder builder, MeasureDto measureDto) {
113 if (measureDto.getAlertStatus() != null) {
114 Optional<Measure.Level> qualityGateStatus = toLevel(measureDto.getAlertStatus());
115 if (qualityGateStatus.isPresent()) {
116 builder.setQualityGateStatus(new QualityGateStatus(qualityGateStatus.get(), measureDto.getAlertText()));