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