]> source.dussan.org Git - sonarqube.git/commitdiff
add Measure#equals definition and improve javadoc of MeasureRepository methods
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 12 Jun 2015 14:25:28 +0000 (16:25 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 15 Jun 2015 11:07:53 +0000 (13:07 +0200)
emphasis on Measure for rule or characteristics

server/sonar-server/src/main/java/org/sonar/server/computation/measure/Measure.java
server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepository.java
server/sonar-server/src/main/java/org/sonar/server/computation/measure/MeasureRepositoryImpl.java

index 39aae3160ca941d7b3a10b140ba19dda36f4effe..2b5544c3c93329b279678d28386a9f2bcc9eb8f8 100644 (file)
@@ -149,6 +149,10 @@ public interface Measure {
   @CheckForNull
   String getDescription();
 
-
+  /**
+   * a Metric is equal to another Metric if it has the same ruleId/characteristicId paar (both being potentially
+   * {@code null} but only one of them can be non {@code null}.
+   */
+  boolean equals(Object o);
 
 }
index 673a8bf58a2a843e708a87728a8bdf49fab94056..bcbda94ec1195662317f8c5267942e00ebd7f594 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.computation.measure;
 
 import java.util.Locale;
+import java.util.Objects;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
@@ -235,4 +236,37 @@ public final class MeasureImpl implements Measure {
   public String getDescription() {
     return description;
   }
+
+  @Override
+  public boolean equals(@Nullable Object o) {
+    if (this == o) {
+      return true;
+    }
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+    MeasureImpl measure = (MeasureImpl) o;
+    return Objects.equals(ruleId, measure.ruleId) &&
+        Objects.equals(characteristicId, measure.characteristicId);
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(ruleId, characteristicId);
+  }
+
+  @Override
+  public String toString() {
+    return com.google.common.base.Objects.toStringHelper(this)
+        .add("valueType", valueType)
+        .add("ruleId", ruleId)
+        .add("characteristicId", characteristicId)
+        .add("value", value)
+        .add("data", data)
+        .add("dataLevel", dataLevel)
+        .add("qualityGateStatus", qualityGateStatus)
+        .add("variations", variations)
+        .add("description", description)
+        .toString();
+  }
 }
index a8d447af382fc8d1d864c12028627333f342d4d8..80341ef81aba040aa1b070017c9ae4132b502f3e 100644 (file)
@@ -20,7 +20,7 @@
 package org.sonar.server.computation.measure;
 
 import com.google.common.base.Optional;
-import com.google.common.collect.Multimap;
+import com.google.common.collect.SetMultimap;
 import org.sonar.server.computation.component.Component;
 import org.sonar.server.computation.metric.Metric;
 import org.sonar.server.computation.metric.MetricImpl;
@@ -30,6 +30,14 @@ public interface MeasureRepository {
   /**
    * Retrieves the base measure (ie. the one currently existing in DB) for the specified {@link Component} for
    * the specified {@link MetricImpl} if it exists.
+   * <p>
+   * This method searches for Measure which are specific to the Component and not associated to a rule or a
+   * characteristic.
+   * </p>
+   * <strong>
+   * It will never return a Measure with a non {@code null} {@link Measure#getRuleId()}
+   * or {@link Measure#getCharacteristicId()}
+   * </strong>
    *
    * @throws NullPointerException if either argument is {@code null}
    */
@@ -37,14 +45,19 @@ public interface MeasureRepository {
 
   /**
    * Retrieves the measure created during the current analysis for the specified {@link Component} for the specified
-   * {@link MetricImpl} if it exists (ie. one created by the Compute Engine or the Batch).
+   * {@link Metric} if it exists (ie. one created by the Compute Engine or the Batch) and which is <strong>not</strong>
+   * associated to a rule or a characteristic.
    */
   Optional<Measure> getRawMeasure(Component component, Metric metric);
 
   /**
-   * @return {@link Measure}s for the specified {@link Component} mapped by their metric key.
+   * Returns the {@link Measure}s for the specified {@link Component} mapped by their metric key.
+   * <p>
+   * Their can be multiple measures for the same Metric but only one which has no rule nor characteristic, one with a
+   * specific ruleId and one with specific characteristicId (see {@link Measure#equals(Object)}.
+   * </p>
    */
-  Multimap<String, Measure> getRawMeasures(Component component);
+  SetMultimap<String, Measure> getRawMeasures(Component component);
 
   /**
    * Adds the specified measure for the specified Component and Metric. There can be no more than one measure for a
index 4e4ebf830084d1fad314f08e297d7a36c3e0dcca..592613174a777a3449da8b366b975c98586ddf01 100644 (file)
@@ -22,12 +22,11 @@ package org.sonar.server.computation.measure;
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
-import com.google.common.collect.ImmutableListMultimap;
 import com.google.common.collect.ImmutableSetMultimap;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.ListMultimap;
-import com.google.common.collect.Multimap;
 import com.google.common.collect.Multimaps;
+import com.google.common.collect.SetMultimap;
 import java.util.HashMap;
 import java.util.Map;
 import javax.annotation.Nonnull;
@@ -126,13 +125,13 @@ public class MeasureRepositoryImpl implements MeasureRepository {
   }
 
   @Override
-  public Multimap<String, Measure> getRawMeasures(Component component) {
+  public SetMultimap<String, Measure> getRawMeasures(Component component) {
     Map<String, Measure> rawMeasures = measures.get(component.getRef());
     ListMultimap<String, BatchReport.Measure> batchMeasures = from(reportReader.readComponentMeasures(component.getRef()))
       .index(BatchMeasureToMetricKey.INSTANCE);
 
     if (rawMeasures == null && batchMeasures.isEmpty()) {
-      return ImmutableListMultimap.of();
+      return ImmutableSetMultimap.of();
     }
 
     ListMultimap<String, Measure> rawMeasuresFromBatch = Multimaps.transformValues(batchMeasures, batchMeasureToMeasureFunction);