]> source.dussan.org Git - sonarqube.git/commitdiff
Simplify keys of measure repository
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 30 Jul 2019 20:37:02 +0000 (15:37 -0500)
committerSonarTech <sonartech@sonarsource.com>
Wed, 4 Sep 2019 18:21:04 +0000 (20:21 +0200)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/MapBasedRawMeasureRepository.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/MeasureKey.java [deleted file]
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/MeasureKeyTest.java [deleted file]

index 36bc9643707ce74d5a10c479a51b6565bc11902e..0aed2b16ddc4dbcd0c4f9018ef2e1d463df5757c 100644 (file)
@@ -26,10 +26,6 @@ import java.util.Map;
 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;
@@ -44,7 +40,7 @@ import static java.util.Objects.requireNonNull;
  */
 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);
@@ -110,39 +106,32 @@ public final class MapBasedRawMeasureRepository<T> implements MeasureRepository
   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) {
@@ -152,37 +141,13 @@ public final class MapBasedRawMeasureRepository<T> implements MeasureRepository
     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();
-    }
-  }
 }
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/MeasureKey.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/MeasureKey.java
deleted file mode 100644 (file)
index e283f1b..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * 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 + '\'' +
-      '}';
-  }
-}
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/MeasureKeyTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/MeasureKeyTest.java
deleted file mode 100644 (file)
index 6c42059..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * 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'}");
-  }
-}