src/java.base/share/classes/java/util/AbstractCollection.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 44743 jdk/src/java.base/share/classes/java/util/AbstractCollection.java@f0bbd698c486
child 49433 b6671a111395
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22114
diff changeset
     2
 * Copyright (c) 1997, 2013, 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: 5466
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: 5466
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: 5466
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5466
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 Collection}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * interface, to minimize the effort required to implement this interface. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * To implement an unmodifiable collection, the programmer needs only to
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    33
 * extend this class and provide implementations for the {@code iterator} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    34
 * {@code size} methods.  (The iterator returned by the {@code iterator}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    35
 * method must implement {@code hasNext} and {@code next}.)<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * To implement a modifiable collection, the programmer must additionally
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    38
 * override this class's {@code add} method (which otherwise throws an
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    39
 * {@code UnsupportedOperationException}), and the iterator returned by the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    40
 * {@code iterator} method must additionally implement its {@code remove}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * method.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The programmer should generally provide a void (no argument) and
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    44
 * {@code Collection} constructor, as per the recommendation in the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    45
 * {@code Collection} interface specification.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The documentation for each non-abstract method in this class describes its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * implementation in detail.  Each of these methods may be overridden if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the collection being implemented admits a more efficient implementation.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * This class is a member of the
44743
f0bbd698c486 8177789: fix collections framework links to point to java.util package doc
smarks
parents: 32108
diff changeset
    52
 * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public abstract class AbstractCollection<E> implements Collection<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected AbstractCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // Query Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Returns an iterator over the elements contained in this collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @return an iterator over the elements contained in this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public abstract Iterator<E> iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public abstract int size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
    83
     * @implSpec
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    84
     * This implementation returns {@code size() == 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return size() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
    93
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
    94
     * This implementation iterates over the elements in the collection,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * checking each element in turn for equality with the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @throws ClassCastException   {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public boolean contains(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   101
        Iterator<E> it = iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (o==null) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   103
            while (it.hasNext())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   104
                if (it.next()==null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   107
            while (it.hasNext())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   108
                if (o.equals(it.next()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   117
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   118
     * This implementation returns an array containing all the elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * returned by this collection's iterator, in the same order, stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * consecutive elements of the array, starting with index {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * The length of the returned array is equal to the number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * returned by the iterator, even if the size of this collection changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * during iteration, as might happen if the collection permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * concurrent modification during iteration.  The {@code size} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * called only as an optimization hint; the correct result is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * even if the iterator returns a different number of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p>This method is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * List<E> list = new ArrayList<E>(size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * for (E e : this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *     list.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * return list.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // Estimate size of array; be prepared to see more or fewer elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Object[] r = new Object[size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        Iterator<E> it = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        for (int i = 0; i < r.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (! it.hasNext()) // fewer elements than expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                return Arrays.copyOf(r, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            r[i] = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return it.hasNext() ? finishToArray(r, it) : r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   152
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   153
     * This implementation returns an array containing all the elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * returned by this collection's iterator in the same order, stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * consecutive elements of the array, starting with index {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * If the number of elements returned by the iterator is too large to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * fit into the specified array, then the elements are returned in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * newly allocated array with length equal to the number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * returned by the iterator, even if the size of this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * changes during iteration, as might happen if the collection permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * concurrent modification during iteration.  The {@code size} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * called only as an optimization hint; the correct result is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * even if the iterator returns a different number of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <p>This method is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * List<E> list = new ArrayList<E>(size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * for (E e : this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *     list.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * return list.toArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @throws ArrayStoreException  {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 12422
diff changeset
   177
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // Estimate size of array; be prepared to see more or fewer elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        T[] r = a.length >= size ? a :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                  (T[])java.lang.reflect.Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                  .newInstance(a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Iterator<E> it = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        for (int i = 0; i < r.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (! it.hasNext()) { // fewer elements than expected
12422
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   188
                if (a == r) {
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   189
                    r[i] = null; // null-terminate
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   190
                } else if (a.length < i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    return Arrays.copyOf(r, i);
12422
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   192
                } else {
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   193
                    System.arraycopy(r, 0, a, 0, i);
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   194
                    if (a.length > i) {
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   195
                        a[i] = null;
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   196
                    }
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   197
                }
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   198
                return a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            r[i] = (T)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
12422
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   202
        // more elements than expected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return it.hasNext() ? finishToArray(r, it) : r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   207
     * The maximum size of array to allocate.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   208
     * Some VMs reserve some header words in an array.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   209
     * Attempts to allocate larger arrays may result in
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   210
     * OutOfMemoryError: Requested array size exceeds VM limit
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   211
     */
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   212
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   213
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   214
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Reallocates the array being used within toArray when the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * returned more elements than expected, and finishes filling it from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * the iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param r the array, replete with previously stored elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param it the in-progress iterator over this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return array containing the elements in the given array, plus any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *         further elements returned by the iterator, trimmed to size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 12422
diff changeset
   224
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int i = r.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            int cap = r.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            if (i == cap) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   230
                int newCap = cap + (cap >> 1) + 1;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   231
                // overflow-conscious code
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   232
                if (newCap - MAX_ARRAY_SIZE > 0)
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   233
                    newCap = hugeCapacity(cap + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                r = Arrays.copyOf(r, newCap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            r[i++] = (T)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        // trim if overallocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return (i == r.length) ? r : Arrays.copyOf(r, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   242
    private static int hugeCapacity(int minCapacity) {
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   243
        if (minCapacity < 0) // overflow
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   244
            throw new OutOfMemoryError
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   245
                ("Required array size too large");
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   246
        return (minCapacity > MAX_ARRAY_SIZE) ?
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   247
            Integer.MAX_VALUE :
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   248
            MAX_ARRAY_SIZE;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   249
    }
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   250
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    // Modification Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   256
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   257
     * This implementation always throws an
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   258
     * {@code UnsupportedOperationException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @throws IllegalStateException         {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   273
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   274
     * This implementation iterates over the collection looking for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * specified element.  If it finds the element, it removes the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * from the collection using the iterator's remove method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p>Note that this implementation throws an
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   279
     * {@code UnsupportedOperationException} if the iterator returned by this
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   280
     * collection's iterator method does not implement the {@code remove}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * method and this collection contains the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public boolean remove(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   288
        Iterator<E> it = iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (o==null) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   290
            while (it.hasNext()) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   291
                if (it.next()==null) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   292
                    it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        } else {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   297
            while (it.hasNext()) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   298
                if (o.equals(it.next())) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   299
                    it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   313
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   314
     * This implementation iterates over the specified collection,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * checking each element returned by the iterator in turn to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * if it's contained in this collection.  If all elements are so
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   317
     * contained {@code true} is returned, otherwise {@code false}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public boolean containsAll(Collection<?> c) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   324
        for (Object e : c)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   325
            if (!contains(e))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   333
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   334
     * This implementation iterates over the specified collection, and adds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * each object returned by the iterator to this collection, in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <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
   338
     * {@code UnsupportedOperationException} unless {@code add} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * overridden (assuming the specified collection is non-empty).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @throws IllegalStateException         {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see #add(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        boolean modified = false;
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   351
        for (E e : c)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   352
            if (add(e))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   360
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   361
     * This implementation iterates over this collection, checking each
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * element returned by the iterator in turn to see if it's contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * in the specified collection.  If it's so contained, it's removed from
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   364
     * this collection with the iterator's {@code remove} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <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
   367
     * {@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
   368
     * {@code iterator} method does not implement the {@code remove} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * and this collection contains one or more elements in common with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public boolean removeAll(Collection<?> c) {
17441
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 14342
diff changeset
   380
        Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        boolean modified = false;
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   382
        Iterator<?> it = iterator();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   383
        while (it.hasNext()) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   384
            if (c.contains(it.next())) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   385
                it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   395
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   396
     * This implementation iterates over this collection, checking each
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * element returned by the iterator in turn to see if it's contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * in the specified collection.  If it's not so contained, it's removed
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   399
     * from this collection with the iterator's {@code remove} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <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
   402
     * {@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
   403
     * {@code iterator} method does not implement the {@code remove} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * and this collection contains one or more elements not present in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    public boolean retainAll(Collection<?> c) {
17441
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 14342
diff changeset
   415
        Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        boolean modified = false;
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   417
        Iterator<E> it = iterator();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   418
        while (it.hasNext()) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   419
            if (!c.contains(it.next())) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   420
                it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
22114
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   430
     * @implSpec
e9be73bceec1 8031142: AbstractCollection and AbstractList should specify their default implementation using @implSpec
chegar
parents: 17441
diff changeset
   431
     * This implementation iterates over this collection, removing each
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   432
     * element using the {@code Iterator.remove} operation.  Most
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * implementations will probably choose to override this method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * efficiency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <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
   437
     * {@code UnsupportedOperationException} if the iterator returned by this
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   438
     * collection's {@code iterator} method does not implement the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   439
     * {@code remove} method and this collection is non-empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public void clear() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   444
        Iterator<E> it = iterator();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   445
        while (it.hasNext()) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   446
            it.next();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   447
            it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    //  String conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Returns a string representation of this collection.  The string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * representation consists of a list of the collection's elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * order they are returned by its iterator, enclosed in square brackets
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   458
     * ({@code "[]"}).  Adjacent elements are separated by the characters
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   459
     * {@code ", "} (comma and space).  Elements are converted to strings as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * by {@link String#valueOf(Object)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @return a string representation of this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public String toString() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   465
        Iterator<E> it = iterator();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   466
        if (! it.hasNext())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        for (;;) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   472
            E e = it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            sb.append(e == this ? "(this Collection)" : e);
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   474
            if (! it.hasNext())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                return sb.append(']').toString();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   476
            sb.append(',').append(' ');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
}