]> source.dussan.org Git - sonarqube.git/blob
a0edc84c61d8fccab97115d0b340bee24547dfc7
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.measure;
21
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;
26
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;
30
31 public class MeasureDtoToMeasure {
32
33   public Optional<Measure> toMeasure(@Nullable MeasureDto measureDto, Metric metric) {
34     requireNonNull(metric);
35     if (measureDto == null) {
36       return Optional.empty();
37     }
38     Double value = measureDto.getValue();
39     String data = measureDto.getData();
40     switch (metric.getType().getValueType()) {
41       case INT:
42         return toIntegerMeasure(measureDto, value, data);
43       case LONG:
44         return toLongMeasure(measureDto, value, data);
45       case DOUBLE:
46         return toDoubleMeasure(measureDto, value, data);
47       case BOOLEAN:
48         return toBooleanMeasure(measureDto, value, data);
49       case STRING:
50         return toStringMeasure(measureDto, data);
51       case LEVEL:
52         return toLevelMeasure(measureDto, data);
53       case NO_VALUE:
54         return toNoValueMeasure(measureDto);
55       default:
56         throw new IllegalArgumentException("Unsupported Measure.ValueType " + metric.getType().getValueType());
57     }
58   }
59
60   private static Optional<Measure> toIntegerMeasure(MeasureDto measureDto, @Nullable Double value, String data) {
61     if (value == null) {
62       return toNoValueMeasure(measureDto);
63     }
64     return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).create(value.intValue(), data));
65   }
66
67   private static Optional<Measure> toLongMeasure(MeasureDto measureDto, @Nullable Double value, String data) {
68     if (value == null) {
69       return toNoValueMeasure(measureDto);
70     }
71     return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).create(value.longValue(), data));
72   }
73
74   private static Optional<Measure> toDoubleMeasure(MeasureDto measureDto, @Nullable Double value, String data) {
75     if (value == null) {
76       return toNoValueMeasure(measureDto);
77     }
78
79     return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto)
80       .create(value, org.sonar.api.measures.Metric.MAX_DECIMAL_SCALE, data));
81   }
82
83   private static Optional<Measure> toBooleanMeasure(MeasureDto measureDto, @Nullable Double value, String data) {
84     if (value == null) {
85       return toNoValueMeasure(measureDto);
86     }
87     return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).create(Double.compare(value, 1.0D) == 0, data));
88   }
89
90   private static Optional<Measure> toStringMeasure(MeasureDto measureDto, @Nullable String data) {
91     if (data == null) {
92       return toNoValueMeasure(measureDto);
93     }
94     return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).create(data));
95   }
96
97   private static Optional<Measure> toLevelMeasure(MeasureDto measureDto, @Nullable String data) {
98     if (data == null) {
99       return toNoValueMeasure(measureDto);
100     }
101     Optional<Measure.Level> level = toLevel(data);
102     if (!level.isPresent()) {
103       return toNoValueMeasure(measureDto);
104     }
105     return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).create(level.get()));
106   }
107
108   private static Optional<Measure> toNoValueMeasure(MeasureDto measureDto) {
109     return of(setCommonProperties(Measure.newMeasureBuilder(), measureDto).createNoValue());
110   }
111
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()));
117       }
118     }
119     return builder;
120   }
121 }