1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/* ========================================================================== *
* Copyright (C) 2004-2006, Pier Fumagalli <http://could.it/> *
* All rights reserved. *
* ========================================================================== *
* *
* Licensed under the Apache License, Version 2.0 (the "License"). You may *
* not use this file except in compliance with the License. You may obtain a *
* copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>. *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT *
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
* License for the specific language governing permissions and limitations *
* under the License. *
* *
* ========================================================================== */
package it.could.util.location;
import it.could.util.StringTools;
import it.could.util.encoding.Encodable;
import it.could.util.encoding.EncodingTools;
import java.io.UnsupportedEncodingException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* <p>The {@link Parameters Parameters} class represents a never empty and
* immutable {@link List} of {@link Parameters.Parameter Parameter} instances,
* normally created parsing a query string.</p>
*
* @author <a href="http://could.it/">Pier Fumagalli</a>
*/
public class Parameters extends AbstractList implements Encodable {
/** <p>The default delimiter for a {@link Parameters} instance.</p> */
public static final char DEFAULT_DELIMITER = '&';
/** <p>All the {@link Parameter}s in order.</p> */
private final Parameter parameters[];
/** <p>The {@link Map} view over all parameters (names are keys).</p> */
private final Map map;
/** <p>The {@link Set} of all parameter names.</p> */
final Set names;
/** <p>The character delimiting different parameters.</p> */
private final char delimiter;
/** <p>The encoded {@link String} representation of this.</p> */
private final String string;
/**
* <p>Create a new {@link Parameters Parameters} instance from
* a {@link List} of {@link Parameters.Parameter Parameter} instances
* using the {@link #DEFAULT_DELIMITER default parameter delimiter}.</p>
*
* @throws NullPointerExceptoin if the {@link List} was <b>null</b>.
* @throws IllegalArgumentException if the {@link List} was empty.
* @throws ClassCastException if any of the elements in the {@link List} was
* not a {@link Parameters.Parameter Parameter}.
*/
public Parameters(List parameters) {
this(parameters, DEFAULT_DELIMITER);
}
/**
* <p>Create a new {@link Parameters Parameters} instance from
* a {@link List} of {@link Parameters.Parameter Parameter} instances
* using the specified character as the parameters delimiter.</p>
*
* @throws NullPointerExceptoin if the {@link List} was <b>null</b>.
* @throws IllegalArgumentException if the {@link List} was empty.
* @throws ClassCastException if any of the elements in the {@link List} was
* not a {@link Parameters.Parameter Parameter}.
*/
public Parameters(List parameters, char delimiter) {
if (parameters.size() == 0) throw new IllegalArgumentException();
final Parameter array[] = new Parameter[parameters.size()];
final Map map = new HashMap();
for (int x = 0; x < array.length; x ++) {
final Parameter parameter = (Parameter) parameters.get(x);
final String key = parameter.getName();
List values = (List) map.get(key);
if (values == null) {
values = new ArrayList();
map.put(key, values);
}
values.add(parameter.getValue());
array[x] = parameter;
}
/* Make all parameter value lists unmodifiable */
for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
final Map.Entry entry = (Map.Entry) iter.next();
final List list = (List) entry.getValue();
entry.setValue(Collections.unmodifiableList(list));
}
/* Store the current values */
this.delimiter = delimiter;
this.map = Collections.unmodifiableMap(map);
this.names = Collections.unmodifiableSet(map.keySet());
this.parameters = array;
this.string = EncodingTools.toString(this);
}
/* ====================================================================== */
/* STATIC CONSTRUCTION METHODS */
/* ====================================================================== */
/**
* <p>Utility method to create a new {@link Parameters} instance from a
* {@link List} of {@link Parameters.Parameter Parameter} instances.</p>
*
* @return a <b>non-null</b> and not empty {@link Parameters} instance or
* <b>null</b> if the specified {@link List} was <b>null</b>, empty
* or did not contain any {@link Parameters.Parameter Parameter}.
* @throws ClassCastException if any of the elements in the {@link List} was
* not a {@link Parameters.Parameter Parameter}.
*/
public static Parameters create(List parameters) {
return create(parameters, DEFAULT_DELIMITER);
}
/**
* <p>Utility method to create a new {@link Parameters} instance from a
* {@link List} of {@link Parameters.Parameter Parameter} instances.</p>
*
* @return a <b>non-null</b> and not empty {@link Parameters} instance or
* <b>null</b> if the specified {@link List} was <b>null</b>, empty
* or did not contain any {@link Parameters.Parameter Parameter}.
* @throws ClassCastException if any of the elements in the {@link List} was
* not a {@link Parameters.Parameter Parameter}.
*/
public static Parameters create(List parameters, char delimiter) {
if (parameters == null) return null;
final List dedupes = new ArrayList();
for (Iterator iter = parameters.iterator(); iter.hasNext(); ) {
Object next = iter.next();
if (dedupes.contains(next)) continue;
dedupes.add(next);
}
if (dedupes.size() == 0) return null;
return new Parameters(dedupes, delimiter);
}
/**
* <p>Parse the specified parameters {@link String} into a
* {@link Parameters} instance using the {@link #DEFAULT_DELIMITER default
* parameter delimiter}.</p>
*
* @return a <b>non-null</b> and not empty {@link Parameters} instance or
* <b>null</b> if the specified string was <b>null</b>, empty or
* did not contain any {@link Parameters.Parameter Parameter}.
*/
public static Parameters parse(String parameters) {
try {
return parse(parameters, DEFAULT_DELIMITER, DEFAULT_ENCODING);
} catch (UnsupportedEncodingException exception) {
final String message = "Unsupported encoding " + DEFAULT_ENCODING;
final InternalError error = new InternalError(message);
throw (InternalError) error.initCause(exception);
}
}
/**
* <p>Parse the specified parameters {@link String} into a
* {@link Parameters} instance using the specified character as the
* parameters delimiter.</p>
*
* @return a <b>non-null</b> and not empty {@link Parameters} instance or
* <b>null</b> if the specified string was <b>null</b>, empty or
* did not contain any {@link Parameters.Parameter Parameter}.
*/
public static Parameters parse(String parameters, char delimiter) {
try {
return parse(parameters, delimiter, DEFAULT_ENCODING);
} catch (UnsupportedEncodingException exception) {
final String message = "Unsupported encoding " + DEFAULT_ENCODING;
final InternalError error = new InternalError(message);
throw (InternalError) error.initCause(exception);
}
}
/**
* <p>Parse the specified parameters {@link String} into a
* {@link Parameters} instance using the {@link #DEFAULT_DELIMITER default
* parameter delimiter}.</p>
*
* @return a <b>non-null</b> and not empty {@link Parameters} instance or
* <b>null</b> if the specified string was <b>null</b>, empty or
* did not contain any {@link Parameters.Parameter Parameter}.
*/
public static Parameters parse(String parameters, String encoding)
throws UnsupportedEncodingException {
return parse(parameters, DEFAULT_DELIMITER, encoding);
}
/**
* <p>Parse the specified parameters {@link String} into a
* {@link Parameters} instance using the specified character as the
* parameters delimiter.</p>
*
* @return a <b>non-null</b> and not empty {@link Parameters} instance or
* <b>null</b> if the specified string was <b>null</b>, empty or
* did not contain any {@link Parameters.Parameter Parameter}.
*/
public static Parameters parse(String parameters, char delimiter,
String encoding)
throws UnsupportedEncodingException {
if (parameters == null) return null;
if (parameters.length() == 0) return null;
if (encoding == null) encoding = DEFAULT_ENCODING;
final String split[] = StringTools.splitAll(parameters, delimiter);
final List list = new ArrayList();
for (int x = 0; x < split.length; x ++) {
if (split[x] == null) continue;
if (split[x].length() == 0) continue;
Parameter parameter = Parameter.parse(split[x], encoding);
if (parameter != null) list.add(parameter);
}
if (list.size() == 0) return null;
return new Parameters(list, delimiter);
}
/* ====================================================================== */
/* PUBLIC EXPOSED METHODS */
/* ====================================================================== */
/**
* <p>Return the number of {@link Parameters.Parameter Parameter}s
* contained by this instance.</p>
*/
public int size() {
return this.parameters.length;
}
/**
* <p>Return the {@link Parameters.Parameter Parameter} stored by this\
* instance at the specified index.</p>
*/
public Object get(int index) {
return this.parameters[index];
}
/**
* <p>Return an immutable {@link Set} of {@link String}s containing all
* known {@link Parameters.Parameter Parameter}
* {@link Parameters.Parameter#getName() names}.</p>
*/
public Set getNames() {
return this.names;
}
/**
* <p>Return the first {@link String} value associated with the
* specified parameter name, or <b>null</b>.</p>
*/
public String getValue(String name) {
final List values = (List) this.map.get(name);
return values == null ? null : (String) values.get(0);
}
/**
* <p>Return an immutable {@link List} of all {@link String} values
* associated with the specified parameter name, or <b>null</b>.</p>
*/
public List getValues(String name) {
return (List) this.map.get(name);
}
/* ====================================================================== */
/* OBJECT METHODS */
/* ====================================================================== */
/**
* <p>Return the URL-encoded {@link String} representation of this
* {@link Parameters Parameters} instance.</p>
*/
public String toString() {
return this.string;
}
/**
* <p>Return the URL-encoded {@link String} representation of this
* {@link Parameters Parameters} instance using the specified
* character encoding.</p>
*/
public String toString(String encoding)
throws UnsupportedEncodingException {
StringBuffer buffer = new StringBuffer();
for (int x = 0; x < this.parameters.length; x ++) {
buffer.append(this.delimiter);
buffer.append(this.parameters[x].toString(encoding));
}
return buffer.substring(1);
}
/**
* <p>Return the hash code value of this
* {@link Parameters Parameters} instance.</p>
*/
public int hashCode() {
return this.string.hashCode();
}
/**
* <p>Check if the specified {@link Object} is equal to this
* {@link Parameters Parameters} instance.</p>
*
* <p>The specified {@link Object} is considered equal to this one if
* it is <b>non-null</b>, it is a {@link Parameters Parameters}
* instance, and its {@link #toString() string representation} equals
* this one's.</p>
*/
public boolean equals(Object object) {
if ((object != null) && (object instanceof Parameters)) {
return this.string.equals(((Parameters) object).string);
} else {
return false;
}
}
/* ====================================================================== */
/* PUBLIC INNER CLASSES */
/* ====================================================================== */
/**
* <p>The {@link Parameters.Parameter Parameter} class represents a single
* parameter either parsed from a query string or a path element.</p>
*
* @author <a href="http://could.it/">Pier Fumagalli</a>
*/
public static class Parameter implements Encodable {
/** <p>The name of the parameter (decoded).</p> */
private final String name;
/** <p>The value of the parameter (decoded).</p> */
private final String value;
/** <p>The encoded {@link String} representation of this.</p> */
private final String string;
/**
* <p>Create a new {@link Parameters.Parameter Parameter} given an
* encoded parameter name and value.</p>
*
* @throws NullPointerException if the name was <b>null</b>.
* @throws IllegalArgumentException if the name was an empty string.
*/
public Parameter(String name, String value) {
if (name == null) throw new NullPointerException();
if (name.length() == 0) throw new IllegalArgumentException();
this.name = name;
this.value = value;
this.string = EncodingTools.toString(this);
}
/* ================================================================== */
/* STATIC CONSTRUCTION METHODS */
/* ================================================================== */
/**
* <p>Parse the specified parameters {@link String} into a
* {@link Parameters.Parameter} instance.</p>
*
* @return a <b>non-null</b> and not empty {@link Parameters.Parameter}
* instance or <b>null</b> if the specified string was
* <b>null</b> or empty.
*/
public static Parameter parse(String parameter)
throws UnsupportedEncodingException {
try {
return parse(parameter, DEFAULT_ENCODING);
} catch (UnsupportedEncodingException exception) {
final String message = "Unsupported encoding " + DEFAULT_ENCODING;
final InternalError error = new InternalError(message);
throw (InternalError) error.initCause(exception);
}
}
/**
* <p>Parse the specified parameters {@link String} into a
* {@link Parameters.Parameter} instance.</p>
*
* @return a <b>non-null</b> and not empty {@link Parameters.Parameter}
* instance or <b>null</b> if the specified string was
* <b>null</b> or empty.
*/
public static Parameter parse(String parameter, String encoding)
throws UnsupportedEncodingException {
if (parameter == null) return null;
if (encoding == null) encoding = DEFAULT_ENCODING;
String split[] = StringTools.splitOnce(parameter, '=', false);
if (split[0] == null) return null;
return new Parameter(split[0], split[1]);
}
/* ================================================================== */
/* PUBLIC EXPOSED METHODS */
/* ================================================================== */
/**
* <p>Return the URL-decoded name of this
* {@link Parameters.Parameter Parameter} instance.</p>
*/
public String getName() {
return this.name;
}
/**
* <p>Return the URL-decoded value of this
* {@link Parameters.Parameter Parameter} instance.</p>
*/
public String getValue() {
return this.value;
}
/* ================================================================== */
/* OBJECT METHODS */
/* ================================================================== */
/**
* <p>Return the URL-encoded {@link String} representation of this
* {@link Parameters.Parameter Parameter} instance.</p>
*/
public String toString() {
return this.string;
}
/**
* <p>Return the URL-encoded {@link String} representation of this
* {@link Parameters.Parameter Parameter} instance using the specified
* character encoding.</p>
*/
public String toString(String encoding)
throws UnsupportedEncodingException {
if (this.value != null) {
return EncodingTools.urlEncode(this.name, encoding) + "=" +
EncodingTools.urlEncode(this.value, encoding);
} else {
return EncodingTools.urlEncode(this.name, encoding);
}
}
/**
* <p>Return the hash code value for this
* {@link Parameters.Parameter Parameter} instance.</p>
*/
public int hashCode() {
return this.string.hashCode();
}
/**
* <p>Check if the specified {@link Object} is equal to this
* {@link Parameters.Parameter Parameter} instance.</p>
*
* <p>The specified {@link Object} is considered equal to this one if
* it is <b>non-null</b>, it is a {@link Parameters.Parameter Parameter}
* instance, and its {@link #toString() string representation} equals
* this one's.</p>
*/
public boolean equals(Object object) {
if ((object != null) && (object instanceof Parameter)) {
return this.string.equals(((Parameter) object).string);
} else {
return false;
}
}
}
}
|