1
2
3
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
17
18
19
20
21
22
23
24
25
26 final class Summary {
27
28
29
30
31 private final Collection<File> files;
32
33
34
35
36 private final Collection<ResourceValidator> validators;
37
38
39
40
41
42
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 }