3 * Copyright (C) 2009-2023 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.metric;
22 import java.util.HashMap;
23 import java.util.List;
25 import java.util.Optional;
26 import java.util.stream.Collectors;
28 import org.junit.rules.ExternalResource;
30 import static com.google.common.base.Preconditions.checkState;
31 import static java.lang.String.format;
32 import static java.util.Objects.requireNonNull;
34 public class MetricRepositoryRule extends ExternalResource implements MetricRepository {
35 private final Map<String, Metric> metricsByKey = new HashMap<>();
36 private final Map<String, Metric> metricsByUuid = new HashMap<>();
39 * Convenience method to add a {@link Metric} to the repository created from a {@link org.sonar.api.measures.Metric},
40 * most of the time it will be a constant of the {@link org.sonar.api.measures.CoreMetrics} class.
42 * For the id of the created metric, this method uses the hashCode of the metric's key. If you want to specify
43 * the id of the create {@link Metric}, use {@link #add(String, org.sonar.api.measures.Metric)}
46 public MetricRepositoryRule add(org.sonar.api.measures.Metric<?> coreMetric) {
47 add(from(coreMetric));
52 * Convenience method to add a {@link Metric} to the repository created from a {@link org.sonar.api.measures.Metric}
53 * and with the specified id, most of the time it will be a constant of the {@link org.sonar.api.measures.CoreMetrics}
56 public MetricRepositoryRule add(String uuid, org.sonar.api.measures.Metric<?> coreMetric) {
57 add(from(uuid, coreMetric));
61 private static Metric from(org.sonar.api.measures.Metric<?> coreMetric) {
62 return from(Long.toString(coreMetric.getKey().hashCode()), coreMetric);
65 private static Metric from(String uuid, org.sonar.api.measures.Metric<?> coreMetric) {
66 return new MetricImpl(
67 uuid, coreMetric.getKey(), coreMetric.getName(),
68 convert(coreMetric.getType()),
69 coreMetric.getDecimalScale(),
70 coreMetric.getBestValue(), coreMetric.isOptimizedBestValue(), coreMetric.getDeleteHistoricalData());
73 private static Metric.MetricType convert(org.sonar.api.measures.Metric.ValueType coreMetricType) {
74 return Metric.MetricType.valueOf(coreMetricType.name());
77 public MetricRepositoryRule add(Metric metric) {
78 requireNonNull(metric.getKey(), "key can not be null");
80 checkState(!metricsByKey.containsKey(metric.getKey()), format("Repository already contains a metric for key %s", metric.getKey()));
81 checkState(!metricsByUuid.containsKey(metric.getUuid()), format("Repository already contains a metric for id %s", metric.getUuid()));
83 metricsByKey.put(metric.getKey(), metric);
84 metricsByUuid.put(metric.getUuid(), metric);
90 protected void after() {
91 this.metricsByUuid.clear();
92 this.metricsByUuid.clear();
96 public Metric getByKey(String key) {
97 Metric res = metricsByKey.get(key);
98 checkState(res != null, format("No Metric can be found for key %s", key));
103 public Metric getByUuid(String uuid) {
104 Metric res = metricsByUuid.get(uuid);
105 checkState(res != null, format("No Metric can be found for uuid %s", uuid));
110 public Optional<Metric> getOptionalByUuid(String uuid) {
111 return Optional.of(metricsByUuid.get(uuid));
115 public Iterable<Metric> getAll() {
116 return metricsByKey.values();
120 public List<Metric> getMetricsByType(Metric.MetricType type) {
121 return metricsByKey.values().stream().filter(m -> m.getType() == type).collect(Collectors.toList());