Author: rwhitcomb
Date: Thu May  7 15:54:35 2020
New Revision: 1877476

URL: http://svn.apache.org/viewvc?rev=1877476&view=rev
Log:
PIVOT-1032: Update the style summary program to generate a cleaner, nicer
report with number of files information as well.


Modified:
    pivot/trunk/StyleErrors.java

Modified: pivot/trunk/StyleErrors.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/StyleErrors.java?rev=1877476&r1=1877475&r2=1877476&view=diff
==============================================================================
--- pivot/trunk/StyleErrors.java (original)
+++ pivot/trunk/StyleErrors.java Thu May  7 15:54:35 2020
@@ -31,7 +31,8 @@ import java.util.regex.Pattern;
 
 /**
  * Read the file(s) given on the command line, which are presumed to be
- * the output of the "check styles", and generate a summary of the results.
+ * the output of the "check styles", and generate a summary of the results
+ * for each one.
  */
 public final class StyleErrors {
     /** Private constructor because we only use static methods here. */
@@ -47,15 +48,32 @@ public final class StyleErrors {
         private String errorClass;
         /** The final count of how many times this error was encountered. */
         private Integer count;
+        /** A set of the files it was found in. */
+        private Set<String> files;
 
-        /** Construct one of these with the given information.
+        /**
+         * Construct one with a starting count of 1 and the given error class
+         * and first file name.
          * @param errClass The checkstyle error.
-         * @param c The final count of these errors.
+         * @param fileName The first file encountered with this error.
          */
-        Info(final String errClass, final Integer c) {
+        Info(final String errClass, final String fileName) {
             this.errorClass = errClass;
-            this.count = c;
+            this.count = Integer.valueOf(1);
+            this.files = new HashSet<>();
+            this.files.add(fileName);
         }
+
+        /**
+         * Record another occurrence of this error in the given file.
+         * @param fileName The next file name to add (which also
+         * increases the count).
+         */
+        void addFile(final String fileName) {
+            count++;
+            files.add(fileName);
+        }
+
         /** @return The saved checkstyle error name. */
         String getErrorClass() {
             return errorClass;
@@ -64,6 +82,10 @@ public final class StyleErrors {
         Integer getCount() {
             return count;
         }
+        /** @return The set of files this error was found in. */
+        Set<String> getFileSet() {
+            return files;
+        }
     }
 
     /**
@@ -83,6 +105,21 @@ public final class StyleErrors {
         }
     };
 
+    /**
+     * Get a list of strings as a parenthesized list.
+     */
+    private static String list(Set<String> strings) {
+        StringBuilder buf = new StringBuilder("(");
+        int i = 0;
+        for (String s : strings) {
+            if (i++ > 0)
+                buf.append(", ");
+            buf.append(s);
+        }
+        buf.append(')');
+        return buf.toString();
+    }
+
     /** Pattern used to parse each input line. */
     private static final Pattern LINE_PATTERN =
         
Pattern.compile("^\\[[A-Z]+\\]\\s+(([a-zA-Z]\\:)?([^:]+))(\\:[0-9]+\\:)([0-9]+\\:)?\\s+(.+)\\s+(\\[[a-zA-Z]+\\])$");
@@ -90,14 +127,18 @@ public final class StyleErrors {
     private static final int FILE_NAME_GROUP = 1;
     /** The group in the {@link #LINE_PATTERN} that contains the checkstyle 
error name. */
     private static final int CLASS_NAME_GROUP = 7;
-    /** A format string used to output all the information in a uniform 
manner. */
-    private static final String FORMAT = "%1$-32s%2$5d%n";
+    /** Format error info with a number of files suffix. */
+    private static final String FORMAT1 = "%1$2d. %2$-30s%3$5d (%4$d)%n";
+    /** Same as {@link #FORMAT1} except we have a list of file names instead 
of a number. */
+    private static final String FORMAT2 = "%1$2d. %2$-30s%3$5d %4$s%n";
+    /** Format postreport info. */
+    private static final String FORMAT3 = "    %1$-30s%2$5d (%3$d)%n";
     /** Format string used to print the underlines. */
-    private static final String UNDER_FORMAT = "%1$-32s%2$5s%n";
+    private static final String UNDER_FORMAT = "%1$3s %2$-30s%3$5s %4$s%n";
     /** The set of unique file names found in the list. */
     private static Set<String> fileNameSet = new HashSet<>();
     /** For each type of checkstyle error, the name and running count for 
each. */
-    private static Map<String, Integer> counts = new TreeMap<>();
+    private static Map<String, Info> workingSet = new TreeMap<>();
     /** At the end of each file, the list used to sort by count and name. */
     private static List<Info> sortedList = new ArrayList<>();
 
@@ -108,8 +149,9 @@ public final class StyleErrors {
      */
     public static void main(final String[] args) {
         for (String arg : args) {
+            int total = 0;
             int lineNo = 0;
-            counts.clear();
+            workingSet.clear();
             sortedList.clear();
             try (BufferedReader reader = new BufferedReader(new FileReader(new 
File(arg)))) {
                 String line;
@@ -119,15 +161,16 @@ public final class StyleErrors {
                     if (m.matches()) {
                         String fileName = m.group(FILE_NAME_GROUP);
                         fileNameSet.add(fileName);
+                        File f = new File(fileName);
+                        String nameOnly = f.getName();
                         String errorClass = m.group(CLASS_NAME_GROUP);
-                        Integer count = counts.get(errorClass);
-                        if (count == null) {
-                            count = Integer.valueOf(1);
+                        Info info = workingSet.get(errorClass);
+                        if (info == null) {
+                            workingSet.put(errorClass, new Info(errorClass, 
nameOnly));
                         } else {
-                            int i = count.intValue() + 1;
-                            count = Integer.valueOf(i);
+                            info.addFile(nameOnly);
                         }
-                        counts.put(errorClass, count);
+                        total++;
                     } else if (line.equals("Starting audit...") || 
line.equals("Audit done.")) {
                         continue;
                     } else {
@@ -138,21 +181,30 @@ public final class StyleErrors {
             } catch (IOException ioe) {
                 System.err.println("Error reading the \"" + arg + "\" file: " 
+ ioe.getMessage());
             }
-            int total = 0;
-            for (String key : counts.keySet()) {
-                Integer count = counts.get(key);
-                total += count.intValue();
-                Info info = new Info(key, count);
+
+            // Once we're done, resort according to error counts
+            for (String key : workingSet.keySet()) {
+                Info info = workingSet.get(key);
                 sortedList.add(info);
             }
             Collections.sort(sortedList, comparator);
+
+            // Output the final summary report for this input file
+            System.out.format(UNDER_FORMAT, " # ", "Category", "Count", 
"File(s)");
+            System.out.format(UNDER_FORMAT, "---", 
"----------------------------", "-----", "---------------");
+            int categoryNo = 0;
             for (Info info : sortedList) {
-                System.out.format(FORMAT, info.getErrorClass(), 
info.getCount());
+                categoryNo++;
+                Set<String> files = info.getFileSet();
+                if (files.size() > 3) {
+                    System.out.format(FORMAT1, categoryNo, 
info.getErrorClass(), info.getCount(), files.size());
+                } else {
+                    System.out.format(FORMAT2, categoryNo, 
info.getErrorClass(), info.getCount(), list(files));
+                }
             }
-            System.out.format(UNDER_FORMAT, "----------------------------", 
"-----");
-            System.out.format(FORMAT, "Grand Total", total);
-            System.out.format(FORMAT, "Total Files With Errors", 
fileNameSet.size());
-            System.out.format(FORMAT, "Number of Error Categories", 
sortedList.size());
+
+            System.out.format(UNDER_FORMAT, "---", 
"----------------------------", "-----", "---------------");
+            System.out.format(FORMAT3, "Totals", total, fileNameSet.size());
             System.out.println();
         }
     }


Reply via email to