KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > 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.server.util;
10
11 import java.io.Serializable JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18
19 /**
20  * A set of parameters.
21  *
22  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
23  * @version $Revision: 1.2 $
24  */

25 public class Parameters implements Serializable JavaDoc
26 {
27
28    private Map JavaDoc parameterMap;
29
30    /**
31     * Creates an empty parameter set.
32     */

33    public Parameters()
34    {
35       parameterMap = new HashMap JavaDoc();
36    }
37
38    /**
39     * Copy constructor.
40     * @exception IllegalArgumentException if the parameters is null
41     */

42    public Parameters(Parameters parameters)
43    {
44       if (parameters == null)
45       {
46          throw new IllegalArgumentException JavaDoc("parameters must not be null");
47       }
48       parameterMap = new HashMap JavaDoc(parameters.parameterMap); // The danger here is that one String[] is modified, but will someone really do that ?
49
}
50
51    /**
52     * Copy the parameter map to initialize the object state.
53     * @exception IllegalArgumentException if the parameter map is null
54     */

55    public Parameters(Map JavaDoc parameterMap)
56    {
57       validateMap(parameterMap);
58       this.parameterMap = new HashMap JavaDoc(parameterMap);
59    }
60
61    public String JavaDoc getParameter(String JavaDoc name) throws IllegalArgumentException JavaDoc
62    {
63       if (name == null)
64       {
65          throw new IllegalArgumentException JavaDoc("name cannot be null");
66       }
67       String JavaDoc[] value = (String JavaDoc[])parameterMap.get(name);
68       return value == null ? null : value[0];
69    }
70
71    public Enumeration JavaDoc getParameterNames()
72    {
73       return Collections.enumeration(parameterMap.keySet());
74    }
75
76    public String JavaDoc[] getParameterValues(String JavaDoc name) throws IllegalArgumentException JavaDoc
77    {
78       if (name == null)
79       {
80          throw new IllegalArgumentException JavaDoc("name cannot be null");
81       }
82       return (String JavaDoc[])parameterMap.get(name);
83    }
84
85    public Map JavaDoc getParameterMap()
86    {
87       return parameterMap; // Should we make it immutable ?
88
}
89
90    public void clear()
91    {
92       parameterMap.clear();
93    }
94
95    /**
96     * @exception IllegalArgumentException if the map is null
97     */

98    public void setParameterMap(Map JavaDoc parameterMap)
99    {
100       validateMap(parameterMap);
101       this.parameterMap.clear();
102       this.parameterMap.putAll(parameterMap);
103    }
104    
105    /**
106     * @exception IllegalArgumentException if the parameters is null
107     */

108    public void setParameters(Parameters parameters)
109    {
110       if (parameters == null)
111       {
112          throw new IllegalArgumentException JavaDoc("parameters must not be null");
113       }
114       setParameterMap(parameters.getParameterMap());
115    }
116
117    /**
118     * @exception IllegalArgumentException if the name is null
119     * @exception IllegalArgumentException if the value is null
120     */

121    public void setParameter(String JavaDoc name, String JavaDoc value)
122    {
123       if (name == null)
124       {
125          throw new IllegalArgumentException JavaDoc("name must not be null");
126       }
127       if (value == null)
128       {
129          throw new IllegalArgumentException JavaDoc("value must not be null");
130       }
131       else
132       {
133          parameterMap.put(name, new String JavaDoc[]{value});
134       }
135    }
136
137    /**
138     * @exception IllegalArgumentException if the name is null
139     */

140    public void setParameterValues(String JavaDoc name, String JavaDoc[] values)
141    {
142       if (name == null)
143       {
144          throw new IllegalArgumentException JavaDoc("name must not be null");
145       }
146       validateStringArray(values);
147       // julien : we don't copy the array, the client is not supposed to modify the array
148
// the test before are just defensive against bad arguments
149
parameterMap.put(name, values);
150    }
151
152    /**
153     * @exception IllegalArgumentException if the name is null
154     */

155    public void removeParameter(String JavaDoc name)
156    {
157       if (name == null)
158       {
159          throw new IllegalArgumentException JavaDoc("name must not be null");
160       }
161       parameterMap.remove(name);
162    }
163
164    public String JavaDoc toString()
165    {
166       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("Parameters[");
167       for (Iterator JavaDoc i = parameterMap.entrySet().iterator();i.hasNext();)
168       {
169          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
170          String JavaDoc name = (String JavaDoc)entry.getKey();
171          String JavaDoc[] values = (String JavaDoc[])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 JavaDoc map)
183    {
184       if (map == null)
185       {
186          throw new IllegalArgumentException JavaDoc("parameter map must not be null");
187       }
188       for (Iterator JavaDoc i = map.entrySet().iterator();i.hasNext();)
189       {
190          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
191          Object JavaDoc key = entry.getKey();
192          if (!(key instanceof String JavaDoc))
193          {
194             throw new IllegalArgumentException JavaDoc("parameter map keys must all be string");
195          }
196          Object JavaDoc value = entry.getValue();
197          if (!(value instanceof String JavaDoc[]))
198          {
199             throw new IllegalArgumentException JavaDoc("parameter map values must all be string arrays");
200          }
201          validateStringArray((String JavaDoc[])value);
202       }
203    }
204
205    private void validateStringArray(String JavaDoc[] values)
206    {
207       if (values == null)
208       {
209          throw new IllegalArgumentException JavaDoc("Array must not be null");
210       }
211       if (values.length == 0)
212       {
213          throw new IllegalArgumentException JavaDoc("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 JavaDoc("Array must not contain null values");
220          }
221       }
222    }
223 }
224
Popular Tags