1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.license;
20
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Optional;
24 import java.util.SortedSet;
25 import java.util.TreeSet;
26 import java.util.function.Predicate;
27
28 import org.apache.rat.analysis.IHeaderMatcher;
29 import org.apache.rat.analysis.IHeaders;
30 import org.apache.rat.utils.Log;
31 import org.apache.rat.utils.ReportingSet;
32
33
34
35
36
37 public class LicenseSetFactory {
38
39
40
41
42
43
44
45 public static ILicenseFamily familySearch(final String target, final SortedSet<ILicenseFamily> licenseFamilies) {
46 ILicenseFamily family = ILicenseFamily.builder().setLicenseFamilyCategory(target).setLicenseFamilyName("Searching family")
47 .build();
48 return familySearch(family, licenseFamilies);
49 }
50
51
52
53
54
55
56
57 public static ILicenseFamily familySearch(final ILicenseFamily target, final SortedSet<ILicenseFamily> licenseFamilies) {
58 SortedSet<ILicenseFamily> part = licenseFamilies.tailSet(target);
59 return (!part.isEmpty() && part.first().compareTo(target) == 0) ? part.first() : null;
60 }
61
62
63
64
65 public enum LicenseFilter {
66
67 ALL,
68
69 APPROVED,
70
71 NONE
72 }
73
74
75 private final ReportingSet<ILicenseFamily> families;
76
77 private final ReportingSet<ILicense> licenses;
78
79
80 private final SortedSet<String> approvedLicenseCategories;
81
82
83
84
85 private final SortedSet<String> removedLicenseCategories;
86
87
88
89
90 private final SortedSet<String> approvedLicenseIds;
91
92
93
94
95 private final SortedSet<String> removedLicenseIds;
96
97
98
99
100
101 public LicenseSetFactory() {
102 families = new ReportingSet<>(new TreeSet<ILicenseFamily>())
103 .setMsgFormat(s -> String.format("Duplicate LicenseFamily category: %s", s.getFamilyCategory()));
104 licenses = new ReportingSet<>(new TreeSet<ILicense>())
105 .setMsgFormat(s -> String.format("Duplicate License %s (%s) of type %s", s.getName(), s.getId(), s.getLicenseFamily().getFamilyCategory()));
106
107 approvedLicenseCategories = new TreeSet<>();
108 removedLicenseCategories = new TreeSet<>();
109 approvedLicenseIds = new TreeSet<>();
110 removedLicenseIds = new TreeSet<>();
111 }
112
113
114
115
116
117
118 public LicenseSetFactory(final SortedSet<ILicense> licenses) {
119 this();
120 this.licenses.addAll(licenses);
121 licenses.forEach(l -> families.addIfNotPresent(l.getLicenseFamily()));
122 }
123
124 public void add(final LicenseSetFactory other) {
125 this.families.addAll(other.families);
126 this.licenses.addAll(other.licenses);
127 this.approvedLicenseCategories.addAll(other.approvedLicenseCategories);
128 this.removedLicenseCategories.addAll(other.removedLicenseCategories);
129 this.approvedLicenseIds.addAll(other.approvedLicenseIds);
130 this.removedLicenseIds.addAll(other.removedLicenseIds);
131 }
132
133
134
135
136
137
138 public void logFamilyCollisions(final Log.Level level) {
139 families.setLogLevel(level);
140 }
141
142
143
144
145
146 public void familyDuplicateOption(final ReportingSet.Options state) {
147 families.setDuplicateOption(state);
148 }
149
150
151
152
153
154 public void logLicenseCollisions(final Log.Level level) {
155 licenses.setLogLevel(level);
156 }
157
158
159
160
161
162 public void licenseDuplicateOption(final ReportingSet.Options state) {
163 licenses.setDuplicateOption(state);
164 }
165
166
167
168
169
170
171 private static SortedSet<ILicenseFamily> extractFamily(final Collection<ILicense> licenses) {
172 SortedSet<ILicenseFamily> result = new TreeSet<>();
173 licenses.stream().map(ILicense::getLicenseFamily).forEach(result::add);
174 return result;
175 }
176
177
178
179
180
181
182 public void addLicense(final ILicense license) {
183 if (license != null) {
184 this.licenses.add(license);
185 this.families.addIfNotPresent(license.getLicenseFamily());
186 }
187 }
188
189
190
191
192
193
194
195 public ILicense addLicense(final ILicense.Builder builder) {
196 if (builder != null) {
197 ILicense license = builder.setLicenseFamilies(families).build();
198 this.licenses.add(license);
199 return license;
200 }
201 return null;
202 }
203
204
205
206
207
208
209 public void addLicenses(final Collection<ILicense> licenses) {
210 this.licenses.addAll(licenses);
211 licenses.stream().map(ILicense::getLicenseFamily).forEach(families::add);
212 }
213
214
215
216
217
218
219 public void addFamily(final ILicenseFamily family) {
220 if (family != null) {
221 this.families.add(family);
222 }
223 }
224
225
226
227
228
229
230
231 public void addFamily(final ILicenseFamily.Builder builder) {
232 if (builder != null) {
233 this.families.add(builder.build());
234 }
235 }
236
237
238
239
240
241 public void approveLicenseCategory(final String familyCategory) {
242 approvedLicenseCategories.add(ILicenseFamily.makeCategory(familyCategory));
243 }
244
245
246
247
248
249 public void removeLicenseCategory(final String familyCategory) {
250 removedLicenseCategories.add(ILicenseFamily.makeCategory(familyCategory));
251 }
252
253
254
255
256
257 public void approveLicenseId(final String licenseId) {
258 approvedLicenseIds.add(licenseId);
259 }
260
261
262
263
264
265 public void removeLicenseId(final String licenseId) {
266 removedLicenseIds.add(licenseId);
267 }
268
269
270
271
272
273
274 private boolean isApprovedCategory(final ILicenseFamily family) {
275 return approvedLicenseCategories.contains(family.getFamilyCategory()) && !removedLicenseCategories.contains(family.getFamilyCategory());
276 }
277
278
279
280
281
282 public Predicate<ILicense> getApprovedLicensePredicate() {
283 return lic -> !removedLicenseIds.contains(lic.getId()) && (approvedLicenseIds.contains(lic.getId()) ||
284 isApprovedCategory(lic.getLicenseFamily()));
285 }
286
287
288
289
290
291
292 public SortedSet<ILicense> getLicenses(final LicenseFilter filter) {
293 switch (filter) {
294 case ALL:
295 return Collections.unmodifiableSortedSet(licenses);
296 case APPROVED:
297 SortedSet<ILicense> result = new TreeSet<>();
298 licenses.stream().filter(getApprovedLicensePredicate()).forEach(result::add);
299 return result;
300 case NONE:
301 default:
302 return Collections.emptySortedSet();
303 }
304 }
305
306
307
308
309
310
311 public SortedSet<ILicenseFamily> getLicenseFamilies(final LicenseFilter filter) {
312 SortedSet<ILicenseFamily> result;
313 switch (filter) {
314 case ALL:
315 result = extractFamily(licenses);
316 result.addAll(families);
317 return result;
318 case APPROVED:
319 result = new TreeSet<>();
320 licenses.stream().map(ILicense::getLicenseFamily).filter(this::isApprovedCategory).forEach(result::add);
321 return result;
322 case NONE:
323 default:
324 return Collections.emptySortedSet();
325 }
326 }
327
328
329
330
331
332
333
334 public SortedSet<String> getLicenseCategories(final LicenseFilter filter) {
335 SortedSet<String> result = new TreeSet<>();
336 switch (filter) {
337 case ALL:
338 licenses.forEach(l -> result.add(l.getLicenseFamily().getFamilyCategory()));
339 families.forEach(f -> result.add(f.getFamilyCategory()));
340 result.addAll(approvedLicenseCategories);
341 result.addAll(removedLicenseCategories);
342 return result;
343 case APPROVED:
344 approvedLicenseCategories.stream().filter(s -> !removedLicenseCategories.contains(s)).forEach(result::add);
345 families.stream().filter(this::isApprovedCategory).forEach(f -> result.add(f.getFamilyCategory()));
346 return result;
347 case NONE:
348 default:
349 return Collections.emptySortedSet();
350 }
351 }
352
353
354
355
356
357
358
359 public SortedSet<String> getLicenseIds(final LicenseFilter filter) {
360 Predicate<ILicense> approved = l -> (isApprovedCategory(l.getLicenseFamily()) ||
361 approvedLicenseIds.contains(l.getId())) && !removedLicenseIds.contains(l.getId());
362 SortedSet<String> result = new TreeSet<>();
363 switch (filter) {
364 case ALL:
365 licenses.forEach(l -> result.add(l.getId()));
366 result.addAll(approvedLicenseCategories);
367 result.addAll(removedLicenseCategories);
368 result.addAll(approvedLicenseIds);
369 result.addAll(removedLicenseIds);
370 return result;
371 case APPROVED:
372 licenses.stream().filter(approved).forEach(l -> result.add(l.getId()));
373 families.stream().filter(this::isApprovedCategory).forEach(f -> result.add(f.getFamilyCategory()));
374 approvedLicenseIds.stream().filter(s -> !removedLicenseIds.contains(s)).forEach(result::add);
375 return result;
376 case NONE:
377 default:
378 return Collections.emptySortedSet();
379 }
380 }
381
382
383
384
385
386
387
388
389 public static Optional<ILicense> search(final String familyId, final String licenseId, final SortedSet<ILicense> licenses) {
390 ILicenseFamily searchFamily = ILicenseFamily.builder().setLicenseFamilyCategory(familyId)
391 .setLicenseFamilyName("searching proxy").build();
392 ILicense target = new ILicense() {
393
394 @Override
395 public String getId() {
396 return licenseId;
397 }
398
399 @Override
400 public void reset() {
401
402 }
403
404 @Override
405 public boolean matches(final IHeaders headers) {
406 return false;
407 }
408
409 @Override
410 public boolean equals(final Object o) {
411 return ILicense.equals(this, o);
412 }
413
414 @Override
415 public int hashCode() {
416 return ILicense.hash(this);
417 }
418
419 @Override
420 public ILicenseFamily getLicenseFamily() {
421 return searchFamily;
422 }
423
424 @Override
425 public String getNote() {
426 return null;
427 }
428
429 @Override
430 public String getName() {
431 return searchFamily.getFamilyName();
432 }
433
434 @Override
435 public IHeaderMatcher getMatcher() {
436 return null;
437 }
438
439 };
440 return search(target, licenses);
441 }
442
443
444
445
446
447
448
449
450
451 public static Optional<ILicense> search(final ILicense target, final SortedSet<ILicense> licenses) {
452 SortedSet<ILicense> part = licenses.tailSet(target);
453 return Optional.ofNullable((!part.isEmpty() && part.first().compareTo(target) == 0) ? part.first() : null);
454 }
455 }