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 org.apache.commons.lang3.StringUtils;
22 import org.apache.rat.ConfigurationException;
23
24 /**
25 * The definition of the license family.
26 */
27 public interface ILicenseFamily extends Comparable<ILicenseFamily> {
28
29 /** The license family for unknown licenses */
30 ILicenseFamily UNKNOWN = new Builder().setLicenseFamilyName("Unknown license").setLicenseFamilyCategory("?????").build();
31
32 /**
33 * Gets the family name.
34 * @return the license family name.
35 */
36 String getFamilyName();
37
38 /**
39 * Gets the family category.
40 * @return the license family category.
41 */
42 String getFamilyCategory();
43
44 /**
45 * Gets the Builder for license families.
46 * @return A builder for an ILicenseFamily.
47 */
48 static ILicenseFamily.Builder builder() {
49 return new Builder();
50 }
51
52 /**
53 * Convert a potential category string into a category string of exactly 5
54 * characters either by truncating the string or appending spaces as necessary.
55 *
56 * @param cat the string to convert.
57 * @return a string of exactly 5 characters.
58 */
59 static String makeCategory(String cat) {
60 return cat == null ? " " : cat.concat(" ").substring(0, Builder.CATEGORY_LENGTH);
61 }
62
63 @Override
64 default int compareTo(ILicenseFamily other) {
65 return getFamilyCategory().compareTo(other.getFamilyCategory());
66 }
67
68 /**
69 * The definition of an ILicenseFamily builder.
70 */
71 class Builder {
72 /** The maximum length of the category */
73 private static final int CATEGORY_LENGTH = 5;
74 /** The category for the family */
75 private String licenseFamilyCategory;
76 /** The name of the family */
77 private String licenseFamilyName;
78
79 /**
80 * Sets the license family category. Will trim or extend the string with spaces
81 * to ensure that it is exactly 5 characters.
82 *
83 * @param licenseFamilyCategory the category string
84 * @return this builder for chaining.
85 */
86 public Builder setLicenseFamilyCategory(final String licenseFamilyCategory) {
87 this.licenseFamilyCategory = licenseFamilyCategory;
88 return this;
89 }
90
91 /**
92 * Sets the license family name.
93 *
94 * @param licenseFamilyName the name string
95 * @return this builder for chaining.
96 */
97 public Builder setLicenseFamilyName(final String licenseFamilyName) {
98 this.licenseFamilyName = licenseFamilyName;
99 return this;
100 }
101
102 /**
103 * Gets the category that this builder is building.
104 *
105 * @return the category that this builder is building.
106 */
107 public String getCategory() {
108 return licenseFamilyCategory;
109 }
110
111 /**
112 * Builds the defined license family.
113 * @return a new ILicenseFamily instance.
114 */
115 public ILicenseFamily build() {
116 if (StringUtils.isBlank(licenseFamilyCategory)) {
117 throw new ConfigurationException("LicenseFamily Category must be specified");
118 }
119 if (StringUtils.isBlank(licenseFamilyName)) {
120 throw new ConfigurationException("LicenseFamily Name must be specified");
121 }
122 return new ILicenseFamily() {
123 private final String cat = ILicenseFamily.makeCategory(licenseFamilyCategory);
124 private final String name = licenseFamilyName;
125 @Override
126 public String toString() {
127 return String.format("%s %s", getFamilyCategory(), getFamilyName());
128 }
129
130 @Override
131 public String getFamilyName() {
132 return name;
133 }
134
135 @Override
136 public String getFamilyCategory() {
137 return cat;
138 }
139
140 @Override
141 public boolean equals(final Object other) {
142 if (other == this) {
143 return true;
144 }
145 if (other instanceof ILicenseFamily) {
146 return this.getFamilyCategory().equals(((ILicenseFamily) other).getFamilyCategory());
147 }
148 return false;
149 }
150
151 @Override
152 public int hashCode() {
153 return getFamilyCategory().hashCode();
154 }
155 };
156 }
157 }
158 }