View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.maven;
6   
7   import com.qulice.spi.ResourceValidator;
8   import java.io.File;
9   import java.util.Collection;
10  import java.util.LinkedHashMap;
11  import java.util.Locale;
12  import java.util.Map;
13  import java.util.stream.Collectors;
14  
15  /**
16   * The scope of a finished check, as one line of text.
17   *
18   * <p>Tells the reader what the run has just covered: how many Java files
19   * went through the validators and how many rules judged them, validator
20   * by validator. A validator that cannot count its rules stays out of the
21   * breakdown, instead of claiming zero, and a module that had no file to
22   * give them counts no rules at all.</p>
23   *
24   * @since 1.0
25   */
26  final class Summary {
27  
28      /**
29       * Files handed to the validators.
30       */
31      private final Collection<File> files;
32  
33      /**
34       * Validators that read them.
35       */
36      private final Collection<ResourceValidator> validators;
37  
38      /**
39       * Constructor.
40       *
41       * @param files Files handed to the validators
42       * @param validators Validators that read them
43       */
44      Summary(final Collection<File> files,
45          final Collection<ResourceValidator> validators) {
46          this.files = files;
47          this.validators = validators;
48      }
49  
50      @Override
51      public String toString() {
52          final Map<String, Integer> counts = this.counts();
53          return String.format(
54              "checked %s against %d rules%s",
55              this.javas(),
56              counts.values().stream().mapToInt(Integer::intValue).sum(),
57              Summary.breakdown(counts)
58          );
59      }
60  
61      private Map<String, Integer> counts() {
62          final Map<String, Integer> counts =
63              new LinkedHashMap<>(this.validators.size());
64          if (!this.files.isEmpty()) {
65              for (final ResourceValidator validator : this.validators) {
66                  final int rules = validator.rules();
67                  if (rules > 0) {
68                      counts.put(validator.name(), rules);
69                  }
70              }
71          }
72          return counts;
73      }
74  
75      private String javas() {
76          final long count = this.files.stream().filter(
77              file -> file.getName().toLowerCase(Locale.ROOT).endsWith(".java")
78          ).count();
79          final String text;
80          if (count == 1L) {
81              text = "1 .java file";
82          } else {
83              text = String.format("%d .java files", count);
84          }
85          return text;
86      }
87  
88      private static String breakdown(final Map<String, Integer> counts) {
89          final String text;
90          if (counts.isEmpty()) {
91              text = "";
92          } else {
93              text = counts.entrySet().stream().map(
94                  entry -> String.format("%d %s", entry.getValue(), entry.getKey())
95              ).collect(Collectors.joining(", ", " (", ")"));
96          }
97          return text;
98      }
99  }