1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
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
27 import org.apache.rat.analysis.IHeaderMatcher;
28 import org.apache.rat.analysis.IHeaders;
29
30 /**
31 * Class to take a set of ILicenses and collection of approved license
32 * categories and extract Subsets.
33 */
34 public class LicenseSetFactory {
35
36 /**
37 * An enum that defines the types of Licenses to extract.
38 */
39 public enum LicenseFilter {
40 /** All defined licenses are returned */
41 ALL,
42 /** Only approved licenses are returned */
43 APPROVED,
44 /** No licenses are returned */
45 NONE;
46
47 /**
48 * Converts from a String to an enum value.
49 *
50 * @param s String representation.
51 * @return given licenseFilter for the given String representation.
52 */
53 static public LicenseFilter fromText(String s) {
54 return LicenseFilter.valueOf(s.toUpperCase());
55 }
56 }
57
58 private final SortedSet<ILicense> licenses;
59 private final Collection<String> approvedLicenses;
60
61 /**
62 * Constructs a factory with the specified set of Licenses and the approved
63 * license collection.
64 *
65 * @param licenses the set of defined licenses.
66 * @param approvedLicenses the list of approved licenses.
67 */
68 public LicenseSetFactory(SortedSet<ILicense> licenses, Collection<String> approvedLicenses) {
69 this.licenses = licenses;
70 this.approvedLicenses = approvedLicenses;
71 }
72
73 /**
74 * Create an empty sorted Set with proper comparator.
75 *
76 * @return An empty sorted set of ILicense objects.
77 */
78 public static SortedSet<ILicense> emptyLicenseSet() {
79 return new TreeSet<>();
80 }
81
82 /**
83 * Create a sorted set of licenses families from the collection.
84 *
85 * @param licenses the collection of all licenses.
86 * @return a SortedSet of license families from the collection.
87 */
88 private static SortedSet<ILicenseFamily> extractFamily(Collection<ILicense> licenses) {
89 SortedSet<ILicenseFamily> result = new TreeSet<>();
90 licenses.stream().map(ILicense::getLicenseFamily).forEach(result::add);
91 return result;
92 }
93
94 /**
95 * Gets the License objects based on the filter.
96 *
97 * @param filter the types of LicenseFamily objects to return.
98 * @return a SortedSet of ILicense objects.
99 */
100 public SortedSet<ILicense> getLicenses(LicenseFilter filter) {
101 switch (filter) {
102 case ALL:
103 return Collections.unmodifiableSortedSet(licenses);
104 case APPROVED:
105 SortedSet<ILicense> result = LicenseSetFactory.emptyLicenseSet();
106 licenses.stream().filter(x -> approvedLicenses.contains(x.getLicenseFamily().getFamilyCategory()))
107 .forEach(result::add);
108 return result;
109 case NONE:
110 default:
111 return Collections.emptySortedSet();
112 }
113 }
114
115 /**
116 * Gets the LicenseFamily objects based on the filter.
117 *
118 * @param filter the types of LicenseFamily objects to return.
119 * @return a SortedSet of ILicenseFamily objects.
120 */
121 public SortedSet<ILicenseFamily> getLicenseFamilies(LicenseFilter filter) {
122 switch (filter) {
123 case ALL:
124 return extractFamily(licenses);
125 case APPROVED:
126 SortedSet<ILicenseFamily> result = LicenseFamilySetFactory.emptyLicenseFamilySet();
127 licenses.stream().map(ILicense::getLicenseFamily)
128 .filter(x -> approvedLicenses.contains(x.getFamilyCategory())).forEach(result::add);
129 return result;
130 case NONE:
131 default:
132 return Collections.emptySortedSet();
133 }
134 }
135
136 /**
137 * Gets the categories of LicenseFamily objects based on the filter.
138 *
139 * @param filter the types of LicenseFamily objects to return.
140 * @return a SortedSet of ILicenseFamily categories.
141 */
142 public SortedSet<String> getLicenseFamilyIds(LicenseFilter filter) {
143 SortedSet<String> result = new TreeSet<>();
144 switch (filter) {
145 case ALL:
146 licenses.stream().map(x -> x.getLicenseFamily().getFamilyCategory()).forEach(result::add);
147 break;
148 case APPROVED:
149 result.addAll(approvedLicenses);
150 break;
151 case NONE:
152 default:
153 // do nothing
154 }
155 return result;
156 }
157
158 /**
159 * Search a SortedSet of licenses for the matching license id.
160 *
161 * @param licenseId the id to search for.
162 * @param licenses the SortedSet of licenses to search.
163 * @return the matching license or {@code null} if not found.
164 */
165 public static Optional<ILicense> search(String familyId, String licenseId, SortedSet<ILicense> licenses) {
166 ILicenseFamily searchFamily = ILicenseFamily.builder().setLicenseFamilyCategory(familyId)
167 .setLicenseFamilyName("searching proxy").build();
168 ILicense target = new ILicense() {
169
170 @Override
171 public String getId() {
172 return licenseId;
173 }
174
175 @Override
176 public void reset() {
177 // do nothing
178 }
179
180 @Override
181 public boolean matches(IHeaders headers) {
182 return false;
183 }
184
185 @Override
186 public boolean equals(Object o) {
187 return ILicense.equals(this, o);
188 }
189
190 @Override
191 public int hashCode() {
192 return ILicense.hash(this);
193 }
194
195 @Override
196 public ILicenseFamily getLicenseFamily() {
197 return searchFamily;
198 }
199
200 @Override
201 public String getNote() {
202 return null;
203 }
204
205 @Override
206 public String getName() {
207 return searchFamily.getFamilyName();
208 }
209
210 @Override
211 public IHeaderMatcher getMatcher() {
212 return null;
213 }
214
215 };
216 return search(target, licenses);
217 }
218
219 /**
220 * Search a SortedSet of licenses for the matching license.
221 * License must mach both family code, and license id.
222 *
223 * @param target the license to search for. Must not be null.
224 * @param licenses the SortedSet of licenses to search.
225 * @return the matching license or {@code null} if not found.
226 */
227 public static Optional<ILicense> search(ILicense target, SortedSet<ILicense> licenses) {
228 SortedSet<ILicense> part = licenses.tailSet(target);
229 return Optional.ofNullable((!part.isEmpty() && part.first().compareTo(target) == 0) ? part.first() : null);
230 }
231 }