]> source.dussan.org Git - sonarqube.git/commitdiff
fix quality flaws
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 4 Mar 2015 11:28:39 +0000 (12:28 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 4 Mar 2015 11:28:57 +0000 (12:28 +0100)
server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesEmailTemplate.java
server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesNotification.java
server/sonar-server/src/main/java/org/sonar/server/issue/notification/NewIssuesStatistics.java
sonar-core/src/main/java/org/sonar/core/util/MultiSets.java

index d545ab9e9a14b97400e6008d3d5893fce2a2649f..df98f6eee5e13155824daff0cb5a1d1de4870821 100644 (file)
@@ -48,6 +48,12 @@ public class NewIssuesEmailTemplate extends EmailTemplate {
   public static final String FIELD_PROJECT_DATE = "projectDate";
   public static final String FIELD_PROJECT_UUID = "projectUuid";
 
+  private static final char NEW_LINE = '\n';
+  private static final String TAB = "   ";
+  private static final String DOT = ".";
+  private static final String COUNT = DOT + "count";
+  private static final String LABEL = DOT + "label";
+
   private final EmailSettings settings;
   private final I18n i18n;
   private final UserIndex userIndex;
@@ -74,7 +80,7 @@ public class NewIssuesEmailTemplate extends EmailTemplate {
     String projectName = notification.getFieldValue(FIELD_PROJECT_NAME);
 
     StringBuilder message = new StringBuilder();
-    message.append("Project: ").append(projectName).append("\n\n");
+    message.append("Project: ").append(projectName).append(NEW_LINE).append(NEW_LINE);
     appendSeverity(message, notification);
     appendAssignees(message, notification);
     appendTags(message, notification);
@@ -83,7 +89,7 @@ public class NewIssuesEmailTemplate extends EmailTemplate {
 
     return new EmailMessage()
       .setMessageId("new-issues/" + notification.getFieldValue(FIELD_PROJECT_KEY))
-      .setSubject(projectName + ": " + notification.getFieldValue(METRIC.SEVERITY + ".count") + " new issues")
+      .setSubject(projectName + ": " + notification.getFieldValue(METRIC.SEVERITY + COUNT) + " new issues")
       .setMessage(message.toString());
   }
 
@@ -94,33 +100,34 @@ public class NewIssuesEmailTemplate extends EmailTemplate {
 
     message.append("   Components:\n");
     int i = 1;
-    while (notification.getFieldValue(METRIC.COMPONENT + "." + i + ".label") != null && i <= 5) {
-      String component = notification.getFieldValue(METRIC.COMPONENT + "." + i + ".label");
-      message.append("      ")
+    while (notification.getFieldValue(METRIC.COMPONENT + DOT + i + LABEL) != null && i <= 5) {
+      String component = notification.getFieldValue(METRIC.COMPONENT + DOT + i + LABEL);
+      message
+        .append(TAB).append(TAB)
         .append(component)
         .append(" : ")
-        .append(notification.getFieldValue(METRIC.COMPONENT + "." + i + ".count"))
+        .append(notification.getFieldValue(METRIC.COMPONENT + DOT + i + COUNT))
         .append("\n");
       i += 1;
     }
   }
 
   private void appendAssignees(StringBuilder message, Notification notification) {
-    if (notification.getFieldValue(METRIC.LOGIN + ".1.label") == null) {
+    if (notification.getFieldValue(METRIC.LOGIN + DOT + "1" + LABEL) == null) {
       return;
     }
 
-    message.append("   Assignee - ");
+    message.append(TAB + "Assignee - ");
     int i = 1;
-    while (notification.getFieldValue(METRIC.LOGIN + "." + i + ".label") != null && i <= 5) {
-      String login = notification.getFieldValue(METRIC.LOGIN + "." + i + ".label");
+    while (notification.getFieldValue(METRIC.LOGIN + DOT + i + LABEL) != null && i <= 5) {
+      String login = notification.getFieldValue(METRIC.LOGIN + DOT + i + LABEL);
       UserDoc user = userIndex.getNullableByLogin(login);
       String name = user == null ? null : user.name();
       message.append(Objects.firstNonNull(name, login))
         .append(": ")
-        .append(notification.getFieldValue(METRIC.LOGIN + "." + i + ".count"));
+        .append(notification.getFieldValue(METRIC.LOGIN + DOT + i + COUNT));
       if (i < 5) {
-        message.append("   ");
+        message.append(TAB);
       }
       i += 1;
     }
@@ -129,39 +136,39 @@ public class NewIssuesEmailTemplate extends EmailTemplate {
   }
 
   private void appendTags(StringBuilder message, Notification notification) {
-    if (notification.getFieldValue(METRIC.TAGS + ".1.label") == null) {
+    if (notification.getFieldValue(METRIC.TAGS + DOT + "1" + LABEL) == null) {
       return;
     }
 
-    message.append("   Tags - ");
+    message.append(TAB + "Tags - ");
     int i = 1;
-    while (notification.getFieldValue(METRIC.TAGS + "." + i + ".label") != null && i <= 5) {
-      String tag = notification.getFieldValue(METRIC.TAGS + "." + i + ".label");
+    while (notification.getFieldValue(METRIC.TAGS + DOT + i + LABEL) != null && i <= 5) {
+      String tag = notification.getFieldValue(METRIC.TAGS + DOT + i + LABEL);
       message.append(tag)
         .append(": ")
-        .append(notification.getFieldValue(METRIC.TAGS + "." + i + ".count"));
+        .append(notification.getFieldValue(METRIC.TAGS + DOT + i + COUNT));
       if (i < 5) {
-        message.append("   ");
+        message.append(TAB);
       }
       i += 1;
     }
-    message.append("\n");
+    message.append(NEW_LINE);
   }
 
   private void appendSeverity(StringBuilder message, Notification notification) {
-    message.append(notification.getFieldValue(METRIC.SEVERITY + ".count")).append(" new issues - Total debt: ")
-      .append(notification.getFieldValue(METRIC.DEBT + ".count"))
-      .append("\n\n")
-      .append("   Severity - ");
+    message.append(notification.getFieldValue(METRIC.SEVERITY + COUNT)).append(" new issues - Total debt: ")
+      .append(notification.getFieldValue(METRIC.DEBT + COUNT))
+      .append(NEW_LINE).append(NEW_LINE)
+      .append(TAB + "Severity - ");
     for (Iterator<String> severityIterator = Lists.reverse(Severity.ALL).iterator(); severityIterator.hasNext();) {
       String severity = severityIterator.next();
       String severityLabel = i18n.message(getLocale(), "severity." + severity, severity);
-      message.append(severityLabel).append(": ").append(notification.getFieldValue(METRIC.SEVERITY + "." + severity + ".count"));
+      message.append(severityLabel).append(": ").append(notification.getFieldValue(METRIC.SEVERITY + DOT + severity + COUNT));
       if (severityIterator.hasNext()) {
-        message.append("   ");
+        message.append(TAB);
       }
     }
-    message.append('\n');
+    message.append(NEW_LINE);
   }
 
   private void appendFooter(StringBuilder message, Notification notification) {
@@ -171,7 +178,7 @@ public class NewIssuesEmailTemplate extends EmailTemplate {
       Date date = DateUtils.parseDateTime(dateString);
       String url = String.format("%s/issues/search#projectUuids=%s|createdAt=%s",
         settings.getServerBaseURL(), encode(projectUuid), encode(DateUtils.formatDateTime(date)));
-      message.append("\n").append("See it in SonarQube: ").append(url).append("\n");
+      message.append("\n").append("See it in SonarQube: ").append(url).append(NEW_LINE);
     }
   }
 
index a3f08c2a58b283fb94b70acef705bad1eb7a1db8..ae387b1598d06adc28655e95d2f1a30501c28590 100644 (file)
@@ -36,6 +36,7 @@ import static org.sonar.server.issue.notification.NewIssuesStatistics.METRIC.SEV
 public class NewIssuesNotification extends Notification {
 
   public static final String TYPE = "new-issues";
+  private static final String COUNT = ".count";
 
   public NewIssuesNotification() {
     super(TYPE);
@@ -65,22 +66,22 @@ public class NewIssuesNotification extends Notification {
   }
 
   public NewIssuesNotification setDebt(String debt) {
-    setFieldValue(METRIC.DEBT + ".count", debt);
+    setFieldValue(METRIC.DEBT + COUNT, debt);
     return this;
   }
 
   private void setTop5CountsForMetric(NewIssuesStatistics stats, METRIC metric) {
     List<Multiset.Entry<String>> loginStats = stats.statsForMetric(metric);
     for (int i = 0; i < 5 && i < loginStats.size(); i++) {
-      setFieldValue(metric + "." + (i + 1) + ".count", String.valueOf(loginStats.get(i).getCount()));
+      setFieldValue(metric + "." + (i + 1) + COUNT, String.valueOf(loginStats.get(i).getCount()));
       setFieldValue(metric + "." + (i + 1) + ".label", loginStats.get(i).getElement());
     }
   }
 
   private void setSeverityStatistics(NewIssuesStatistics stats) {
-    setFieldValue(SEVERITY + ".count", String.valueOf(stats.countForMetric(SEVERITY)));
+    setFieldValue(SEVERITY + COUNT, String.valueOf(stats.countForMetric(SEVERITY)));
     for (String severity : Severity.ALL) {
-      setFieldValue(SEVERITY + "." + severity + ".count", String.valueOf(stats.countForMetric(SEVERITY, severity)));
+      setFieldValue(SEVERITY + "." + severity + COUNT, String.valueOf(stats.countForMetric(SEVERITY, severity)));
     }
   }
 }
index de4c45ee43e621b84519c60e27770189e226f2af..e2ca668151c9f60b6f3267b148463bce3739d53c 100644 (file)
@@ -107,8 +107,9 @@ public class NewIssuesStatistics {
       for (String tag : issue.tags()) {
         distributions.get(TAGS).add(tag);
       }
-      if (issue.debt() != null) {
-        debtInMinutes += issue.debt().toMinutes();
+      Duration debt = issue.debt();
+      if (debt != null) {
+        debtInMinutes += debt.toMinutes();
       }
     }
 
index 1a79e38a3ead725831b539fa9a69fa655f9509de..05c93b572b2153e4d9e790b9dd017a3e9eb99491 100644 (file)
@@ -26,18 +26,14 @@ import com.google.common.primitives.Ints;
 
 import java.util.List;
 
-import static com.google.common.base.Preconditions.checkNotNull;
-
 /**
  * Utility class heavily inspired by Guava
  */
 public class MultiSets {
 
-  private static final Ordering<Multiset.Entry<?>> DECREASING_COUNT_ORDERING = new Ordering<Multiset.Entry<?>>() {
+  private static final Ordering<Multiset.Entry<?>> DECREASING_COUNT_ORDERING = new NonNullOrdering<Multiset.Entry<?>>() {
     @Override
-    public int compare(Multiset.Entry<?> entry1, Multiset.Entry<?> entry2) {
-      checkNotNull(entry1);
-      checkNotNull(entry2);
+    public int doCompare(Multiset.Entry<?> entry1, Multiset.Entry<?> entry2) {
       return Ints.compare(entry2.getCount(), entry1.getCount());
     }
   };
@@ -53,4 +49,14 @@ public class MultiSets {
   public static <E> List<Multiset.Entry<E>> listOrderedByHighestCounts(Multiset<E> multiset) {
     return DECREASING_COUNT_ORDERING.sortedCopy(multiset.entrySet());
   }
+
+  private abstract static class NonNullOrdering<T> extends Ordering<T> {
+
+    @Override
+    public int compare(T left, T right) {
+      return doCompare(left, right);
+    }
+
+    public abstract int doCompare(T left, T right);
+  }
 }