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   */
19  package org.apache.rat.license;
20  
21  import java.util.Comparator;
22  import java.util.Objects;
23  import java.util.SortedSet;
24  
25  import org.apache.rat.analysis.IHeaderMatcher;
26  
27  /**
28   * The definition of a License.
29   */
30  public interface ILicense extends IHeaderMatcher, Comparable<ILicense> {
31      /**
32       * @return the ILicenseFamily implementation for this license.
33       */
34      ILicenseFamily getLicenseFamily();
35  
36      /**
37       * @return the notes associated with this license.  May be null or empty.
38       */
39      String getNotes();
40  
41      /**
42       * @return the id of a license that this license is derived from. May be null.
43       */
44      String derivedFrom();
45      
46      /**
47       * Returns the name of this license.  If no name was specified then the name of the family is returned.
48       * @return the name of this license.
49       */
50      String getName();
51  
52      /**
53       * @return An ILicense.Builder instance.
54       */
55      static ILicense.Builder builder() {
56          return new Builder();
57      }
58  
59      /**
60       * @return The comparator for used to sort Licenses. 
61       */
62      static Comparator<ILicense> getComparator() {
63          return Comparator.comparing(IHeaderMatcher::getId);
64      }
65  
66      /**
67       * A builder for ILicense instances.
68       */
69      class Builder  {
70  
71          private IHeaderMatcher.Builder matcher;
72  
73          private String notes;
74  
75          private String derivedFrom;
76          
77          private String name;
78          
79          private String id;
80  
81          private final ILicenseFamily.Builder licenseFamily = ILicenseFamily.builder();
82  
83          /**
84           * Sets the matcher from a builder.
85           * @param matcher the builder for the matcher for the license.
86           * @return this builder for chaining.
87           */
88          public Builder setMatcher(IHeaderMatcher.Builder matcher) {
89              this.matcher = matcher;
90              return this;
91          }
92  
93          /**
94           * Sets the matcher.
95           * @param matcher the matcher for the license.
96           * @return this builder for chaining.
97           */
98          public Builder setMatcher(IHeaderMatcher matcher) {
99              this.matcher = ()->matcher;
100             return this;
101         }
102 
103         /**
104          * Sets the notes for the license.
105          * If called multiple times the notes are concatenated to create a single note.
106          * @param notes the notes for the license.
107          * @return this builder for chaining.
108          */
109         public Builder setNotes(String notes) {
110             this.notes = notes;
111             return this;
112         }
113 
114         /**
115          * Sets the ID of the license.
116          * If the ID is not set then the ID of the license family is used.
117          * @param id the ID for the license
118          * @return this builder for chaining.
119          */
120         public Builder setId(String id) {
121             this.id = id;
122             return this;
123         }
124 
125         /**
126          * Sets the derived from fields in the license.
127          * @param derivedFrom the family category of the license this license was derived from.
128          * @return this builder for chaining.
129          */
130         public Builder setDerivedFrom(String derivedFrom) {
131             this.derivedFrom = derivedFrom;
132             return this;
133         }
134 
135         /**
136          * Set the family category for this license.
137          * The category must be unique across all licenses and must be 5 characters. If more than 
138          * 5 characters are provided then only the first 5 are taken.  If fewer than 5 characters are provided
139          * the category is padded with spaces.
140          * @param licenseFamilyCategory the family category for the license.
141          * @return this builder for chaining.
142          */
143         public Builder setLicenseFamilyCategory(String licenseFamilyCategory) {
144             this.licenseFamily.setLicenseFamilyCategory(licenseFamilyCategory);
145             this.licenseFamily.setLicenseFamilyName("License Family for searching");
146             return this;
147         }
148 
149         /**
150          * Sets the name of the license.
151          * If the name is not set then the name of the license family is used.
152          * @param name the name for the license
153          * @return this builder for chaining.
154          */
155         public Builder setName(String name) {
156             this.name = name;
157             return this;
158         }
159 
160         /**
161          * @param licenseFamilies the set of defined license families.
162          * @return A new License implementation.
163          */
164         public ILicense build(SortedSet<ILicenseFamily> licenseFamilies) {
165         	Objects.requireNonNull(matcher, "Matcher must not be null");
166             ILicenseFamily family = LicenseFamilySetFactory.search(licenseFamily.build(), licenseFamilies);
167             Objects.requireNonNull(family, "License family "+licenseFamily.getCategory()+" not found.");
168             return new SimpleLicense(family, matcher.build(), derivedFrom, notes, name, id); 
169         }
170     }
171 }