]> source.dussan.org Git - sonarqube.git/commitdiff
Cache measures for ratings
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 30 Jul 2019 20:39:53 +0000 (15:39 -0500)
committerSonarTech <sonartech@sonarsource.com>
Wed, 4 Sep 2019 18:21:05 +0000 (20:21 +0200)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/RatingMeasures.java [new file with mode: 0644]
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitymodel/MaintainabilityMeasuresVisitor.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitymodel/ReliabilityAndSecurityRatingMeasuresVisitor.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/qualitymodel/SecurityReviewRatingVisitor.java

diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/RatingMeasures.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/measure/RatingMeasures.java
new file mode 100644 (file)
index 0000000..6b49422
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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.EnumMap;
+import org.sonar.server.measure.Rating;
+
+import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
+
+public class RatingMeasures {
+  private static final EnumMap<Rating, Measure> ratingMeasureCache;
+
+  static {
+    ratingMeasureCache = new EnumMap<>(Rating.class);
+    for (Rating r : Rating.values()) {
+      ratingMeasureCache.put(r, newMeasureBuilder().create(r.getIndex(), r.name()));
+    }
+  }
+
+  private RatingMeasures() {
+    // static only
+  }
+
+  public static Measure get(Rating rating) {
+    return ratingMeasureCache.get(rating);
+  }
+}
index f6a31224c6382df8e17f1544a81efc2a91ff8cb6..933890586524bc715b8f68600d8e46f61231fd56 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.ce.task.projectanalysis.component.PathAwareVisitorAdapter;
 import org.sonar.ce.task.projectanalysis.formula.counter.RatingValue;
 import org.sonar.ce.task.projectanalysis.measure.Measure;
 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
+import org.sonar.ce.task.projectanalysis.measure.RatingMeasures;
 import org.sonar.ce.task.projectanalysis.metric.Metric;
 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
 import org.sonar.server.measure.Rating;
@@ -127,7 +128,7 @@ public class MaintainabilityMeasuresVisitor extends PathAwareVisitorAdapter<Main
 
   private void addMaintainabilityRatingMeasure(Component component, double density) {
     Rating rating = ratingSettings.getDebtRatingGrid().getRatingForDensity(density);
-    measureRepository.add(component, maintainabilityRatingMetric, newMeasureBuilder().create(rating.getIndex(), rating.name()));
+    measureRepository.add(component, maintainabilityRatingMetric, RatingMeasures.get(rating));
   }
 
   private void addEffortToMaintainabilityRatingAMeasure(Component component, Path<Counter> path) {
index e6b27ff47b31a5161323dbf64b4a0a86e2623acb..a4eb9234515594e1b876a344f0df174d74dac373 100644 (file)
 package org.sonar.ce.task.projectanalysis.qualitymodel;
 
 import com.google.common.collect.ImmutableMap;
+import java.util.EnumMap;
+import java.util.HashMap;
 import java.util.Map;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.ce.task.projectanalysis.component.Component;
 import org.sonar.ce.task.projectanalysis.component.PathAwareVisitorAdapter;
 import org.sonar.ce.task.projectanalysis.formula.counter.RatingValue;
 import org.sonar.ce.task.projectanalysis.issue.ComponentIssuesRepository;
+import org.sonar.ce.task.projectanalysis.measure.Measure;
 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
+import org.sonar.ce.task.projectanalysis.measure.RatingMeasures;
 import org.sonar.ce.task.projectanalysis.metric.Metric;
 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
 import org.sonar.server.measure.Rating;
@@ -83,7 +87,7 @@ public class ReliabilityAndSecurityRatingMeasuresVisitor extends PathAwareVisito
     processIssues(component, path);
     path.current().ratingValueByMetric.forEach((key, value) -> {
       Rating rating = value.getValue();
-      measureRepository.add(component, metricsByKey.get(key), newMeasureBuilder().create(rating.getIndex(), rating.name()));
+      measureRepository.add(component, metricsByKey.get(key), RatingMeasures.get(rating));
     });
     if (!path.isRoot()) {
       path.parent().add(path.current());
index cafc9d7c13716dc694da787c073b09deafa1bf51..866b8ac4a016fd207d43595ea5f62b9a372df728 100644 (file)
@@ -26,6 +26,7 @@ import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
 import org.sonar.ce.task.projectanalysis.measure.Measure;
 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
+import org.sonar.ce.task.projectanalysis.measure.RatingMeasures;
 import org.sonar.ce.task.projectanalysis.metric.Metric;
 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
 import org.sonar.server.measure.Rating;
@@ -77,7 +78,7 @@ public class SecurityReviewRatingVisitor extends TypeAwareVisitorAdapter {
     int ncloc = nclocMeasure.get().getIntValue();
     int securityHotspots = securityHostspotsMeasure.get().getIntValue();
     Rating rating = SecurityReviewRating.compute(ncloc, securityHotspots);
-    measureRepository.add(component, securityReviewRatingMetric, newMeasureBuilder().create(rating.getIndex(), rating.name()));
+    measureRepository.add(component, securityReviewRatingMetric, RatingMeasures.get(rating));
   }
 
 }