You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MeasureComputerContextImpl.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.api.measurecomputer;
  21. import com.google.common.base.Function;
  22. import com.google.common.base.Predicates;
  23. import com.google.common.collect.FluentIterable;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Optional;
  27. import java.util.Set;
  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;
  43. import static com.google.common.base.Preconditions.checkArgument;
  44. import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
  45. public class MeasureComputerContextImpl implements MeasureComputerContext {
  46. private final ConfigurationRepository config;
  47. private final MeasureRepository measureRepository;
  48. private final MetricRepository metricRepository;
  49. private final org.sonar.ce.task.projectanalysis.component.Component internalComponent;
  50. private final Component component;
  51. private final List<DefaultIssue> componentIssues;
  52. private MeasureComputerDefinition definition;
  53. private Set<String> allowedMetrics;
  54. public MeasureComputerContextImpl(org.sonar.ce.task.projectanalysis.component.Component component, ConfigurationRepository config,
  55. MeasureRepository measureRepository, MetricRepository metricRepository, ComponentIssuesRepository componentIssuesRepository) {
  56. this.config = config;
  57. this.internalComponent = component;
  58. this.measureRepository = measureRepository;
  59. this.metricRepository = metricRepository;
  60. this.component = newComponent(component);
  61. this.componentIssues = componentIssuesRepository.getIssues(component);
  62. }
  63. /**
  64. * Definition needs to be reset each time a new computer is processed.
  65. * 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).
  66. */
  67. public MeasureComputerContextImpl setDefinition(MeasureComputerDefinition definition) {
  68. this.definition = definition;
  69. this.allowedMetrics = allowedMetric(definition);
  70. return this;
  71. }
  72. private static Set<String> allowedMetric(MeasureComputerDefinition definition) {
  73. Set<String> allowedMetrics = new HashSet<>();
  74. allowedMetrics.addAll(definition.getInputMetrics());
  75. allowedMetrics.addAll(definition.getOutputMetrics());
  76. return allowedMetrics;
  77. }
  78. @Override
  79. public Component getComponent() {
  80. return component;
  81. }
  82. @Override
  83. public Settings getSettings() {
  84. return new Settings() {
  85. @Override
  86. @CheckForNull
  87. public String getString(String key) {
  88. return config.getConfiguration().get(key).orElse(null);
  89. }
  90. @Override
  91. public String[] getStringArray(String key) {
  92. return config.getConfiguration().getStringArray(key);
  93. }
  94. };
  95. }
  96. @Override
  97. @CheckForNull
  98. public Measure getMeasure(String metric) {
  99. validateInputMetric(metric);
  100. Optional<org.sonar.ce.task.projectanalysis.measure.Measure> measure = measureRepository.getRawMeasure(internalComponent, metricRepository.getByKey(metric));
  101. if (measure.isPresent()) {
  102. return new MeasureImpl(measure.get());
  103. }
  104. return null;
  105. }
  106. @Override
  107. public Iterable<Measure> getChildrenMeasures(String metric) {
  108. validateInputMetric(metric);
  109. return FluentIterable.from(internalComponent.getChildren())
  110. .transform(new ComponentToMeasure(metricRepository.getByKey(metric)))
  111. .transform(ToMeasureAPI.INSTANCE)
  112. .filter(Predicates.notNull());
  113. }
  114. @Override
  115. public void addMeasure(String metricKey, int value) {
  116. Metric metric = metricRepository.getByKey(metricKey);
  117. validateAddMeasure(metric);
  118. measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
  119. }
  120. @Override
  121. public void addMeasure(String metricKey, double value) {
  122. Metric metric = metricRepository.getByKey(metricKey);
  123. validateAddMeasure(metric);
  124. measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value, metric.getDecimalScale()));
  125. }
  126. @Override
  127. public void addMeasure(String metricKey, long value) {
  128. Metric metric = metricRepository.getByKey(metricKey);
  129. validateAddMeasure(metric);
  130. measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
  131. }
  132. @Override
  133. public void addMeasure(String metricKey, String value) {
  134. Metric metric = metricRepository.getByKey(metricKey);
  135. validateAddMeasure(metric);
  136. measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
  137. }
  138. @Override
  139. public void addMeasure(String metricKey, boolean value) {
  140. Metric metric = metricRepository.getByKey(metricKey);
  141. validateAddMeasure(metric);
  142. measureRepository.add(internalComponent, metric, newMeasureBuilder().create(value));
  143. }
  144. private void validateInputMetric(String metric) {
  145. checkArgument(allowedMetrics.contains(metric), "Only metrics in %s can be used to load measures", definition.getInputMetrics());
  146. }
  147. private void validateAddMeasure(Metric metric) {
  148. checkArgument(definition.getOutputMetrics().contains(metric.getKey()), "Only metrics in %s can be used to add measures. Metric '%s' is not allowed.",
  149. definition.getOutputMetrics(), metric.getKey());
  150. if (measureRepository.getRawMeasure(internalComponent, metric).isPresent()) {
  151. throw new UnsupportedOperationException(String.format("A measure on metric '%s' already exists on component '%s'", metric.getKey(), internalComponent.getDbKey()));
  152. }
  153. }
  154. @Override
  155. public List<? extends Issue> getIssues() {
  156. return componentIssues;
  157. }
  158. private static Component newComponent(org.sonar.ce.task.projectanalysis.component.Component component) {
  159. return new ComponentImpl(
  160. component.getDbKey(),
  161. Component.Type.valueOf(component.getType().name()),
  162. component.getType() == org.sonar.ce.task.projectanalysis.component.Component.Type.FILE
  163. ? new ComponentImpl.FileAttributesImpl(component.getFileAttributes().getLanguageKey(), component.getFileAttributes().isUnitTest())
  164. : null);
  165. }
  166. private class ComponentToMeasure
  167. implements Function<org.sonar.ce.task.projectanalysis.component.Component, Optional<org.sonar.ce.task.projectanalysis.measure.Measure>> {
  168. private final Metric metric;
  169. public ComponentToMeasure(Metric metric) {
  170. this.metric = metric;
  171. }
  172. @Override
  173. public Optional<org.sonar.ce.task.projectanalysis.measure.Measure> apply(@Nonnull org.sonar.ce.task.projectanalysis.component.Component input) {
  174. return measureRepository.getRawMeasure(input, metric);
  175. }
  176. }
  177. private enum ToMeasureAPI implements Function<Optional<org.sonar.ce.task.projectanalysis.measure.Measure>, Measure> {
  178. INSTANCE;
  179. @Nullable
  180. @Override
  181. public Measure apply(@Nonnull Optional<org.sonar.ce.task.projectanalysis.measure.Measure> input) {
  182. return input.isPresent() ? new MeasureImpl(input.get()) : null;
  183. }
  184. }
  185. }