src/java.base/share/classes/java/util/AbstractSet.java
author jlaskey
Thu, 14 Nov 2019 12:50:08 -0400
branchJDK-8193209-branch
changeset 59088 da026c172c1e
parent 49433 b6671a111395
permissions -rw-r--r--
add missing files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 49283
diff changeset
     2
 * Copyright (c) 1997, 2018, 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
/**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    29
 * This class provides a skeletal implementation of the {@code Set}
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    36
 * class must obey the additional constraints imposed by the {@code Set}
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    41
 * the {@code AbstractCollection} class.  It merely adds implementations
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    42
 * for {@code equals} and {@code hashCode}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * This class is a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 49283
diff changeset
    45
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    70
     * {@code true} if the given object is also a set, the two sets have
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * the same size, and every member of the given set is contained in
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    72
     * this set.  This ensures that the {@code equals} method works
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    73
     * properly across different implementations of the {@code Set}
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    77
     * set; if so it returns {@code true}.  Then, it checks if the
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    80
     * {@code containsAll((Collection) o)}.
2
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
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    83
     * @return {@code true} if the specified object is equal to this set
2
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);
49283
a14ede52a278 8193128: Reduce number of implementation classes returned by List/Set/Map.of()
redestad
parents: 47216
diff changeset
    96
        } catch (ClassCastException | NullPointerException unused) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Returns the hash code value for this set.  The hash code of a set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * defined to be the sum of the hash codes of the elements in the set,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   104
     * where the hash code of a {@code null} element is defined to be zero.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   105
     * This ensures that {@code s1.equals(s2)} implies that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   106
     * {@code s1.hashCode()==s2.hashCode()} for any two sets {@code s1}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   107
     * and {@code s2}, as required by the general contract of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>This implementation iterates over the set, calling the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   111
     * {@code hashCode} method on each element in the set, and adding up
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * the results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @return the hash code value for this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @see Set#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Iterator<E> i = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            E obj = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (obj != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                h += obj.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Removes from this set all of its elements that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * specified collection (optional operation).  If the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * collection is also a set, this operation effectively modifies this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * set so that its value is the <i>asymmetric set difference</i> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * the two sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>This implementation determines which is the smaller of this set
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   137
     * and the specified collection, by invoking the {@code size}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * method on each.  If this set has fewer elements, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * implementation iterates over this set, checking each element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * returned by the iterator in turn to see if it is contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * the specified collection.  If it is so contained, it is removed
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   142
     * from this set with the iterator's {@code remove} method.  If
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the specified collection has fewer elements, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * implementation iterates over the specified collection, removing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * from this set each element returned by the iterator, using this
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   146
     * set's {@code remove} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p>Note that this implementation will throw an
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   149
     * {@code UnsupportedOperationException} if the iterator returned by the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   150
     * {@code iterator} method does not implement the {@code remove} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param  c collection containing elements to be removed from this set
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   153
     * @return {@code true} if this set changed as a result of the call
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   154
     * @throws UnsupportedOperationException if the {@code removeAll} operation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *         is not supported by this set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @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
   157
     *         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
   158
     * (<a href="Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @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
   160
     *         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
   161
     * (<a href="Collection.html#optional-restrictions">optional</a>),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *         or if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public boolean removeAll(Collection<?> c) {
17441
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 14342
diff changeset
   167
        Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        boolean modified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (size() > c.size()) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 17441
diff changeset
   171
            for (Object e : c)
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 17441
diff changeset
   172
                modified |= remove(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            for (Iterator<?> i = iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (c.contains(i.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}