1 9 package org.jboss.portal.server.util; 10 11 import java.io.Serializable ; 12 import java.util.Collections ; 13 import java.util.Enumeration ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 19 25 public class Parameters implements Serializable 26 { 27 28 private Map parameterMap; 29 30 33 public Parameters() 34 { 35 parameterMap = new HashMap (); 36 } 37 38 42 public Parameters(Parameters parameters) 43 { 44 if (parameters == null) 45 { 46 throw new IllegalArgumentException ("parameters must not be null"); 47 } 48 parameterMap = new HashMap (parameters.parameterMap); } 50 51 55 public Parameters(Map parameterMap) 56 { 57 validateMap(parameterMap); 58 this.parameterMap = new HashMap (parameterMap); 59 } 60 61 public String getParameter(String name) throws IllegalArgumentException 62 { 63 if (name == null) 64 { 65 throw new IllegalArgumentException ("name cannot be null"); 66 } 67 String [] value = (String [])parameterMap.get(name); 68 return value == null ? null : value[0]; 69 } 70 71 public Enumeration getParameterNames() 72 { 73 return Collections.enumeration(parameterMap.keySet()); 74 } 75 76 public String [] getParameterValues(String name) throws IllegalArgumentException 77 { 78 if (name == null) 79 { 80 throw new IllegalArgumentException ("name cannot be null"); 81 } 82 return (String [])parameterMap.get(name); 83 } 84 85 public Map getParameterMap() 86 { 87 return parameterMap; } 89 90 public void clear() 91 { 92 parameterMap.clear(); 93 } 94 95 98 public void setParameterMap(Map parameterMap) 99 { 100 validateMap(parameterMap); 101 this.parameterMap.clear(); 102 this.parameterMap.putAll(parameterMap); 103 } 104 105 108 public void setParameters(Parameters parameters) 109 { 110 if (parameters == null) 111 { 112 throw new IllegalArgumentException ("parameters must not be null"); 113 } 114 setParameterMap(parameters.getParameterMap()); 115 } 116 117 121 public void setParameter(String name, String value) 122 { 123 if (name == null) 124 { 125 throw new IllegalArgumentException ("name must not be null"); 126 } 127 if (value == null) 128 { 129 throw new IllegalArgumentException ("value must not be null"); 130 } 131 else 132 { 133 parameterMap.put(name, new String []{value}); 134 } 135 } 136 137 140 public void setParameterValues(String name, String [] values) 141 { 142 if (name == null) 143 { 144 throw new IllegalArgumentException ("name must not be null"); 145 } 146 validateStringArray(values); 147 parameterMap.put(name, values); 150 } 151 152 155 public void removeParameter(String name) 156 { 157 if (name == null) 158 { 159 throw new IllegalArgumentException ("name must not be null"); 160 } 161 parameterMap.remove(name); 162 } 163 164 public String toString() 165 { 166 StringBuffer buffer = new StringBuffer ("Parameters["); 167 for (Iterator i = parameterMap.entrySet().iterator();i.hasNext();) 168 { 169 Map.Entry entry = (Map.Entry )i.next(); 170 String name = (String )entry.getKey(); 171 String [] values = (String [])entry.getValue(); 172 buffer.append(name); 173 for (int j = 0;j < values.length;j++) 174 { 175 buffer.append(j > 0 ? ',' : '=').append(values[j]); 176 } 177 } 178 buffer.append(']'); 179 return buffer.toString(); 180 } 181 182 private void validateMap(Map map) 183 { 184 if (map == null) 185 { 186 throw new IllegalArgumentException ("parameter map must not be null"); 187 } 188 for (Iterator i = map.entrySet().iterator();i.hasNext();) 189 { 190 Map.Entry entry = (Map.Entry )i.next(); 191 Object key = entry.getKey(); 192 if (!(key instanceof String )) 193 { 194 throw new IllegalArgumentException ("parameter map keys must all be string"); 195 } 196 Object value = entry.getValue(); 197 if (!(value instanceof String [])) 198 { 199 throw new IllegalArgumentException ("parameter map values must all be string arrays"); 200 } 201 validateStringArray((String [])value); 202 } 203 } 204 205 private void validateStringArray(String [] values) 206 { 207 if (values == null) 208 { 209 throw new IllegalArgumentException ("Array must not be null"); 210 } 211 if (values.length == 0) 212 { 213 throw new IllegalArgumentException ("Array must not be zero length"); 214 } 215 for (int i = values.length - 1;i >= 0;i--) 216 { 217 if (values[i] == null) 218 { 219 throw new IllegalArgumentException ("Array must not contain null values"); 220 } 221 } 222 } 223 } 224 | Popular Tags |