KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > util > Properties


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 org.jboss.portal.common.util.Tools;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Set JavaDoc;
20
21 /**
22  * A set of multi valued properties produced by a response.
23  *
24  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
25  * @version $Revision: 1.1 $
26  */

27 public class Properties
28 {
29
30    private Map JavaDoc content;
31
32    public Properties()
33    {
34    }
35
36    /**
37     * @param name the property name
38     * @param value the property value
39     * @throws IllegalArgumentException if name or value is null
40     */

41    public void addProperty(String JavaDoc name, String JavaDoc value)
42    {
43       if (name == null)
44       {
45          throw new IllegalArgumentException JavaDoc("Name cannot be null");
46       }
47       if (value == null)
48       {
49          throw new IllegalArgumentException JavaDoc("Value cannot be null");
50       }
51       if (content == null)
52       {
53          content = new HashMap JavaDoc();
54       }
55       Object JavaDoc o = content.get(name);
56       if (o == null)
57       {
58          content.put(name, value);
59       }
60       else if (o instanceof String JavaDoc)
61       {
62          ArrayList JavaDoc values = new ArrayList JavaDoc();
63          values.add(o);
64          values.add(value);
65          content.put(name, values);
66       }
67       else
68       {
69          ArrayList JavaDoc values = (ArrayList JavaDoc)o;
70          values.add(value);
71       }
72    }
73
74    /**
75     * @param name the property name
76     * @param value the property value
77     * @throws IllegalArgumentException if name or value is null
78     */

79    public void setProperty(String JavaDoc name, String JavaDoc value)
80    {
81       if (name == null)
82       {
83          throw new IllegalArgumentException JavaDoc("Name cannot be null");
84       }
85       if (value == null)
86       {
87          throw new IllegalArgumentException JavaDoc("Value cannot be null");
88       }
89       if (content == null)
90       {
91          content = new HashMap JavaDoc();
92       }
93       content.put(name, value);
94    }
95
96    /**
97     * Clear the properties.
98     */

99    public void clear()
100    {
101       if (content != null)
102       {
103          content.clear();
104       }
105    }
106
107    public String JavaDoc getProperty(String JavaDoc name)
108    {
109       if (name == null)
110       {
111          throw new IllegalArgumentException JavaDoc("Name cannot be null");
112       }
113       if (content == null)
114       {
115          return null;
116       }
117       Object JavaDoc o = content.get(name);
118       if (o == null)
119       {
120          return null;
121       }
122       else if (o instanceof String JavaDoc)
123       {
124          return (String JavaDoc)o;
125       }
126       else
127       {
128          return (String JavaDoc)((ArrayList JavaDoc)o).get(0);
129       }
130    }
131
132    /**
133     * @param name the property name
134     * @return the list of properties for the specified name
135     * @throws IllegalArgumentException if name is null
136     */

137    public List JavaDoc getProperties(String JavaDoc name)
138    {
139       if (name == null)
140       {
141          throw new IllegalArgumentException JavaDoc("Name cannot be null");
142       }
143       if (content == null)
144       {
145          return Collections.EMPTY_LIST;
146       }
147       Object JavaDoc o = content.get(name);
148       if (o == null)
149       {
150          return Collections.EMPTY_LIST;
151       }
152       if (o instanceof String JavaDoc)
153       {
154          return Collections.singletonList(o);
155       }
156       else
157       {
158          return (List JavaDoc)o;
159       }
160    }
161
162    public Set JavaDoc getPropertyNames()
163    {
164       if (content == null)
165       {
166          return Collections.EMPTY_SET;
167       }
168       return content.keySet();
169    }
170 }
171
Popular Tags