jdk/src/share/classes/java/util/AbstractSet.java
author psandoz
Wed, 04 Sep 2013 09:34:25 +0200
changeset 19806 dda89341ee2d
parent 17441 5ae43433d158
child 22078 bdec5d53e98c
permissions -rw-r--r--
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black) 8012913: LinkedHashMap key/value/entry spliterators should report ORDERED Reviewed-by: mduigou, forax, bchristi, alanb Contributed-by: Doug Lea <dl@cs.oswego.edu>, Paul Sandoz <paul.sandoz@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12448
diff changeset
     2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class provides a skeletal implementation of the <tt>Set</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * interface to minimize the effort required to implement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * interface. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The process of implementing a set by extending this class is identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * to that of implementing a Collection by extending AbstractCollection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * except that all of the methods and constructors in subclasses of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * class must obey the additional constraints imposed by the <tt>Set</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * interface (for instance, the add method must not permit addition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * multiple instances of an object to a set).<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Note that this class does not override any of the implementations from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the <tt>AbstractCollection</tt> class.  It merely adds implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * for <tt>equals</tt> and <tt>hashCode</tt>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @param <E> the type of elements maintained by this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @see Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see AbstractCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected AbstractSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // Comparison and hashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Compares the specified object with this set for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <tt>true</tt> if the given object is also a set, the two sets have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * the same size, and every member of the given set is contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * this set.  This ensures that the <tt>equals</tt> method works
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * properly across different implementations of the <tt>Set</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * interface.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * This implementation first checks if the specified object is this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * set; if so it returns <tt>true</tt>.  Then, it checks if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * specified object is a set whose size is identical to the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * this set; if not, it returns false.  If so, it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <tt>containsAll((Collection) o)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param o object to be compared for equality with this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return <tt>true</tt> if the specified object is equal to this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (!(o instanceof Set))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9503
diff changeset
    91
        Collection<?> c = (Collection<?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (c.size() != size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return containsAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } catch (ClassCastException unused)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } catch (NullPointerException unused) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Returns the hash code value for this set.  The hash code of a set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * defined to be the sum of the hash codes of the elements in the set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * where the hash code of a <tt>null</tt> element is defined to be zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * This ensures that <tt>s1.equals(s2)</tt> implies that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * and <tt>s2</tt>, as required by the general contract of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>This implementation iterates over the set, calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <tt>hashCode</tt> method on each element in the set, and adding up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * the results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return the hash code value for this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @see Set#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Iterator<E> i = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            E obj = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (obj != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                h += obj.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Removes from this set all of its elements that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * specified collection (optional operation).  If the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * collection is also a set, this operation effectively modifies this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * set so that its value is the <i>asymmetric set difference</i> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * the two sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p>This implementation determines which is the smaller of this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * and the specified collection, by invoking the <tt>size</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * method on each.  If this set has fewer elements, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * implementation iterates over this set, checking each element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * returned by the iterator in turn to see if it is contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the specified collection.  If it is so contained, it is removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * from this set with the iterator's <tt>remove</tt> method.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * the specified collection has fewer elements, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * implementation iterates over the specified collection, removing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * from this set each element returned by the iterator, using this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * set's <tt>remove</tt> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <p>Note that this implementation will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <tt>UnsupportedOperationException</tt> if the iterator returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <tt>iterator</tt> method does not implement the <tt>remove</tt> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param  c collection containing elements to be removed from this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return <tt>true</tt> if this set changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *         is not supported by this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @throws ClassCastException if the class of an element of this set
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 5506
diff changeset
   159
     *         is incompatible with the specified collection
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 5506
diff changeset
   160
     * (<a href="Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @throws NullPointerException if this set contains a null element and the
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 5506
diff changeset
   162
     *         specified collection does not permit null elements
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 5506
diff changeset
   163
     * (<a href="Collection.html#optional-restrictions">optional</a>),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public boolean removeAll(Collection<?> c) {
17441
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 14342
diff changeset
   169
        Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        boolean modified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (size() > c.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            for (Iterator<?> i = c.iterator(); i.hasNext(); )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                modified |= remove(i.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            for (Iterator<?> i = iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (c.contains(i.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}