src/java.base/share/classes/java/util/Vector.java
author darcy
Wed, 09 Oct 2019 10:17:50 -0700
changeset 58520 e036ee8bae56
parent 57956 e0b8b019d2f5
child 58679 9c3209ff7550
permissions -rw-r--r--
8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes Reviewed-by: rriggs, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49774
diff changeset
     2
 * Copyright (c) 1994, 2019, 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
49774
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
    28
import java.io.IOException;
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
    29
import java.io.ObjectInputStream;
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
    30
import java.io.StreamCorruptedException;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
    31
import java.util.function.Consumer;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
    32
import java.util.function.Predicate;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
    33
import java.util.function.UnaryOperator;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
    34
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49774
diff changeset
    35
import jdk.internal.util.ArraysSupport;
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49774
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The {@code Vector} class implements a growable array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * objects. Like an array, it contains components that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * accessed using an integer index. However, the size of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * {@code Vector} can grow or shrink as needed to accommodate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * adding and removing items after the {@code Vector} has been created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>Each vector tries to optimize storage management by maintaining a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * {@code capacity} and a {@code capacityIncrement}. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * {@code capacity} is always at least as large as the vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * size; it is usually larger because as components are added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * vector, the vector's storage increases in chunks the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * {@code capacityIncrement}. An application can increase the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * capacity of a vector before inserting a large number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * components; this reduces the amount of incremental reallocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 22078
diff changeset
    53
 * <p id="fail-fast">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * The iterators returned by this class's {@link #iterator() iterator} and
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 22078
diff changeset
    55
 * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * if the vector is structurally modified at any time after the iterator is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * created, in any way except through the iterator's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * {@link ListIterator#remove() remove} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * {@link ListIterator#add(Object) add} methods, the iterator will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * {@link ConcurrentModificationException}.  Thus, in the face of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * concurrent modification, the iterator fails quickly and cleanly, rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * than risking arbitrary, non-deterministic behavior at an undetermined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * time in the future.  The {@link Enumeration Enumerations} returned by
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
    64
 * the {@link #elements() elements} method are <em>not</em> fail-fast; if the
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
    65
 * Vector is structurally modified at any time after the enumeration is
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
    66
 * created then the results of enumerating are undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * throw {@code ConcurrentModificationException} on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * exception for its correctness:  <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>As of the Java 2 platform v1.2, this class was retrofitted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * implement the {@link List} interface, making it a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48541
diff changeset
    78
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
64
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 2
diff changeset
    79
 * Java Collections Framework</a>.  Unlike the new collection
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 2
diff changeset
    80
 * implementations, {@code Vector} is synchronized.  If a thread-safe
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 2
diff changeset
    81
 * implementation is not needed, it is recommended to use {@link
3244b8bab101 6583872: (coll) Direct uninformed users away from Vector/Hashtable
martin
parents: 2
diff changeset
    82
 * ArrayList} in place of {@code Vector}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
    84
 * @param <E> Type of component elements
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
    85
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @author  Lee Boynton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @author  Jonathan Payne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * @see Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @see LinkedList
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24255
diff changeset
    90
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
public class Vector<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    extends AbstractList<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The array buffer into which the components of the vector are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * stored. The capacity of the vector is the length of this array buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * and is at least large enough to contain all the vector's elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <p>Any array elements following the last element in the Vector are null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
58520
e036ee8bae56 8231202: Suppress warnings on non-serializable non-transient instance fields in serializable classes
darcy
parents: 57956
diff changeset
   105
    @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected Object[] elementData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * The number of valid components in this {@code Vector} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Components {@code elementData[0]} through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * {@code elementData[elementCount-1]} are the actual items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected int elementCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The amount by which the capacity of the vector is automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * incremented when its size becomes greater than its capacity.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * the capacity increment is less than or equal to zero, the capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * of the vector is doubled each time it needs to grow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected int capacityIncrement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55036
diff changeset
   128
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static final long serialVersionUID = -2767605614048989439L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Constructs an empty vector with the specified initial capacity and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * capacity increment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param   initialCapacity     the initial capacity of the vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param   capacityIncrement   the amount by which the capacity is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *                              increased when the vector overflows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws IllegalArgumentException if the specified initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *         is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public Vector(int initialCapacity, int capacityIncrement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new IllegalArgumentException("Illegal Capacity: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.elementData = new Object[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        this.capacityIncrement = capacityIncrement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Constructs an empty vector with the specified initial capacity and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * with its capacity increment equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param   initialCapacity   the initial capacity of the vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws IllegalArgumentException if the specified initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *         is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public Vector(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this(initialCapacity, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Constructs an empty vector so that its internal data array
45985
dcdb2e744a54 8177549: Typo in Vector.java
rriggs
parents: 45984
diff changeset
   164
     * has size {@code 10} and its standard capacity increment is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public Vector() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Constructs a vector containing the elements of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * collection, in the order they are returned by the collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param c the collection whose elements are to be placed into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *       vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public Vector(Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        elementData = c.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        elementCount = elementData.length;
31540
6efd719b3330 6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents: 25859
diff changeset
   184
        // defend against c.toArray (incorrectly) not returning Object[]
6efd719b3330 6260652: (coll) Arrays.asList(x).toArray().getClass() should be Object[].class
martin
parents: 25859
diff changeset
   185
        // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (elementData.getClass() != Object[].class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Copies the components of this vector into the specified array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * The item at index {@code k} in this vector is copied into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * component {@code k} of {@code anArray}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param  anArray the array into which the components get copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @throws NullPointerException if the given array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @throws IndexOutOfBoundsException if the specified array is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *         large enough to hold all the components of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @throws ArrayStoreException if a component of this vector is not of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *         a runtime type that can be stored in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @see #toArray(Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public synchronized void copyInto(Object[] anArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        System.arraycopy(elementData, 0, anArray, 0, elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Trims the capacity of this vector to be the vector's current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * size. If the capacity of this vector is larger than its current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * size, then the capacity is changed to equal the size by replacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * its internal data array, kept in the field {@code elementData},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * with a smaller one. An application can use this operation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * minimize the storage of a vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public synchronized void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int oldCapacity = elementData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (elementCount < oldCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            elementData = Arrays.copyOf(elementData, elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Increases the capacity of this vector, if necessary, to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * that it can hold at least the number of components specified by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * the minimum capacity argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <p>If the current capacity of this vector is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * {@code minCapacity}, then its capacity is increased by replacing its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * internal data array, kept in the field {@code elementData}, with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * larger one.  The size of the new data array will be the old size plus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * {@code capacityIncrement}, unless the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * {@code capacityIncrement} is less than or equal to zero, in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * the new capacity will be twice the old capacity; but if this new size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * is still smaller than {@code minCapacity}, then the new capacity will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * be {@code minCapacity}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param minCapacity the desired minimum capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public synchronized void ensureCapacity(int minCapacity) {
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   241
        if (minCapacity > 0) {
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   242
            modCount++;
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   243
            if (minCapacity > elementData.length)
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   244
                grow(minCapacity);
7020
25638687fe82 6992121: StringBuilder.ensureCapacity(int minCap) throws OutOfMemoryError with minCap=Integer.MIN_VALUE
mchung
parents: 5506
diff changeset
   245
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   249
     * Increases the capacity to ensure that it can hold at least the
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   250
     * number of elements specified by the minimum capacity argument.
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   251
     *
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   252
     * @param minCapacity the desired minimum capacity
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   253
     * @throws OutOfMemoryError if minCapacity is less than zero
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   254
     */
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   255
    private Object[] grow(int minCapacity) {
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49774
diff changeset
   256
        int oldCapacity = elementData.length;
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49774
diff changeset
   257
        int newCapacity = ArraysSupport.newLength(oldCapacity,
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49774
diff changeset
   258
                minCapacity - oldCapacity, /* minimum growth */
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49774
diff changeset
   259
                capacityIncrement > 0 ? capacityIncrement : oldCapacity
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49774
diff changeset
   260
                                           /* preferred growth */);
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 49774
diff changeset
   261
        return elementData = Arrays.copyOf(elementData, newCapacity);
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   262
    }
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   263
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   264
    private Object[] grow() {
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   265
        return grow(elementCount + 1);
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   266
    }
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   267
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   268
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Sets the size of this vector. If the new size is greater than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * current size, new {@code null} items are added to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * the vector. If the new size is less than the current size, all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * components at index {@code newSize} and greater are discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param  newSize   the new size of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @throws ArrayIndexOutOfBoundsException if the new size is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public synchronized void setSize(int newSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        modCount++;
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   279
        if (newSize > elementData.length)
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   280
            grow(newSize);
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   281
        final Object[] es = elementData;
43520
65d31ea930aa 8173706: Is able to set a negative j.u.Vector size in JDK9 b151
dl
parents: 42927
diff changeset
   282
        for (int to = elementCount, i = newSize; i < to; i++)
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   283
            es[i] = null;
43520
65d31ea930aa 8173706: Is able to set a negative j.u.Vector size in JDK9 b151
dl
parents: 42927
diff changeset
   284
        elementCount = newSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Returns the current capacity of this vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return  the current capacity (the length of its internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *          data array, kept in the field {@code elementData}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *          of this vector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public synchronized int capacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return elementData.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns the number of components in this vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @return  the number of components in this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public synchronized int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return elementCount;
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
     * Tests if this vector has no components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @return  {@code true} if and only if this vector has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *          no components, that is, its size is zero;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *          {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public synchronized boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return elementCount == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Returns an enumeration of the components of this vector. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * returned {@code Enumeration} object will generate all items in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * this vector. The first item generated is the item at index {@code 0},
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   322
     * then the item at index {@code 1}, and so on. If the vector is
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   323
     * structurally modified while enumerating over the elements then the
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   324
     * results of enumerating are undefined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @return  an enumeration of the components of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @see     Iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public Enumeration<E> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return new Enumeration<E>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return count < elementCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            public E nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    if (count < elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        return elementData(count++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                throw new NoSuchElementException("Vector Enumeration");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Returns {@code true} if this vector contains the specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * More formally, returns {@code true} if and only if this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * contains at least one element {@code e} such that
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31540
diff changeset
   352
     * {@code Objects.equals(o, e)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param o element whose presence in this vector is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @return {@code true} if this vector contains the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return indexOf(o, 0) >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Returns the index of the first occurrence of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * in this vector, or -1 if this vector does not contain the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * More formally, returns the lowest index {@code i} such that
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31540
diff changeset
   365
     * {@code Objects.equals(o, get(i))},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @return the index of the first occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *         this vector, or -1 if this vector does not contain the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public int indexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        return indexOf(o, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Returns the index of the first occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * this vector, searching forwards from {@code index}, or returns -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * More formally, returns the lowest index {@code i} such that
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31540
diff changeset
   381
     * {@code (i >= index && Objects.equals(o, get(i)))},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param index index to start searching from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @return the index of the first occurrence of the element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *         this vector at position {@code index} or later in the vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *         {@code -1} if the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @see     Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public synchronized int indexOf(Object o, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            for (int i = index ; i < elementCount ; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                if (elementData[i]==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            for (int i = index ; i < elementCount ; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                if (o.equals(elementData[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Returns the index of the last occurrence of the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * in this vector, or -1 if this vector does not contain the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * More formally, returns the highest index {@code i} such that
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31540
diff changeset
   409
     * {@code Objects.equals(o, get(i))},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @return the index of the last occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *         this vector, or -1 if this vector does not contain the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public synchronized int lastIndexOf(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return lastIndexOf(o, elementCount-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Returns the index of the last occurrence of the specified element in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * this vector, searching backwards from {@code index}, or returns -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * More formally, returns the highest index {@code i} such that
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31540
diff changeset
   425
     * {@code (i <= index && Objects.equals(o, get(i)))},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * or -1 if there is no such index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param o element to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param index index to start searching backwards from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @return the index of the last occurrence of the element at position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *         less than or equal to {@code index} in this vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *         -1 if the element is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @throws IndexOutOfBoundsException if the specified index is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *         than or equal to the current size of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public synchronized int lastIndexOf(Object o, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (index >= elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            for (int i = index; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                if (elementData[i]==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            for (int i = index; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if (o.equals(elementData[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Returns the component at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * <p>This method is identical in functionality to the {@link #get(int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * method (which is part of the {@link List} interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @param      index   an index into this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @return     the component at the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *         ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public synchronized E elementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (index >= elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return elementData(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Returns the first component (the item at index {@code 0}) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * this vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @return     the first component of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @throws NoSuchElementException if this vector has no components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public synchronized E firstElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (elementCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return elementData(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Returns the last component of the vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return  the last component of the vector, i.e., the component at index
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   489
     *          {@code size() - 1}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @throws NoSuchElementException if this vector is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public synchronized E lastElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (elementCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        return elementData(elementCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * Sets the component at the specified {@code index} of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * vector to be the specified object. The previous component at that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * position is discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * <p>The index must be a value greater than or equal to {@code 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * and less than the current size of the vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * <p>This method is identical in functionality to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * {@link #set(int, Object) set(int, E)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * method (which is part of the {@link List} interface). Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * {@code set} method reverses the order of the parameters, to more closely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * match array usage.  Note also that the {@code set} method returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * old value that was stored at the specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @param      obj     what the component is to be set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @param      index   the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *         ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public synchronized void setElementAt(E obj, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (index >= elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                                     elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        elementData[index] = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Deletes the component at the specified index. Each component in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * this vector with an index greater or equal to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * {@code index} is shifted downward to have an index one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * smaller than the value it had previously. The size of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * is decreased by {@code 1}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <p>The index must be a value greater than or equal to {@code 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * and less than the current size of the vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * <p>This method is identical in functionality to the {@link #remove(int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * method (which is part of the {@link List} interface).  Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * {@code remove} method returns the old value that was stored at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * specified position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @param      index   the index of the object to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *         ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public synchronized void removeElementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (index >= elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                                     elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        else if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        int j = elementCount - index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        if (j > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            System.arraycopy(elementData, index + 1, elementData, index, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   558
        modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        elementCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        elementData[elementCount] = null; /* to let gc do its work */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * Inserts the specified object as a component in this vector at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * specified {@code index}. Each component in this vector with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * an index greater or equal to the specified {@code index} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * shifted upward to have an index one greater than the value it had
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * previously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * <p>The index must be a value greater than or equal to {@code 0}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * and less than or equal to the current size of the vector. (If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * index is equal to the current size of the vector, the new element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * is appended to the Vector.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <p>This method is identical in functionality to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * {@link #add(int, Object) add(int, E)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * method (which is part of the {@link List} interface).  Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * {@code add} method reverses the order of the parameters, to more closely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * match array usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param      obj     the component to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param      index   where to insert the new component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *         ({@code index < 0 || index > size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public synchronized void insertElementAt(E obj, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (index > elementCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            throw new ArrayIndexOutOfBoundsException(index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                                                     + " > " + elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   591
        modCount++;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   592
        final int s = elementCount;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   593
        Object[] elementData = this.elementData;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   594
        if (s == elementData.length)
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   595
            elementData = grow();
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   596
        System.arraycopy(elementData, index,
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   597
                         elementData, index + 1,
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   598
                         s - index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        elementData[index] = obj;
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   600
        elementCount = s + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Adds the specified component to the end of this vector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * increasing its size by one. The capacity of this vector is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * increased if its size becomes greater than its capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <p>This method is identical in functionality to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * {@link #add(Object) add(E)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * method (which is part of the {@link List} interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @param   obj   the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    public synchronized void addElement(E obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        modCount++;
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   616
        add(obj, elementData, elementCount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Removes the first (lowest-indexed) occurrence of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * from this vector. If the object is found in this vector, each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * component in the vector with an index greater or equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * object's index is shifted downward to have an index one smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * than the value it had previously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * <p>This method is identical in functionality to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * {@link #remove(Object)} method (which is part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * {@link List} interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @param   obj   the component to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @return  {@code true} if the argument was a component of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *          vector; {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public synchronized boolean removeElement(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        int i = indexOf(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Removes all components from this vector and sets its size to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <p>This method is identical in functionality to the {@link #clear}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * method (which is part of the {@link List} interface).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public synchronized void removeAllElements() {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   651
        final Object[] es = elementData;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   652
        for (int to = elementCount, i = elementCount = 0; i < to; i++)
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   653
            es[i] = null;
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   654
        modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Returns a clone of this vector. The copy will contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * reference to a clone of the internal data array, not a reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * to the original internal data array of this {@code Vector} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @return  a clone of this vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    public synchronized Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            @SuppressWarnings("unchecked")
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   667
            Vector<E> v = (Vector<E>) super.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            v.elementData = Arrays.copyOf(elementData, elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            v.modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            // this shouldn't happen, since we are Cloneable
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9503
diff changeset
   673
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Returns an array containing all of the elements in this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * in the correct order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public synchronized Object[] toArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return Arrays.copyOf(elementData, elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Returns an array containing all of the elements in this Vector in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * correct order; the runtime type of the returned array is that of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * specified array.  If the Vector fits in the specified array, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * returned therein.  Otherwise, a new array is allocated with the runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * type of the specified array and the size of this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * <p>If the Vector fits in the specified array with room to spare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * (i.e., the array has more elements than the Vector),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * the element in the array immediately following the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Vector is set to null.  (This is useful in determining the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * of the Vector <em>only</em> if the caller knows that the Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * does not contain any null elements.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   701
     * @param <T> type of array elements. The same type as {@code <E>} or a
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   702
     * supertype of {@code <E>}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @param a the array into which the elements of the Vector are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *          be stored, if it is big enough; otherwise, a new array of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *          same runtime type is allocated for this purpose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @return an array containing the elements of the Vector
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   707
     * @throws ArrayStoreException if the runtime type of a, {@code <T>}, is not
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   708
     * a supertype of the runtime type, {@code <E>}, of every element in this
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   709
     * Vector
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @throws NullPointerException if the given array is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    public synchronized <T> T[] toArray(T[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        if (a.length < elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        System.arraycopy(elementData, 0, a, 0, elementCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (a.length > elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            a[elementCount] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    // Positional Access Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    E elementData(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        return (E) elementData[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   733
    @SuppressWarnings("unchecked")
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   734
    static <E> E elementAt(Object[] es, int index) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   735
        return (E) es[index];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   736
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   737
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Returns the element at the specified position in this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @param index index of the element to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @return object at the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *            ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    public synchronized E get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        if (index >= elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        return elementData(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * Replaces the element at the specified position in this Vector with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * specified element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @param index index of the element to replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @param element element to be stored at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * @return the element previously at the specified position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *         ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    public synchronized E set(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (index >= elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        E oldValue = elementData(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        elementData[index] = element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   775
     * This helper method split out from add(E) to keep method
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   776
     * bytecode size under 35 (the -XX:MaxInlineSize default value),
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   777
     * which helps when add(E) is called in a C1-compiled loop.
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   778
     */
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   779
    private void add(E e, Object[] elementData, int s) {
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   780
        if (s == elementData.length)
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   781
            elementData = grow();
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   782
        elementData[s] = e;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   783
        elementCount = s + 1;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   784
    }
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   785
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   786
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * Appends the specified element to the end of this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @param e element to be appended to this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @return {@code true} (as specified by {@link Collection#add})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    public synchronized boolean add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        modCount++;
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   795
        add(e, elementData, elementCount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * Removes the first occurrence of the specified element in this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * If the Vector does not contain the element, it is unchanged.  More
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * formally, removes the element with the lowest index i such that
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31540
diff changeset
   803
     * {@code Objects.equals(o, get(i))} (if such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * an element exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @param o element to be removed from this Vector, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @return true if the Vector contained the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        return removeElement(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * Inserts the specified element at the specified position in this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Shifts the element currently at that position (if any) and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * subsequent elements to the right (adds one to their indices).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @param index index at which the specified element is to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @param element element to be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *         ({@code index < 0 || index > size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    public void add(int index, E element) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        insertElementAt(element, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * Removes the element at the specified position in this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * Shifts any subsequent elements to the left (subtracts one from their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * indices).  Returns the element that was removed from the Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   834
     * @param index the index of the element to be removed
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   835
     * @return element that was removed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *         ({@code index < 0 || index >= size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public synchronized E remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (index >= elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        E oldValue = elementData(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        int numMoved = elementCount - index - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (numMoved > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            System.arraycopy(elementData, index+1, elementData, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                             numMoved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        elementData[--elementCount] = null; // Let gc do its work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * Removes all of the elements from this Vector.  The Vector will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * be empty after this call returns (unless it throws an exception).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    // Bulk Operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * Returns true if this Vector contains all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * specified Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @param   c a collection whose elements will be tested for containment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *          in this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * @return true if this Vector contains all of the elements in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *         specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    public synchronized boolean containsAll(Collection<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        return super.containsAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * Appends all of the elements in the specified Collection to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * this Vector, in the order that they are returned by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * Collection's Iterator.  The behavior of this operation is undefined if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * the specified Collection is modified while the operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * (This implies that the behavior of this call is undefined if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * specified Collection is this Vector, and this Vector is nonempty.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @param c elements to be inserted into this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @return {@code true} if this Vector changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     */
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   894
    public boolean addAll(Collection<? extends E> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        Object[] a = c.toArray();
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   896
        modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        int numNew = a.length;
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   898
        if (numNew == 0)
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   899
            return false;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   900
        synchronized (this) {
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   901
            Object[] elementData = this.elementData;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   902
            final int s = elementCount;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   903
            if (numNew > elementData.length - s)
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   904
                elementData = grow(s + numNew);
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   905
            System.arraycopy(a, 0, elementData, s, numNew);
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   906
            elementCount = s + numNew;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
   907
            return true;
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
   908
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * Removes from this Vector all of its elements that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * specified Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @param c a collection of elements to be removed from the Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @return true if this Vector changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @throws ClassCastException if the types of one or more elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     *         in this vector are incompatible with the specified
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9035
diff changeset
   919
     *         collection
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9035
diff changeset
   920
     * (<a href="Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @throws NullPointerException if this vector contains one or more null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *         elements and the specified collection does not support null
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9035
diff changeset
   923
     *         elements
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9035
diff changeset
   924
     * (<a href="Collection.html#optional-restrictions">optional</a>),
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9035
diff changeset
   925
     *         or if the specified collection is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   928
    public boolean removeAll(Collection<?> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   929
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   930
        return bulkRemove(e -> c.contains(e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * Retains only the elements in this Vector that are contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * specified Collection.  In other words, removes from this Vector all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * of its elements that are not contained in the specified Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @param c a collection of elements to be retained in this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     *          (all other elements are removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @return true if this Vector changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * @throws ClassCastException if the types of one or more elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *         in this vector are incompatible with the specified
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9035
diff changeset
   943
     *         collection
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9035
diff changeset
   944
     * (<a href="Collection.html#optional-restrictions">optional</a>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @throws NullPointerException if this vector contains one or more null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *         elements and the specified collection does not support null
9503
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9035
diff changeset
   947
     *         elements
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9035
diff changeset
   948
     *         (<a href="Collection.html#optional-restrictions">optional</a>),
588cf31d584a 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.
mduigou
parents: 9035
diff changeset
   949
     *         or if the specified collection is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   952
    public boolean retainAll(Collection<?> c) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   953
        Objects.requireNonNull(c);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   954
        return bulkRemove(e -> !c.contains(e));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   955
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   956
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   957
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   958
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
   959
     */
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   960
    @Override
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   961
    public boolean removeIf(Predicate<? super E> filter) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   962
        Objects.requireNonNull(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   963
        return bulkRemove(filter);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   964
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   965
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   966
    // A tiny bit set implementation
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   967
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   968
    private static long[] nBits(int n) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   969
        return new long[((n - 1) >> 6) + 1];
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   970
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   971
    private static void setBit(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   972
        bits[i >> 6] |= 1L << i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   973
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   974
    private static boolean isClear(long[] bits, int i) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   975
        return (bits[i >> 6] & (1L << i)) == 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   976
    }
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   977
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   978
    private synchronized boolean bulkRemove(Predicate<? super E> filter) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   979
        int expectedModCount = modCount;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   980
        final Object[] es = elementData;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   981
        final int end = elementCount;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   982
        int i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   983
        // Optimize for initial run of survivors
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   984
        for (i = 0; i < end && !filter.test(elementAt(es, i)); i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   985
            ;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   986
        // Tolerate predicates that reentrantly access the collection for
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   987
        // read (but writers still get CME), so traverse once to find
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   988
        // elements to delete, a second pass to physically expunge.
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   989
        if (i < end) {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   990
            final int beg = i;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   991
            final long[] deathRow = nBits(end - beg);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   992
            deathRow[0] = 1L;   // set bit 0
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   993
            for (i = beg + 1; i < end; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   994
                if (filter.test(elementAt(es, i)))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   995
                    setBit(deathRow, i - beg);
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   996
            if (modCount != expectedModCount)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   997
                throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   998
            modCount++;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
   999
            int w = beg;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1000
            for (i = beg; i < end; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1001
                if (isClear(deathRow, i - beg))
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1002
                    es[w++] = es[i];
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1003
            for (i = elementCount = w; i < end; i++)
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1004
                es[i] = null;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1005
            return true;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1006
        } else {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1007
            if (modCount != expectedModCount)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1008
                throw new ConcurrentModificationException();
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1009
            return false;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1010
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * Inserts all of the elements in the specified Collection into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * Vector at the specified position.  Shifts the element currently at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * that position (if any) and any subsequent elements to the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * (increases their indices).  The new elements will appear in the Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * in the order that they are returned by the specified Collection's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * iterator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * @param index index at which to insert the first element from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *              specified collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * @param c elements to be inserted into this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @return {@code true} if this Vector changed as a result of the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *         ({@code index < 0 || index > size()})
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @throws NullPointerException if the specified collection is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    public synchronized boolean addAll(int index, Collection<? extends E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (index < 0 || index > elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        Object[] a = c.toArray();
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1035
        modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        int numNew = a.length;
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1037
        if (numNew == 0)
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1038
            return false;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1039
        Object[] elementData = this.elementData;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1040
        final int s = elementCount;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1041
        if (numNew > elementData.length - s)
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1042
            elementData = grow(s + numNew);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
35383
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1044
        int numMoved = s - index;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1045
        if (numMoved > 0)
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1046
            System.arraycopy(elementData, index,
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1047
                             elementData, index + numNew,
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1048
                             numMoved);
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1049
        System.arraycopy(a, 0, elementData, index, numNew);
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1050
        elementCount = s + numNew;
61820dd2360e 8148174: NegativeArraySizeException in Vector.grow(int)
martin
parents: 32108
diff changeset
  1051
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * Compares the specified Object with this Vector for equality.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * true if and only if the specified Object is also a List, both Lists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * have the same size, and all corresponding pairs of elements in the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * Lists are <em>equal</em>.  (Two elements {@code e1} and
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31540
diff changeset
  1059
     * {@code e2} are <em>equal</em> if {@code Objects.equals(e1, e2)}.)
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 31540
diff changeset
  1060
     * In other words, two Lists are defined to be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * equal if they contain the same elements in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @param o the Object to be compared for equality with this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @return true if the specified Object is equal to this Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    public synchronized boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        return super.equals(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * Returns the hash code value for this Vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    public synchronized int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        return super.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * Returns a string representation of this Vector, containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * the String representation of each element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        return super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * Returns a view of the portion of this List between fromIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * inclusive, and toIndex, exclusive.  (If fromIndex and toIndex are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * equal, the returned List is empty.)  The returned List is backed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * List, so changes in the returned List are reflected in this List, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * vice-versa.  The returned List supports all of the optional List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * operations supported by this List.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * <p>This method eliminates the need for explicit range operations (of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * the sort that commonly exist for arrays).  Any operation that expects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * a List can be used as a range operation by operating on a subList view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * instead of a whole List.  For example, the following idiom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * removes a range of elements from a List:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *      list.subList(from, to).clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * Similar idioms may be constructed for indexOf and lastIndexOf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * and all of the algorithms in the Collections class can be applied to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * a subList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * <p>The semantics of the List returned by this method become undefined if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * the backing list (i.e., this List) is <i>structurally modified</i> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * any way other than via the returned List.  (Structural modifications are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * those that change the size of the List, or otherwise perturb it in such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * a fashion that iterations in progress may yield incorrect results.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @param fromIndex low endpoint (inclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @param toIndex high endpoint (exclusive) of the subList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @return a view of the specified range within this List
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @throws IndexOutOfBoundsException if an endpoint index value is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *         {@code (fromIndex < 0 || toIndex > size)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * @throws IllegalArgumentException if the endpoint indices are out of order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     *         {@code (fromIndex > toIndex)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    public synchronized List<E> subList(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        return Collections.synchronizedList(super.subList(fromIndex, toIndex),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                                            this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * Removes from this list all of the elements whose index is between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * Shifts any succeeding elements to the left (reduces their index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * (If {@code toIndex==fromIndex}, this operation has no effect.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    protected synchronized void removeRange(int fromIndex, int toIndex) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1132
        modCount++;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1133
        shiftTailOverGap(elementData, fromIndex, toIndex);
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1134
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1136
    /** Erases the gap from lo to hi, by sliding down following elements. */
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1137
    private void shiftTailOverGap(Object[] es, int lo, int hi) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1138
        System.arraycopy(es, hi, es, lo, elementCount - hi);
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1139
        for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++)
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1140
            es[i] = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    /**
49774
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1144
     * Loads a {@code Vector} instance from a stream
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1145
     * (that is, deserializes it).
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1146
     * This method performs checks to ensure the consistency
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1147
     * of the fields.
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1148
     *
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1149
     * @param in the stream
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1150
     * @throws java.io.IOException if an I/O error occurs
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1151
     * @throws ClassNotFoundException if the stream contains data
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1152
     *         of a non-existing class
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1153
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55036
diff changeset
  1154
    @java.io.Serial
49774
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1155
    private void readObject(ObjectInputStream in)
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1156
            throws IOException, ClassNotFoundException {
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1157
        ObjectInputStream.GetField gfields = in.readFields();
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1158
        int count = gfields.get("elementCount", 0);
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1159
        Object[] data = (Object[])gfields.get("elementData", null);
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1160
        if (count < 0 || data == null || count > data.length) {
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1161
            throw new StreamCorruptedException("Inconsistent vector internals");
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1162
        }
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1163
        elementCount = count;
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1164
        elementData = data.clone();
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1165
    }
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1166
5950773b665c 8189977: Improve permission portability
weijun
parents: 49433
diff changeset
  1167
    /**
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1168
     * Saves the state of the {@code Vector} instance to a stream
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1169
     * (that is, serializes it).
8392
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1170
     * This method performs synchronization to ensure the consistency
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1171
     * of the serialized data.
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1172
     *
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1173
     * @param s the stream
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1174
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55036
diff changeset
  1176
    @java.io.Serial
8392
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1177
    private void writeObject(java.io.ObjectOutputStream s)
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1178
            throws java.io.IOException {
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1179
        final java.io.ObjectOutputStream.PutField fields = s.putFields();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1180
        final Object[] data;
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1181
        synchronized (this) {
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1182
            fields.put("capacityIncrement", capacityIncrement);
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1183
            fields.put("elementCount", elementCount);
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1184
            data = elementData.clone();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1185
        }
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1186
        fields.put("elementData", data);
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents: 7668
diff changeset
  1187
        s.writeFields();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * Returns a list iterator over the elements in this list (in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * sequence), starting at the specified position in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * The specified index indicates the first element that would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * returned by an initial call to {@link ListIterator#next next}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * An initial call to {@link ListIterator#previous previous} would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * return the element with the specified index minus one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * @throws IndexOutOfBoundsException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    public synchronized ListIterator<E> listIterator(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        if (index < 0 || index > elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            throw new IndexOutOfBoundsException("Index: "+index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        return new ListItr(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * Returns a list iterator over the elements in this list (in proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * sequence).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * @see #listIterator(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    public synchronized ListIterator<E> listIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        return new ListItr(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * Returns an iterator over the elements in this list in proper sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * @return an iterator over the elements in this list in proper sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    public synchronized Iterator<E> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        return new Itr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * An optimized version of AbstractList.Itr
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    private class Itr implements Iterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        int cursor;       // index of next element to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        int lastRet = -1; // index of last element returned; -1 if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        int expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            // Racy but within spec, since modifications are checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            // within or after synchronization in next/previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            return cursor != elementCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        public E next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                if (i >= elementCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                return elementData(lastRet = i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            if (lastRet == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                Vector.this.remove(lastRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            cursor = lastRet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1268
        @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1269
        public void forEachRemaining(Consumer<? super E> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1270
            Objects.requireNonNull(action);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1271
            synchronized (Vector.this) {
17180
f568bc4ece21 8005051: optimized defaults for Iterator.forEachRemaining
akhil
parents: 17168
diff changeset
  1272
                final int size = elementCount;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1273
                int i = cursor;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1274
                if (i >= size) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1275
                    return;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1276
                }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1277
                final Object[] es = elementData;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1278
                if (i >= es.length)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1279
                    throw new ConcurrentModificationException();
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1280
                while (i < size && modCount == expectedModCount)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1281
                    action.accept(elementAt(es, i++));
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1282
                // update once at end of iteration to reduce heap write traffic
17432
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents: 17180
diff changeset
  1283
                cursor = i;
efdf6eb85a17 8013150: Iterator.remove and forEachRemaining relationship not specified
mduigou
parents: 17180
diff changeset
  1284
                lastRet = i - 1;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1285
                checkForComodification();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1286
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1287
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1288
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        final void checkForComodification() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * An optimized version of AbstractList.ListItr
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    final class ListItr extends Itr implements ListIterator<E> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        ListItr(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            cursor = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        public boolean hasPrevious() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            return cursor != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        public int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        public int previousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            return cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        public E previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                int i = cursor - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                cursor = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                return elementData(lastRet = i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        public void set(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            if (lastRet == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                Vector.this.set(lastRet, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        public void add(E e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            synchronized (Vector.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                checkForComodification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                Vector.this.add(i, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            cursor = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            lastRet = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
    }
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1347
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1348
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1349
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1350
     */
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1351
    @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1352
    public synchronized void forEach(Consumer<? super E> action) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1353
        Objects.requireNonNull(action);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1354
        final int expectedModCount = modCount;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1355
        final Object[] es = elementData;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1356
        final int size = elementCount;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1357
        for (int i = 0; modCount == expectedModCount && i < size; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1358
            action.accept(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1359
        if (modCount != expectedModCount)
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1360
            throw new ConcurrentModificationException();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1361
    }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1362
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1363
    /**
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1364
     * @throws NullPointerException {@inheritDoc}
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1365
     */
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1366
    @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1367
    public synchronized void replaceAll(UnaryOperator<E> operator) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1368
        Objects.requireNonNull(operator);
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1369
        final int expectedModCount = modCount;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1370
        final Object[] es = elementData;
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1371
        final int size = elementCount;
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1372
        for (int i = 0; modCount == expectedModCount && i < size; i++)
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1373
            es[i] = operator.apply(elementAt(es, i));
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1374
        if (modCount != expectedModCount)
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1375
            throw new ConcurrentModificationException();
55036
265b110fc022 8223245: Miscellaneous changes imported from jsr166 CVS 2019-06
dl
parents: 54971
diff changeset
  1376
        // TODO(8203662): remove increment of modCount from ...
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1377
        modCount++;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1378
    }
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1379
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 19074
diff changeset
  1380
    @SuppressWarnings("unchecked")
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1381
    @Override
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1382
    public synchronized void sort(Comparator<? super E> c) {
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1383
        final int expectedModCount = modCount;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1384
        Arrays.sort((E[]) elementData, 0, elementCount, c);
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1385
        if (modCount != expectedModCount)
17166
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1386
            throw new ConcurrentModificationException();
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1387
        modCount++;
c83a0fa44906 8001647: default methods for Collections - forEach, removeIf, replaceAll, sort
akhil
parents: 10419
diff changeset
  1388
    }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1389
19435
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1390
    /**
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1391
     * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1392
     * and <em>fail-fast</em> {@link Spliterator} over the elements in this
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1393
     * list.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1394
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1395
     * <p>The {@code Spliterator} reports {@link Spliterator#SIZED},
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1396
     * {@link Spliterator#SUBSIZED}, and {@link Spliterator#ORDERED}.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1397
     * Overriding implementations should document the reporting of additional
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1398
     * characteristic values.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1399
     *
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1400
     * @return a {@code Spliterator} over the elements in this list
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1401
     * @since 1.8
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 19208
diff changeset
  1402
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1403
    @Override
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1404
    public Spliterator<E> spliterator() {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1405
        return new VectorSpliterator(null, 0, -1, 0);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1406
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1407
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1408
    /** Similar to ArrayList Spliterator */
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1409
    final class VectorSpliterator implements Spliterator<E> {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1410
        private Object[] array;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1411
        private int index; // current index, modified on advance/split
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1412
        private int fence; // -1 until used; then one past last index
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1413
        private int expectedModCount; // initialized when fence set
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1414
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1415
        /** Creates new spliterator covering the given range. */
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1416
        VectorSpliterator(Object[] array, int origin, int fence,
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1417
                          int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1418
            this.array = array;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1419
            this.index = origin;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1420
            this.fence = fence;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1421
            this.expectedModCount = expectedModCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1422
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1423
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1424
        private int getFence() { // initialize on first use
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1425
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1426
            if ((hi = fence) < 0) {
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1427
                synchronized (Vector.this) {
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1428
                    array = elementData;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1429
                    expectedModCount = modCount;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1430
                    hi = fence = elementCount;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1431
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1432
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1433
            return hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1434
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1435
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1436
        public Spliterator<E> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1437
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1438
            return (lo >= mid) ? null :
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1439
                new VectorSpliterator(array, lo, index = mid, expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1440
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1441
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1442
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1443
        public boolean tryAdvance(Consumer<? super E> action) {
43520
65d31ea930aa 8173706: Is able to set a negative j.u.Vector size in JDK9 b151
dl
parents: 42927
diff changeset
  1444
            Objects.requireNonNull(action);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1445
            int i;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1446
            if (getFence() > (i = index)) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1447
                index = i + 1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1448
                action.accept((E)array[i]);
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1449
                if (modCount != expectedModCount)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1450
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1451
                return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1452
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1453
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1454
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1455
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1456
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1457
        public void forEachRemaining(Consumer<? super E> action) {
43520
65d31ea930aa 8173706: Is able to set a negative j.u.Vector size in JDK9 b151
dl
parents: 42927
diff changeset
  1458
            Objects.requireNonNull(action);
42927
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1459
            final int hi = getFence();
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1460
            final Object[] a = array;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1461
            int i;
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1462
            for (i = index, index = hi; i < hi; i++)
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1463
                action.accept((E) a[i]);
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1464
            if (modCount != expectedModCount)
1d31e540bfcb 8170484: Miscellaneous changes imported from jsr166 CVS 2016-12
dl
parents: 42319
diff changeset
  1465
                throw new ConcurrentModificationException();
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1466
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1467
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1468
        public long estimateSize() {
24255
91f5e4399160 8020860: cluster Hashtable/Vector field updates for better transactional memory behaviour
mduigou
parents: 24196
diff changeset
  1469
            return getFence() - index;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1470
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1471
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1472
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1473
            return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1474
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 17166
diff changeset
  1475
    }
42319
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1476
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1477
    void checkInvariants() {
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1478
        // assert elementCount >= 0;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1479
        // assert elementCount == elementData.length || elementData[elementCount] == null;
0193886267c3 8143577: optimize ArrayList.removeIf
dl
parents: 35383
diff changeset
  1480
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
}