KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > QueryParameterMap


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 JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
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 JavaDoc _parameters;
33
34     public QueryParameterMap()
35     {
36         this(new HashMap JavaDoc());
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     public QueryParameterMap(Map JavaDoc parameterMap)
46     {
47         Defense.notNull(parameterMap, "parameterMap");
48
49         _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     public void setParameterValue(String JavaDoc name, String JavaDoc value)
57     {
58         Defense.notNull(name, "name");
59
60         _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     public void setParameterValues(String JavaDoc name, String JavaDoc[] values)
69     {
70         Defense.notNull(name, "name");
71
72         _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     public String JavaDoc getParameterValue(String JavaDoc name)
81     {
82         Defense.notNull(name, "name");
83
84         Object JavaDoc values = _parameters.get(name);
85
86         if (values == null || values instanceof String JavaDoc)
87             return (String JavaDoc) values;
88
89         String JavaDoc[] array = (String JavaDoc[]) values;
90
91         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     public String JavaDoc[] getParameterValues(String JavaDoc name)
100     {
101         Defense.notNull(name, "name");
102
103         Object JavaDoc values = _parameters.get(name);
104
105         if (values == null || values instanceof String JavaDoc[])
106             return (String JavaDoc[]) values;
107
108         String JavaDoc loneValue = (String JavaDoc) values;
109
110         return new String JavaDoc[]
111         { loneValue };
112     }
113
114     /**
115      * Returns the names of all parameters, sorted alphabetically.
116      */

117     public String JavaDoc[] getParameterNames()
118     {
119         int count = _parameters.size();
120
121         String JavaDoc[] result = (String JavaDoc[]) _parameters.keySet().toArray(new String JavaDoc[count]);
122
123         Arrays.sort(result);
124
125         return result;
126     }
127 }
Popular Tags