1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.rat.report.claim;
21
22 import java.util.ArrayList;
23 import java.util.Comparator;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.concurrent.ConcurrentHashMap;
27
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.rat.api.Document;
30
31
32
33
34
35 public class ClaimStatistic {
36
37
38 public enum Counter {
39
40 APPROVED("A count of approved licenses.", -1, 0),
41
42 ARCHIVES("A count of archive files.", -1, 0),
43
44 BINARIES("A count of binary files.", -1, 0),
45
46 DOCUMENT_TYPES("A count of distinct document types.", -1, 1),
47
48 IGNORED("A count of ignored files.", -1, 0),
49
50 LICENSE_CATEGORIES("A count of distinct license categories.", -1, 1),
51
52 LICENSE_NAMES("A count of distinct license names.", -1, 1),
53
54 NOTICES("A count of notice files.", -1, 0),
55
56 STANDARDS("A count of standard files.", -1, 1),
57
58 UNAPPROVED("A count of unapproved licenses.", 0, 0),
59
60 UNKNOWN("A count of unknown file types.", -1, 0);
61
62
63 private final String description;
64
65 private final int defaultMaxValue;
66
67 private final int defaultMinValue;
68
69 Counter(final String description, final int defaultMaxValue, final int defaultMinValue) {
70 this.description = description;
71 this.defaultMaxValue = defaultMaxValue;
72 this.defaultMinValue = defaultMinValue;
73 }
74
75
76
77
78
79 public String getDescription() {
80 return description;
81 }
82
83
84
85
86
87 public int getDefaultMaxValue() {
88 return defaultMaxValue;
89 }
90
91
92
93
94 public int getDefaultMinValue() {
95 return defaultMinValue;
96 }
97
98
99
100
101
102 public String displayName() {
103 return StringUtils.capitalize(name().replaceAll("_", " ").toLowerCase(Locale.ROOT));
104 }
105 }
106
107
108 private final ConcurrentHashMap<String, IntCounter> licenseNameMap = new ConcurrentHashMap<>();
109
110 private final ConcurrentHashMap<String, IntCounter> licenseFamilyCategoryMap = new ConcurrentHashMap<>();
111
112 private final ConcurrentHashMap<Document.Type, IntCounter> documentTypeMap = new ConcurrentHashMap<>();
113
114 private final ConcurrentHashMap<ClaimStatistic.Counter, IntCounter> counterMap = new ConcurrentHashMap<>();
115
116
117
118
119
120
121
122 private int getValue(final IntCounter counter) {
123 return counter == null ? 0 : counter.value();
124 }
125
126
127
128
129
130
131 public int getCounter(final Counter counter) {
132 return getValue(counterMap.get(counter));
133 }
134
135
136
137
138
139
140 public void incCounter(final Counter counter, final int value) {
141 counterMap.compute(counter, (k, v) -> v == null ? new IntCounter().increment(value) : v.increment(value));
142 }
143
144
145
146
147
148
149 public int getCounter(final Document.Type documentType) {
150 return getValue(documentTypeMap.get(documentType));
151 }
152
153
154
155
156
157 public List<Document.Type> getDocumentTypes() {
158 List<Document.Type> result = new ArrayList<>(documentTypeMap.keySet());
159 result.sort(Comparator.comparing(Enum::name));
160 return result;
161 }
162
163
164
165
166
167
168 public void incCounter(final Document.Type documentType, final int value) {
169 documentTypeMap.compute(documentType, (k, v) -> updateCounter(Counter.DOCUMENT_TYPES, v, value));
170 switch (documentType) {
171 case STANDARD:
172 incCounter(Counter.STANDARDS, value);
173 break;
174 case ARCHIVE:
175 incCounter(Counter.ARCHIVES, value);
176 break;
177 case BINARY:
178 incCounter(Counter.BINARIES, value);
179 break;
180 case NOTICE:
181 incCounter(Counter.NOTICES, value);
182 break;
183 case UNKNOWN:
184 incCounter(Counter.UNKNOWN, value);
185 break;
186 case IGNORED:
187 incCounter(Counter.IGNORED, value);
188 break;
189 }
190 }
191
192
193
194
195
196
197 public int getLicenseCategoryCount(final String licenseFamilyCategory) {
198 return getValue(licenseFamilyCategoryMap.get(licenseFamilyCategory));
199 }
200
201
202
203
204
205
206 public int getLicenseNameCount(final String licenseName) {
207 return getValue(licenseNameMap.get(licenseName));
208 }
209
210
211
212
213
214
215
216
217
218 private IntCounter updateCounter(final Counter counter, final IntCounter intCounter, final int value) {
219 if (intCounter == null) {
220 incCounter(counter, 1);
221 return new IntCounter().increment(value);
222 } else {
223 return intCounter.increment(value);
224 }
225 }
226
227
228
229
230
231
232 public void incLicenseCategoryCount(final String licenseFamilyCategory, final int value) {
233 licenseFamilyCategoryMap.compute(licenseFamilyCategory, (k, v) -> updateCounter(Counter.LICENSE_CATEGORIES, v, value));
234 }
235
236
237
238
239
240 public List<String> getLicenseFamilyCategories() {
241 List<String> result = new ArrayList<>(licenseFamilyCategoryMap.keySet());
242 result.sort(String::compareTo);
243 return result;
244 }
245
246
247
248
249
250 public List<String> getLicenseNames() {
251 List<String> result = new ArrayList<>(licenseNameMap.keySet());
252 result.sort(String::compareTo);
253 return result;
254 }
255
256
257
258
259
260
261 public void incLicenseNameCount(final String licenseName, final int value) {
262 licenseNameMap.compute(licenseName, (k, v) -> updateCounter(Counter.LICENSE_NAMES, v, value));
263 }
264
265
266
267
268 static class IntCounter {
269
270
271
272 private int value;
273
274
275
276
277
278
279 public IntCounter increment(final int count) {
280 value += count;
281 return this;
282 }
283
284
285
286
287
288 public int value() {
289 return value;
290 }
291 }
292 }