View Javadoc
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   */package org.apache.rat.license;
19  
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.SortedSet;
23  import java.util.TreeSet;
24  
25  /**
26   * Class to take a set of ILicenses and collection of approved license categories and extract Subsets.
27   */
28  public class LicenseSetFactory {
29      
30      /**
31       * An enum that defines the types of Licenses to extract.
32       */
33      public enum LicenseFilter {
34          /** All defined licenses are returned */
35          all,
36          /** Only approved licenses are returned */
37          approved,
38          /** No licenses are returned */
39          none;
40          
41      	/**
42      	 * Converts from a String to an enum value. 
43      	 * @param s String representation.
44      	 * @return given licenseFilter for the given String representation.
45      	 */
46          static public LicenseFilter fromText(String s) {
47              return LicenseFilter.valueOf(s.toLowerCase());
48          }
49      }
50  
51      private final SortedSet<ILicense> licenses;
52      private final Collection<String> approvedLicenses;
53      
54      /**
55       * Constructs a factory with the specified set of Licenses and the approved license collection.
56       * @param licenses the set of defined licenses.
57       * @param approvedLicenses the list of approved licenses.
58       */
59      public LicenseSetFactory(SortedSet<ILicense> licenses, Collection<String> approvedLicenses) {
60          this.licenses = licenses;
61          this.approvedLicenses = approvedLicenses;
62      }
63      
64      /**
65       * Create an empty sorted Set with proper comparator.
66       * @return An empty sorted set of ILicense objects.
67       */
68      public static SortedSet<ILicense> emptyLicenseSet() {
69          return new TreeSet<>(ILicense.getComparator());
70      }
71  
72      /**
73       * Create a sorted set of licenses families from the collection.
74       * @param licenses the collection of all licenses.
75       * @return a SortedSet of license families from the collection.
76       */
77      private static SortedSet<ILicenseFamily> extractFamily(Collection<ILicense> licenses) {
78          SortedSet<ILicenseFamily> result = new TreeSet<>();
79          licenses.stream().map( ILicense::getLicenseFamily ).forEach(result::add);
80          return result;
81      }
82  
83      /**
84       * Gets the License objects based on the filter.
85       * @param filter the types of LicenseFamily objects to return.
86       * @return a SortedSet of ILicense objects.
87       */
88      public SortedSet<ILicense> getLicenses(LicenseFilter filter) {
89          switch (filter) {
90          case all:
91              return Collections.unmodifiableSortedSet(licenses);
92          case approved:
93              SortedSet<ILicense> result = LicenseSetFactory.emptyLicenseSet();
94              licenses.stream().filter(x -> approvedLicenses.contains(x.getLicenseFamily().getFamilyCategory()))
95                      .forEach(result::add);
96              return result;
97          case none:
98          default:
99              return Collections.emptySortedSet();
100         }
101     }
102     
103     /**
104      * Gets the LicenseFamily objects based on the filter.
105      * @param filter the types of LicenseFamily objects to return.
106      * @return a SortedSet of ILicenseFamily objects.
107      */
108     public SortedSet<ILicenseFamily> getLicenseFamilies(LicenseFilter filter) {
109         switch (filter) {
110         case all:
111             return extractFamily(licenses);
112         case approved:
113             SortedSet<ILicenseFamily> result = LicenseFamilySetFactory.emptyLicenseFamilySet();
114             licenses.stream().map(ILicense::getLicenseFamily)
115             .filter(x -> approvedLicenses.contains(x.getFamilyCategory()))
116                     .forEach(result::add);
117             return result;
118         case none:
119         default:
120             return Collections.emptySortedSet();
121         }
122     }
123     
124     /**
125      * Gets the categories of LicenseFamily objects based on the filter.
126      * @param filter the types of LicenseFamily objects to return.
127      * @return a SortedSet of ILicenseFamily categories.
128      */
129     public SortedSet<String> getLicenseFamilyIds(LicenseFilter filter) {
130         SortedSet<String> result = new TreeSet<>();
131         switch (filter) {
132         case all:
133             licenses.stream().map(x -> x.getLicenseFamily().getFamilyCategory())
134                     .forEach(result::add);
135             break;
136         case approved:
137             result.addAll(approvedLicenses);
138             break;
139         case none:
140         default:
141             // do nothing
142         }
143         return result;
144     }
145 
146     /**
147      * Search a SortedSet of licenses for the matching license id.
148      * 
149      * @param licenseId the id to search for.
150      * @param licenses the SortedSet of licenses to search.
151      * @return the matching license or {@code null} if not found.
152      */
153     public static ILicense search(String licenseId, SortedSet<ILicense> licenses) {
154         ILicenseFamily searchFamily = ILicenseFamily.builder().setLicenseFamilyCategory(licenseId)
155                 .setLicenseFamilyName("searching proxy").build();
156         ILicense target = new ILicense() {
157     
158             @Override
159             public String getId() {
160                 return licenseId;
161             }
162     
163             @Override
164             public void reset() {
165                 // do nothing
166             }
167     
168             @Override
169             public State matches(String line) {
170                 return State.f;
171             }
172     
173             @Override
174             public int compareTo(ILicense arg0) {
175                 return searchFamily.compareTo(arg0.getLicenseFamily());
176             }
177     
178             @Override
179             public ILicenseFamily getLicenseFamily() {
180                 return searchFamily;
181             }
182     
183             @Override
184             public String getNotes() {
185                 return null;
186             }
187     
188             @Override
189             public String derivedFrom() {
190                 return null;
191             }
192             
193             @Override
194             public String getName() {
195                 return searchFamily.getFamilyName();
196             }
197     
198             @Override
199             public State finalizeState() {
200                 return State.f;
201             }
202     
203             @Override
204             public State currentState() {
205                 return State.f;
206             }
207     
208         };
209         return search(target, licenses);
210     }
211 
212     /**
213      * Search a SortedSet of licenses for the matching license.
214      * 
215      * @param target the license to search for.
216      * @param licenses the SortedSet of licenses to search.
217      * @return the matching license or {@code null} if not found.
218      */
219     public static ILicense search(ILicense target, SortedSet<ILicense> licenses) {
220         SortedSet<ILicense> part = licenses.tailSet(target);
221         return (!part.isEmpty() && part.first().compareTo(target) == 0) ? part.first() : null;
222     }
223     
224 }