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.Map;
020: import java.util.Set;
021:
022: import net.sf.oval.internal.CollectionFactoryHolder;
023:
024: public class IdentitySet<E> implements Set<E>, Serializable {
025: private static final long serialVersionUID = 1L;
026:
027: private transient Map<Integer, E> map;
028:
029: /**
030: * Constructs a new, empty <tt>IdentitySet</tt>; the backing <tt>Map</tt> instance has
031: * default initial capacity (16) and load factor (0.75).
032: */
033: public IdentitySet() {
034: map = CollectionFactoryHolder.getFactory().createMap();
035: }
036:
037: /**
038: * Constructs a new, empty <tt>IdentitySet</tt>; the backing <tt>Map</tt> instance has
039: * the given initial capacity and the default load factor (0.75).
040: */
041: public IdentitySet(final int initialCapacity) {
042: map = CollectionFactoryHolder.getFactory().createMap(
043: initialCapacity);
044: }
045:
046: public boolean add(final E o) {
047: final int hash = System.identityHashCode(o);
048: return map.put(hash, o) == null;
049: }
050:
051: public boolean addAll(final Collection<? extends E> c) {
052: int count = 0;
053: for (final E e : c) {
054: if (add(e))
055: count++;
056: }
057: return count > 0;
058: }
059:
060: public void clear() {
061: map.clear();
062: }
063:
064: public boolean contains(final Object o) {
065: final int hash = System.identityHashCode(o);
066: return map.containsKey(hash);
067: }
068:
069: public boolean containsAll(final Collection<?> c) {
070: throw new UnsupportedOperationException();
071: }
072:
073: public boolean isEmpty() {
074: return map.isEmpty();
075: }
076:
077: public Iterator<E> iterator() {
078: return map.values().iterator();
079: }
080:
081: /**
082: * Reconstitute the <tt>IdentitySet</tt> instance from a stream (that is,
083: * deserialize it).
084: */
085: @SuppressWarnings("unchecked")
086: private void readObject(final ObjectInputStream ois)
087: throws java.io.IOException, ClassNotFoundException {
088: // materialize any hidden serialization magic
089: ois.defaultReadObject();
090:
091: // materialize the size
092: final int size = ois.readInt();
093:
094: // materialize the elements
095: map = CollectionFactoryHolder.getFactory().createMap(size);
096: for (int i = 0; i < size; i++) {
097: final E o = (E) ois.readObject();
098: final int hash = System.identityHashCode(o);
099: map.put(hash, o);
100: }
101: }
102:
103: public boolean remove(final Object o) {
104: final int hash = System.identityHashCode(o);
105: return map.remove(hash) != null;
106: }
107:
108: public boolean removeAll(final Collection<?> c) {
109: boolean modified = false;
110: for (final Object e : c) {
111: if (remove(e))
112: modified = true;
113: }
114: return modified;
115: }
116:
117: public boolean retainAll(final Collection<?> c) {
118: throw new UnsupportedOperationException();
119: }
120:
121: public int size() {
122: return map.size();
123: }
124:
125: public Object[] toArray() {
126: return map.values().toArray();
127: }
128:
129: public <T> T[] toArray(final T[] a) {
130: return map.values().toArray(a);
131: }
132:
133: /**
134: * Save the state of this <tt>IdentitySet</tt> instance to a stream (that is,
135: * serialize this set).
136: */
137: private void writeObject(final ObjectOutputStream oos)
138: throws java.io.IOException {
139: // serialize any hidden serialization magic
140: oos.defaultWriteObject();
141:
142: // serialize the set's size
143: oos.writeInt(map.size());
144:
145: // serialize the set's elements
146: for (final E e : map.values()) {
147: oos.writeObject(e);
148: }
149: }
150: }
|