KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > util > Parameters


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.util;
10
11 import java.util.Map JavaDoc;
12
13 /**
14  * @author <a HREF="theute@jboss.org">Thomas Heute </a> $Revision: 1.2 $
15  */

16 public class Parameters
17 {
18
19    private Map JavaDoc parameters;
20
21    /**
22     * @param parameterMap
23     */

24    public Parameters(Map JavaDoc parameterMap)
25    {
26       this.parameters = parameterMap;
27    }
28
29    public String JavaDoc getParameter(String JavaDoc name)
30    {
31       if (name == null)
32       {
33          return null;
34       }
35       String JavaDoc[] value = (String JavaDoc[]) parameters.get(name);
36       return value == null ? null : value[0];
37    }
38
39    public String JavaDoc get(String JavaDoc key, String JavaDoc def)
40    {
41       String JavaDoc value = getParameter(key);
42       if (value != null)
43       {
44          return value;
45       }
46       else
47       {
48          return def;
49       }
50    }
51
52    /**
53     * Returns the value as a boolean
54     *
55     * @param key
56     * Key of the parameter
57     * @param def
58     * Default value if value is different from true or false
59     * @return boolean value for the string "true" or "false" (not sensitive to
60     * uppercase/lowercase, and leading/trailing spaces).
61     */

62    public boolean getBoolean(String JavaDoc key, boolean def)
63    {
64
65       String JavaDoc 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 JavaDoc getBooleanObject(String JavaDoc key, boolean def)
88    {
89       Boolean JavaDoc bool = getBooleanObject(key);
90       return (bool != null) ? bool : new Boolean JavaDoc(def);
91    }
92
93    public Boolean JavaDoc getBooleanObject(String JavaDoc key)
94    {
95
96       String JavaDoc 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 JavaDoc key, byte[] def)
119    {
120       String JavaDoc 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 JavaDoc key, double def)
138    {
139       String JavaDoc 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 JavaDoc e)
149          {
150             return def;
151          }
152       }
153       else
154       {
155          return def;
156       }
157    }
158
159    public Double JavaDoc getDoubleObject(String JavaDoc key, double defaultValue)
160    {
161       Double JavaDoc value = getDoubleObject(key);
162       return value != null ? value : new Double JavaDoc(defaultValue);
163    }
164
165    public Double JavaDoc getDoubleObject(String JavaDoc key)
166    {
167       try
168       {
169          String JavaDoc value = getParameter(key);
170          return (value != null) ? new Double JavaDoc(value) : null;
171       }
172       catch (NumberFormatException JavaDoc e)
173       {
174          return null;
175       }
176    }
177
178    public float getFloat(String JavaDoc key, float def)
179    {
180       String JavaDoc 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 JavaDoc e)
190          {
191             return def;
192          }
193       }
194       else
195       {
196          return def;
197       }
198    }
199
200    public Float JavaDoc getFloatObject(String JavaDoc key, float defaultValue)
201    {
202       Float JavaDoc value = getFloatObject(key);
203       return value != null ? value : new Float JavaDoc(defaultValue);
204    }
205
206    public Float JavaDoc getFloatObject(String JavaDoc key)
207    {
208       try
209       {
210          String JavaDoc value = getParameter(key);
211          return (value != null) ? new Float JavaDoc(value) : null;
212       }
213       catch (NumberFormatException JavaDoc e)
214       {
215          return null;
216       }
217    }
218
219    public short getShort(String JavaDoc key, short def)
220    {
221       String JavaDoc 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 JavaDoc e)
231          {
232             return def;
233          }
234       }
235       else
236       {
237          return def;
238       }
239    }
240
241    public Short JavaDoc getShortObject(String JavaDoc key, short defaultValue)
242    {
243       Short JavaDoc value = getShortObject(key);
244       return value != null ? value : new Short JavaDoc(defaultValue);
245    }
246
247    public Short JavaDoc getShortObject(String JavaDoc key)
248    {
249       try
250       {
251          String JavaDoc value = getParameter(key);
252          return (value != null) ? new Short JavaDoc(value) : null;
253       }
254       catch (NumberFormatException JavaDoc e)
255       {
256          return null;
257       }
258    }
259
260    public int getInt(String JavaDoc key, int def)
261    {
262       String JavaDoc 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 JavaDoc e)
272          {
273             return def;
274          }
275       }
276       else
277       {
278          return def;
279       }
280    }
281
282    public Integer JavaDoc getIntObject(String JavaDoc key, int defaultValue)
283    {
284       Integer JavaDoc value = getIntObject(key);
285       return value != null ? value : new Integer JavaDoc(defaultValue);
286    }
287
288    public Integer JavaDoc getIntObject(String JavaDoc key)
289    {
290       try
291       {
292          String JavaDoc value = getParameter(key);
293          return (value != null) ? new Integer JavaDoc(value) : null;
294       }
295       catch (NumberFormatException JavaDoc e)
296       {
297          return null;
298       }
299    }
300
301    public long getLong(String JavaDoc key, long def)
302    {
303       String JavaDoc 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 JavaDoc e)
313          {
314             return def;
315          }
316       }
317       else
318       {
319          return def;
320
321       }
322    }
323
324    public Long JavaDoc getLongObject(String JavaDoc key, long defaultValue)
325    {
326       Long JavaDoc value = getLongObject(key);
327       return value != null ? value : new Long JavaDoc(defaultValue);
328    }
329
330    public Long JavaDoc getLongObject(String JavaDoc key)
331    {
332       try
333       {
334          String JavaDoc value = getParameter(key);
335          return (value != null) ? new Long JavaDoc(value) : null;
336       }
337       catch (NumberFormatException JavaDoc e)
338       {
339          return null;
340       }
341    }
342
343    public boolean getParameterExists(String JavaDoc param)
344    {
345       String JavaDoc 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