|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
QueryParameterMap.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.tapestry.util;
|
|
16 |
|
|
17 |
import java.util.Arrays;
|
|
18 |
import java.util.HashMap;
|
|
19 |
import java.util.Map;
|
|
20 |
|
|
21 |
import org.apache.hivemind.util.Defense;
|
|
22 |
|
|
23 |
/**
|
|
24 |
* A wrapper around a Map that stores query parameter values. Map keys are strings. Map values can
|
|
25 |
* be simple strings or array of string (or null).
|
|
26 |
*
|
|
27 |
* @author Howard M. Lewis Ship
|
|
28 |
* @since 4.0
|
|
29 |
*/
|
|
30 |
public class QueryParameterMap |
|
31 |
{ |
|
32 |
private final Map _parameters;
|
|
33 |
|
|
34 | 159 |
public QueryParameterMap()
|
35 |
{ |
|
36 | 159 |
this(new HashMap()); |
37 |
} |
|
38 |
|
|
39 |
/**
|
|
40 |
* Constructor around an existing Map whose keys and values are expected to conform expected use
|
|
41 |
* (keys are strings, values are null, string or string array). The map passed in is retained (
|
|
42 |
* not copied).
|
|
43 |
*/
|
|
44 |
|
|
45 | 614 |
public QueryParameterMap(Map parameterMap)
|
46 |
{ |
|
47 | 614 |
Defense.notNull(parameterMap, "parameterMap");
|
48 |
|
|
49 | 614 |
_parameters = parameterMap; |
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* Replaces the parameter value for the given name wit the new value (which may be null).
|
|
54 |
*/
|
|
55 |
|
|
56 | 449 |
public void setParameterValue(String name, String value) |
57 |
{ |
|
58 | 449 |
Defense.notNull(name, "name");
|
59 |
|
|
60 | 449 |
_parameters.put(name, value); |
61 |
} |
|
62 |
|
|
63 |
/**
|
|
64 |
* Replaces the parameter value for the given name wit the new list of values (which may be
|
|
65 |
* empty or null).
|
|
66 |
*/
|
|
67 |
|
|
68 | 21 |
public void setParameterValues(String name, String[] values) |
69 |
{ |
|
70 | 21 |
Defense.notNull(name, "name");
|
71 |
|
|
72 | 21 |
_parameters.put(name, values); |
73 |
} |
|
74 |
|
|
75 |
/**
|
|
76 |
* Gets a query parameter value. If an array of values was stored, this returns the first value.
|
|
77 |
* May return null.
|
|
78 |
*/
|
|
79 |
|
|
80 | 602 |
public String getParameterValue(String name)
|
81 |
{ |
|
82 | 602 |
Defense.notNull(name, "name");
|
83 |
|
|
84 | 602 |
Object values = _parameters.get(name); |
85 |
|
|
86 | 602 |
if (values == null || values instanceof String) |
87 | 601 |
return (String) values;
|
88 |
|
|
89 | 1 |
String[] array = (String[]) values; |
90 |
|
|
91 | 1 |
return array[0];
|
92 |
} |
|
93 |
|
|
94 |
/**
|
|
95 |
* Returns the array of values for the specified parameter. If only a lone value was stored (via
|
|
96 |
* {@link #setParameterValue(String, String)}, then the value is wrapped as a string array and
|
|
97 |
* returned.
|
|
98 |
*/
|
|
99 | 1011 |
public String[] getParameterValues(String name)
|
100 |
{ |
|
101 | 1011 |
Defense.notNull(name, "name");
|
102 |
|
|
103 | 1011 |
Object values = _parameters.get(name); |
104 |
|
|
105 | 1011 |
if (values == null || values instanceof String[]) |
106 | 365 |
return (String[]) values;
|
107 |
|
|
108 | 646 |
String loneValue = (String) values; |
109 |
|
|
110 | 646 |
return new String[] |
111 |
{ loneValue }; |
|
112 |
} |
|
113 |
|
|
114 |
/**
|
|
115 |
* Returns the names of all parameters, sorted alphabetically.
|
|
116 |
*/
|
|
117 | 231 |
public String[] getParameterNames()
|
118 |
{ |
|
119 | 231 |
int count = _parameters.size();
|
120 |
|
|
121 | 231 |
String[] result = (String[]) _parameters.keySet().toArray(new String[count]);
|
122 |
|
|
123 | 231 |
Arrays.sort(result); |
124 |
|
|
125 | 231 |
return result;
|
126 |
} |
|
127 |
} |
|