001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.internal.util;
013:
014: import java.io.ObjectInputStream;
015: import java.io.ObjectOutputStream;
016: import java.io.Serializable;
017: import java.util.Collection;
018: import java.util.Iterator;
019: import java.util.Set;
020: import java.util.WeakHashMap;
021:
022: /**
023: * @author Sebastian Thomschke
024: */
025: public class WeakHashSet<E> implements Set<E>, Serializable {
026: private static final long serialVersionUID = 1L;
027:
028: private transient WeakHashMap<E, Object> map;
029:
030: /**
031: * Constructs a new, empty <tt>WeakHashSet</tt>; the backing <tt>WeakHashMap</tt> instance has
032: * default initial capacity (16) and load factor (0.75).
033: */
034: public WeakHashSet() {
035: map = new WeakHashMap<E, Object>();
036: }
037:
038: /**
039: * Constructs a new, empty <tt>WeakHashSet</tt>; the backing <tt>WeakHashMap</tt> instance has
040: * the given initial capacity and the default load factor (0.75).
041: */
042: public WeakHashSet(final int initialCapacity) {
043: map = new WeakHashMap<E, Object>(initialCapacity);
044: }
045:
046: public boolean add(final E o) {
047: return map.put(o, Boolean.TRUE) == null;
048: }
049:
050: public boolean addAll(final Collection<? extends E> c) {
051: int count = 0;
052: for (final E e : c) {
053: if (add(e))
054: count++;
055: }
056: return count > 0;
057: }
058:
059: public void clear() {
060: map.clear();
061: }
062:
063: public boolean contains(final Object o) {
064: return map.containsKey(o);
065: }
066:
067: public boolean containsAll(final Collection<?> c) {
068: return map.keySet().containsAll(c);
069: }
070:
071: @Override
072: public boolean equals(Object o) {
073: if (o == this )
074: return true;
075:
076: if (!(o instanceof Set))
077: return false;
078:
079: if (((Set) o).size() != size())
080: return false;
081:
082: return containsAll((Set) o);
083: }
084:
085: @Override
086: public int hashCode() {
087: int hash = 0;
088: for (final E e : map.keySet()) {
089: if (e != null)
090: hash += e.hashCode();
091: }
092: return hash;
093: }
094:
095: public boolean isEmpty() {
096: return map.isEmpty();
097: }
098:
099: public Iterator<E> iterator() {
100: return map.keySet().iterator();
101: }
102:
103: /**
104: * Reconstitute the <tt>WeakHashSet</tt> instance from a stream (that is,
105: * deserialize it).
106: */
107: @SuppressWarnings("unchecked")
108: private void readObject(final ObjectInputStream ois)
109: throws java.io.IOException, ClassNotFoundException {
110: // materialize any hidden serialization magic
111: ois.defaultReadObject();
112:
113: // materialize the size
114: final int size = ois.readInt();
115:
116: // materialize the elements
117: map = new WeakHashMap<E, Object>(size);
118: for (int i = 0; i < size; i++) {
119: map.put((E) ois.readObject(), Boolean.TRUE);
120: }
121: }
122:
123: public boolean remove(final Object o) {
124: return map.remove(o) == Boolean.TRUE;
125: }
126:
127: public boolean removeAll(final Collection<?> c) {
128: return map.keySet().removeAll(c);
129: }
130:
131: public boolean retainAll(final Collection<?> c) {
132: return map.keySet().retainAll(c);
133: }
134:
135: public int size() {
136: return map.size();
137: }
138:
139: public Object[] toArray() {
140: return map.keySet().toArray();
141: }
142:
143: public <T> T[] toArray(final T[] a) {
144: return map.keySet().toArray(a);
145: }
146:
147: /**
148: * Save the state of this <tt>WeakHashSet</tt> instance to a stream (that is,
149: * serialize this set).
150: */
151: private void writeObject(final ObjectOutputStream oos)
152: throws java.io.IOException {
153: // serialize any hidden serialization magic
154: oos.defaultWriteObject();
155:
156: // serialize the set's size
157: oos.writeInt(map.size());
158:
159: // serialize the set's elements
160: for (final E e : map.keySet()) {
161: oos.writeObject(e);
162: }
163: }
164: }
|