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  import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
26  
27  /**
28   * Class to take a set of ILicenses and collection of approved license categories and extract Subsets.
29   */
30  public class LicenseFamilySetFactory {
31  
32      private final SortedSet<ILicenseFamily> families;
33      private final Collection<String> approvedLicenses;
34      
35      /**
36       * Constructs a factory with the specified set of Licenses and the approved license collection.
37       * @param licenses the set of defined licenses.
38       * @param approvedLicenses the list of approved licenses.
39       */
40      public LicenseFamilySetFactory(SortedSet<ILicenseFamily> licenses, Collection<String> approvedLicenses) {
41          this.families = licenses;
42          this.approvedLicenses = approvedLicenses;
43      }
44      
45      /**
46       * Create an empty sorted Set with proper comparator.
47       * @return An empty sorted set of ILicenseFamily objects.
48       */
49      public static SortedSet<ILicenseFamily> emptyLicenseFamilySet() {
50          return new TreeSet<>();
51      }
52  
53  
54      /**
55       * Gets the License objects based on the filter.
56       * @param filter the types of LicenseFamily objects to return.
57       * @return a SortedSet of ILicense objects.
58       */
59      public SortedSet<ILicenseFamily> getFamilies(LicenseFilter filter) {
60          switch (filter) {
61          case all:
62              return Collections.unmodifiableSortedSet(families);
63          case approved:
64              SortedSet<ILicenseFamily> result = emptyLicenseFamilySet();
65              families.stream().filter(x -> approvedLicenses.contains(x.getFamilyCategory()))
66                      .forEach(result::add);
67              return result;
68          case none:
69          default:
70              return Collections.emptySortedSet();
71          }
72      }
73      
74      
75      /**
76       * Gets the categories of LicenseFamily objects based on the filter.
77       * @param filter the types of LicenseFamily objects to return.
78       * @return a SortedSet of ILicenseFamily categories.
79       */
80      public SortedSet<String> getFamilyIds(LicenseFilter filter) {
81          SortedSet<String> result = new TreeSet<>();
82          switch (filter) {
83          case all:
84              families.stream().map(ILicenseFamily::getFamilyCategory)
85                      .forEach(result::add);
86              break;
87          case approved:
88              result.addAll(approvedLicenses);
89              break;
90          case none:
91          default:
92              // do nothing
93          }
94          return result;
95      }
96  
97      
98      /**
99       * Search a SortedSet of ILicenseFamily instances looking for a matching instance.
100      * @param target 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 ILicenseFamily search(String target, SortedSet<ILicenseFamily> licenseFamilies) {
105         ILicenseFamily family = ILicenseFamily.builder().setLicenseFamilyCategory(target).setLicenseFamilyName("Searching family")
106                 .build();
107         return search( family, licenseFamilies);
108     }
109 
110     /**
111      * Search a SortedSet of ILicenseFamily instances looking for a matching instance.
112      * @param target 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 ILicenseFamily search(ILicenseFamily target, SortedSet<ILicenseFamily> licenseFamilies) {
117         SortedSet<ILicenseFamily> part = licenseFamilies.tailSet(target);
118         return (!part.isEmpty() && part.first().compareTo(target) == 0) ? part.first() : null;
119     }
120     
121 }