KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > util > SetMap


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.common.util;
10
11 import java.util.Collections JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.io.Serializable JavaDoc;
18
19 /**
20  * A map of set. This object does not handle synchronization and use HashMap and HashSet
21  * as underlying data structures;
22  *
23  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
24  * @version $Revision: 1.3 $
25  */

26 public class SetMap implements Serializable JavaDoc
27 {
28
29    /** Version. */
30    static final long serialVersionUID = -7239767000556095977L;
31
32    /** The underlying map. */
33    private Map JavaDoc map;
34
35    public SetMap()
36    {
37       map = new HashMap JavaDoc();
38    }
39
40    /**
41     * Copy constructor.
42     *
43     * @throws IllegalArgumentException if the argument is null
44     */

45    public SetMap(SetMap other) throws IllegalArgumentException JavaDoc
46    {
47       if (other == null)
48       {
49          throw new IllegalArgumentException JavaDoc("Cannot copy null argument");
50       }
51       map = new HashMap JavaDoc();
52       for (Iterator JavaDoc i = other.map.entrySet().iterator(); i.hasNext();)
53       {
54          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
55          Object JavaDoc key = entry.getKey();
56          Set JavaDoc value = (Set JavaDoc)entry.getValue();
57          map.put(key, new HashSet JavaDoc(value));
58       }
59    }
60
61    /**
62     * Add an object in the set keyed under the specified key.
63     */

64    public void put(Object JavaDoc key, Object JavaDoc o)
65    {
66       Set JavaDoc set = (Set JavaDoc)map.get(key);
67       if (set == null)
68       {
69          set = new HashSet JavaDoc();
70          map.put(key, set);
71       }
72       set.add(o);
73    }
74
75    /**
76     * Return the set of keys.
77     */

78    public Set JavaDoc keySet()
79    {
80       return map.keySet();
81    }
82
83    /**
84     * Remove the entire set of objects specified by the key.
85     */

86    public void remove(Object JavaDoc key)
87    {
88       map.remove(key);
89    }
90
91    /**
92     * Remove an object in the set keyed under the specified key.
93     */

94    public void remove(Object JavaDoc key, Object JavaDoc o)
95    {
96       Set JavaDoc set = (Set JavaDoc)map.get(key);
97       if (set != null)
98       {
99          set.remove(o);
100          if (set.isEmpty())
101          {
102             map.remove(key);
103          }
104       }
105    }
106
107    /**
108     * Return true if the specified set contains the object o.
109     */

110    public boolean contains(Object JavaDoc key, Object JavaDoc o)
111    {
112       Set JavaDoc set = (Set JavaDoc)map.get(key);
113       if (set == null)
114       {
115          return false;
116       }
117       else
118       {
119          return set.contains(o);
120       }
121    }
122
123    /**
124     * Return the set specified by the key.
125     */

126    public Set JavaDoc get(Object JavaDoc key)
127    {
128       return (Set JavaDoc)map.get(key);
129    }
130
131    /**
132     * Return an iterator over the values in the set specified by the key.
133     */

134    public Iterator JavaDoc iterator(final Object JavaDoc key)
135    {
136       Set JavaDoc set = (Set JavaDoc)map.get(key);
137       if (set == null)
138       {
139          return Collections.EMPTY_SET.iterator();
140       }
141       else
142       {
143          final Iterator JavaDoc iterator = set.iterator();
144          return new Iterator JavaDoc()
145          {
146             public boolean hasNext()
147             {
148                return iterator.hasNext();
149             }
150             public Object JavaDoc next()
151             {
152                return iterator.next();
153             }
154             public void remove()
155             {
156                iterator.remove();
157                if (!iterator.hasNext())
158                {
159                   map.remove(key);
160                }
161             }
162          };
163       }
164    }
165 }
166
Popular Tags