1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.analysis;
20
21 import java.util.Collection;
22 import java.util.Collections;
23
24 import org.apache.rat.analysis.matchers.AbstractMatcherContainer;
25 import org.apache.rat.license.ILicense;
26 import org.apache.rat.license.ILicenseFamily;
27
28
29
30
31
32
33
34
35 class LicenseCollection extends AbstractMatcherContainer implements ILicense {
36
37 private static final ILicenseFamily DEFAULT = ILicenseFamily.builder().setLicenseFamilyCategory("Dummy")
38 .setLicenseFamilyName("HeaderMatcherCollection default license family").build();
39 private final Collection<ILicense> enclosed;
40 private ILicense matchingLicense;
41 private State lastState;
42
43
44
45
46
47 public LicenseCollection(Collection<ILicense> enclosed) {
48 super(enclosed);
49 this.enclosed = Collections.unmodifiableCollection(enclosed);
50 this.matchingLicense = null;
51 this.lastState = State.i;
52 }
53
54 @Override
55 public String getId() {
56 return "Default License Collection";
57 }
58
59 @Override
60 public void reset() {
61 enclosed.forEach(ILicense::reset);
62 this.lastState = State.i;
63 this.matchingLicense = null;
64 }
65
66 @Override
67 public State matches(String line) {
68 State dflt = State.f;
69 for (ILicense license : enclosed) {
70 switch (license.matches(line)) {
71 case t:
72 this.matchingLicense = license;
73 lastState = State.t;
74 return State.t;
75 case i:
76 dflt = State.i;
77 break;
78 default:
79
80 break;
81 }
82 }
83 lastState = dflt;
84 return dflt;
85 }
86
87 @Override
88 public State currentState() {
89 if (lastState == State.t) {
90 return lastState;
91 }
92 for (ILicense license : enclosed) {
93 switch (license.currentState()) {
94 case t:
95 this.matchingLicense = license;
96 lastState = State.t;
97 return lastState;
98 case i:
99 lastState = State.i;
100 return lastState;
101 case f:
102
103 break;
104 }
105 }
106 lastState = State.f;
107 return lastState;
108 }
109
110 @Override
111 public int compareTo(ILicense arg0) {
112 return getLicenseFamily().compareTo(arg0.getLicenseFamily());
113 }
114
115 @Override
116 public ILicenseFamily getLicenseFamily() {
117 return matchingLicense == null ? DEFAULT : matchingLicense.getLicenseFamily();
118 }
119
120 @Override
121 public String getNotes() {
122 return matchingLicense == null ? null : matchingLicense.getNotes();
123 }
124
125 @Override
126 public String derivedFrom() {
127 return matchingLicense == null ? null : matchingLicense.derivedFrom();
128 }
129
130 @Override
131 public String getName() {
132 return getLicenseFamily().getFamilyName();
133 }
134 }