import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
-import java.util.function.Predicate;
-import java.util.stream.Collectors;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.metric.Metric;
import vlsi.utils.CompactHashMap;
*/
public final class MapBasedRawMeasureRepository<T> implements MeasureRepository {
private final Function<Component, T> componentToKey;
- private final Map<T, Map<MeasureKey, Measure>> measures = new CompactHashMap<>();
+ private final Map<T, Map<String, Measure>> measures = new CompactHashMap<>();
public MapBasedRawMeasureRepository(Function<Component, T> componentToKey) {
this.componentToKey = requireNonNull(componentToKey);
public Set<Measure> getRawMeasures(Component component, Metric metric) {
requireNonNull(metric);
requireNonNull(component);
- T componentKey = componentToKey.apply(component);
- Map<MeasureKey, Measure> rawMeasures = measures.get(componentKey);
- if (rawMeasures == null) {
- return Collections.emptySet();
- }
- return rawMeasures.entrySet().stream()
- .filter(new MatchMetric(metric))
- .map(ToMeasure.INSTANCE)
- .collect(Collectors.toSet());
+ Optional<Measure> measure = find(component, metric);
+ return measure.isPresent() ? Collections.singleton(measure.get()) : Collections.emptySet();
}
@Override
public SetMultimap<String, Measure> getRawMeasures(Component component) {
T componentKey = componentToKey.apply(component);
- Map<MeasureKey, Measure> rawMeasures = measures.get(componentKey);
+ Map<String, Measure> rawMeasures = measures.get(componentKey);
if (rawMeasures == null) {
return ImmutableSetMultimap.of();
}
ImmutableSetMultimap.Builder<String, Measure> builder = ImmutableSetMultimap.builder();
- for (Map.Entry<MeasureKey, Measure> entry : rawMeasures.entrySet()) {
- builder.put(entry.getKey().getMetricKey(), entry.getValue());
+ for (Map.Entry<String, Measure> entry : rawMeasures.entrySet()) {
+ builder.put(entry.getKey(), entry.getValue());
}
return builder.build();
}
private Optional<Measure> find(Component component, Metric metric) {
T componentKey = componentToKey.apply(component);
- Map<MeasureKey, Measure> measuresPerMetric = measures.get(componentKey);
+ Map<String, Measure> measuresPerMetric = measures.get(componentKey);
if (measuresPerMetric == null) {
return Optional.empty();
}
- return Optional.ofNullable(measuresPerMetric.get(new MeasureKey(metric.getKey())));
+ return Optional.ofNullable(measuresPerMetric.get(metric.getKey()));
}
public void add(Component component, Metric metric, Measure measure, OverridePolicy overridePolicy) {
requireNonNull(overridePolicy);
T componentKey = componentToKey.apply(component);
- Map<MeasureKey, Measure> measuresPerMetric = measures.computeIfAbsent(componentKey, key -> new CompactHashMap<>());
- MeasureKey key = new MeasureKey(metric.getKey());
- if (!measuresPerMetric.containsKey(key) || overridePolicy == OverridePolicy.OVERRIDE) {
- measuresPerMetric.put(key, measure);
+ Map<String, Measure> measuresPerMetric = measures.computeIfAbsent(componentKey, key -> new CompactHashMap<>());
+ if (!measuresPerMetric.containsKey(metric.getKey()) || overridePolicy == OverridePolicy.OVERRIDE) {
+ measuresPerMetric.put(metric.getKey(), measure);
}
}
public enum OverridePolicy {
OVERRIDE, DO_NOT_OVERRIDE
}
-
- private static class MatchMetric implements Predicate<Map.Entry<MeasureKey, Measure>> {
- private final Metric metric;
-
- public MatchMetric(Metric metric) {
- this.metric = metric;
- }
-
- @Override
- public boolean test(@Nonnull Map.Entry<MeasureKey, Measure> input) {
- return input.getKey().getMetricKey().equals(metric.getKey());
- }
- }
-
- private enum ToMeasure implements Function<Map.Entry<MeasureKey, Measure>, Measure> {
- INSTANCE;
-
- @Nullable
- @Override
- public Measure apply(@Nonnull Map.Entry<MeasureKey, Measure> input) {
- return input.getValue();
- }
- }
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.ce.task.projectanalysis.measure;
-
-import java.util.Objects;
-import javax.annotation.Nullable;
-import javax.annotation.concurrent.Immutable;
-
-import static java.util.Objects.requireNonNull;
-
-@Immutable
-public final class MeasureKey {
-
- private final String metricKey;
-
- public MeasureKey(String metricKey) {
- this.metricKey = requireNonNull(metricKey, "MetricKey can not be null");
- }
-
- public String getMetricKey() {
- return metricKey;
- }
-
- @Override
- public boolean equals(@Nullable Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- MeasureKey that = (MeasureKey) o;
- return metricKey.equals(that.metricKey);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(metricKey);
- }
-
- @Override
- public String toString() {
- return "MeasureKey{" +
- "metricKey='" + metricKey + '\'' +
- '}';
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2019 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.ce.task.projectanalysis.measure;
-
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class MeasureKeyTest {
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Test
- public void fail_with_NPE_when_metric_key_is_null() {
- thrown.expect(NullPointerException.class);
-
- new MeasureKey(null);
- }
-
- @Test
- public void test_equals_and_hashcode() {
- MeasureKey measureKey = new MeasureKey("metricKey");
- MeasureKey measureKey2 = new MeasureKey("metricKey");
- MeasureKey anotherMeasureKey = new MeasureKey("anotherMetricKey");
-
- assertThat(measureKey).isEqualTo(measureKey);
- assertThat(measureKey).isEqualTo(measureKey2);
- assertThat(measureKey).isNotEqualTo(null);
- assertThat(measureKey).isNotEqualTo(anotherMeasureKey);
-
-
- assertThat(measureKey.hashCode()).isEqualTo(measureKey.hashCode());
- assertThat(measureKey.hashCode()).isEqualTo(measureKey2.hashCode());
- assertThat(measureKey.hashCode()).isNotEqualTo(anotherMeasureKey.hashCode());
- }
-
- @Test
- public void to_string() {
- assertThat(new MeasureKey("metricKey").toString()).isEqualTo(
- "MeasureKey{metricKey='metricKey'}");
- assertThat(new MeasureKey("metricKey").toString()).isEqualTo("MeasureKey{metricKey='metricKey'}");
- }
-}