3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.server.computation.task.projectanalysis.api.measurecomputer;
22 import com.google.common.base.Function;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Predicates;
25 import com.google.common.collect.FluentIterable;
26 import java.util.HashSet;
27 import java.util.List;
29 import javax.annotation.CheckForNull;
30 import javax.annotation.Nonnull;
31 import javax.annotation.Nullable;
32 import org.sonar.api.ce.measure.Component;
33 import org.sonar.api.ce.measure.Issue;
34 import org.sonar.api.ce.measure.Measure;
35 import org.sonar.api.ce.measure.Settings;
36 import org.sonar.core.issue.DefaultIssue;
37 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
38 import org.sonar.server.computation.task.projectanalysis.issue.ComponentIssuesRepository;
39 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
40 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
41 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
43 import static com.google.common.base.Preconditions.checkArgument;
44 import static org.sonar.api.ce.measure.MeasureComputer.MeasureComputerContext;
45 import static org.sonar.api.ce.measure.MeasureComputer.MeasureComputerDefinition;
46 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.newMeasureBuilder;
48 public class MeasureComputerContextImpl implements MeasureComputerContext {
50 private final SettingsRepository settings;
51 private final MeasureRepository measureRepository;
52 private final MetricRepository metricRepository;
54 private final org.sonar.server.computation.task.projectanalysis.component.Component internalComponent;
55 private final Component component;
56 private final List<DefaultIssue> componentIssues;
58 private MeasureComputerDefinition definition;
59 private Set<String> allowedMetrics;
61 public MeasureComputerContextImpl(org.sonar.server.computation.task.projectanalysis.component.Component component, SettingsRepository settings,
62 MeasureRepository measureRepository, MetricRepository metricRepository, ComponentIssuesRepository componentIssuesRepository) {
63 this.settings = settings;
64 this.internalComponent = component;
65 this.measureRepository = measureRepository;
66 this.metricRepository = metricRepository;
67 this.component = newComponent(component);
68 this.componentIssues = componentIssuesRepository.getIssues(component);
72 * Definition needs to be reset each time a new computer is processed.
73 * 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).
75 public MeasureComputerContextImpl setDefinition(MeasureComputerDefinition definition) {
76 this.definition = definition;
77 this.allowedMetrics = allowedMetric(definition);
81 private static Set<String> allowedMetric(MeasureComputerDefinition definition) {
82 Set<String> allowedMetrics = new HashSet<>();
83 allowedMetrics.addAll(definition.getInputMetrics());
84 allowedMetrics.addAll(definition.getOutputMetrics());
85 return allowedMetrics;
89 public Component getComponent() {
94 public Settings getSettings() {
95 return new Settings() {
98 public String getString(String key) {
99 return getComponentSettings().getString(key);
103 public String[] getStringArray(String key) {
104 return getComponentSettings().getStringArray(key);
109 private org.sonar.api.config.Settings getComponentSettings() {
110 return settings.getSettings(internalComponent);
115 public Measure getMeasure(String metric) {
116 validateInputMetric(metric);
117 Optional<org.sonar.server.computation.task.projectanalysis.measure.Measure> measure = measureRepository.getRawMeasure(internalComponent, metricRepository.getByKey(metric));
118 if (measure.isPresent()) {
119 return new MeasureImpl(measure.get());
125 public Iterable<Measure> getChildrenMeasures(String metric) {
126 validateInputMetric(metric);
127 return FluentIterable.from(internalComponent.getChildren())
128 .transform(new ComponentToMeasure(metricRepository.getByKey(metric)))
129 .transform(ToMeasureAPI.INSTANCE)
130 .filter(Predicates.notNull());
134 public void addMeasure(String metricKey, int value) {
135 Metric metric = metricRepository.getByKey(metricKey);
136 validateAddMeasure(metric);
137 measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
141 public void addMeasure(String metricKey, double value) {
142 Metric metric = metricRepository.getByKey(metricKey);
143 validateAddMeasure(metric);
144 measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value, metric.getDecimalScale()));
148 public void addMeasure(String metricKey, long value) {
149 Metric metric = metricRepository.getByKey(metricKey);
150 validateAddMeasure(metric);
151 measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
155 public void addMeasure(String metricKey, String value) {
156 Metric metric = metricRepository.getByKey(metricKey);
157 validateAddMeasure(metric);
158 measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
162 public void addMeasure(String metricKey, boolean value) {
163 Metric metric = metricRepository.getByKey(metricKey);
164 validateAddMeasure(metric);
165 measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
168 private void validateInputMetric(String metric) {
169 checkArgument(allowedMetrics.contains(metric), "Only metrics in %s can be used to load measures", definition.getInputMetrics());
172 private void validateAddMeasure(Metric metric) {
173 checkArgument(definition.getOutputMetrics().contains(metric.getKey()), "Only metrics in %s can be used to add measures. Metric '%s' is not allowed.",
174 definition.getOutputMetrics(), metric.getKey());
175 if (measureRepository.getRawMeasure(internalComponent, metric).isPresent()) {
176 throw new UnsupportedOperationException(String.format("A measure on metric '%s' already exists on component '%s'", metric.getKey(), internalComponent.getKey()));
181 public List<? extends Issue> getIssues() {
182 return componentIssues;
185 private static Component newComponent(org.sonar.server.computation.task.projectanalysis.component.Component component) {
186 return new ComponentImpl(
188 Component.Type.valueOf(component.getType().name()),
189 component.getType() == org.sonar.server.computation.task.projectanalysis.component.Component.Type.FILE
190 ? new ComponentImpl.FileAttributesImpl(component.getFileAttributes().getLanguageKey(), component.getFileAttributes().isUnitTest()) : null);
193 private class ComponentToMeasure implements Function<org.sonar.server.computation.task.projectanalysis.component.Component, Optional<org.sonar.server.computation.task.projectanalysis.measure.Measure>> {
195 private final Metric metric;
197 public ComponentToMeasure(Metric metric) {
198 this.metric = metric;
202 public Optional<org.sonar.server.computation.task.projectanalysis.measure.Measure> apply(@Nonnull org.sonar.server.computation.task.projectanalysis.component.Component input) {
203 return measureRepository.getRawMeasure(input, metric);
207 private enum ToMeasureAPI implements Function<Optional<org.sonar.server.computation.task.projectanalysis.measure.Measure>, Measure> {
212 public Measure apply(@Nonnull Optional<org.sonar.server.computation.task.projectanalysis.measure.Measure> input) {
213 return input.isPresent() ? new MeasureImpl(input.get()) : null;