|
|||||||||||||||||||
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 | 138 | public QueryParameterMap() |
35 | { | |
36 | 138 | 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 | 555 | public QueryParameterMap(Map parameterMap) |
46 | { | |
47 | 555 | Defense.notNull(parameterMap, "parameterMap"); |
48 | ||
49 | 555 | _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 | 353 | public void setParameterValue(String name, String value) |
57 | { | |
58 | 353 | Defense.notNull(name, "name"); |
59 | ||
60 | 353 | _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 | 26 | public void setParameterValues(String name, String[] values) |
69 | { | |
70 | 26 | Defense.notNull(name, "name"); |
71 | ||
72 | 26 | _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 | 501 | public String getParameterValue(String name) |
81 | { | |
82 | 501 | Defense.notNull(name, "name"); |
83 | ||
84 | 501 | Object values = _parameters.get(name); |
85 | ||
86 | 501 | if (values == null || values instanceof String) |
87 | 500 | 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 | 852 | public String[] getParameterValues(String name) |
100 | { | |
101 | 852 | Defense.notNull(name, "name"); |
102 | ||
103 | 852 | Object values = _parameters.get(name); |
104 | ||
105 | 852 | if (values == null || values instanceof String[]) |
106 | 276 | return (String[]) values; |
107 | ||
108 | 576 | String loneValue = (String) values; |
109 | ||
110 | 576 | return new String[] |
111 | { loneValue }; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Returns the names of all parameters, sorted alphabetically. | |
116 | */ | |
117 | 212 | public String[] getParameterNames() |
118 | { | |
119 | 212 | int count = _parameters.size(); |
120 | ||
121 | 212 | String[] result = (String[]) _parameters.keySet().toArray(new String[count]); |
122 | ||
123 | 212 | Arrays.sort(result); |
124 | ||
125 | 212 | return result; |
126 | } | |
127 | } |
|