jdk/src/share/classes/java/util/AbstractCollection.java
author psandoz
Wed, 04 Sep 2013 09:34:25 +0200
changeset 19806 dda89341ee2d
parent 17441 5ae43433d158
child 22114 e9be73bceec1
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: 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class provides a skeletal implementation of the <tt>Collection</tt>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * extend this class and provide implementations for the <tt>iterator</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <tt>size</tt> methods.  (The iterator returned by the <tt>iterator</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * method must implement <tt>hasNext</tt> and <tt>next</tt>.)<p>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * override this class's <tt>add</tt> method (which otherwise throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <tt>UnsupportedOperationException</tt>), and the iterator returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <tt>iterator</tt> method must additionally implement its <tt>remove</tt>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <tt>Collection</tt> constructor, as per the recommendation in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <tt>Collection</tt> interface specification.<p>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <p>This implementation returns <tt>size() == 0</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return size() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <p>This implementation iterates over the elements in the collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * checking each element in turn for equality with the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @throws ClassCastException   {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public boolean contains(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    99
        Iterator<E> it = iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (o==null) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   101
            while (it.hasNext())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   102
                if (it.next()==null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   105
            while (it.hasNext())
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   106
                if (o.equals(it.next()))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p>This implementation returns an array containing all the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * returned by this collection's iterator, in the same order, stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * consecutive elements of the array, starting with index {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The length of the returned array is equal to the number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * returned by the iterator, even if the size of this collection changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * during iteration, as might happen if the collection permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * concurrent modification during iteration.  The {@code size} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * called only as an optimization hint; the correct result is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * even if the iterator returns a different number of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p>This method is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * List<E> list = new ArrayList<E>(size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * for (E e : this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *     list.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * return list.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // Estimate size of array; be prepared to see more or fewer elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        Object[] r = new Object[size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        Iterator<E> it = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        for (int i = 0; i < r.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (! it.hasNext()) // fewer elements than expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                return Arrays.copyOf(r, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            r[i] = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return it.hasNext() ? finishToArray(r, it) : r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>This implementation returns an array containing all the elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * returned by this collection's iterator in the same order, stored in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * consecutive elements of the array, starting with index {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * If the number of elements returned by the iterator is too large to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * fit into the specified array, then the elements are returned in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * newly allocated array with length equal to the number of elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * returned by the iterator, even if the size of this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * changes during iteration, as might happen if the collection permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * concurrent modification during iteration.  The {@code size} method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * called only as an optimization hint; the correct result is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * even if the iterator returns a different number of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>This method is equivalent to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * List<E> list = new ArrayList<E>(size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * for (E e : this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *     list.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * return list.toArray(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @throws ArrayStoreException  {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 12422
diff changeset
   173
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // Estimate size of array; be prepared to see more or fewer elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        T[] r = a.length >= size ? a :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                  (T[])java.lang.reflect.Array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                  .newInstance(a.getClass().getComponentType(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        Iterator<E> it = iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        for (int i = 0; i < r.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (! it.hasNext()) { // fewer elements than expected
12422
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   184
                if (a == r) {
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   185
                    r[i] = null; // null-terminate
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   186
                } else if (a.length < i) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    return Arrays.copyOf(r, i);
12422
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   188
                } else {
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   189
                    System.arraycopy(r, 0, a, 0, i);
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   190
                    if (a.length > i) {
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   191
                        a[i] = null;
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   192
                    }
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   193
                }
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   194
                return a;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            r[i] = (T)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
12422
7058f568e4cc 7121314: Behavior mismatch between AbstractCollection.toArray(T[] ) and its spec
littlee
parents: 7668
diff changeset
   198
        // more elements than expected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return it.hasNext() ? finishToArray(r, it) : r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   203
     * The maximum size of array to allocate.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   204
     * Some VMs reserve some header words in an array.
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   205
     * Attempts to allocate larger arrays may result in
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   206
     * OutOfMemoryError: Requested array size exceeds VM limit
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   207
     */
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   208
    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
   209
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   210
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Reallocates the array being used within toArray when the iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * returned more elements than expected, and finishes filling it from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * the iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param r the array, replete with previously stored elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param it the in-progress iterator over this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @return array containing the elements in the given array, plus any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *         further elements returned by the iterator, trimmed to size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 12422
diff changeset
   220
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int i = r.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            int cap = r.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (i == cap) {
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   226
                int newCap = cap + (cap >> 1) + 1;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   227
                // overflow-conscious code
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   228
                if (newCap - MAX_ARRAY_SIZE > 0)
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   229
                    newCap = hugeCapacity(cap + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                r = Arrays.copyOf(r, newCap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            r[i++] = (T)it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        // trim if overallocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return (i == r.length) ? r : Arrays.copyOf(r, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
5466
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   238
    private static int hugeCapacity(int minCapacity) {
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   239
        if (minCapacity < 0) // overflow
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   240
            throw new OutOfMemoryError
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   241
                ("Required array size too large");
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   242
        return (minCapacity > MAX_ARRAY_SIZE) ?
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   243
            Integer.MAX_VALUE :
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   244
            MAX_ARRAY_SIZE;
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   245
    }
f130bb07764b 6933217: Huge arrays handled poorly in core libraries
martin
parents: 2
diff changeset
   246
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    // Modification Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p>This implementation always throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <tt>UnsupportedOperationException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws IllegalStateException         {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <p>This implementation iterates over the collection looking for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * specified element.  If it finds the element, it removes the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * from the collection using the iterator's remove method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <p>Note that this implementation throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <tt>UnsupportedOperationException</tt> if the iterator returned by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * collection's iterator method does not implement the <tt>remove</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * method and this collection contains the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public boolean remove(Object o) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   282
        Iterator<E> it = iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (o==null) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   284
            while (it.hasNext()) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   285
                if (it.next()==null) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   286
                    it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        } else {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   291
            while (it.hasNext()) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   292
                if (o.equals(it.next())) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   293
                    it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <p>This implementation iterates over the specified collection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * checking each element returned by the iterator in turn to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * if it's contained in this collection.  If all elements are so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * contained <tt>true</tt> is returned, otherwise <tt>false</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public boolean containsAll(Collection<?> c) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   317
        for (Object e : c)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   318
            if (!contains(e))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <p>This implementation iterates over the specified collection, and adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * each object returned by the iterator to this collection, in turn.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <p>Note that this implementation will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <tt>UnsupportedOperationException</tt> unless <tt>add</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * overridden (assuming the specified collection is non-empty).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @throws IllegalArgumentException      {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws IllegalStateException         {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @see #add(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public boolean addAll(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        boolean modified = false;
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   343
        for (E e : c)
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   344
            if (add(e))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <p>This implementation iterates over this collection, checking each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * element returned by the iterator in turn to see if it's contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * in the specified collection.  If it's so contained, it's removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * this collection with the iterator's <tt>remove</tt> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * <p>Note that this implementation will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <tt>UnsupportedOperationException</tt> if the iterator returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * and this collection contains one or more elements in common with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public boolean removeAll(Collection<?> c) {
17441
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 14342
diff changeset
   371
        Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        boolean modified = false;
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   373
        Iterator<?> it = iterator();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   374
        while (it.hasNext()) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   375
            if (c.contains(it.next())) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   376
                it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <p>This implementation iterates over this collection, checking each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * element returned by the iterator in turn to see if it's contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * in the specified collection.  If it's not so contained, it's removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * from this collection with the iterator's <tt>remove</tt> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <p>Note that this implementation will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <tt>UnsupportedOperationException</tt> if the iterator returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * and this collection contains one or more elements not present in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * specified collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @throws ClassCastException            {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @throws NullPointerException          {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @see #remove(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @see #contains(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    public boolean retainAll(Collection<?> c) {
17441
5ae43433d158 4802647: Throw required NPEs from removeAll()/retainAll()
mduigou
parents: 14342
diff changeset
   405
        Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        boolean modified = false;
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   407
        Iterator<E> it = iterator();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   408
        while (it.hasNext()) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   409
            if (!c.contains(it.next())) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   410
                it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <p>This implementation iterates over this collection, removing each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * element using the <tt>Iterator.remove</tt> operation.  Most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * implementations will probably choose to override this method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * efficiency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * <p>Note that this implementation will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <tt>UnsupportedOperationException</tt> if the iterator returned by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * collection's <tt>iterator</tt> method does not implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <tt>remove</tt> method and this collection is non-empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @throws UnsupportedOperationException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public void clear() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   433
        Iterator<E> it = iterator();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   434
        while (it.hasNext()) {
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   435
            it.next();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   436
            it.remove();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    //  String conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Returns a string representation of this collection.  The string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * representation consists of a list of the collection's elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * order they are returned by its iterator, enclosed in square brackets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * <tt>", "</tt> (comma and space).  Elements are converted to strings as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * by {@link String#valueOf(Object)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @return a string representation of this collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public String toString() {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   454
        Iterator<E> it = iterator();
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   455
        if (! it.hasNext())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return "[]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        for (;;) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   461
            E e = it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            sb.append(e == this ? "(this Collection)" : e);
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   463
            if (! it.hasNext())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                return sb.append(']').toString();
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   465
            sb.append(',').append(' ');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
}