1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.creadur.whisker.model;
20
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Map;
24 import java.util.Set;
25
26
27
28
29 public class License implements Comparable<License> {
30
31
32
33
34
35 private final boolean isSourceRequired;
36
37 private final String baseText;
38
39 private final Collection<String> expectedParameters;
40
41 private final String id;
42
43 private final String url;
44
45 private final String name;
46
47
48
49
50
51
52
53
54
55
56
57
58 public License(final boolean isSourceRequired, final String baseText,
59 final Collection<String> expectedParameters, final String id,
60 final String url, final String name) {
61 super();
62 this.isSourceRequired = isSourceRequired;
63 this.baseText = baseText;
64 this.expectedParameters = Collections
65 .unmodifiableCollection(expectedParameters);
66 this.id = id;
67 this.url = url;
68 this.name = name;
69 }
70
71
72
73
74
75
76
77 public boolean isSourceRequired() {
78 return this.isSourceRequired;
79 }
80
81
82
83
84
85
86
87 public String getText() throws LicenseTemplateException {
88 return getText(null);
89 }
90
91
92
93
94
95
96
97
98
99 public String getText(final Map<String, String> parameters)
100 throws LicenseTemplateException {
101 return substituteInto(validate(parameters), this.baseText);
102 }
103
104
105
106
107
108
109 public Collection<String> getExpectedParameters() {
110 return this.expectedParameters;
111 }
112
113
114
115
116
117
118
119
120
121 @SuppressWarnings("unchecked")
122 private Map<String, String> validate(final Map<String, String> parameters)
123 throws LicenseTemplateException {
124 if (parameters == null) {
125 return validate(Collections.EMPTY_MAP);
126 }
127
128 if (this.expectedParameters.isEmpty() && parameters != null
129 && !parameters.isEmpty()) {
130 throw LicenseTemplateException.notLicenseTemplate(parameters,
131 getName());
132 }
133
134 if (!parametersMatch(parameters, this.expectedParameters)) {
135 throw LicenseTemplateException.parameterMismatch(
136 this.expectedParameters, parameters.keySet(), getName());
137 }
138
139 return parameters;
140 }
141
142
143
144
145
146
147
148
149 private boolean parametersMatch(final Map<String, String> parameters,
150 final Collection<String> expectedParameters) {
151 final Set<String> keySet = parameters.keySet();
152 return keySet.containsAll(expectedParameters) && expectedParameters
153 .containsAll(keySet);
154 }
155
156
157
158
159
160
161
162 private String variable(final String parameterName) {
163 return "${" + parameterName + "}";
164 }
165
166
167
168
169
170
171
172
173
174 private String substituteInto(final Map<String, String> parameters,
175 final String text) {
176 String result = text;
177 for (final Map.Entry<String, String> entry : parameters.entrySet()) {
178 result = result.replace(variable(entry.getKey()), entry.getValue());
179 }
180 return result;
181 }
182
183
184
185
186
187
188 public License storeIn(final Map<String, License> map) {
189 map.put(getId(), this);
190 return this;
191 }
192
193
194
195
196
197 public String getId() {
198 return this.id;
199 }
200
201
202
203
204
205 public String getURL() {
206 return this.url;
207 }
208
209
210
211
212
213
214 public String getName() {
215 return this.name;
216 }
217
218
219
220
221 @Override
222 public int hashCode() {
223 return getId().hashCode();
224 }
225
226
227
228
229 @Override
230 public boolean equals(final Object obj) {
231 if (this == obj) {
232 return true;
233 }
234 if (obj == null) {
235 return false;
236 }
237 if (getClass() != obj.getClass()) {
238 return false;
239 }
240 final License other = (License) obj;
241 return getId().equals(other);
242 }
243
244
245
246
247 public int compareTo(final License other) {
248 final int nameDifference = getName().compareTo(other.getName());
249 return nameDifference == 0 ? getId().compareTo(other.getId())
250 : nameDifference;
251 }
252
253 @Override
254 public String toString() {
255 return "License [id=" + this.id + ", name=" + this.name + "]";
256 }
257
258 }