3 * Copyright (C) 2009-2019 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<Long, Metric> metricsById = 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(int, 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(int id, org.sonar.api.measures.Metric<?> coreMetric) {
54 add(from(id, coreMetric));
58 private static Metric from(org.sonar.api.measures.Metric<?> coreMetric) {
59 return from(coreMetric.getKey().hashCode(), coreMetric);
62 private static Metric from(int id, org.sonar.api.measures.Metric<?> coreMetric) {
63 return new MetricImpl(
64 id, coreMetric.getKey(), coreMetric.getName(),
65 convert(coreMetric.getType()),
66 coreMetric.getDecimalScale(),
67 coreMetric.getBestValue(), coreMetric.isOptimizedBestValue());
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");
76 requireNonNull(metric.getId(), "id can not be null");
78 checkState(!metricsByKey.containsKey(metric.getKey()), format("Repository already contains a metric for key %s", metric.getKey()));
79 checkState(!metricsById.containsKey((long) metric.getId()), format("Repository already contains a metric for id %s", metric.getId()));
81 metricsByKey.put(metric.getKey(), metric);
82 metricsById.put((long) metric.getId(), metric);
88 protected void after() {
89 this.metricsById.clear();
90 this.metricsById.clear();
94 public Metric getByKey(String key) {
95 Metric res = metricsByKey.get(key);
96 checkState(res != null, format("No Metric can be found for key %s", key));
101 public Metric getById(long id) {
102 Metric res = metricsById.get(id);
103 checkState(res != null, format("No Metric can be found for id %s", id));
108 public Optional<Metric> getOptionalById(long id) {
109 return Optional.of(metricsById.get(id));
113 public Iterable<Metric> getAll() {
114 return metricsByKey.values();