1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.maven.issues.bugzilla;
17
18 import java.io.IOException;
19 import java.io.ObjectInputStream;
20 import java.io.Serializable;
21 import java.util.List;
22
23 import org.apache.commons.lang.ObjectUtils;
24 import org.apache.maven.artifact.versioning.ArtifactVersion;
25 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
26 import org.apache.maven.artifact.versioning.Restriction;
27 import org.apache.maven.artifact.versioning.VersionRange;
28 import org.eclipse.mylyn.internal.bugzilla.core.BugzillaAttribute;
29
30 import de.smartics.maven.issues.ArtifactVersionRange;
31
32
33
34
35
36
37
38 public class BugzillaVersionRange implements Serializable, ArtifactVersionRange
39 {
40
41
42
43
44
45
46
47
48
49 private static final long serialVersionUID = 1L;
50
51
52
53
54
55
56
57 private static final String VALUE_PARAM_NAME = "value";
58
59
60
61
62
63
64
65 private static final String TYPE_PARAM_NAME = "type";
66
67
68
69
70
71
72
73 private static final String FIELD_PARAM_NAME = "field";
74
75
76
77
78
79
80 private final String versionId;
81
82
83
84
85 private final String versionSpecification;
86
87
88
89
90 private transient VersionRange range;
91
92
93
94
95
96 private final int fieldStartIndex;
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public BugzillaVersionRange(final String versionSpecification)
111 throws InvalidVersionSpecificationException
112 {
113 this(BugzillaAttribute.TARGET_MILESTONE.getKey(), versionSpecification);
114 }
115
116
117
118
119
120
121
122
123
124
125 public BugzillaVersionRange(final String versionId,
126 final String versionSpecification)
127 throws InvalidVersionSpecificationException
128 {
129 this(versionId, versionSpecification, 0);
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143 public BugzillaVersionRange(final String versionId,
144 final String versionSpecification, final int fieldStartIndex)
145 throws InvalidVersionSpecificationException
146 {
147 this.versionId = versionId;
148 this.versionSpecification = versionSpecification;
149 this.fieldStartIndex = fieldStartIndex;
150 this.range = VersionRange.createFromVersionSpec(versionSpecification);
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166 public String getVersionId()
167 {
168 return versionId;
169 }
170
171
172
173
174
175
176 public String getVersionSpecification()
177 {
178 return versionSpecification;
179 }
180
181
182
183
184
185
186
187
188 public int getFieldStartIndex()
189 {
190 return fieldStartIndex;
191 }
192
193
194
195
196
197
198
199
200 public boolean containsVersion(final ArtifactVersion version)
201 throws NullPointerException
202 {
203 return range.containsVersion(version);
204 }
205
206
207
208
209
210
211 @SuppressWarnings("unchecked")
212 public StringBuilder appendToUrl(final StringBuilder buffer)
213 {
214 final List<Restriction> restrictions =
215 (List<Restriction>) range.getRestrictions();
216 int index = fieldStartIndex;
217 for (Restriction restriction : restrictions)
218 {
219 appendAmpersand(buffer);
220 final ArtifactVersion lowerBound = restriction.getLowerBound();
221 final ArtifactVersion upperBound = restriction.getUpperBound();
222 if (ObjectUtils.equals(lowerBound, upperBound))
223 {
224 buffer.append(versionId).append('=').append(lowerBound.toString());
225 }
226 else
227 {
228 final int oldIndex = index;
229 index = appendLowerBound(buffer, index, restriction);
230 if (lowerBound != null && upperBound != null)
231 {
232 appendAmpersand(buffer);
233 }
234 index =
235 appendUpperBound(buffer, index, index == oldIndex ? 0 : 1,
236 restriction);
237 }
238 }
239 return buffer;
240 }
241
242
243
244
245
246
247 private void appendAmpersand(final StringBuilder buffer)
248 {
249 final int lastIndex = buffer.length() - 1;
250 if (lastIndex >= 0)
251 {
252 final char lastChar = buffer.charAt(lastIndex);
253 if (lastChar != '?' && lastChar != '&')
254 {
255 buffer.append('&');
256 }
257 }
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272 private int appendLowerBound(final StringBuilder buffer, final int index,
273 final Restriction restriction)
274 {
275 final ArtifactVersion lowerBound = restriction.getLowerBound();
276 if (lowerBound != null)
277 {
278 final String version = lowerBound.toString();
279 buffer.append(FIELD_PARAM_NAME).append(index).append("-0-0=")
280 .append(versionId).append('&').append(TYPE_PARAM_NAME).append(index)
281 .append("-0-0=greaterthan&").append(VALUE_PARAM_NAME).append(index)
282 .append("-0-0=").append(version);
283 if (restriction.isLowerBoundInclusive())
284 {
285 buffer.append('&').append(FIELD_PARAM_NAME).append(index)
286 .append("-0-1=").append(versionId).append('&')
287 .append(TYPE_PARAM_NAME).append(index).append("-0-1=equals&")
288 .append(VALUE_PARAM_NAME).append(index).append("-0-1=")
289 .append(version);
290 }
291 return index + 1;
292 }
293 return index;
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307
308 private int appendUpperBound(final StringBuilder buffer, final int index,
309 final int index2, final Restriction restriction)
310 {
311 final ArtifactVersion upperBound = restriction.getUpperBound();
312 if (upperBound != null)
313 {
314 final String version = upperBound.toString();
315 buffer.append(FIELD_PARAM_NAME).append(index).append('-').append(index2)
316 .append("-0=").append(versionId).append('&').append(TYPE_PARAM_NAME)
317 .append(index).append('-').append(index2).append("-0=lessthan&")
318 .append(VALUE_PARAM_NAME).append(index).append('-').append(index2)
319 .append("-0=").append(version);
320 if (restriction.isUpperBoundInclusive())
321 {
322 buffer.append('&').append(FIELD_PARAM_NAME).append(index).append('-')
323 .append(index2).append("-1=").append(versionId).append('&')
324 .append(TYPE_PARAM_NAME).append(index).append('-').append(index2)
325 .append("-1=equals&").append(VALUE_PARAM_NAME).append(index)
326 .append('-').append(index2).append("-1=").append(version);
327 }
328 return index + 1;
329 }
330 return index;
331 }
332
333
334
335
336
337
338
339
340 private void readObject(final ObjectInputStream in) throws IOException,
341 ClassNotFoundException
342 {
343 in.defaultReadObject();
344
345 try
346 {
347 this.range = VersionRange.createFromVersionSpec(versionSpecification);
348 }
349 catch (final InvalidVersionSpecificationException e)
350 {
351 final IOException ex = new IOException("Invalid version specification.");
352 ex.initCause(e);
353 throw ex;
354 }
355 }
356
357
358
359
360
361
362 @Override
363 public String toString()
364 {
365 return versionSpecification;
366 }
367 }