3 * Copyright (C) 2009-2022 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.api.measurecomputer;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Objects;
25 import java.util.Optional;
27 import java.util.function.Function;
28 import javax.annotation.CheckForNull;
29 import javax.annotation.Nonnull;
30 import javax.annotation.Nullable;
31 import org.sonar.api.ce.measure.Component;
32 import org.sonar.api.ce.measure.Issue;
33 import org.sonar.api.ce.measure.Measure;
34 import org.sonar.api.ce.measure.MeasureComputer.MeasureComputerContext;
35 import org.sonar.api.ce.measure.MeasureComputer.MeasureComputerDefinition;
36 import org.sonar.api.ce.measure.Settings;
37 import org.sonar.ce.task.projectanalysis.component.ConfigurationRepository;
38 import org.sonar.ce.task.projectanalysis.issue.ComponentIssuesRepository;
39 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
40 import org.sonar.ce.task.projectanalysis.metric.Metric;
41 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
42 import org.sonar.core.issue.DefaultIssue;
44 import static com.google.common.base.Preconditions.checkArgument;
45 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
47 public class MeasureComputerContextImpl implements MeasureComputerContext {
49 private final ConfigurationRepository config;
50 private final MeasureRepository measureRepository;
51 private final MetricRepository metricRepository;
53 private final org.sonar.ce.task.projectanalysis.component.Component internalComponent;
54 private final Component component;
55 private final List<DefaultIssue> componentIssues;
57 private MeasureComputerDefinition definition;
58 private Set<String> allowedMetrics;
60 public MeasureComputerContextImpl(org.sonar.ce.task.projectanalysis.component.Component component, ConfigurationRepository config,
61 MeasureRepository measureRepository, MetricRepository metricRepository, ComponentIssuesRepository componentIssuesRepository) {
63 this.internalComponent = component;
64 this.measureRepository = measureRepository;
65 this.metricRepository = metricRepository;
66 this.component = newComponent(component);
67 this.componentIssues = componentIssuesRepository.getIssues(component);
71 * Definition needs to be reset each time a new computer is processed.
72 * Defining it by a setter allows to reduce the number of this class to be created (one per component instead of one per component and per computer).
74 public MeasureComputerContextImpl setDefinition(MeasureComputerDefinition definition) {
75 this.definition = definition;
76 this.allowedMetrics = allowedMetric(definition);
80 private static Set<String> allowedMetric(MeasureComputerDefinition definition) {
81 Set<String> allowedMetrics = new HashSet<>();
82 allowedMetrics.addAll(definition.getInputMetrics());
83 allowedMetrics.addAll(definition.getOutputMetrics());
84 return allowedMetrics;
88 public Component getComponent() {
93 public Settings getSettings() {
94 return new Settings() {
97 public String getString(String key) {
98 return config.getConfiguration().get(key).orElse(null);
102 public String[] getStringArray(String key) {
103 return config.getConfiguration().getStringArray(key);
110 public Measure getMeasure(String metric) {
111 validateInputMetric(metric);
112 Optional<org.sonar.ce.task.projectanalysis.measure.Measure> measure = measureRepository.getRawMeasure(internalComponent, metricRepository.getByKey(metric));
113 if (measure.isPresent()) {
114 return new MeasureImpl(measure.get());
120 public Iterable<Measure> getChildrenMeasures(String metric) {
121 validateInputMetric(metric);
122 return () -> internalComponent.getChildren().stream()
123 .map(new ComponentToMeasure(metricRepository.getByKey(metric)))
124 .map(ToMeasureAPI.INSTANCE)
125 .filter(Objects::nonNull)
130 public void addMeasure(String metricKey, int value) {
131 Metric metric = metricRepository.getByKey(metricKey);
132 validateAddMeasure(metric);
133 measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
137 public void addMeasure(String metricKey, double value) {
138 Metric metric = metricRepository.getByKey(metricKey);
139 validateAddMeasure(metric);
140 measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value, metric.getDecimalScale()));
144 public void addMeasure(String metricKey, long value) {
145 Metric metric = metricRepository.getByKey(metricKey);
146 validateAddMeasure(metric);
147 measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
151 public void addMeasure(String metricKey, String value) {
152 Metric metric = metricRepository.getByKey(metricKey);
153 validateAddMeasure(metric);
154 measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
158 public void addMeasure(String metricKey, boolean value) {
159 Metric metric = metricRepository.getByKey(metricKey);
160 validateAddMeasure(metric);
161 measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
164 private void validateInputMetric(String metric) {
165 checkArgument(allowedMetrics.contains(metric), "Only metrics in %s can be used to load measures", definition.getInputMetrics());
168 private void validateAddMeasure(Metric metric) {
169 checkArgument(definition.getOutputMetrics().contains(metric.getKey()), "Only metrics in %s can be used to add measures. Metric '%s' is not allowed.",
170 definition.getOutputMetrics(), metric.getKey());
171 if (measureRepository.getRawMeasure(internalComponent, metric).isPresent()) {
172 throw new UnsupportedOperationException(String.format("A measure on metric '%s' already exists on component '%s'", metric.getKey(), internalComponent.getDbKey()));
177 public List<? extends Issue> getIssues() {
178 return componentIssues;
181 private static Component newComponent(org.sonar.ce.task.projectanalysis.component.Component component) {
182 return new ComponentImpl(
183 component.getDbKey(),
184 Component.Type.valueOf(component.getType().name()),
185 component.getType() == org.sonar.ce.task.projectanalysis.component.Component.Type.FILE
186 ? new ComponentImpl.FileAttributesImpl(component.getFileAttributes().getLanguageKey(), component.getFileAttributes().isUnitTest())
190 private class ComponentToMeasure
191 implements Function<org.sonar.ce.task.projectanalysis.component.Component, Optional<org.sonar.ce.task.projectanalysis.measure.Measure>> {
193 private final Metric metric;
195 public ComponentToMeasure(Metric metric) {
196 this.metric = metric;
200 public Optional<org.sonar.ce.task.projectanalysis.measure.Measure> apply(@Nonnull org.sonar.ce.task.projectanalysis.component.Component input) {
201 return measureRepository.getRawMeasure(input, metric);
205 private enum ToMeasureAPI implements Function<Optional<org.sonar.ce.task.projectanalysis.measure.Measure>, Measure> {
210 public Measure apply(@Nonnull Optional<org.sonar.ce.task.projectanalysis.measure.Measure> input) {
211 return input.isPresent() ? new MeasureImpl(input.get()) : null;