001: /*
002: * @(#)AbstractSet.java 1.21 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.util;
029:
030: /**
031: * This class provides a skeletal implementation of the <tt>Set</tt>
032: * interface to minimize the effort required to implement this
033: * interface. <p>
034: *
035: * The process of implementing a set by extending this class is identical
036: * to that of implementing a Collection by extending AbstractCollection,
037: * except that all of the methods and constructors in subclasses of this
038: * class must obey the additional constraints imposed by the <tt>Set</tt>
039: * interface (for instance, the add method must not permit addition of
040: * multiple intances of an object to a set).<p>
041: *
042: * Note that this class does not override any of the implementations from
043: * the <tt>AbstractCollection</tt> class. It merely adds implementations
044: * for <tt>equals</tt> and <tt>hashCode</tt>.<p>
045: *
046: * This class is a member of the
047: * <a href="{@docRoot}/../guide/collections/index.html">
048: * Java Collections Framework</a>.
049: *
050: * @author Josh Bloch
051: * @version 1.14, 02/02/00
052: * @see Collection
053: * @see AbstractCollection
054: * @see Set
055: * @since 1.2
056: */
057:
058: public abstract class AbstractSet extends AbstractCollection implements
059: Set {
060: /**
061: * Sole constructor. (For invocation by subclass constructors, typically
062: * implicit.)
063: */
064: protected AbstractSet() {
065: }
066:
067: // Comparison and hashing
068:
069: /**
070: * Compares the specified object with this set for equality. Returns
071: * <tt>true</tt> if the given object is also a set, the two sets have
072: * the same size, and every member of the given set is contained in
073: * this set. This ensures that the <tt>equals</tt> method works
074: * properly across different implementations of the <tt>Set</tt>
075: * interface.<p>
076: *
077: * This implementation first checks if the specified object is this
078: * set; if so it returns <tt>true</tt>. Then, it checks if the
079: * specified object is a set whose size is identical to the size of
080: * this set; if not, it it returns false. If so, it returns
081: * <tt>containsAll((Collection) o)</tt>.
082: *
083: * @param o Object to be compared for equality with this set.
084: * @return <tt>true</tt> if the specified object is equal to this set.
085: */
086: public boolean equals(Object o) {
087: if (o == this )
088: return true;
089:
090: if (!(o instanceof Set))
091: return false;
092: Collection c = (Collection) o;
093: if (c.size() != size())
094: return false;
095: try {
096: return containsAll(c);
097: } catch (ClassCastException unused) {
098: return false;
099: } catch (NullPointerException unused) {
100: return false;
101: }
102: }
103:
104: /**
105: * Returns the hash code value for this set. The hash code of a set is
106: * defined to be the sum of the hash codes of the elements in the set.
107: * This ensures that <tt>s1.equals(s2)</tt> implies that
108: * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
109: * and <tt>s2</tt>, as required by the general contract of
110: * Object.hashCode.<p>
111: *
112: * This implementation enumerates over the set, calling the
113: * <tt>hashCode</tt> method on each element in the collection, and
114: * adding up the results.
115: *
116: * @return the hash code value for this set.
117: */
118: public int hashCode() {
119: int h = 0;
120: Iterator i = iterator();
121: while (i.hasNext()) {
122: Object obj = i.next();
123: if (obj != null)
124: h += obj.hashCode();
125: }
126: return h;
127: }
128:
129: /**
130: * Removes from this set all of its elements that are contained in
131: * the specified collection (optional operation).<p>
132: *
133: * This implementation determines which is the smaller of this set
134: * and the specified collection, by invoking the <tt>size</tt>
135: * method on each. If this set has fewer elements, then the
136: * implementation iterates over this set, checking each element
137: * returned by the iterator in turn to see if it is contained in
138: * the specified collection. If it is so contained, it is removed
139: * from this set with the iterator's <tt>remove</tt> method. If
140: * the specified collection has fewer elements, then the
141: * implementation iterates over the specified collection, removing
142: * from this set each element returned by the iterator, using this
143: * set's <tt>remove</tt> method.<p>
144: *
145: * Note that this implementation will throw an
146: * <tt>UnsupportedOperationException</tt> if the iterator returned by the
147: * <tt>iterator</tt> method does not implement the <tt>remove</tt> method.
148: *
149: * @param c elements to be removed from this set.
150: * @return <tt>true</tt> if this set changed as a result of the call.
151: *
152: * @throws UnsupportedOperationException removeAll is not supported
153: * by this set.
154: * @throws NullPointerException if the specified collection is null.
155: * @see #remove(Object)
156: * @see #contains(Object)
157: */
158: public boolean removeAll(Collection c) {
159: boolean modified = false;
160:
161: if (size() > c.size()) {
162: for (Iterator i = c.iterator(); i.hasNext();)
163: modified |= remove(i.next());
164: } else {
165: for (Iterator i = iterator(); i.hasNext();) {
166: if (c.contains(i.next())) {
167: i.remove();
168: modified = true;
169: }
170: }
171: }
172: return modified;
173: }
174:
175: }
|