0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/A/**
0N/A * This class provides a skeletal implementation of the <tt>Set</tt>
0N/A * interface to minimize the effort required to implement this
0N/A * interface. <p>
0N/A *
0N/A * The process of implementing a set by extending this class is identical
0N/A * to that of implementing a Collection by extending AbstractCollection,
0N/A * except that all of the methods and constructors in subclasses of this
0N/A * class must obey the additional constraints imposed by the <tt>Set</tt>
0N/A * interface (for instance, the add method must not permit addition of
0N/A * multiple instances of an object to a set).<p>
0N/A *
0N/A * Note that this class does not override any of the implementations from
0N/A * the <tt>AbstractCollection</tt> class. It merely adds implementations
0N/A * for <tt>equals</tt> and <tt>hashCode</tt>.<p>
0N/A *
0N/A * This class is a member of the
0N/A * <a href="{@docRoot}/../technotes/guides/collections/index.html">
0N/A * Java Collections Framework</a>.
0N/A *
0N/A * @param <E> the type of elements maintained by this set
0N/A *
0N/A * @author Josh Bloch
0N/A * @author Neal Gafter
0N/A * @see Collection
0N/A * @see AbstractCollection
0N/A * @see Set
0N/A * @since 1.2
0N/A */
0N/A
0N/Apublic abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {
0N/A /**
0N/A * Sole constructor. (For invocation by subclass constructors, typically
0N/A * implicit.)
0N/A */
0N/A protected AbstractSet() {
0N/A }
0N/A
0N/A // Comparison and hashing
0N/A
0N/A /**
0N/A * Compares the specified object with this set for equality. Returns
0N/A * <tt>true</tt> if the given object is also a set, the two sets have
0N/A * the same size, and every member of the given set is contained in
0N/A * this set. This ensures that the <tt>equals</tt> method works
0N/A * properly across different implementations of the <tt>Set</tt>
0N/A * interface.<p>
0N/A *
0N/A * This implementation first checks if the specified object is this
0N/A * set; if so it returns <tt>true</tt>. Then, it checks if the
0N/A * specified object is a set whose size is identical to the size of
0N/A * this set; if not, it returns false. If so, it returns
0N/A * <tt>containsAll((Collection) o)</tt>.
0N/A *
0N/A * @param o object to be compared for equality with this set
0N/A * @return <tt>true</tt> if the specified object is equal to this set
0N/A */
0N/A public boolean equals(Object o) {
0N/A if (o == this)
0N/A return true;
0N/A
0N/A if (!(o instanceof Set))
0N/A return false;
0N/A Collection c = (Collection) o;
0N/A if (c.size() != size())
0N/A return false;
0N/A try {
0N/A return containsAll(c);
0N/A } catch (ClassCastException unused) {
0N/A return false;
0N/A } catch (NullPointerException unused) {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the hash code value for this set. The hash code of a set is
0N/A * defined to be the sum of the hash codes of the elements in the set,
0N/A * where the hash code of a <tt>null</tt> element is defined to be zero.
0N/A * This ensures that <tt>s1.equals(s2)</tt> implies that
0N/A * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
0N/A * and <tt>s2</tt>, as required by the general contract of
0N/A * {@link Object#hashCode}.
0N/A *
0N/A * <p>This implementation iterates over the set, calling the
0N/A * <tt>hashCode</tt> method on each element in the set, and adding up
0N/A * the results.
0N/A *
0N/A * @return the hash code value for this set
0N/A * @see Object#equals(Object)
0N/A * @see Set#equals(Object)
0N/A */
0N/A public int hashCode() {
0N/A int h = 0;
0N/A Iterator<E> i = iterator();
0N/A while (i.hasNext()) {
0N/A E obj = i.next();
0N/A if (obj != null)
0N/A h += obj.hashCode();
0N/A }
0N/A return h;
0N/A }
0N/A
0N/A /**
0N/A * Removes from this set all of its elements that are contained in the
0N/A * specified collection (optional operation). If the specified
0N/A * collection is also a set, this operation effectively modifies this
0N/A * set so that its value is the <i>asymmetric set difference</i> of
0N/A * the two sets.
0N/A *
0N/A * <p>This implementation determines which is the smaller of this set
0N/A * and the specified collection, by invoking the <tt>size</tt>
0N/A * method on each. If this set has fewer elements, then the
0N/A * implementation iterates over this set, checking each element
0N/A * returned by the iterator in turn to see if it is contained in
0N/A * the specified collection. If it is so contained, it is removed
0N/A * from this set with the iterator's <tt>remove</tt> method. If
0N/A * the specified collection has fewer elements, then the
0N/A * implementation iterates over the specified collection, removing
0N/A * from this set each element returned by the iterator, using this
0N/A * set's <tt>remove</tt> method.
0N/A *
0N/A * <p>Note that this implementation will throw an
0N/A * <tt>UnsupportedOperationException</tt> if the iterator returned by the
0N/A * <tt>iterator</tt> method does not implement the <tt>remove</tt> method.
0N/A *
0N/A * @param c collection containing elements to be removed from this set
0N/A * @return <tt>true</tt> if this set changed as a result of the call
0N/A * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
0N/A * is not supported by this set
0N/A * @throws ClassCastException if the class of an element of this set
4106N/A * is incompatible with the specified collection
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws NullPointerException if this set contains a null element and the
4106N/A * specified collection does not permit null elements
4106N/A * (<a href="Collection.html#optional-restrictions">optional</a>),
0N/A * or if the specified collection is null
0N/A * @see #remove(Object)
0N/A * @see #contains(Object)
0N/A */
0N/A public boolean removeAll(Collection<?> c) {
0N/A boolean modified = false;
0N/A
0N/A if (size() > c.size()) {
0N/A for (Iterator<?> i = c.iterator(); i.hasNext(); )
0N/A modified |= remove(i.next());
0N/A } else {
0N/A for (Iterator<?> i = iterator(); i.hasNext(); ) {
0N/A if (c.contains(i.next())) {
0N/A i.remove();
0N/A modified = true;
0N/A }
0N/A }
0N/A }
0N/A return modified;
0N/A }
0N/A
0N/A}