001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021:
022: package org.jacorb.notification.util;
023:
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.Set;
028: import java.util.WeakHashMap;
029:
030: /**
031: * @author Alphonse Bendt
032: * @version $Id: WeakHashSet.java,v 1.1 2005/02/14 00:13:05 alphonse.bendt Exp $
033: */
034: public class WeakHashSet implements Set {
035: private final Map entries_ = new WeakHashMap();
036:
037: private static final Object PRESENT = new Object();
038:
039: /*
040: * (non-Javadoc)
041: *
042: * @see java.util.Collection#size()
043: */
044: public int size() {
045: return entries_.size();
046: }
047:
048: /*
049: * (non-Javadoc)
050: *
051: * @see java.util.Collection#isEmpty()
052: */
053: public boolean isEmpty() {
054: return entries_.isEmpty();
055: }
056:
057: /*
058: * (non-Javadoc)
059: *
060: * @see java.util.Collection#contains(java.lang.Object)
061: */
062: public boolean contains(Object o) {
063: return PRESENT == entries_.get(o);
064: }
065:
066: /*
067: * (non-Javadoc)
068: *
069: * @see java.util.Collection#iterator()
070: */
071: public Iterator iterator() {
072: return entries_.keySet().iterator();
073: }
074:
075: /*
076: * (non-Javadoc)
077: *
078: * @see java.util.Collection#toArray()
079: */
080: public Object[] toArray() {
081: return entries_.keySet().toArray();
082: }
083:
084: /*
085: * (non-Javadoc)
086: *
087: * @see java.util.Collection#toArray(java.lang.Object[])
088: */
089: public Object[] toArray(Object[] a) {
090: return entries_.keySet().toArray(a);
091: }
092:
093: /*
094: * (non-Javadoc)
095: *
096: * @see java.util.Collection#add(java.lang.Object)
097: */
098: public boolean add(Object o) {
099: return entries_.put(o, PRESENT) == null;
100: }
101:
102: /*
103: * (non-Javadoc)
104: *
105: * @see java.util.Collection#remove(java.lang.Object)
106: */
107: public boolean remove(Object o) {
108: return entries_.remove(o) != null;
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see java.util.Collection#containsAll(java.util.Collection)
115: */
116: public boolean containsAll(Collection c) {
117: Iterator i = c.iterator();
118: while (i.hasNext()) {
119: if (!(PRESENT == entries_.get(i.next()))) {
120: return false;
121: }
122: }
123:
124: return true;
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see java.util.Collection#addAll(java.util.Collection)
131: */
132: public boolean addAll(Collection c) {
133: boolean modified = false;
134: Iterator i = c.iterator();
135: while (i.hasNext()) {
136: modified |= add(i.next());
137: }
138: return modified;
139: }
140:
141: /*
142: * (non-Javadoc)
143: *
144: * @see java.util.Collection#retainAll(java.util.Collection)
145: */
146: public boolean retainAll(Collection c) {
147: boolean modified = false;
148: Iterator i = entries_.keySet().iterator();
149: while (i.hasNext()) {
150: if (!c.contains(i.next())) {
151: i.remove();
152: modified = true;
153: }
154: }
155: return modified;
156: }
157:
158: /*
159: * (non-Javadoc)
160: *
161: * @see java.util.Collection#removeAll(java.util.Collection)
162: */
163: public boolean removeAll(Collection c) {
164: boolean modified = false;
165: Iterator i = c.iterator();
166: while (i.hasNext()) {
167: modified |= remove(i.next());
168: }
169: return modified;
170: }
171:
172: /*
173: * (non-Javadoc)
174: *
175: * @see java.util.Collection#clear()
176: */
177: public void clear() {
178: entries_.clear();
179: }
180: }
|