Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/util/AbstractSet.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627/**28* This class provides a skeletal implementation of the <tt>Set</tt>29* interface to minimize the effort required to implement this30* interface. <p>31*32* The process of implementing a set by extending this class is identical33* to that of implementing a Collection by extending AbstractCollection,34* except that all of the methods and constructors in subclasses of this35* class must obey the additional constraints imposed by the <tt>Set</tt>36* interface (for instance, the add method must not permit addition of37* multiple instances of an object to a set).<p>38*39* Note that this class does not override any of the implementations from40* the <tt>AbstractCollection</tt> class. It merely adds implementations41* for <tt>equals</tt> and <tt>hashCode</tt>.<p>42*43* This class is a member of the44* <a href="{@docRoot}/../technotes/guides/collections/index.html">45* Java Collections Framework</a>.46*47* @param <E> the type of elements maintained by this set48*49* @author Josh Bloch50* @author Neal Gafter51* @see Collection52* @see AbstractCollection53* @see Set54* @since 1.255*/5657public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {58/**59* Sole constructor. (For invocation by subclass constructors, typically60* implicit.)61*/62protected AbstractSet() {63}6465// Comparison and hashing6667/**68* Compares the specified object with this set for equality. Returns69* <tt>true</tt> if the given object is also a set, the two sets have70* the same size, and every member of the given set is contained in71* this set. This ensures that the <tt>equals</tt> method works72* properly across different implementations of the <tt>Set</tt>73* interface.<p>74*75* This implementation first checks if the specified object is this76* set; if so it returns <tt>true</tt>. Then, it checks if the77* specified object is a set whose size is identical to the size of78* this set; if not, it returns false. If so, it returns79* <tt>containsAll((Collection) o)</tt>.80*81* @param o object to be compared for equality with this set82* @return <tt>true</tt> if the specified object is equal to this set83*/84public boolean equals(Object o) {85if (o == this)86return true;8788if (!(o instanceof Set))89return false;90Collection<?> c = (Collection<?>) o;91if (c.size() != size())92return false;93try {94return containsAll(c);95} catch (ClassCastException unused) {96return false;97} catch (NullPointerException unused) {98return false;99}100}101102/**103* Returns the hash code value for this set. The hash code of a set is104* defined to be the sum of the hash codes of the elements in the set,105* where the hash code of a <tt>null</tt> element is defined to be zero.106* This ensures that <tt>s1.equals(s2)</tt> implies that107* <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>108* and <tt>s2</tt>, as required by the general contract of109* {@link Object#hashCode}.110*111* <p>This implementation iterates over the set, calling the112* <tt>hashCode</tt> method on each element in the set, and adding up113* the results.114*115* @return the hash code value for this set116* @see Object#equals(Object)117* @see Set#equals(Object)118*/119public int hashCode() {120int h = 0;121Iterator<E> i = iterator();122while (i.hasNext()) {123E obj = i.next();124if (obj != null)125h += obj.hashCode();126}127return h;128}129130/**131* Removes from this set all of its elements that are contained in the132* specified collection (optional operation). If the specified133* collection is also a set, this operation effectively modifies this134* set so that its value is the <i>asymmetric set difference</i> of135* the two sets.136*137* <p>This implementation determines which is the smaller of this set138* and the specified collection, by invoking the <tt>size</tt>139* method on each. If this set has fewer elements, then the140* implementation iterates over this set, checking each element141* returned by the iterator in turn to see if it is contained in142* the specified collection. If it is so contained, it is removed143* from this set with the iterator's <tt>remove</tt> method. If144* the specified collection has fewer elements, then the145* implementation iterates over the specified collection, removing146* from this set each element returned by the iterator, using this147* set's <tt>remove</tt> method.148*149* <p>Note that this implementation will throw an150* <tt>UnsupportedOperationException</tt> if the iterator returned by the151* <tt>iterator</tt> method does not implement the <tt>remove</tt> method.152*153* @param c collection containing elements to be removed from this set154* @return <tt>true</tt> if this set changed as a result of the call155* @throws UnsupportedOperationException if the <tt>removeAll</tt> operation156* is not supported by this set157* @throws ClassCastException if the class of an element of this set158* is incompatible with the specified collection159* (<a href="Collection.html#optional-restrictions">optional</a>)160* @throws NullPointerException if this set contains a null element and the161* specified collection does not permit null elements162* (<a href="Collection.html#optional-restrictions">optional</a>),163* or if the specified collection is null164* @see #remove(Object)165* @see #contains(Object)166*/167public boolean removeAll(Collection<?> c) {168Objects.requireNonNull(c);169boolean modified = false;170171if (size() > c.size()) {172for (Iterator<?> i = c.iterator(); i.hasNext(); )173modified |= remove(i.next());174} else {175for (Iterator<?> i = iterator(); i.hasNext(); ) {176if (c.contains(i.next())) {177i.remove();178modified = true;179}180}181}182return modified;183}184185}186187188