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 that)) {
95 return false;
96 }
97 return license1.compareTo(that) == 0;
98 }
99
100 /**
101 * Gets a builder for licenses.
102 *
103 * @return An ILicense.Builder instance.
104 */
105 static ILicense.Builder builder() {
106 return new SimpleLicense.Builder();
107 }
108
109 /**
110 * A builder for ILicense instances.
111 */
112 interface Builder extends IHeaderMatcher.Builder {
113
114 /**
115 * Sets the matcher from a builder.
116 *
117 * @param matcher the builder for the matcher for the license.
118 * @return this builder for chaining.
119 */
120 Builder setMatcher(IHeaderMatcher.Builder matcher);
121
122 /**
123 * Sets the matcher.
124 *
125 * @param matcher the matcher for the license.
126 * @return this builder for chaining.
127 */
128 Builder setMatcher(IHeaderMatcher matcher);
129
130 /**
131 * Sets the notes for the license. If called multiple times the notes are
132 * concatenated to create a single note.
133 *
134 * @param notes the notes for the license.
135 * @return this builder for chaining.
136 */
137 Builder setNote(String notes);
138
139 /**
140 * Sets the ID of the license. If the ID is not set then the ID of the license
141 * family is used.
142 *
143 * @param id the ID for the license
144 * @return this builder for chaining.
145 */
146 Builder setId(String id);
147
148 /**
149 * Set the family category for this license. The category must be unique across
150 * all licenses and must be 5 characters. If more than 5 characters are provided
151 * then only the first 5 are taken. If fewer than 5 characters are provided the
152 * category is padded with spaces.
153 *
154 * @param licenseFamilyCategory the family category for the license.
155 * @return this builder for chaining.
156 */
157 Builder setFamily(String licenseFamilyCategory);
158
159 /**
160 * Sets the name of the license. If the name is not set then the name of the
161 * license family is used.
162 *
163 * @param name the name for the license
164 * @return this builder for chaining.
165 */
166 Builder setName(String name);
167
168 /**
169 * Sets the set of license families to use during build.
170 *
171 * @param licenseFamilies the license families to use
172 * @return this builder.
173 */
174 Builder setLicenseFamilies(SortedSet<ILicenseFamily> licenseFamilies);
175
176 /**
177 * Builds the license.
178 *
179 * @return A new License implementation.
180 */
181 @Override
182 ILicense build();
183 }
184 }