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