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.Optional;
23  import java.util.SortedSet;
24  import java.util.TreeSet;
25  
26  import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
27  
28  /**
29   * Class to take a set of ILicenses and collection of approved license categories and extract Subsets.
30   */
31  public class LicenseFamilySetFactory {
32  
33      private final SortedSet<ILicenseFamily> families;
34      private final Collection<String> approvedLicenses;
35      
36      /**
37       * Constructs a factory with the specified set of Licenses and the approved license collection.
38       * @param licenses the set of defined licenses.
39       * @param approvedLicenses the list of approved licenses.
40       */
41      public LicenseFamilySetFactory(SortedSet<ILicenseFamily> licenses, Collection<String> approvedLicenses) {
42          this.families = licenses;
43          this.approvedLicenses = approvedLicenses;
44      }
45      
46      /**
47       * Create an empty sorted Set with proper comparator.
48       * @return An empty sorted set of ILicenseFamily objects.
49       */
50      public static SortedSet<ILicenseFamily> emptyLicenseFamilySet() {
51          return new TreeSet<>();
52      }
53  
54  
55      /**
56       * Gets the License objects based on the filter.
57       * @param filter the types of LicenseFamily objects to return.
58       * @return a SortedSet of ILicense objects.
59       */
60      public SortedSet<ILicenseFamily> getFamilies(LicenseFilter filter) {
61          switch (filter) {
62          case ALL:
63              return Collections.unmodifiableSortedSet(families);
64          case APPROVED:
65              SortedSet<ILicenseFamily> result = emptyLicenseFamilySet();
66              families.stream().filter(x -> approvedLicenses.contains(x.getFamilyCategory()))
67                      .forEach(result::add);
68              return result;
69          case NONE:
70          default:
71              return Collections.emptySortedSet();
72          }
73      }
74      
75      
76      /**
77       * Gets the categories of LicenseFamily objects based on the filter.
78       * @param filter the types of LicenseFamily objects to return.
79       * @return a SortedSet of ILicenseFamily categories.
80       */
81      public SortedSet<String> getFamilyIds(LicenseFilter filter) {
82          SortedSet<String> result = new TreeSet<>();
83          switch (filter) {
84          case ALL:
85              families.stream().map(ILicenseFamily::getFamilyCategory)
86                      .forEach(result::add);
87              break;
88          case APPROVED:
89              result.addAll(approvedLicenses);
90              break;
91          case NONE:
92          default:
93              // do nothing
94          }
95          return result;
96      }
97  
98      /**
99       * Search a SortedSet of ILicenseFamily instances looking for a matching instance.
100      * @param familyId The instance to search for.
101      * @param licenseFamilies the license families to search
102      * @return the matching instance of the target given.
103      */
104     public static boolean hasFamily(String familyId, SortedSet<ILicenseFamily> licenseFamilies) {
105         String familyCat = ILicenseFamily.makeCategory(familyId);
106         SortedSet<String> result = new TreeSet<>();
107         return licenseFamilies.stream().map(ILicenseFamily::getFamilyCategory).filter( f ->  f.equals(familyCat)).findFirst().isPresent();
108     }
109 
110     /**
111      * Search a SortedSet of ILicenseFamily instances looking for a matching instance.
112      * @param familyId The instance to search for.
113      * @param licenseFamilies the license families to search
114      * @return the matching instance of the target given.
115      */
116     public static Optional<ILicenseFamily> findFamily(String familyId, SortedSet<ILicenseFamily> licenseFamilies) {
117         String familyCat = ILicenseFamily.makeCategory(familyId);
118         SortedSet<String> result = new TreeSet<>();
119         return licenseFamilies.stream().filter( f ->  f.getFamilyCategory().equals(familyCat)).findFirst();
120     }
121 
122 
123     /**
124      * Search a SortedSet of ILicenseFamily instances looking for a matching instance.
125      * @param target The instance to search for.
126      * @param licenseFamilies the license families to search
127      * @return the matching instance of the target given.
128      */
129     public static ILicenseFamily search(String target, SortedSet<ILicenseFamily> licenseFamilies) {
130         ILicenseFamily family = ILicenseFamily.builder().setLicenseFamilyCategory(target).setLicenseFamilyName("Searching family")
131                 .build();
132         return search( family, licenseFamilies);
133     }
134 
135     /**
136      * Search a SortedSet of ILicenseFamily instances looking for a matching instance.
137      * @param target The instance to search for.
138      * @param licenseFamilies the license families to search
139      * @return the matching instance of the target given.
140      */
141     public static ILicenseFamily search(ILicenseFamily target, SortedSet<ILicenseFamily> licenseFamilies) {
142         SortedSet<ILicenseFamily> part = licenseFamilies.tailSet(target);
143         return (!part.isEmpty() && part.first().compareTo(target) == 0) ? part.first() : null;
144     }
145     
146 }