1 /*
   2  * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 /**
  29  * This class provides a skeletal implementation of the {@code Set}
  30  * interface to minimize the effort required to implement this
  31  * interface. <p>
  32  *
  33  * The process of implementing a set by extending this class is identical
  34  * to that of implementing a Collection by extending AbstractCollection,
  35  * except that all of the methods and constructors in subclasses of this
  36  * class must obey the additional constraints imposed by the {@code Set}
  37  * interface (for instance, the add method must not permit addition of
  38  * multiple instances of an object to a set).<p>
  39  *
  40  * Note that this class does not override any of the implementations from
  41  * the {@code AbstractCollection} class.  It merely adds implementations
  42  * for {@code equals} and {@code hashCode}.<p>
  43  *
  44  * This class is a member of the
  45  * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
  46  * Java Collections Framework</a>.
  47  *
  48  * @param <E> the type of elements maintained by this set
  49  *
  50  * @author  Josh Bloch
  51  * @author  Neal Gafter
  52  * @see Collection
  53  * @see AbstractCollection
  54  * @see Set
  55  * @since 1.2
  56  */
  57 
  58 public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {
  59     /**
  60      * Sole constructor.  (For invocation by subclass constructors, typically
  61      * implicit.)
  62      */
  63     protected AbstractSet() {
  64     }
  65 
  66     // Comparison and hashing
  67 
  68     // FIXME: the first paragraph below is copied from Set.equals, because
  69     // {@inheritDoc} inherits text from superclasses, not interfaces (see JDK-6376959).
  70     /**
  71      * Compares the specified object with this set for equality. Returns
  72      * {@code true} if the specified object is also a set, the two sets
  73      * have the same size, and every member of the specified set is
  74      * contained in this set. This operation uses the membership semantics
  75      * of this set. This definition ensures that the {@code equals} method
  76      * works properly across different implementations of the
  77      * {@code Set} interface.
  78      *
  79      * @implSpec
  80      * This implementation first checks if the specified object is this
  81      * set; if so it returns {@code true}.  Then, it checks if the
  82      * specified object is a set whose size is identical to the size of
  83      * this set; if not, it returns {@code false}.  If so, it returns
  84      * {@code containsAll((Collection) o)}.
  85      *
  86      * @implNote
  87      * {@inheritDoc}
  88      *
  89      * @param o object to be compared for equality with this set
  90      * @return {@code true} if the specified object is equal to this set
  91      */
  92     public boolean equals(Object o) {
  93         if (o == this)
  94             return true;
  95 
  96         if (!(o instanceof Set))
  97             return false;
  98         Collection<?> c = (Collection<?>) o;
  99         if (c.size() != size())
 100             return false;
 101         try {
 102             return containsAll(c);
 103         } catch (ClassCastException | NullPointerException unused) {
 104             return false;
 105         }
 106     }
 107 
 108     /**
 109      * Returns the hash code value for this set.  The hash code of a set is
 110      * defined to be the sum of the hash codes of the elements in the set,
 111      * where the hash code of a {@code null} element is defined to be zero.
 112      * This ensures that {@code s1.equals(s2)} implies that
 113      * {@code s1.hashCode()==s2.hashCode()} for any two sets {@code s1}
 114      * and {@code s2}, as required by the general contract of
 115      * {@link Object#hashCode}.
 116      *
 117      * <p>This implementation iterates over the set, calling the
 118      * {@code hashCode} method on each element in the set, and adding up
 119      * the results.
 120      *
 121      * @return the hash code value for this set
 122      * @see Object#equals(Object)
 123      * @see Set#equals(Object)
 124      */
 125     public int hashCode() {
 126         int h = 0;
 127         Iterator<E> i = iterator();
 128         while (i.hasNext()) {
 129             E obj = i.next();
 130             if (obj != null)
 131                 h += obj.hashCode();
 132         }
 133         return h;
 134     }
 135 }