001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2004 Ondrej Lhotak
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package soot.util;
021:
022: import java.util.AbstractSet;
023: import java.util.IdentityHashMap;
024: import java.util.Iterator;
025: import java.util.Set;
026:
027: /**
028: * Implements a hashset with comparison over identity.
029: * @author Eric Bodden
030: */
031: public class IdentityHashSet<E> extends AbstractSet<E> implements
032: Set<E> {
033:
034: protected IdentityHashMap<E, E> delegate;
035:
036: /**
037: * Creates a new, empty IdentityHashSet.
038: */
039: @SuppressWarnings("unchecked")
040: public IdentityHashSet() {
041: delegate = new IdentityHashMap();
042: }
043:
044: /**
045: * {@inheritDoc}
046: */
047: public int size() {
048: return delegate.size();
049: }
050:
051: /**
052: * {@inheritDoc}
053: */
054: public boolean contains(Object o) {
055: return delegate.containsKey(o);
056: }
057:
058: /**
059: * {@inheritDoc}
060: */
061: public Iterator<E> iterator() {
062: return delegate.keySet().iterator();
063: }
064:
065: /**
066: * {@inheritDoc}
067: */
068: public boolean add(E o) {
069: return delegate.put(o, o) == null;
070: }
071:
072: /**
073: * {@inheritDoc}
074: */
075: public boolean remove(Object o) {
076: return delegate.remove(o) != null;
077: }
078:
079: /**
080: * {@inheritDoc}
081: */
082: public void clear() {
083: delegate.entrySet().clear();
084: }
085:
086: /*
087: * Equality based on identity.
088: */
089: public int hashCode() {
090: final int PRIME = 31;
091: int result = 1;
092: result = PRIME * result
093: + ((delegate == null) ? 0 : delegate.hashCode());
094: return result;
095: }
096:
097: /*
098: * Hash code based on identity.
099: */
100: public boolean equals(Object obj) {
101: if (this == obj)
102: return true;
103: if (obj == null)
104: return false;
105: if (getClass() != obj.getClass())
106: return false;
107: final IdentityHashSet other = (IdentityHashSet) obj;
108: if (delegate == null) {
109: if (other.delegate != null)
110: return false;
111: } else if (!delegate.equals(other.delegate))
112: return false;
113: return true;
114: }
115:
116: /**
117: * {@inheritDoc}
118: */
119: public String toString() {
120: return delegate.keySet().toString();
121: }
122:
123: }
|