3 * Copyright (C) 2009-2021 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;
24 import java.util.Optional;
25 import org.junit.rules.ExternalResource;
27 import static com.google.common.base.Preconditions.checkState;
28 import static java.lang.String.format;
29 import static java.util.Objects.requireNonNull;
31 public class MetricRepositoryRule extends ExternalResource implements MetricRepository {
32 private final Map<String, Metric> metricsByKey = new HashMap<>();
33 private final Map<String, Metric> metricsByUuid = new HashMap<>();
36 * Convenience method to add a {@link Metric} to the repository created from a {@link org.sonar.api.measures.Metric},
37 * most of the time it will be a constant of the {@link org.sonar.api.measures.CoreMetrics} class.
39 * For the id of the created metric, this method uses the hashCode of the metric's key. If you want to specify
40 * the id of the create {@link Metric}, use {@link #add(String, org.sonar.api.measures.Metric)}
43 public MetricRepositoryRule add(org.sonar.api.measures.Metric<?> coreMetric) {
44 add(from(coreMetric));
49 * Convenience method to add a {@link Metric} to the repository created from a {@link org.sonar.api.measures.Metric}
50 * and with the specified id, most of the time it will be a constant of the {@link org.sonar.api.measures.CoreMetrics}
53 public MetricRepositoryRule add(String uuid, org.sonar.api.measures.Metric<?> coreMetric) {
54 add(from(uuid, coreMetric));
58 private static Metric from(org.sonar.api.measures.Metric<?> coreMetric) {
59 return from(Long.toString(coreMetric.getKey().hashCode()), coreMetric);
62 private static Metric from(String uuid, org.sonar.api.measures.Metric<?> coreMetric) {
63 return new MetricImpl(
64 uuid, coreMetric.getKey(), coreMetric.getName(),
65 convert(coreMetric.getType()),
66 coreMetric.getDecimalScale(),
67 coreMetric.getBestValue(), coreMetric.isOptimizedBestValue(), coreMetric.getDeleteHistoricalData());
70 private static Metric.MetricType convert(org.sonar.api.measures.Metric.ValueType coreMetricType) {
71 return Metric.MetricType.valueOf(coreMetricType.name());
74 public MetricRepositoryRule add(Metric metric) {
75 requireNonNull(metric.getKey(), "key can not be null");
77 checkState(!metricsByKey.containsKey(metric.getKey()), format("Repository already contains a metric for key %s", metric.getKey()));
78 checkState(!metricsByUuid.containsKey(metric.getUuid()), format("Repository already contains a metric for id %s", metric.getUuid()));
80 metricsByKey.put(metric.getKey(), metric);
81 metricsByUuid.put(metric.getUuid(), metric);
87 protected void after() {
88 this.metricsByUuid.clear();
89 this.metricsByUuid.clear();
93 public Metric getByKey(String key) {
94 Metric res = metricsByKey.get(key);
95 checkState(res != null, format("No Metric can be found for key %s", key));
100 public Metric getByUuid(String uuid) {
101 Metric res = metricsByUuid.get(uuid);
102 checkState(res != null, format("No Metric can be found for uuid %s", uuid));
107 public Optional<Metric> getOptionalByUuid(String uuid) {
108 return Optional.of(metricsByUuid.get(uuid));
112 public Iterable<Metric> getAll() {
113 return metricsByKey.values();