Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
QueryParameterMap |
|
| 1.7142857142857142;1.714 |
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 org.apache.hivemind.util.Defense; |
|
18 | ||
19 | import java.util.Arrays; |
|
20 | import java.util.HashMap; |
|
21 | import java.util.Map; |
|
22 | import java.util.TreeMap; |
|
23 | ||
24 | /** |
|
25 | * A wrapper around a Map that stores query parameter values. Map keys are |
|
26 | * strings. Map values can be simple strings or array of string (or null). |
|
27 | * |
|
28 | * @author Howard M. Lewis Ship |
|
29 | * @since 4.0 |
|
30 | */ |
|
31 | public class QueryParameterMap |
|
32 | { |
|
33 | private final Map _parameters; |
|
34 | ||
35 | public QueryParameterMap() |
|
36 | { |
|
37 | 11 | this(new HashMap()); |
38 | 11 | } |
39 | ||
40 | /** |
|
41 | * Constructor around an existing Map whose keys and values are expected to |
|
42 | * conform expected use (keys are strings, values are null, string or string |
|
43 | * array). The map passed in is retained ( not copied). |
|
44 | */ |
|
45 | ||
46 | public QueryParameterMap(Map parameterMap) |
|
47 | 27 | { |
48 | 27 | Defense.notNull(parameterMap, "parameterMap"); |
49 | ||
50 | 27 | _parameters = parameterMap; |
51 | 27 | } |
52 | ||
53 | /** |
|
54 | * Replaces the parameter value for the given name wit the new value (which |
|
55 | * may be null). |
|
56 | */ |
|
57 | ||
58 | public void setParameterValue(String name, String value) |
|
59 | { |
|
60 | 15 | Defense.notNull(name, "name"); |
61 | ||
62 | 15 | _parameters.put(name, value); |
63 | 15 | } |
64 | ||
65 | /** |
|
66 | * Replaces the parameter value for the given name wit the new list of |
|
67 | * values (which may be empty or null). |
|
68 | */ |
|
69 | ||
70 | public void setParameterValues(String name, String[] values) |
|
71 | { |
|
72 | 3 | Defense.notNull(name, "name"); |
73 | ||
74 | 3 | _parameters.put(name, values); |
75 | 3 | } |
76 | ||
77 | /** |
|
78 | * Gets a query parameter value. If an array of values was stored, this |
|
79 | * returns the first value. May return null. |
|
80 | */ |
|
81 | ||
82 | public String getParameterValue(String name) |
|
83 | { |
|
84 | 10 | Defense.notNull(name, "name"); |
85 | ||
86 | 10 | Object values = _parameters.get(name); |
87 | ||
88 | 10 | if (values == null || values instanceof String) return (String) values; |
89 | ||
90 | 1 | String[] array = (String[]) values; |
91 | ||
92 | 1 | return array[0]; |
93 | } |
|
94 | ||
95 | /** |
|
96 | * Returns the array of values for the specified parameter. If only a lone |
|
97 | * value was stored (via {@link #setParameterValue(String, String)}, then |
|
98 | * the value is wrapped as a string array and returned. |
|
99 | */ |
|
100 | public String[] getParameterValues(String name) |
|
101 | { |
|
102 | 29 | Defense.notNull(name, "name"); |
103 | ||
104 | 29 | Object values = _parameters.get(name); |
105 | ||
106 | 29 | if (values == null || values instanceof String[]) |
107 | 15 | return (String[]) values; |
108 | ||
109 | 14 | String loneValue = (String) values; |
110 | ||
111 | 14 | return new String[] { loneValue }; |
112 | } |
|
113 | ||
114 | /** |
|
115 | * Returns the names of all parameters, sorted alphabetically. |
|
116 | */ |
|
117 | public String[] getParameterNames() |
|
118 | { |
|
119 | 15 | int count = _parameters.size(); |
120 | ||
121 | 15 | String[] result = (String[]) _parameters.keySet().toArray(new String[count]); |
122 | ||
123 | 16 | if (!TreeMap.class.isInstance(_parameters)) |
124 | 15 | Arrays.sort(result); |
125 | ||
126 | 15 | return result; |
127 | } |
|
128 | } |