KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > property > PropertyGroup


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.property;
23
24 import java.util.Properties JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.jboss.util.NullArgumentException;
32
33 /**
34  * This is a helper class to access a group of properties with out having to
35  * refer to their full names.
36  *
37  * <p>This class needs more work to be fully functional. It should suffice
38  * for adding property listeners and getting/setting property values,
39  * but other activies might not work out so well.
40  *
41  * @version <tt>$Revision: 1958 $</tt>
42  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
43  * @author <a HREF="mailto:adrian@jboss.com">Adrian Brock</a>
44  */

45 public class PropertyGroup extends PropertyMap
46 {
47    /** Serial version uid */
48    private static final long serialVersionUID = -2557641199743063159L;
49    /** Base property name */
50    protected final String JavaDoc basename;
51
52    /**
53     * Construct a <tt>PropertyGroup</tt>.
54     *
55     * @param basename Base property name.
56     * @param container Property container.
57     *
58     * @throws NullArgumentException Basename is <tt>null</tt>.
59     */

60    public PropertyGroup(final String JavaDoc basename, final Properties JavaDoc container)
61    {
62       super(container);
63
64       if (basename == null)
65          throw new NullArgumentException("basename");
66
67       this.basename = basename;
68    }
69
70    /**
71     * Get the base property name for this group.
72     *
73     * @return Base property name.
74     */

75    public final String JavaDoc getBaseName()
76    {
77       return basename;
78    }
79
80    /**
81     * Make a fully qualified property name.
82     *
83     * @param suffix Property name suffix.
84     */

85    private String JavaDoc makePropertyName(final String JavaDoc suffix)
86    {
87       return basename + PropertyMap.PROPERTY_NAME_SEPARATOR + suffix;
88    }
89
90    /**
91     * Make a fully qualified property name.
92     *
93     * @param suffix Property name suffix.
94     */

95    private String JavaDoc makePropertyName(final Object JavaDoc suffix)
96    {
97       return makePropertyName(String.valueOf(suffix));
98    }
99
100    /**
101     * Check if this <tt>PropertyMap</tt> contains a given property name.
102     *
103     * @param name Property name.
104     * @return True if property map or defaults contains key.
105     */

106    public boolean containsKey(final Object JavaDoc name)
107    {
108       if (name == null)
109          throw new NullArgumentException("name");
110
111       return super.containsKey(makePropertyName(name));
112    }
113
114    /**
115     * Set a property.
116     *
117     * @param name Property name.
118     * @param value Property value.
119     * @return Previous property value or <tt>null</tt>.
120     */

121    public Object JavaDoc put(final Object JavaDoc name, final Object JavaDoc value)
122    {
123       if (name == null)
124          throw new NullArgumentException("name");
125
126       return super.put(makePropertyName(name), value);
127    }
128
129    /**
130     * Get a property
131     *
132     * @param name Property name.
133     * @return Property value or <tt>null</tt>.
134     */

135    public Object JavaDoc get(final Object JavaDoc name)
136    {
137       if (name == null)
138          throw new NullArgumentException("name");
139
140       return super.get(makePropertyName(name));
141    }
142
143    /**
144     * Remove a property.
145     *
146     * @param name Property name.
147     * @return Removed property value.
148     */

149    public Object JavaDoc remove(final Object JavaDoc name)
150    {
151       if (name == null)
152          throw new NullArgumentException("name");
153
154       return super.remove(makePropertyName(name));
155    }
156
157    /**
158     * Returns an entry set for all properties in this group.
159     *
160     * <p>
161     * This is currently ver inefficient, but should get the
162     * job done for now.
163     */

164    public Set JavaDoc entrySet()
165    {
166       final Set JavaDoc superSet = super.entrySet(true);
167
168       return new java.util.AbstractSet JavaDoc()
169       {
170          private boolean isInGroup(Map.Entry JavaDoc entry)
171          {
172             String JavaDoc key = (String JavaDoc) entry.getKey();
173             return key.startsWith(basename);
174          }
175
176          public int size()
177          {
178             Iterator JavaDoc iter = superSet.iterator();
179             int count = 0;
180             while (iter.hasNext())
181             {
182                Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
183                if (isInGroup(entry))
184                {
185                   count++;
186                }
187             }
188
189             return count;
190          }
191
192          public Iterator JavaDoc iterator()
193          {
194             return new Iterator JavaDoc()
195             {
196                private Iterator JavaDoc iter = superSet.iterator();
197
198                private Object JavaDoc next;
199
200                public boolean hasNext()
201                {
202                   if (next != null)
203                      return true;
204
205                   while (next == null)
206                   {
207                      if (iter.hasNext())
208                      {
209                         Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
210                         if (isInGroup(entry))
211                         {
212                            next = entry;
213                            return true;
214                         }
215                      }
216                      else
217                      {
218                         break;
219                      }
220                   }
221
222                   return false;
223                }
224
225                public Object JavaDoc next()
226                {
227                   if (next == null)
228                      throw new java.util.NoSuchElementException JavaDoc();
229
230                   Object JavaDoc obj = next;
231                   next = null;
232
233                   return obj;
234                }
235
236                public void remove()
237                {
238                   iter.remove();
239                }
240             };
241          }
242       };
243    }
244
245    /**
246     * Add a bound property listener.
247     *
248     * <p>Generates a fully qualified property name and adds the listener
249     * under that name.
250     *
251     * @param listener Bound property listener to add.
252     */

253    protected void addPropertyListener(final BoundPropertyListener listener)
254    {
255       // get the bound property name
256
String JavaDoc name = makePropertyName(listener.getPropertyName());
257
258       // get the bound listener list for the property
259
List JavaDoc list = (List JavaDoc) boundListeners.get(name);
260
261       // if list is null, then add a new list
262
if (list == null)
263       {
264          list = new ArrayList JavaDoc();
265          boundListeners.put(name, list);
266       }
267
268       // if listener is not in the list already, then add it
269
if (!list.contains(listener))
270       {
271          list.add(listener);
272          // notify listener that is is bound
273
listener.propertyBound(this);
274       }
275    }
276
277    /**
278     * Remove a bound property listener.
279     *
280     * <p>Generates a fully qualified property name and removes the listener
281     * under that name.
282     *
283     * @param listener Bound property listener to remove.
284     * @return True if listener was removed.
285     */

286    protected boolean removePropertyListener(final BoundPropertyListener listener)
287    {
288       // get the bound property name
289
String JavaDoc name = makePropertyName(listener.getPropertyName());
290
291       // get the bound listener list for the property
292
List JavaDoc list = (List JavaDoc) boundListeners.get(name);
293       boolean removed = false;
294       if (list != null)
295       {
296          removed = list.remove(listener);
297
298          // notify listener that is was unbound
299
if (removed)
300             listener.propertyUnbound(this);
301       }
302
303       return removed;
304    }
305 }
306
Popular Tags