1 9 package org.jboss.portal.core.util; 10 11 import java.util.Map ; 12 13 16 public class Parameters 17 { 18 19 private Map parameters; 20 21 24 public Parameters(Map parameterMap) 25 { 26 this.parameters = parameterMap; 27 } 28 29 public String getParameter(String name) 30 { 31 if (name == null) 32 { 33 return null; 34 } 35 String [] value = (String []) parameters.get(name); 36 return value == null ? null : value[0]; 37 } 38 39 public String get(String key, String def) 40 { 41 String value = getParameter(key); 42 if (value != null) 43 { 44 return value; 45 } 46 else 47 { 48 return def; 49 } 50 } 51 52 62 public boolean getBoolean(String key, boolean def) 63 { 64 65 String value = getParameter(key); 66 if (value != null) 67 { 68 if ("true".equalsIgnoreCase(value.trim())) 69 { 70 return true; 71 } 72 else if ("false".equalsIgnoreCase(value.trim())) 73 { 74 return false; 75 } 76 else 77 { 78 return def; 79 } 80 } 81 else 82 { 83 return def; 84 } 85 } 86 87 public Boolean getBooleanObject(String key, boolean def) 88 { 89 Boolean bool = getBooleanObject(key); 90 return (bool != null) ? bool : new Boolean (def); 91 } 92 93 public Boolean getBooleanObject(String key) 94 { 95 96 String value = getParameter(key); 97 if (value != null) 98 { 99 if ("true".equalsIgnoreCase(value.trim())) 100 { 101 return Boolean.TRUE; 102 } 103 else if ("false".equalsIgnoreCase(value.trim())) 104 { 105 return Boolean.FALSE; 106 } 107 else 108 { 109 return null; 110 } 111 } 112 else 113 { 114 return null; 115 } 116 } 117 118 public byte[] getByteArray(String key, byte[] def) 119 { 120 String value = getParameter(key); 121 byte[] returnValue = def; 122 if (value != null) 123 { 124 returnValue = value.getBytes(); 125 if (returnValue == null) 126 { 127 returnValue = def; 128 } 129 return returnValue; 130 } 131 else 132 { 133 return def; 134 } 135 } 136 137 public double getDouble(String key, double def) 138 { 139 String value = getParameter(key); 140 double returnValue = def; 141 if (value != null) 142 { 143 try 144 { 145 returnValue = Double.parseDouble(value); 146 return returnValue; 147 } 148 catch (NumberFormatException e) 149 { 150 return def; 151 } 152 } 153 else 154 { 155 return def; 156 } 157 } 158 159 public Double getDoubleObject(String key, double defaultValue) 160 { 161 Double value = getDoubleObject(key); 162 return value != null ? value : new Double (defaultValue); 163 } 164 165 public Double getDoubleObject(String key) 166 { 167 try 168 { 169 String value = getParameter(key); 170 return (value != null) ? new Double (value) : null; 171 } 172 catch (NumberFormatException e) 173 { 174 return null; 175 } 176 } 177 178 public float getFloat(String key, float def) 179 { 180 String value = getParameter(key); 181 float returnValue = def; 182 if (value != null) 183 { 184 try 185 { 186 returnValue = Float.parseFloat(value); 187 return returnValue; 188 } 189 catch (NumberFormatException e) 190 { 191 return def; 192 } 193 } 194 else 195 { 196 return def; 197 } 198 } 199 200 public Float getFloatObject(String key, float defaultValue) 201 { 202 Float value = getFloatObject(key); 203 return value != null ? value : new Float (defaultValue); 204 } 205 206 public Float getFloatObject(String key) 207 { 208 try 209 { 210 String value = getParameter(key); 211 return (value != null) ? new Float (value) : null; 212 } 213 catch (NumberFormatException e) 214 { 215 return null; 216 } 217 } 218 219 public short getShort(String key, short def) 220 { 221 String value = getParameter(key); 222 short returnValue = def; 223 if (value != null) 224 { 225 try 226 { 227 returnValue = Short.parseShort(value); 228 return returnValue; 229 } 230 catch (NumberFormatException e) 231 { 232 return def; 233 } 234 } 235 else 236 { 237 return def; 238 } 239 } 240 241 public Short getShortObject(String key, short defaultValue) 242 { 243 Short value = getShortObject(key); 244 return value != null ? value : new Short (defaultValue); 245 } 246 247 public Short getShortObject(String key) 248 { 249 try 250 { 251 String value = getParameter(key); 252 return (value != null) ? new Short (value) : null; 253 } 254 catch (NumberFormatException e) 255 { 256 return null; 257 } 258 } 259 260 public int getInt(String key, int def) 261 { 262 String value = getParameter(key); 263 int returnValue = def; 264 if (value != null) 265 { 266 try 267 { 268 returnValue = Integer.parseInt(value); 269 return returnValue; 270 } 271 catch (NumberFormatException e) 272 { 273 return def; 274 } 275 } 276 else 277 { 278 return def; 279 } 280 } 281 282 public Integer getIntObject(String key, int defaultValue) 283 { 284 Integer value = getIntObject(key); 285 return value != null ? value : new Integer (defaultValue); 286 } 287 288 public Integer getIntObject(String key) 289 { 290 try 291 { 292 String value = getParameter(key); 293 return (value != null) ? new Integer (value) : null; 294 } 295 catch (NumberFormatException e) 296 { 297 return null; 298 } 299 } 300 301 public long getLong(String key, long def) 302 { 303 String value = getParameter(key); 304 long returnValue = def; 305 if (value != null) 306 { 307 try 308 { 309 returnValue = Long.parseLong(value); 310 return returnValue; 311 } 312 catch (NumberFormatException e) 313 { 314 return def; 315 } 316 } 317 else 318 { 319 return def; 320 321 } 322 } 323 324 public Long getLongObject(String key, long defaultValue) 325 { 326 Long value = getLongObject(key); 327 return value != null ? value : new Long (defaultValue); 328 } 329 330 public Long getLongObject(String key) 331 { 332 try 333 { 334 String value = getParameter(key); 335 return (value != null) ? new Long (value) : null; 336 } 337 catch (NumberFormatException e) 338 { 339 return null; 340 } 341 } 342 343 public boolean getParameterExists(String param) 344 { 345 String result = getParameter(param); 346 if ((result != null) && (result.length() != 0)) 347 { 348 return true; 349 } 350 else 351 { 352 return false; 353 } 354 } 355 } | Popular Tags |