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.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;
29 import static java.util.Optional.of;
30 import static org.apache.commons.lang3.StringUtils.trimToNull;
32 public class BatchMeasureToMeasure {
34 public Optional<Measure> toMeasure(@Nullable ScannerReport.Measure batchMeasure, Metric metric) {
35 Objects.requireNonNull(metric);
36 if (batchMeasure == null) {
37 return Optional.empty();
40 Measure.NewMeasureBuilder builder = Measure.newMeasureBuilder();
41 switch (metric.getType().getValueType()) {
43 return toIntegerMeasure(builder, batchMeasure);
45 return toLongMeasure(builder, batchMeasure);
47 return toDoubleMeasure(builder, batchMeasure);
49 return toBooleanMeasure(builder, batchMeasure);
51 return toStringMeasure(builder, batchMeasure);
53 return toLevelMeasure(builder, batchMeasure);
55 return toNoValueMeasure(builder);
57 throw new IllegalArgumentException("Unsupported Measure.ValueType " + metric.getType().getValueType());
61 private static Optional<Measure> toIntegerMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
62 if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
63 return toNoValueMeasure(builder);
65 return of(builder.create(batchMeasure.getIntValue().getValue(), trimToNull(batchMeasure.getIntValue().getData())));
68 private static Optional<Measure> toLongMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
69 if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
70 return toNoValueMeasure(builder);
72 return of(builder.create(batchMeasure.getLongValue().getValue(), trimToNull(batchMeasure.getLongValue().getData())));
75 private static Optional<Measure> toDoubleMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
76 if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
77 return toNoValueMeasure(builder);
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())));
84 private static Optional<Measure> toBooleanMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
85 if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
86 return toNoValueMeasure(builder);
88 return of(builder.create(batchMeasure.getBooleanValue().getValue(), trimToNull(batchMeasure.getBooleanValue().getData())));
91 private static Optional<Measure> toStringMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
92 if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
93 return toNoValueMeasure(builder);
95 return of(builder.create(batchMeasure.getStringValue().getValue()));
98 private static Optional<Measure> toLevelMeasure(Measure.NewMeasureBuilder builder, ScannerReport.Measure batchMeasure) {
99 if (batchMeasure.getValueCase() == ValueCase.VALUE_NOT_SET) {
100 return toNoValueMeasure(builder);
102 Optional<Measure.Level> level = Measure.Level.toLevel(batchMeasure.getStringValue().getValue());
103 if (level.isEmpty()) {
104 return toNoValueMeasure(builder);
106 return of(builder.create(level.get()));
109 private static Optional<Measure> toNoValueMeasure(Measure.NewMeasureBuilder builder) {
110 return of(builder.createNoValue());