]> source.dussan.org Git - sonarqube.git/blob
93fa85f200c8f5aef5f62b6d8455ea1278bfb0f7
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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.Objects;
23 import java.util.Optional;
24 import javax.annotation.Nullable;
25 import org.sonar.ce.task.projectanalysis.metric.Metric;
26 import org.sonar.scanner.protocol.output.ScannerReport;
27 import org.sonar.scanner.protocol.output.ScannerReport.Measure.ValueCase;
28
29 import static java.util.Optional.of;
30 import static org.apache.commons.lang.StringUtils.trimToNull;
31
32 public class BatchMeasureToMeasure {
33
34   public Optional<Measure> toMeasure(@Nullable ScannerReport.Measure batchMeasure, Metric metric) {
35     Objects.requireNonNull(metric);
36     if (batchMeasure == null) {
37       return Optional.empty();
38     }
39
40     Measure.NewMeasureBuilder builder = Measure.newMeasureBuilder();
41     switch (metric.getType().getValueType()) {
42       case INT:
43         return toIntegerMeasure(builder, batchMeasure);
44       case LONG:
45         return toLongMeasure(builder, batchMeasure);
46       case DOUBLE:
47         return toDoubleMeasure(builder, batchMeasure);
48       case BOOLEAN:
49         return toBooleanMeasure(builder, batchMeasure);
50       case STRING:
51         return toStringMeasure(builder, batchMeasure);
52       case LEVEL:
53         return toLevelMeasure(builder, batchMeasure);
54       case NO_VALUE:
55         return toNoValueMeasure(builder, batchMeasure);
56       default:
57         throw new IllegalArgumentException("Unsupported Measure.ValueType " + metric.getType().getValueType());
58     }
59   }
60
61   private static Optional<Measure> toIntegerMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
62     if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
63       return toNoValueMeasure(builder, batchMeasure);
64     }
65     return of(builder.create(batchMeasure.getIntValue().getValue(), trimToNull(batchMeasure.getIntValue().getData())));
66   }
67
68   private static Optional<Measure> toLongMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
69     if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
70       return toNoValueMeasure(builder, batchMeasure);
71     }
72     return of(builder.create(batchMeasure.getLongValue().getValue(), trimToNull(batchMeasure.getLongValue().getData())));
73   }
74
75   private static Optional<Measure> toDoubleMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
76     if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
77       return toNoValueMeasure(builder, batchMeasure);
78     }
79     return of(builder.create(batchMeasure.getDoubleValue().getValue(),
80       // Decimals are not truncated in scanner report, so an arbitrary decimal scale is applied when reading values from report
81       org.sonar.api.measures.Metric.MAX_DECIMAL_SCALE, trimToNull(batchMeasure.getDoubleValue().getData())));
82   }
83
84   private static Optional<Measure> toBooleanMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
85     if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
86       return toNoValueMeasure(builder, batchMeasure);
87     }
88     return of(builder.create(batchMeasure.getBooleanValue().getValue(), trimToNull(batchMeasure.getBooleanValue().getData())));
89   }
90
91   private static Optional<Measure> toStringMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
92     if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
93       return toNoValueMeasure(builder, batchMeasure);
94     }
95     return of(builder.create(batchMeasure.getStringValue().getValue()));
96   }
97
98   private static Optional<Measure> toLevelMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
99     if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
100       return toNoValueMeasure(builder, batchMeasure);
101     }
102     Optional<Measure.Level> level = Measure.Level.toLevel(batchMeasure.getStringValue().getValue());
103     if (!level.isPresent()) {
104       return toNoValueMeasure(builder, batchMeasure);
105     }
106     return of(builder.create(level.get()));
107   }
108
109   private static Optional<Measure> toNoValueMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
110     return of(builder.createNoValue());
111   }
112 }