]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5912 improve IssuesBySeverity performance and memory consumption
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 6 Jan 2015 16:51:57 +0000 (17:51 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 6 Jan 2015 17:04:45 +0000 (18:04 +0100)
sonar-core/src/main/java/org/sonar/core/issue/IssuesBySeverity.java
sonar-core/src/test/java/org/sonar/core/issue/IssuesBySeverityTest.java

index 4d1d6ff3dc1567b20499c56f8755d1edf06d532b..85286c3abcbc3f8afe25570661af410559e1efdb 100644 (file)
  */
 package org.sonar.core.issue;
 
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Multimap;
+import com.google.common.collect.HashMultiset;
+import com.google.common.collect.Multiset;
 import org.sonar.api.issue.Issue;
 
 public class IssuesBySeverity {
 
-  private Multimap<String, Issue> issuesBySeverity = ArrayListMultimap.create();
+  private final Multiset<String> issuesBySeverity;
 
   public IssuesBySeverity() {
-    this.issuesBySeverity = ArrayListMultimap.create();
+    issuesBySeverity = HashMultiset.create();
   }
 
   public void add(Issue issue) {
-    issuesBySeverity.put(issue.severity(), issue);
+    issuesBySeverity.add(issue.severity());
   }
 
   public int issues(String severity) {
-    return issuesBySeverity.get(severity).size();
+    return issuesBySeverity.count(severity);
   }
 
   public int size() {
-    return issuesBySeverity.values().size();
+    return issuesBySeverity.size();
   }
 
   public boolean isEmpty() {
-    return issuesBySeverity.values().isEmpty();
+    return issuesBySeverity.isEmpty();
   }
 }
index 24d7738195851cf14969405022b3a9f83066d320..6f330bfcc6aeff0f26ad07fd9d3612a49fd55e5f 100644 (file)
@@ -26,27 +26,43 @@ import static org.fest.assertions.Assertions.assertThat;
 
 public class IssuesBySeverityTest {
 
+  IssuesBySeverity sut;
+
   @Test
-  public void add_issue(){
-    IssuesBySeverity issuesBySeverity = new IssuesBySeverity();
-    issuesBySeverity.add(new DefaultIssue().setSeverity("MINOR"));
-    assertThat(issuesBySeverity.size()).isEqualTo(1);
+  public void add_issue() {
+    sut = new IssuesBySeverity();
+
+    sut.add(new DefaultIssue().setSeverity("MINOR"));
+
+    assertThat(sut.isEmpty()).isFalse();
+    assertThat(sut.size()).isEqualTo(1);
   }
 
   @Test
-  public void get_issues_by_severity(){
-    IssuesBySeverity issuesBySeverity = new IssuesBySeverity();
-    issuesBySeverity.add(new DefaultIssue().setSeverity("MINOR"));
-    issuesBySeverity.add(new DefaultIssue().setSeverity("MINOR"));
-    issuesBySeverity.add(new DefaultIssue().setSeverity("MAJOR"));
-    assertThat(issuesBySeverity.issues("MINOR")).isEqualTo(2);
-    assertThat(issuesBySeverity.issues("MAJOR")).isEqualTo(1);
+  public void get_issues_by_severity() {
+    sut = new IssuesBySeverity();
+
+    sut.add(new DefaultIssue().setSeverity("MINOR"));
+    sut.add(new DefaultIssue().setSeverity("MINOR"));
+    sut.add(new DefaultIssue().setSeverity("MAJOR"));
+
+    assertThat(sut.issues("MINOR")).isEqualTo(2);
+    assertThat(sut.issues("MAJOR")).isEqualTo(1);
   }
 
   @Test
-  public void get_zero_issues_on_empty_severity(){
-    IssuesBySeverity issuesBySeverity = new IssuesBySeverity();
-    issuesBySeverity.add(new DefaultIssue().setSeverity("MAJOR"));
-    assertThat(issuesBySeverity.issues("MINOR")).isEqualTo(0);
+  public void get_zero_issues_on_empty_severity() {
+    sut = new IssuesBySeverity();
+
+    sut.add(new DefaultIssue().setSeverity("MAJOR"));
+
+    assertThat(sut.issues("MINOR")).isEqualTo(0);
+  }
+
+  @Test
+  public void is_empty() throws Exception {
+    sut = new IssuesBySeverity();
+
+    assertThat(sut.isEmpty()).isTrue();
   }
 }