KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > util > WeakHashSet


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21
22 package org.jacorb.notification.util;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.WeakHashMap JavaDoc;
29
30 /**
31  * @author Alphonse Bendt
32  * @version $Id: WeakHashSet.java,v 1.1 2005/02/14 00:13:05 alphonse.bendt Exp $
33  */

34 public class WeakHashSet implements Set JavaDoc
35 {
36     private final Map JavaDoc entries_ = new WeakHashMap JavaDoc();
37
38     private static final Object JavaDoc PRESENT = new Object JavaDoc();
39
40     /*
41      * (non-Javadoc)
42      *
43      * @see java.util.Collection#size()
44      */

45     public int size()
46     {
47         return entries_.size();
48     }
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see java.util.Collection#isEmpty()
54      */

55     public boolean isEmpty()
56     {
57         return entries_.isEmpty();
58     }
59
60     /*
61      * (non-Javadoc)
62      *
63      * @see java.util.Collection#contains(java.lang.Object)
64      */

65     public boolean contains(Object JavaDoc o)
66     {
67         return PRESENT == entries_.get(o);
68     }
69
70     /*
71      * (non-Javadoc)
72      *
73      * @see java.util.Collection#iterator()
74      */

75     public Iterator JavaDoc iterator()
76     {
77         return entries_.keySet().iterator();
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see java.util.Collection#toArray()
84      */

85     public Object JavaDoc[] toArray()
86     {
87         return entries_.keySet().toArray();
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see java.util.Collection#toArray(java.lang.Object[])
94      */

95     public Object JavaDoc[] toArray(Object JavaDoc[] a)
96     {
97         return entries_.keySet().toArray(a);
98     }
99
100     /*
101      * (non-Javadoc)
102      *
103      * @see java.util.Collection#add(java.lang.Object)
104      */

105     public boolean add(Object JavaDoc o)
106     {
107         return entries_.put(o, PRESENT) == null;
108     }
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see java.util.Collection#remove(java.lang.Object)
114      */

115     public boolean remove(Object JavaDoc o)
116     {
117         return entries_.remove(o) != null;
118     }
119
120     /*
121      * (non-Javadoc)
122      *
123      * @see java.util.Collection#containsAll(java.util.Collection)
124      */

125     public boolean containsAll(Collection JavaDoc c)
126     {
127         Iterator JavaDoc i = c.iterator();
128         while (i.hasNext())
129         {
130             if (!(PRESENT == entries_.get(i.next())))
131             {
132                 return false;
133             }
134         }
135
136         return true;
137     }
138
139     /*
140      * (non-Javadoc)
141      *
142      * @see java.util.Collection#addAll(java.util.Collection)
143      */

144     public boolean addAll(Collection JavaDoc c)
145     {
146         boolean modified = false;
147         Iterator JavaDoc i = c.iterator();
148         while (i.hasNext())
149         {
150             modified |= add(i.next());
151         }
152         return modified;
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see java.util.Collection#retainAll(java.util.Collection)
159      */

160     public boolean retainAll(Collection JavaDoc c)
161     {
162         boolean modified = false;
163         Iterator JavaDoc i = entries_.keySet().iterator();
164         while(i.hasNext())
165         {
166             if (!c.contains(i.next()))
167             {
168                 i.remove();
169                 modified = true;
170             }
171         }
172         return modified;
173     }
174
175     /*
176      * (non-Javadoc)
177      *
178      * @see java.util.Collection#removeAll(java.util.Collection)
179      */

180     public boolean removeAll(Collection JavaDoc c)
181     {
182         boolean modified = false;
183         Iterator JavaDoc i = c.iterator();
184         while (i.hasNext())
185         {
186             modified |= remove(i.next());
187         }
188         return modified;
189     }
190
191     /*
192      * (non-Javadoc)
193      *
194      * @see java.util.Collection#clear()
195      */

196     public void clear()
197     {
198         entries_.clear();
199     }
200 }
Popular Tags