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.Objects;
22  import java.util.SortedSet;
23  
24  import org.apache.rat.analysis.IHeaderMatcher;
25  
26  /**
27   * The definition of a License.
28   */
29  public interface ILicense extends IHeaderMatcher, Comparable<ILicense> {
30      /**
31       * Gets the license family.
32       * 
33       * @return the ILicenseFamily implementation for this license.
34       */
35      ILicenseFamily getLicenseFamily();
36  
37      /**
38       * Gets the note associated with the license.
39       * 
40       * @return the note associated with this license. May be null or empty.
41       */
42      String getNote();
43  
44      /**
45       * Returns the name of this license. If no name was specified then the name of
46       * the family is returned.
47       * 
48       * @return the name of this license.
49       */
50      String getName();
51  
52      /**
53       * Gets the name of the family that this license if part of.
54       * 
55       * @return the name of the license family that this license is part of.
56       */
57      default String getFamilyName() {
58          return getLicenseFamily().getFamilyName();
59      }
60  
61      /**
62       * Get the header matcher for this license.
63       * 
64       * @return the header matcher for this license.
65       */
66      IHeaderMatcher getMatcher();
67  
68      @Override
69      default int compareTo(ILicense other) {
70          int result = getLicenseFamily().compareTo(other.getLicenseFamily());
71              return result == 0 ? getId().compareTo(other.getId()) : result;
72      }
73  
74      /**
75       * A default implementatin of a License hash
76       * @param license the license to hash
77       * @return the license hash value
78       */
79      static int hash(ILicense license) {
80          return Objects.hash(license.getLicenseFamily(), license.getId());
81      }
82  
83      /**
84       * A default implementation of equals.
85       * @param license1 The license to check for equality.
86       * @param o the object to check for equality to.
87       * @return true if the object is equal to the license1.
88       */
89      static boolean equals(ILicense license1, Object o) {
90          if (license1 == o) return true;
91          if (!(o instanceof ILicense)) return false;
92          ILicense that = (ILicense) o;
93          return license1.compareTo(that) == 0;
94      }
95  
96      /**
97       * Gets a builder for licenses.
98       * 
99       * @return An ILicense.Builder instance.
100      */
101     static ILicense.Builder builder() {
102         return new SimpleLicense.Builder();
103     }
104 
105     /**
106      * A builder for ILicense instances.
107      */
108     interface Builder extends IHeaderMatcher.Builder {
109 
110         /**
111          * Sets the matcher from a builder.
112          * 
113          * @param matcher the builder for the matcher for the license.
114          * @return this builder for chaining.
115          */
116         Builder setMatcher(IHeaderMatcher.Builder matcher);
117 
118         /**
119          * Sets the matcher.
120          * 
121          * @param matcher the matcher for the license.
122          * @return this builder for chaining.
123          */
124         Builder setMatcher(IHeaderMatcher matcher);
125 
126         /**
127          * Sets the notes for the license. If called multiple times the notes are
128          * concatenated to create a single note.
129          * 
130          * @param notes the notes for the license.
131          * @return this builder for chaining.
132          */
133         Builder setNote(String notes);
134 
135         /**
136          * Sets the ID of the license. If the ID is not set then the ID of the license
137          * family is used.
138          * 
139          * @param id the ID for the license
140          * @return this builder for chaining.
141          */
142         Builder setId(String id);
143 
144         /**
145          * Set the family category for this license. The category must be unique across
146          * all licenses and must be 5 characters. If more than 5 characters are provided
147          * then only the first 5 are taken. If fewer than 5 characters are provided the
148          * category is padded with spaces.
149          * 
150          * @param licenseFamilyCategory the family category for the license.
151          * @return this builder for chaining.
152          */
153         Builder setFamily(String licenseFamilyCategory);
154 
155         /**
156          * Sets the name of the license. If the name is not set then the name of the
157          * license family is used.
158          * 
159          * @param name the name for the license
160          * @return this builder for chaining.
161          */
162         public Builder setName(String name);
163 
164         /**
165          * Sets the set of license families to use duirng build.
166          * 
167          * @param licenseFamilies the license families to use
168          * @return this builder.
169          */
170         public Builder setLicenseFamilies(SortedSet<ILicenseFamily> licenseFamilies);
171 
172         /**
173          * Builds the license.
174          *
175          * @return A new License implementation.
176          */
177         @Override
178         public ILicense build();
179     }
180 }