001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.util;
016
017 import org.apache.hivemind.util.Defense;
018
019 import java.util.Arrays;
020 import java.util.HashMap;
021 import java.util.Map;
022 import java.util.TreeMap;
023
024 /**
025 * A wrapper around a Map that stores query parameter values. Map keys are
026 * strings. Map values can be simple strings or array of string (or null).
027 *
028 * @author Howard M. Lewis Ship
029 * @since 4.0
030 */
031 public class QueryParameterMap
032 {
033 private final Map _parameters;
034
035 public QueryParameterMap()
036 {
037 this(new HashMap());
038 }
039
040 /**
041 * Constructor around an existing Map whose keys and values are expected to
042 * conform expected use (keys are strings, values are null, string or string
043 * array). The map passed in is retained ( not copied).
044 */
045
046 public QueryParameterMap(Map parameterMap)
047 {
048 Defense.notNull(parameterMap, "parameterMap");
049
050 _parameters = parameterMap;
051 }
052
053 /**
054 * Replaces the parameter value for the given name wit the new value (which
055 * may be null).
056 */
057
058 public void setParameterValue(String name, String value)
059 {
060 Defense.notNull(name, "name");
061
062 _parameters.put(name, value);
063 }
064
065 /**
066 * Replaces the parameter value for the given name wit the new list of
067 * values (which may be empty or null).
068 */
069
070 public void setParameterValues(String name, String[] values)
071 {
072 Defense.notNull(name, "name");
073
074 _parameters.put(name, values);
075 }
076
077 /**
078 * Gets a query parameter value. If an array of values was stored, this
079 * returns the first value. May return null.
080 */
081
082 public String getParameterValue(String name)
083 {
084 Defense.notNull(name, "name");
085
086 Object values = _parameters.get(name);
087
088 if (values == null || values instanceof String) return (String) values;
089
090 String[] array = (String[]) values;
091
092 return array[0];
093 }
094
095 /**
096 * Returns the array of values for the specified parameter. If only a lone
097 * value was stored (via {@link #setParameterValue(String, String)}, then
098 * the value is wrapped as a string array and returned.
099 */
100 public String[] getParameterValues(String name)
101 {
102 Defense.notNull(name, "name");
103
104 Object values = _parameters.get(name);
105
106 if (values == null || values instanceof String[])
107 return (String[]) values;
108
109 String loneValue = (String) values;
110
111 return new String[] { loneValue };
112 }
113
114 /**
115 * Returns the names of all parameters, sorted alphabetically.
116 */
117 public String[] getParameterNames()
118 {
119 int count = _parameters.size();
120
121 String[] result = (String[]) _parameters.keySet().toArray(new String[count]);
122
123 if (!TreeMap.class.isInstance(_parameters))
124 Arrays.sort(result);
125
126 return result;
127 }
128 }