3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.measure;
22 import com.google.common.base.Function;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Predicate;
25 import com.google.common.collect.ImmutableSetMultimap;
26 import com.google.common.collect.SetMultimap;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
32 import javax.annotation.Nonnull;
33 import javax.annotation.Nullable;
34 import org.sonar.server.computation.task.projectanalysis.component.Component;
35 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
37 import static com.google.common.base.Preconditions.checkArgument;
38 import static com.google.common.collect.FluentIterable.from;
39 import static java.lang.String.format;
40 import static java.util.Objects.requireNonNull;
43 * Map based implementation of MeasureRepository which supports only raw measures.
45 * Intended to be used as a delegate of other MeasureRepository implementations (hence the final keyword).
47 public final class MapBasedRawMeasureRepository<T> implements MeasureRepository {
48 private final Function<Component, T> componentToKey;
49 private final Map<T, Map<MeasureKey, Measure>> measures = new HashMap<>();
51 public MapBasedRawMeasureRepository(Function<Component, T> componentToKey) {
52 this.componentToKey = requireNonNull(componentToKey);
56 * @throws UnsupportedOperationException all the time, not supported
59 public Optional<Measure> getBaseMeasure(Component component, Metric metric) {
60 throw new UnsupportedOperationException("This implementation of MeasureRepository supports only raw measures");
64 public int loadAsRawMeasures(Collection<Component> components, Collection<Metric> metrics) {
65 throw new UnsupportedOperationException("This implementation of MeasureRepository supports only raw measures");
69 public Optional<Measure> getRawMeasure(final Component component, final Metric metric) {
71 requireNonNull(component);
72 requireNonNull(metric);
74 return find(component, metric);
78 public void add(Component component, Metric metric, Measure measure) {
79 requireNonNull(component);
80 checkValueTypeConsistency(metric, measure);
82 Optional<Measure> existingMeasure = find(component, metric, measure);
83 if (existingMeasure.isPresent()) {
84 throw new UnsupportedOperationException(
86 "a measure can be set only once for a specific Component (key=%s), Metric (key=%s). Use update method",
90 add(component, metric, measure, OverridePolicy.OVERRIDE);
94 public void update(Component component, Metric metric, Measure measure) {
95 requireNonNull(component);
96 checkValueTypeConsistency(metric, measure);
98 Optional<Measure> existingMeasure = find(component, metric, measure);
99 if (!existingMeasure.isPresent()) {
100 throw new UnsupportedOperationException(
102 "a measure can be updated only if one already exists for a specific Component (key=%s), Metric (key=%s). Use add method",
106 add(component, metric, measure, OverridePolicy.OVERRIDE);
109 private static void checkValueTypeConsistency(Metric metric, Measure measure) {
111 measure.getValueType() == Measure.ValueType.NO_VALUE || measure.getValueType() == metric.getType().getValueType(),
112 "Measure's ValueType (%s) is not consistent with the Metric's ValueType (%s)",
113 measure.getValueType(), metric.getType().getValueType());
117 public Set<Measure> getRawMeasures(Component component, Metric metric) {
118 requireNonNull(metric);
119 requireNonNull(component);
120 T componentKey = componentToKey.apply(component);
121 Map<MeasureKey, Measure> rawMeasures = measures.get(componentKey);
122 if (rawMeasures == null) {
123 return Collections.emptySet();
125 return from(rawMeasures.entrySet()).filter(new MatchMetric(metric)).transform(ToMeasure.INSTANCE).toSet();
129 public SetMultimap<String, Measure> getRawMeasures(Component component) {
130 T componentKey = componentToKey.apply(component);
131 Map<MeasureKey, Measure> rawMeasures = measures.get(componentKey);
132 if (rawMeasures == null) {
133 return ImmutableSetMultimap.of();
136 ImmutableSetMultimap.Builder<String, Measure> builder = ImmutableSetMultimap.builder();
137 for (Map.Entry<MeasureKey, Measure> entry : rawMeasures.entrySet()) {
138 builder.put(entry.getKey().getMetricKey(), entry.getValue());
140 return builder.build();
143 private Optional<Measure> find(Component component, Metric metric) {
144 T componentKey = componentToKey.apply(component);
145 Map<MeasureKey, Measure> measuresPerMetric = measures.get(componentKey);
146 if (measuresPerMetric == null) {
147 return Optional.absent();
149 return Optional.fromNullable(measuresPerMetric.get(new MeasureKey(metric.getKey(), null)));
152 private Optional<Measure> find(Component component, Metric metric, Measure measure) {
153 T componentKey = componentToKey.apply(component);
154 Map<MeasureKey, Measure> measuresPerMetric = measures.get(componentKey);
155 if (measuresPerMetric == null) {
156 return Optional.absent();
158 return Optional.fromNullable(measuresPerMetric.get(new MeasureKey(metric.getKey(), measure.getDeveloper())));
161 public void add(Component component, Metric metric, Measure measure, OverridePolicy overridePolicy) {
162 requireNonNull(component);
163 requireNonNull(measure);
164 requireNonNull(measure);
165 requireNonNull(overridePolicy);
167 T componentKey = componentToKey.apply(component);
168 Map<MeasureKey, Measure> measuresPerMetric = measures.get(componentKey);
169 if (measuresPerMetric == null) {
170 measuresPerMetric = new HashMap<>();
171 measures.put(componentKey, measuresPerMetric);
173 MeasureKey key = new MeasureKey(metric.getKey(), measure.getDeveloper());
174 if (!measuresPerMetric.containsKey(key) || overridePolicy == OverridePolicy.OVERRIDE) {
175 measuresPerMetric.put(key, measure);
179 public enum OverridePolicy {
180 OVERRIDE, DO_NOT_OVERRIDE
183 private static class MatchMetric implements Predicate<Map.Entry<MeasureKey, Measure>> {
184 private final Metric metric;
186 public MatchMetric(Metric metric) {
187 this.metric = metric;
191 public boolean apply(@Nonnull Map.Entry<MeasureKey, Measure> input) {
192 return input.getKey().getMetricKey().equals(metric.getKey());
196 private enum ToMeasure implements Function<Map.Entry<MeasureKey, Measure>, Measure> {
201 public Measure apply(@Nonnull Map.Entry<MeasureKey, Measure> input) {
202 return input.getValue();