emul/compact/src/main/java/java/util/Vector.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 28 Jan 2013 13:52:28 +0100
changeset 599 d0f57d3ea898
parent 597 ee8a922f4268
child 636 8d0be6a9a809
permissions -rw-r--r--
More java classes requested by FX guys
jaroslav@597
     1
/*
jaroslav@597
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
jaroslav@597
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@597
     4
 *
jaroslav@597
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@597
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@597
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@597
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@597
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@597
    10
 *
jaroslav@597
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@597
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@597
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@597
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@597
    15
 * accompanied this code).
jaroslav@597
    16
 *
jaroslav@597
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@597
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@597
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@597
    20
 *
jaroslav@597
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@597
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@597
    23
 * questions.
jaroslav@597
    24
 */
jaroslav@597
    25
jaroslav@597
    26
package java.util;
jaroslav@597
    27
jaroslav@599
    28
import org.apidesign.bck2brwsr.emul.lang.System;
jaroslav@599
    29
jaroslav@597
    30
/**
jaroslav@597
    31
 * The {@code Vector} class implements a growable array of
jaroslav@597
    32
 * objects. Like an array, it contains components that can be
jaroslav@597
    33
 * accessed using an integer index. However, the size of a
jaroslav@597
    34
 * {@code Vector} can grow or shrink as needed to accommodate
jaroslav@597
    35
 * adding and removing items after the {@code Vector} has been created.
jaroslav@597
    36
 *
jaroslav@597
    37
 * <p>Each vector tries to optimize storage management by maintaining a
jaroslav@597
    38
 * {@code capacity} and a {@code capacityIncrement}. The
jaroslav@597
    39
 * {@code capacity} is always at least as large as the vector
jaroslav@597
    40
 * size; it is usually larger because as components are added to the
jaroslav@597
    41
 * vector, the vector's storage increases in chunks the size of
jaroslav@597
    42
 * {@code capacityIncrement}. An application can increase the
jaroslav@597
    43
 * capacity of a vector before inserting a large number of
jaroslav@597
    44
 * components; this reduces the amount of incremental reallocation.
jaroslav@597
    45
 *
jaroslav@597
    46
 * <p><a name="fail-fast"/>
jaroslav@597
    47
 * The iterators returned by this class's {@link #iterator() iterator} and
jaroslav@597
    48
 * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
jaroslav@597
    49
 * if the vector is structurally modified at any time after the iterator is
jaroslav@597
    50
 * created, in any way except through the iterator's own
jaroslav@597
    51
 * {@link ListIterator#remove() remove} or
jaroslav@597
    52
 * {@link ListIterator#add(Object) add} methods, the iterator will throw a
jaroslav@597
    53
 * {@link ConcurrentModificationException}.  Thus, in the face of
jaroslav@597
    54
 * concurrent modification, the iterator fails quickly and cleanly, rather
jaroslav@597
    55
 * than risking arbitrary, non-deterministic behavior at an undetermined
jaroslav@597
    56
 * time in the future.  The {@link Enumeration Enumerations} returned by
jaroslav@597
    57
 * the {@link #elements() elements} method are <em>not</em> fail-fast.
jaroslav@597
    58
 *
jaroslav@597
    59
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
jaroslav@597
    60
 * as it is, generally speaking, impossible to make any hard guarantees in the
jaroslav@597
    61
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
jaroslav@597
    62
 * throw {@code ConcurrentModificationException} on a best-effort basis.
jaroslav@597
    63
 * Therefore, it would be wrong to write a program that depended on this
jaroslav@597
    64
 * exception for its correctness:  <i>the fail-fast behavior of iterators
jaroslav@597
    65
 * should be used only to detect bugs.</i>
jaroslav@597
    66
 *
jaroslav@597
    67
 * <p>As of the Java 2 platform v1.2, this class was retrofitted to
jaroslav@597
    68
 * implement the {@link List} interface, making it a member of the
jaroslav@597
    69
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@597
    70
 * Java Collections Framework</a>.  Unlike the new collection
jaroslav@597
    71
 * implementations, {@code Vector} is synchronized.  If a thread-safe
jaroslav@597
    72
 * implementation is not needed, it is recommended to use {@link
jaroslav@597
    73
 * ArrayList} in place of {@code Vector}.
jaroslav@597
    74
 *
jaroslav@597
    75
 * @author  Lee Boynton
jaroslav@597
    76
 * @author  Jonathan Payne
jaroslav@597
    77
 * @see Collection
jaroslav@597
    78
 * @see LinkedList
jaroslav@597
    79
 * @since   JDK1.0
jaroslav@597
    80
 */
jaroslav@597
    81
public class Vector<E>
jaroslav@597
    82
    extends AbstractList<E>
jaroslav@597
    83
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
jaroslav@597
    84
{
jaroslav@597
    85
    /**
jaroslav@597
    86
     * The array buffer into which the components of the vector are
jaroslav@597
    87
     * stored. The capacity of the vector is the length of this array buffer,
jaroslav@597
    88
     * and is at least large enough to contain all the vector's elements.
jaroslav@597
    89
     *
jaroslav@597
    90
     * <p>Any array elements following the last element in the Vector are null.
jaroslav@597
    91
     *
jaroslav@597
    92
     * @serial
jaroslav@597
    93
     */
jaroslav@597
    94
    protected Object[] elementData;
jaroslav@597
    95
jaroslav@597
    96
    /**
jaroslav@597
    97
     * The number of valid components in this {@code Vector} object.
jaroslav@597
    98
     * Components {@code elementData[0]} through
jaroslav@597
    99
     * {@code elementData[elementCount-1]} are the actual items.
jaroslav@597
   100
     *
jaroslav@597
   101
     * @serial
jaroslav@597
   102
     */
jaroslav@597
   103
    protected int elementCount;
jaroslav@597
   104
jaroslav@597
   105
    /**
jaroslav@597
   106
     * The amount by which the capacity of the vector is automatically
jaroslav@597
   107
     * incremented when its size becomes greater than its capacity.  If
jaroslav@597
   108
     * the capacity increment is less than or equal to zero, the capacity
jaroslav@597
   109
     * of the vector is doubled each time it needs to grow.
jaroslav@597
   110
     *
jaroslav@597
   111
     * @serial
jaroslav@597
   112
     */
jaroslav@597
   113
    protected int capacityIncrement;
jaroslav@597
   114
jaroslav@597
   115
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
jaroslav@597
   116
    private static final long serialVersionUID = -2767605614048989439L;
jaroslav@597
   117
jaroslav@597
   118
    /**
jaroslav@597
   119
     * Constructs an empty vector with the specified initial capacity and
jaroslav@597
   120
     * capacity increment.
jaroslav@597
   121
     *
jaroslav@597
   122
     * @param   initialCapacity     the initial capacity of the vector
jaroslav@597
   123
     * @param   capacityIncrement   the amount by which the capacity is
jaroslav@597
   124
     *                              increased when the vector overflows
jaroslav@597
   125
     * @throws IllegalArgumentException if the specified initial capacity
jaroslav@597
   126
     *         is negative
jaroslav@597
   127
     */
jaroslav@597
   128
    public Vector(int initialCapacity, int capacityIncrement) {
jaroslav@597
   129
        super();
jaroslav@597
   130
        if (initialCapacity < 0)
jaroslav@597
   131
            throw new IllegalArgumentException("Illegal Capacity: "+
jaroslav@597
   132
                                               initialCapacity);
jaroslav@597
   133
        this.elementData = new Object[initialCapacity];
jaroslav@597
   134
        this.capacityIncrement = capacityIncrement;
jaroslav@597
   135
    }
jaroslav@597
   136
jaroslav@597
   137
    /**
jaroslav@597
   138
     * Constructs an empty vector with the specified initial capacity and
jaroslav@597
   139
     * with its capacity increment equal to zero.
jaroslav@597
   140
     *
jaroslav@597
   141
     * @param   initialCapacity   the initial capacity of the vector
jaroslav@597
   142
     * @throws IllegalArgumentException if the specified initial capacity
jaroslav@597
   143
     *         is negative
jaroslav@597
   144
     */
jaroslav@597
   145
    public Vector(int initialCapacity) {
jaroslav@597
   146
        this(initialCapacity, 0);
jaroslav@597
   147
    }
jaroslav@597
   148
jaroslav@597
   149
    /**
jaroslav@597
   150
     * Constructs an empty vector so that its internal data array
jaroslav@597
   151
     * has size {@code 10} and its standard capacity increment is
jaroslav@597
   152
     * zero.
jaroslav@597
   153
     */
jaroslav@597
   154
    public Vector() {
jaroslav@597
   155
        this(10);
jaroslav@597
   156
    }
jaroslav@597
   157
jaroslav@597
   158
    /**
jaroslav@597
   159
     * Constructs a vector containing the elements of the specified
jaroslav@597
   160
     * collection, in the order they are returned by the collection's
jaroslav@597
   161
     * iterator.
jaroslav@597
   162
     *
jaroslav@597
   163
     * @param c the collection whose elements are to be placed into this
jaroslav@597
   164
     *       vector
jaroslav@597
   165
     * @throws NullPointerException if the specified collection is null
jaroslav@597
   166
     * @since   1.2
jaroslav@597
   167
     */
jaroslav@597
   168
    public Vector(Collection<? extends E> c) {
jaroslav@597
   169
        elementData = c.toArray();
jaroslav@597
   170
        elementCount = elementData.length;
jaroslav@597
   171
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
jaroslav@597
   172
        if (elementData.getClass() != Object[].class)
jaroslav@597
   173
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
jaroslav@597
   174
    }
jaroslav@597
   175
jaroslav@597
   176
    /**
jaroslav@597
   177
     * Copies the components of this vector into the specified array.
jaroslav@597
   178
     * The item at index {@code k} in this vector is copied into
jaroslav@597
   179
     * component {@code k} of {@code anArray}.
jaroslav@597
   180
     *
jaroslav@597
   181
     * @param  anArray the array into which the components get copied
jaroslav@597
   182
     * @throws NullPointerException if the given array is null
jaroslav@597
   183
     * @throws IndexOutOfBoundsException if the specified array is not
jaroslav@597
   184
     *         large enough to hold all the components of this vector
jaroslav@597
   185
     * @throws ArrayStoreException if a component of this vector is not of
jaroslav@597
   186
     *         a runtime type that can be stored in the specified array
jaroslav@597
   187
     * @see #toArray(Object[])
jaroslav@597
   188
     */
jaroslav@597
   189
    public synchronized void copyInto(Object[] anArray) {
jaroslav@597
   190
        System.arraycopy(elementData, 0, anArray, 0, elementCount);
jaroslav@597
   191
    }
jaroslav@597
   192
jaroslav@597
   193
    /**
jaroslav@597
   194
     * Trims the capacity of this vector to be the vector's current
jaroslav@597
   195
     * size. If the capacity of this vector is larger than its current
jaroslav@597
   196
     * size, then the capacity is changed to equal the size by replacing
jaroslav@597
   197
     * its internal data array, kept in the field {@code elementData},
jaroslav@597
   198
     * with a smaller one. An application can use this operation to
jaroslav@597
   199
     * minimize the storage of a vector.
jaroslav@597
   200
     */
jaroslav@597
   201
    public synchronized void trimToSize() {
jaroslav@597
   202
        modCount++;
jaroslav@597
   203
        int oldCapacity = elementData.length;
jaroslav@597
   204
        if (elementCount < oldCapacity) {
jaroslav@597
   205
            elementData = Arrays.copyOf(elementData, elementCount);
jaroslav@597
   206
        }
jaroslav@597
   207
    }
jaroslav@597
   208
jaroslav@597
   209
    /**
jaroslav@597
   210
     * Increases the capacity of this vector, if necessary, to ensure
jaroslav@597
   211
     * that it can hold at least the number of components specified by
jaroslav@597
   212
     * the minimum capacity argument.
jaroslav@597
   213
     *
jaroslav@597
   214
     * <p>If the current capacity of this vector is less than
jaroslav@597
   215
     * {@code minCapacity}, then its capacity is increased by replacing its
jaroslav@597
   216
     * internal data array, kept in the field {@code elementData}, with a
jaroslav@597
   217
     * larger one.  The size of the new data array will be the old size plus
jaroslav@597
   218
     * {@code capacityIncrement}, unless the value of
jaroslav@597
   219
     * {@code capacityIncrement} is less than or equal to zero, in which case
jaroslav@597
   220
     * the new capacity will be twice the old capacity; but if this new size
jaroslav@597
   221
     * is still smaller than {@code minCapacity}, then the new capacity will
jaroslav@597
   222
     * be {@code minCapacity}.
jaroslav@597
   223
     *
jaroslav@597
   224
     * @param minCapacity the desired minimum capacity
jaroslav@597
   225
     */
jaroslav@597
   226
    public synchronized void ensureCapacity(int minCapacity) {
jaroslav@597
   227
        if (minCapacity > 0) {
jaroslav@597
   228
            modCount++;
jaroslav@597
   229
            ensureCapacityHelper(minCapacity);
jaroslav@597
   230
        }
jaroslav@597
   231
    }
jaroslav@597
   232
jaroslav@597
   233
    /**
jaroslav@597
   234
     * This implements the unsynchronized semantics of ensureCapacity.
jaroslav@597
   235
     * Synchronized methods in this class can internally call this
jaroslav@597
   236
     * method for ensuring capacity without incurring the cost of an
jaroslav@597
   237
     * extra synchronization.
jaroslav@597
   238
     *
jaroslav@597
   239
     * @see #ensureCapacity(int)
jaroslav@597
   240
     */
jaroslav@597
   241
    private void ensureCapacityHelper(int minCapacity) {
jaroslav@597
   242
        // overflow-conscious code
jaroslav@597
   243
        if (minCapacity - elementData.length > 0)
jaroslav@597
   244
            grow(minCapacity);
jaroslav@597
   245
    }
jaroslav@597
   246
jaroslav@597
   247
    /**
jaroslav@597
   248
     * The maximum size of array to allocate.
jaroslav@597
   249
     * Some VMs reserve some header words in an array.
jaroslav@597
   250
     * Attempts to allocate larger arrays may result in
jaroslav@597
   251
     * OutOfMemoryError: Requested array size exceeds VM limit
jaroslav@597
   252
     */
jaroslav@597
   253
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
jaroslav@597
   254
jaroslav@597
   255
    private void grow(int minCapacity) {
jaroslav@597
   256
        // overflow-conscious code
jaroslav@597
   257
        int oldCapacity = elementData.length;
jaroslav@597
   258
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
jaroslav@597
   259
                                         capacityIncrement : oldCapacity);
jaroslav@597
   260
        if (newCapacity - minCapacity < 0)
jaroslav@597
   261
            newCapacity = minCapacity;
jaroslav@597
   262
        if (newCapacity - MAX_ARRAY_SIZE > 0)
jaroslav@597
   263
            newCapacity = hugeCapacity(minCapacity);
jaroslav@597
   264
        elementData = Arrays.copyOf(elementData, newCapacity);
jaroslav@597
   265
    }
jaroslav@597
   266
jaroslav@597
   267
    private static int hugeCapacity(int minCapacity) {
jaroslav@597
   268
        if (minCapacity < 0) // overflow
jaroslav@597
   269
            throw new OutOfMemoryError();
jaroslav@597
   270
        return (minCapacity > MAX_ARRAY_SIZE) ?
jaroslav@597
   271
            Integer.MAX_VALUE :
jaroslav@597
   272
            MAX_ARRAY_SIZE;
jaroslav@597
   273
    }
jaroslav@597
   274
jaroslav@597
   275
    /**
jaroslav@597
   276
     * Sets the size of this vector. If the new size is greater than the
jaroslav@597
   277
     * current size, new {@code null} items are added to the end of
jaroslav@597
   278
     * the vector. If the new size is less than the current size, all
jaroslav@597
   279
     * components at index {@code newSize} and greater are discarded.
jaroslav@597
   280
     *
jaroslav@597
   281
     * @param  newSize   the new size of this vector
jaroslav@597
   282
     * @throws ArrayIndexOutOfBoundsException if the new size is negative
jaroslav@597
   283
     */
jaroslav@597
   284
    public synchronized void setSize(int newSize) {
jaroslav@597
   285
        modCount++;
jaroslav@597
   286
        if (newSize > elementCount) {
jaroslav@597
   287
            ensureCapacityHelper(newSize);
jaroslav@597
   288
        } else {
jaroslav@597
   289
            for (int i = newSize ; i < elementCount ; i++) {
jaroslav@597
   290
                elementData[i] = null;
jaroslav@597
   291
            }
jaroslav@597
   292
        }
jaroslav@597
   293
        elementCount = newSize;
jaroslav@597
   294
    }
jaroslav@597
   295
jaroslav@597
   296
    /**
jaroslav@597
   297
     * Returns the current capacity of this vector.
jaroslav@597
   298
     *
jaroslav@597
   299
     * @return  the current capacity (the length of its internal
jaroslav@597
   300
     *          data array, kept in the field {@code elementData}
jaroslav@597
   301
     *          of this vector)
jaroslav@597
   302
     */
jaroslav@597
   303
    public synchronized int capacity() {
jaroslav@597
   304
        return elementData.length;
jaroslav@597
   305
    }
jaroslav@597
   306
jaroslav@597
   307
    /**
jaroslav@597
   308
     * Returns the number of components in this vector.
jaroslav@597
   309
     *
jaroslav@597
   310
     * @return  the number of components in this vector
jaroslav@597
   311
     */
jaroslav@597
   312
    public synchronized int size() {
jaroslav@597
   313
        return elementCount;
jaroslav@597
   314
    }
jaroslav@597
   315
jaroslav@597
   316
    /**
jaroslav@597
   317
     * Tests if this vector has no components.
jaroslav@597
   318
     *
jaroslav@597
   319
     * @return  {@code true} if and only if this vector has
jaroslav@597
   320
     *          no components, that is, its size is zero;
jaroslav@597
   321
     *          {@code false} otherwise.
jaroslav@597
   322
     */
jaroslav@597
   323
    public synchronized boolean isEmpty() {
jaroslav@597
   324
        return elementCount == 0;
jaroslav@597
   325
    }
jaroslav@597
   326
jaroslav@597
   327
    /**
jaroslav@597
   328
     * Returns an enumeration of the components of this vector. The
jaroslav@597
   329
     * returned {@code Enumeration} object will generate all items in
jaroslav@597
   330
     * this vector. The first item generated is the item at index {@code 0},
jaroslav@597
   331
     * then the item at index {@code 1}, and so on.
jaroslav@597
   332
     *
jaroslav@597
   333
     * @return  an enumeration of the components of this vector
jaroslav@597
   334
     * @see     Iterator
jaroslav@597
   335
     */
jaroslav@597
   336
    public Enumeration<E> elements() {
jaroslav@597
   337
        return new Enumeration<E>() {
jaroslav@597
   338
            int count = 0;
jaroslav@597
   339
jaroslav@597
   340
            public boolean hasMoreElements() {
jaroslav@597
   341
                return count < elementCount;
jaroslav@597
   342
            }
jaroslav@597
   343
jaroslav@597
   344
            public E nextElement() {
jaroslav@597
   345
                synchronized (Vector.this) {
jaroslav@597
   346
                    if (count < elementCount) {
jaroslav@597
   347
                        return elementData(count++);
jaroslav@597
   348
                    }
jaroslav@597
   349
                }
jaroslav@597
   350
                throw new NoSuchElementException("Vector Enumeration");
jaroslav@597
   351
            }
jaroslav@597
   352
        };
jaroslav@597
   353
    }
jaroslav@597
   354
jaroslav@597
   355
    /**
jaroslav@597
   356
     * Returns {@code true} if this vector contains the specified element.
jaroslav@597
   357
     * More formally, returns {@code true} if and only if this vector
jaroslav@597
   358
     * contains at least one element {@code e} such that
jaroslav@597
   359
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
jaroslav@597
   360
     *
jaroslav@597
   361
     * @param o element whose presence in this vector is to be tested
jaroslav@597
   362
     * @return {@code true} if this vector contains the specified element
jaroslav@597
   363
     */
jaroslav@597
   364
    public boolean contains(Object o) {
jaroslav@597
   365
        return indexOf(o, 0) >= 0;
jaroslav@597
   366
    }
jaroslav@597
   367
jaroslav@597
   368
    /**
jaroslav@597
   369
     * Returns the index of the first occurrence of the specified element
jaroslav@597
   370
     * in this vector, or -1 if this vector does not contain the element.
jaroslav@597
   371
     * More formally, returns the lowest index {@code i} such that
jaroslav@597
   372
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
jaroslav@597
   373
     * or -1 if there is no such index.
jaroslav@597
   374
     *
jaroslav@597
   375
     * @param o element to search for
jaroslav@597
   376
     * @return the index of the first occurrence of the specified element in
jaroslav@597
   377
     *         this vector, or -1 if this vector does not contain the element
jaroslav@597
   378
     */
jaroslav@597
   379
    public int indexOf(Object o) {
jaroslav@597
   380
        return indexOf(o, 0);
jaroslav@597
   381
    }
jaroslav@597
   382
jaroslav@597
   383
    /**
jaroslav@597
   384
     * Returns the index of the first occurrence of the specified element in
jaroslav@597
   385
     * this vector, searching forwards from {@code index}, or returns -1 if
jaroslav@597
   386
     * the element is not found.
jaroslav@597
   387
     * More formally, returns the lowest index {@code i} such that
jaroslav@597
   388
     * <tt>(i&nbsp;&gt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</tt>,
jaroslav@597
   389
     * or -1 if there is no such index.
jaroslav@597
   390
     *
jaroslav@597
   391
     * @param o element to search for
jaroslav@597
   392
     * @param index index to start searching from
jaroslav@597
   393
     * @return the index of the first occurrence of the element in
jaroslav@597
   394
     *         this vector at position {@code index} or later in the vector;
jaroslav@597
   395
     *         {@code -1} if the element is not found.
jaroslav@597
   396
     * @throws IndexOutOfBoundsException if the specified index is negative
jaroslav@597
   397
     * @see     Object#equals(Object)
jaroslav@597
   398
     */
jaroslav@597
   399
    public synchronized int indexOf(Object o, int index) {
jaroslav@597
   400
        if (o == null) {
jaroslav@597
   401
            for (int i = index ; i < elementCount ; i++)
jaroslav@597
   402
                if (elementData[i]==null)
jaroslav@597
   403
                    return i;
jaroslav@597
   404
        } else {
jaroslav@597
   405
            for (int i = index ; i < elementCount ; i++)
jaroslav@597
   406
                if (o.equals(elementData[i]))
jaroslav@597
   407
                    return i;
jaroslav@597
   408
        }
jaroslav@597
   409
        return -1;
jaroslav@597
   410
    }
jaroslav@597
   411
jaroslav@597
   412
    /**
jaroslav@597
   413
     * Returns the index of the last occurrence of the specified element
jaroslav@597
   414
     * in this vector, or -1 if this vector does not contain the element.
jaroslav@597
   415
     * More formally, returns the highest index {@code i} such that
jaroslav@597
   416
     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
jaroslav@597
   417
     * or -1 if there is no such index.
jaroslav@597
   418
     *
jaroslav@597
   419
     * @param o element to search for
jaroslav@597
   420
     * @return the index of the last occurrence of the specified element in
jaroslav@597
   421
     *         this vector, or -1 if this vector does not contain the element
jaroslav@597
   422
     */
jaroslav@597
   423
    public synchronized int lastIndexOf(Object o) {
jaroslav@597
   424
        return lastIndexOf(o, elementCount-1);
jaroslav@597
   425
    }
jaroslav@597
   426
jaroslav@597
   427
    /**
jaroslav@597
   428
     * Returns the index of the last occurrence of the specified element in
jaroslav@597
   429
     * this vector, searching backwards from {@code index}, or returns -1 if
jaroslav@597
   430
     * the element is not found.
jaroslav@597
   431
     * More formally, returns the highest index {@code i} such that
jaroslav@597
   432
     * <tt>(i&nbsp;&lt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</tt>,
jaroslav@597
   433
     * or -1 if there is no such index.
jaroslav@597
   434
     *
jaroslav@597
   435
     * @param o element to search for
jaroslav@597
   436
     * @param index index to start searching backwards from
jaroslav@597
   437
     * @return the index of the last occurrence of the element at position
jaroslav@597
   438
     *         less than or equal to {@code index} in this vector;
jaroslav@597
   439
     *         -1 if the element is not found.
jaroslav@597
   440
     * @throws IndexOutOfBoundsException if the specified index is greater
jaroslav@597
   441
     *         than or equal to the current size of this vector
jaroslav@597
   442
     */
jaroslav@597
   443
    public synchronized int lastIndexOf(Object o, int index) {
jaroslav@597
   444
        if (index >= elementCount)
jaroslav@597
   445
            throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
jaroslav@597
   446
jaroslav@597
   447
        if (o == null) {
jaroslav@597
   448
            for (int i = index; i >= 0; i--)
jaroslav@597
   449
                if (elementData[i]==null)
jaroslav@597
   450
                    return i;
jaroslav@597
   451
        } else {
jaroslav@597
   452
            for (int i = index; i >= 0; i--)
jaroslav@597
   453
                if (o.equals(elementData[i]))
jaroslav@597
   454
                    return i;
jaroslav@597
   455
        }
jaroslav@597
   456
        return -1;
jaroslav@597
   457
    }
jaroslav@597
   458
jaroslav@597
   459
    /**
jaroslav@597
   460
     * Returns the component at the specified index.
jaroslav@597
   461
     *
jaroslav@597
   462
     * <p>This method is identical in functionality to the {@link #get(int)}
jaroslav@597
   463
     * method (which is part of the {@link List} interface).
jaroslav@597
   464
     *
jaroslav@597
   465
     * @param      index   an index into this vector
jaroslav@597
   466
     * @return     the component at the specified index
jaroslav@597
   467
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597
   468
     *         ({@code index < 0 || index >= size()})
jaroslav@597
   469
     */
jaroslav@597
   470
    public synchronized E elementAt(int index) {
jaroslav@597
   471
        if (index >= elementCount) {
jaroslav@597
   472
            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
jaroslav@597
   473
        }
jaroslav@597
   474
jaroslav@597
   475
        return elementData(index);
jaroslav@597
   476
    }
jaroslav@597
   477
jaroslav@597
   478
    /**
jaroslav@597
   479
     * Returns the first component (the item at index {@code 0}) of
jaroslav@597
   480
     * this vector.
jaroslav@597
   481
     *
jaroslav@597
   482
     * @return     the first component of this vector
jaroslav@597
   483
     * @throws NoSuchElementException if this vector has no components
jaroslav@597
   484
     */
jaroslav@597
   485
    public synchronized E firstElement() {
jaroslav@597
   486
        if (elementCount == 0) {
jaroslav@597
   487
            throw new NoSuchElementException();
jaroslav@597
   488
        }
jaroslav@597
   489
        return elementData(0);
jaroslav@597
   490
    }
jaroslav@597
   491
jaroslav@597
   492
    /**
jaroslav@597
   493
     * Returns the last component of the vector.
jaroslav@597
   494
     *
jaroslav@597
   495
     * @return  the last component of the vector, i.e., the component at index
jaroslav@597
   496
     *          <code>size()&nbsp;-&nbsp;1</code>.
jaroslav@597
   497
     * @throws NoSuchElementException if this vector is empty
jaroslav@597
   498
     */
jaroslav@597
   499
    public synchronized E lastElement() {
jaroslav@597
   500
        if (elementCount == 0) {
jaroslav@597
   501
            throw new NoSuchElementException();
jaroslav@597
   502
        }
jaroslav@597
   503
        return elementData(elementCount - 1);
jaroslav@597
   504
    }
jaroslav@597
   505
jaroslav@597
   506
    /**
jaroslav@597
   507
     * Sets the component at the specified {@code index} of this
jaroslav@597
   508
     * vector to be the specified object. The previous component at that
jaroslav@597
   509
     * position is discarded.
jaroslav@597
   510
     *
jaroslav@597
   511
     * <p>The index must be a value greater than or equal to {@code 0}
jaroslav@597
   512
     * and less than the current size of the vector.
jaroslav@597
   513
     *
jaroslav@597
   514
     * <p>This method is identical in functionality to the
jaroslav@597
   515
     * {@link #set(int, Object) set(int, E)}
jaroslav@597
   516
     * method (which is part of the {@link List} interface). Note that the
jaroslav@597
   517
     * {@code set} method reverses the order of the parameters, to more closely
jaroslav@597
   518
     * match array usage.  Note also that the {@code set} method returns the
jaroslav@597
   519
     * old value that was stored at the specified position.
jaroslav@597
   520
     *
jaroslav@597
   521
     * @param      obj     what the component is to be set to
jaroslav@597
   522
     * @param      index   the specified index
jaroslav@597
   523
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597
   524
     *         ({@code index < 0 || index >= size()})
jaroslav@597
   525
     */
jaroslav@597
   526
    public synchronized void setElementAt(E obj, int index) {
jaroslav@597
   527
        if (index >= elementCount) {
jaroslav@597
   528
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
jaroslav@597
   529
                                                     elementCount);
jaroslav@597
   530
        }
jaroslav@597
   531
        elementData[index] = obj;
jaroslav@597
   532
    }
jaroslav@597
   533
jaroslav@597
   534
    /**
jaroslav@597
   535
     * Deletes the component at the specified index. Each component in
jaroslav@597
   536
     * this vector with an index greater or equal to the specified
jaroslav@597
   537
     * {@code index} is shifted downward to have an index one
jaroslav@597
   538
     * smaller than the value it had previously. The size of this vector
jaroslav@597
   539
     * is decreased by {@code 1}.
jaroslav@597
   540
     *
jaroslav@597
   541
     * <p>The index must be a value greater than or equal to {@code 0}
jaroslav@597
   542
     * and less than the current size of the vector.
jaroslav@597
   543
     *
jaroslav@597
   544
     * <p>This method is identical in functionality to the {@link #remove(int)}
jaroslav@597
   545
     * method (which is part of the {@link List} interface).  Note that the
jaroslav@597
   546
     * {@code remove} method returns the old value that was stored at the
jaroslav@597
   547
     * specified position.
jaroslav@597
   548
     *
jaroslav@597
   549
     * @param      index   the index of the object to remove
jaroslav@597
   550
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597
   551
     *         ({@code index < 0 || index >= size()})
jaroslav@597
   552
     */
jaroslav@597
   553
    public synchronized void removeElementAt(int index) {
jaroslav@597
   554
        modCount++;
jaroslav@597
   555
        if (index >= elementCount) {
jaroslav@597
   556
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
jaroslav@597
   557
                                                     elementCount);
jaroslav@597
   558
        }
jaroslav@597
   559
        else if (index < 0) {
jaroslav@597
   560
            throw new ArrayIndexOutOfBoundsException(index);
jaroslav@597
   561
        }
jaroslav@597
   562
        int j = elementCount - index - 1;
jaroslav@597
   563
        if (j > 0) {
jaroslav@597
   564
            System.arraycopy(elementData, index + 1, elementData, index, j);
jaroslav@597
   565
        }
jaroslav@597
   566
        elementCount--;
jaroslav@597
   567
        elementData[elementCount] = null; /* to let gc do its work */
jaroslav@597
   568
    }
jaroslav@597
   569
jaroslav@597
   570
    /**
jaroslav@597
   571
     * Inserts the specified object as a component in this vector at the
jaroslav@597
   572
     * specified {@code index}. Each component in this vector with
jaroslav@597
   573
     * an index greater or equal to the specified {@code index} is
jaroslav@597
   574
     * shifted upward to have an index one greater than the value it had
jaroslav@597
   575
     * previously.
jaroslav@597
   576
     *
jaroslav@597
   577
     * <p>The index must be a value greater than or equal to {@code 0}
jaroslav@597
   578
     * and less than or equal to the current size of the vector. (If the
jaroslav@597
   579
     * index is equal to the current size of the vector, the new element
jaroslav@597
   580
     * is appended to the Vector.)
jaroslav@597
   581
     *
jaroslav@597
   582
     * <p>This method is identical in functionality to the
jaroslav@597
   583
     * {@link #add(int, Object) add(int, E)}
jaroslav@597
   584
     * method (which is part of the {@link List} interface).  Note that the
jaroslav@597
   585
     * {@code add} method reverses the order of the parameters, to more closely
jaroslav@597
   586
     * match array usage.
jaroslav@597
   587
     *
jaroslav@597
   588
     * @param      obj     the component to insert
jaroslav@597
   589
     * @param      index   where to insert the new component
jaroslav@597
   590
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597
   591
     *         ({@code index < 0 || index > size()})
jaroslav@597
   592
     */
jaroslav@597
   593
    public synchronized void insertElementAt(E obj, int index) {
jaroslav@597
   594
        modCount++;
jaroslav@597
   595
        if (index > elementCount) {
jaroslav@597
   596
            throw new ArrayIndexOutOfBoundsException(index
jaroslav@597
   597
                                                     + " > " + elementCount);
jaroslav@597
   598
        }
jaroslav@597
   599
        ensureCapacityHelper(elementCount + 1);
jaroslav@597
   600
        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
jaroslav@597
   601
        elementData[index] = obj;
jaroslav@597
   602
        elementCount++;
jaroslav@597
   603
    }
jaroslav@597
   604
jaroslav@597
   605
    /**
jaroslav@597
   606
     * Adds the specified component to the end of this vector,
jaroslav@597
   607
     * increasing its size by one. The capacity of this vector is
jaroslav@597
   608
     * increased if its size becomes greater than its capacity.
jaroslav@597
   609
     *
jaroslav@597
   610
     * <p>This method is identical in functionality to the
jaroslav@597
   611
     * {@link #add(Object) add(E)}
jaroslav@597
   612
     * method (which is part of the {@link List} interface).
jaroslav@597
   613
     *
jaroslav@597
   614
     * @param   obj   the component to be added
jaroslav@597
   615
     */
jaroslav@597
   616
    public synchronized void addElement(E obj) {
jaroslav@597
   617
        modCount++;
jaroslav@597
   618
        ensureCapacityHelper(elementCount + 1);
jaroslav@597
   619
        elementData[elementCount++] = obj;
jaroslav@597
   620
    }
jaroslav@597
   621
jaroslav@597
   622
    /**
jaroslav@597
   623
     * Removes the first (lowest-indexed) occurrence of the argument
jaroslav@597
   624
     * from this vector. If the object is found in this vector, each
jaroslav@597
   625
     * component in the vector with an index greater or equal to the
jaroslav@597
   626
     * object's index is shifted downward to have an index one smaller
jaroslav@597
   627
     * than the value it had previously.
jaroslav@597
   628
     *
jaroslav@597
   629
     * <p>This method is identical in functionality to the
jaroslav@597
   630
     * {@link #remove(Object)} method (which is part of the
jaroslav@597
   631
     * {@link List} interface).
jaroslav@597
   632
     *
jaroslav@597
   633
     * @param   obj   the component to be removed
jaroslav@597
   634
     * @return  {@code true} if the argument was a component of this
jaroslav@597
   635
     *          vector; {@code false} otherwise.
jaroslav@597
   636
     */
jaroslav@597
   637
    public synchronized boolean removeElement(Object obj) {
jaroslav@597
   638
        modCount++;
jaroslav@597
   639
        int i = indexOf(obj);
jaroslav@597
   640
        if (i >= 0) {
jaroslav@597
   641
            removeElementAt(i);
jaroslav@597
   642
            return true;
jaroslav@597
   643
        }
jaroslav@597
   644
        return false;
jaroslav@597
   645
    }
jaroslav@597
   646
jaroslav@597
   647
    /**
jaroslav@597
   648
     * Removes all components from this vector and sets its size to zero.
jaroslav@597
   649
     *
jaroslav@597
   650
     * <p>This method is identical in functionality to the {@link #clear}
jaroslav@597
   651
     * method (which is part of the {@link List} interface).
jaroslav@597
   652
     */
jaroslav@597
   653
    public synchronized void removeAllElements() {
jaroslav@597
   654
        modCount++;
jaroslav@597
   655
        // Let gc do its work
jaroslav@597
   656
        for (int i = 0; i < elementCount; i++)
jaroslav@597
   657
            elementData[i] = null;
jaroslav@597
   658
jaroslav@597
   659
        elementCount = 0;
jaroslav@597
   660
    }
jaroslav@597
   661
jaroslav@597
   662
    /**
jaroslav@597
   663
     * Returns a clone of this vector. The copy will contain a
jaroslav@597
   664
     * reference to a clone of the internal data array, not a reference
jaroslav@597
   665
     * to the original internal data array of this {@code Vector} object.
jaroslav@597
   666
     *
jaroslav@597
   667
     * @return  a clone of this vector
jaroslav@597
   668
     */
jaroslav@597
   669
    public synchronized Object clone() {
jaroslav@597
   670
        try {
jaroslav@597
   671
            @SuppressWarnings("unchecked")
jaroslav@597
   672
                Vector<E> v = (Vector<E>) super.clone();
jaroslav@597
   673
            v.elementData = Arrays.copyOf(elementData, elementCount);
jaroslav@597
   674
            v.modCount = 0;
jaroslav@597
   675
            return v;
jaroslav@597
   676
        } catch (CloneNotSupportedException e) {
jaroslav@597
   677
            // this shouldn't happen, since we are Cloneable
jaroslav@597
   678
            throw new InternalError();
jaroslav@597
   679
        }
jaroslav@597
   680
    }
jaroslav@597
   681
jaroslav@597
   682
    /**
jaroslav@597
   683
     * Returns an array containing all of the elements in this Vector
jaroslav@597
   684
     * in the correct order.
jaroslav@597
   685
     *
jaroslav@597
   686
     * @since 1.2
jaroslav@597
   687
     */
jaroslav@597
   688
    public synchronized Object[] toArray() {
jaroslav@597
   689
        return Arrays.copyOf(elementData, elementCount);
jaroslav@597
   690
    }
jaroslav@597
   691
jaroslav@597
   692
    /**
jaroslav@597
   693
     * Returns an array containing all of the elements in this Vector in the
jaroslav@597
   694
     * correct order; the runtime type of the returned array is that of the
jaroslav@597
   695
     * specified array.  If the Vector fits in the specified array, it is
jaroslav@597
   696
     * returned therein.  Otherwise, a new array is allocated with the runtime
jaroslav@597
   697
     * type of the specified array and the size of this Vector.
jaroslav@597
   698
     *
jaroslav@597
   699
     * <p>If the Vector fits in the specified array with room to spare
jaroslav@597
   700
     * (i.e., the array has more elements than the Vector),
jaroslav@597
   701
     * the element in the array immediately following the end of the
jaroslav@597
   702
     * Vector is set to null.  (This is useful in determining the length
jaroslav@597
   703
     * of the Vector <em>only</em> if the caller knows that the Vector
jaroslav@597
   704
     * does not contain any null elements.)
jaroslav@597
   705
     *
jaroslav@597
   706
     * @param a the array into which the elements of the Vector are to
jaroslav@597
   707
     *          be stored, if it is big enough; otherwise, a new array of the
jaroslav@597
   708
     *          same runtime type is allocated for this purpose.
jaroslav@597
   709
     * @return an array containing the elements of the Vector
jaroslav@597
   710
     * @throws ArrayStoreException if the runtime type of a is not a supertype
jaroslav@597
   711
     * of the runtime type of every element in this Vector
jaroslav@597
   712
     * @throws NullPointerException if the given array is null
jaroslav@597
   713
     * @since 1.2
jaroslav@597
   714
     */
jaroslav@597
   715
    @SuppressWarnings("unchecked")
jaroslav@597
   716
    public synchronized <T> T[] toArray(T[] a) {
jaroslav@597
   717
        if (a.length < elementCount)
jaroslav@597
   718
            return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
jaroslav@597
   719
jaroslav@597
   720
        System.arraycopy(elementData, 0, a, 0, elementCount);
jaroslav@597
   721
jaroslav@597
   722
        if (a.length > elementCount)
jaroslav@597
   723
            a[elementCount] = null;
jaroslav@597
   724
jaroslav@597
   725
        return a;
jaroslav@597
   726
    }
jaroslav@597
   727
jaroslav@597
   728
    // Positional Access Operations
jaroslav@597
   729
jaroslav@597
   730
    @SuppressWarnings("unchecked")
jaroslav@597
   731
    E elementData(int index) {
jaroslav@597
   732
        return (E) elementData[index];
jaroslav@597
   733
    }
jaroslav@597
   734
jaroslav@597
   735
    /**
jaroslav@597
   736
     * Returns the element at the specified position in this Vector.
jaroslav@597
   737
     *
jaroslav@597
   738
     * @param index index of the element to return
jaroslav@597
   739
     * @return object at the specified index
jaroslav@597
   740
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597
   741
     *            ({@code index < 0 || index >= size()})
jaroslav@597
   742
     * @since 1.2
jaroslav@597
   743
     */
jaroslav@597
   744
    public synchronized E get(int index) {
jaroslav@597
   745
        if (index >= elementCount)
jaroslav@597
   746
            throw new ArrayIndexOutOfBoundsException(index);
jaroslav@597
   747
jaroslav@597
   748
        return elementData(index);
jaroslav@597
   749
    }
jaroslav@597
   750
jaroslav@597
   751
    /**
jaroslav@597
   752
     * Replaces the element at the specified position in this Vector with the
jaroslav@597
   753
     * specified element.
jaroslav@597
   754
     *
jaroslav@597
   755
     * @param index index of the element to replace
jaroslav@597
   756
     * @param element element to be stored at the specified position
jaroslav@597
   757
     * @return the element previously at the specified position
jaroslav@597
   758
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597
   759
     *         ({@code index < 0 || index >= size()})
jaroslav@597
   760
     * @since 1.2
jaroslav@597
   761
     */
jaroslav@597
   762
    public synchronized E set(int index, E element) {
jaroslav@597
   763
        if (index >= elementCount)
jaroslav@597
   764
            throw new ArrayIndexOutOfBoundsException(index);
jaroslav@597
   765
jaroslav@597
   766
        E oldValue = elementData(index);
jaroslav@597
   767
        elementData[index] = element;
jaroslav@597
   768
        return oldValue;
jaroslav@597
   769
    }
jaroslav@597
   770
jaroslav@597
   771
    /**
jaroslav@597
   772
     * Appends the specified element to the end of this Vector.
jaroslav@597
   773
     *
jaroslav@597
   774
     * @param e element to be appended to this Vector
jaroslav@597
   775
     * @return {@code true} (as specified by {@link Collection#add})
jaroslav@597
   776
     * @since 1.2
jaroslav@597
   777
     */
jaroslav@597
   778
    public synchronized boolean add(E e) {
jaroslav@597
   779
        modCount++;
jaroslav@597
   780
        ensureCapacityHelper(elementCount + 1);
jaroslav@597
   781
        elementData[elementCount++] = e;
jaroslav@597
   782
        return true;
jaroslav@597
   783
    }
jaroslav@597
   784
jaroslav@597
   785
    /**
jaroslav@597
   786
     * Removes the first occurrence of the specified element in this Vector
jaroslav@597
   787
     * If the Vector does not contain the element, it is unchanged.  More
jaroslav@597
   788
     * formally, removes the element with the lowest index i such that
jaroslav@597
   789
     * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such
jaroslav@597
   790
     * an element exists).
jaroslav@597
   791
     *
jaroslav@597
   792
     * @param o element to be removed from this Vector, if present
jaroslav@597
   793
     * @return true if the Vector contained the specified element
jaroslav@597
   794
     * @since 1.2
jaroslav@597
   795
     */
jaroslav@597
   796
    public boolean remove(Object o) {
jaroslav@597
   797
        return removeElement(o);
jaroslav@597
   798
    }
jaroslav@597
   799
jaroslav@597
   800
    /**
jaroslav@597
   801
     * Inserts the specified element at the specified position in this Vector.
jaroslav@597
   802
     * Shifts the element currently at that position (if any) and any
jaroslav@597
   803
     * subsequent elements to the right (adds one to their indices).
jaroslav@597
   804
     *
jaroslav@597
   805
     * @param index index at which the specified element is to be inserted
jaroslav@597
   806
     * @param element element to be inserted
jaroslav@597
   807
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597
   808
     *         ({@code index < 0 || index > size()})
jaroslav@597
   809
     * @since 1.2
jaroslav@597
   810
     */
jaroslav@597
   811
    public void add(int index, E element) {
jaroslav@597
   812
        insertElementAt(element, index);
jaroslav@597
   813
    }
jaroslav@597
   814
jaroslav@597
   815
    /**
jaroslav@597
   816
     * Removes the element at the specified position in this Vector.
jaroslav@597
   817
     * Shifts any subsequent elements to the left (subtracts one from their
jaroslav@597
   818
     * indices).  Returns the element that was removed from the Vector.
jaroslav@597
   819
     *
jaroslav@597
   820
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597
   821
     *         ({@code index < 0 || index >= size()})
jaroslav@597
   822
     * @param index the index of the element to be removed
jaroslav@597
   823
     * @return element that was removed
jaroslav@597
   824
     * @since 1.2
jaroslav@597
   825
     */
jaroslav@597
   826
    public synchronized E remove(int index) {
jaroslav@597
   827
        modCount++;
jaroslav@597
   828
        if (index >= elementCount)
jaroslav@597
   829
            throw new ArrayIndexOutOfBoundsException(index);
jaroslav@597
   830
        E oldValue = elementData(index);
jaroslav@597
   831
jaroslav@597
   832
        int numMoved = elementCount - index - 1;
jaroslav@597
   833
        if (numMoved > 0)
jaroslav@597
   834
            System.arraycopy(elementData, index+1, elementData, index,
jaroslav@597
   835
                             numMoved);
jaroslav@597
   836
        elementData[--elementCount] = null; // Let gc do its work
jaroslav@597
   837
jaroslav@597
   838
        return oldValue;
jaroslav@597
   839
    }
jaroslav@597
   840
jaroslav@597
   841
    /**
jaroslav@597
   842
     * Removes all of the elements from this Vector.  The Vector will
jaroslav@597
   843
     * be empty after this call returns (unless it throws an exception).
jaroslav@597
   844
     *
jaroslav@597
   845
     * @since 1.2
jaroslav@597
   846
     */
jaroslav@597
   847
    public void clear() {
jaroslav@597
   848
        removeAllElements();
jaroslav@597
   849
    }
jaroslav@597
   850
jaroslav@597
   851
    // Bulk Operations
jaroslav@597
   852
jaroslav@597
   853
    /**
jaroslav@597
   854
     * Returns true if this Vector contains all of the elements in the
jaroslav@597
   855
     * specified Collection.
jaroslav@597
   856
     *
jaroslav@597
   857
     * @param   c a collection whose elements will be tested for containment
jaroslav@597
   858
     *          in this Vector
jaroslav@597
   859
     * @return true if this Vector contains all of the elements in the
jaroslav@597
   860
     *         specified collection
jaroslav@597
   861
     * @throws NullPointerException if the specified collection is null
jaroslav@597
   862
     */
jaroslav@597
   863
    public synchronized boolean containsAll(Collection<?> c) {
jaroslav@597
   864
        return super.containsAll(c);
jaroslav@597
   865
    }
jaroslav@597
   866
jaroslav@597
   867
    /**
jaroslav@597
   868
     * Appends all of the elements in the specified Collection to the end of
jaroslav@597
   869
     * this Vector, in the order that they are returned by the specified
jaroslav@597
   870
     * Collection's Iterator.  The behavior of this operation is undefined if
jaroslav@597
   871
     * the specified Collection is modified while the operation is in progress.
jaroslav@597
   872
     * (This implies that the behavior of this call is undefined if the
jaroslav@597
   873
     * specified Collection is this Vector, and this Vector is nonempty.)
jaroslav@597
   874
     *
jaroslav@597
   875
     * @param c elements to be inserted into this Vector
jaroslav@597
   876
     * @return {@code true} if this Vector changed as a result of the call
jaroslav@597
   877
     * @throws NullPointerException if the specified collection is null
jaroslav@597
   878
     * @since 1.2
jaroslav@597
   879
     */
jaroslav@597
   880
    public synchronized boolean addAll(Collection<? extends E> c) {
jaroslav@597
   881
        modCount++;
jaroslav@597
   882
        Object[] a = c.toArray();
jaroslav@597
   883
        int numNew = a.length;
jaroslav@597
   884
        ensureCapacityHelper(elementCount + numNew);
jaroslav@597
   885
        System.arraycopy(a, 0, elementData, elementCount, numNew);
jaroslav@597
   886
        elementCount += numNew;
jaroslav@597
   887
        return numNew != 0;
jaroslav@597
   888
    }
jaroslav@597
   889
jaroslav@597
   890
    /**
jaroslav@597
   891
     * Removes from this Vector all of its elements that are contained in the
jaroslav@597
   892
     * specified Collection.
jaroslav@597
   893
     *
jaroslav@597
   894
     * @param c a collection of elements to be removed from the Vector
jaroslav@597
   895
     * @return true if this Vector changed as a result of the call
jaroslav@597
   896
     * @throws ClassCastException if the types of one or more elements
jaroslav@597
   897
     *         in this vector are incompatible with the specified
jaroslav@597
   898
     *         collection
jaroslav@597
   899
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@597
   900
     * @throws NullPointerException if this vector contains one or more null
jaroslav@597
   901
     *         elements and the specified collection does not support null
jaroslav@597
   902
     *         elements
jaroslav@597
   903
     * (<a href="Collection.html#optional-restrictions">optional</a>),
jaroslav@597
   904
     *         or if the specified collection is null
jaroslav@597
   905
     * @since 1.2
jaroslav@597
   906
     */
jaroslav@597
   907
    public synchronized boolean removeAll(Collection<?> c) {
jaroslav@597
   908
        return super.removeAll(c);
jaroslav@597
   909
    }
jaroslav@597
   910
jaroslav@597
   911
    /**
jaroslav@597
   912
     * Retains only the elements in this Vector that are contained in the
jaroslav@597
   913
     * specified Collection.  In other words, removes from this Vector all
jaroslav@597
   914
     * of its elements that are not contained in the specified Collection.
jaroslav@597
   915
     *
jaroslav@597
   916
     * @param c a collection of elements to be retained in this Vector
jaroslav@597
   917
     *          (all other elements are removed)
jaroslav@597
   918
     * @return true if this Vector changed as a result of the call
jaroslav@597
   919
     * @throws ClassCastException if the types of one or more elements
jaroslav@597
   920
     *         in this vector are incompatible with the specified
jaroslav@597
   921
     *         collection
jaroslav@597
   922
     * (<a href="Collection.html#optional-restrictions">optional</a>)
jaroslav@597
   923
     * @throws NullPointerException if this vector contains one or more null
jaroslav@597
   924
     *         elements and the specified collection does not support null
jaroslav@597
   925
     *         elements
jaroslav@597
   926
     *         (<a href="Collection.html#optional-restrictions">optional</a>),
jaroslav@597
   927
     *         or if the specified collection is null
jaroslav@597
   928
     * @since 1.2
jaroslav@597
   929
     */
jaroslav@597
   930
    public synchronized boolean retainAll(Collection<?> c) {
jaroslav@597
   931
        return super.retainAll(c);
jaroslav@597
   932
    }
jaroslav@597
   933
jaroslav@597
   934
    /**
jaroslav@597
   935
     * Inserts all of the elements in the specified Collection into this
jaroslav@597
   936
     * Vector at the specified position.  Shifts the element currently at
jaroslav@597
   937
     * that position (if any) and any subsequent elements to the right
jaroslav@597
   938
     * (increases their indices).  The new elements will appear in the Vector
jaroslav@597
   939
     * in the order that they are returned by the specified Collection's
jaroslav@597
   940
     * iterator.
jaroslav@597
   941
     *
jaroslav@597
   942
     * @param index index at which to insert the first element from the
jaroslav@597
   943
     *              specified collection
jaroslav@597
   944
     * @param c elements to be inserted into this Vector
jaroslav@597
   945
     * @return {@code true} if this Vector changed as a result of the call
jaroslav@597
   946
     * @throws ArrayIndexOutOfBoundsException if the index is out of range
jaroslav@597
   947
     *         ({@code index < 0 || index > size()})
jaroslav@597
   948
     * @throws NullPointerException if the specified collection is null
jaroslav@597
   949
     * @since 1.2
jaroslav@597
   950
     */
jaroslav@597
   951
    public synchronized boolean addAll(int index, Collection<? extends E> c) {
jaroslav@597
   952
        modCount++;
jaroslav@597
   953
        if (index < 0 || index > elementCount)
jaroslav@597
   954
            throw new ArrayIndexOutOfBoundsException(index);
jaroslav@597
   955
jaroslav@597
   956
        Object[] a = c.toArray();
jaroslav@597
   957
        int numNew = a.length;
jaroslav@597
   958
        ensureCapacityHelper(elementCount + numNew);
jaroslav@597
   959
jaroslav@597
   960
        int numMoved = elementCount - index;
jaroslav@597
   961
        if (numMoved > 0)
jaroslav@597
   962
            System.arraycopy(elementData, index, elementData, index + numNew,
jaroslav@597
   963
                             numMoved);
jaroslav@597
   964
jaroslav@597
   965
        System.arraycopy(a, 0, elementData, index, numNew);
jaroslav@597
   966
        elementCount += numNew;
jaroslav@597
   967
        return numNew != 0;
jaroslav@597
   968
    }
jaroslav@597
   969
jaroslav@597
   970
    /**
jaroslav@597
   971
     * Compares the specified Object with this Vector for equality.  Returns
jaroslav@597
   972
     * true if and only if the specified Object is also a List, both Lists
jaroslav@597
   973
     * have the same size, and all corresponding pairs of elements in the two
jaroslav@597
   974
     * Lists are <em>equal</em>.  (Two elements {@code e1} and
jaroslav@597
   975
     * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
jaroslav@597
   976
     * e1.equals(e2))}.)  In other words, two Lists are defined to be
jaroslav@597
   977
     * equal if they contain the same elements in the same order.
jaroslav@597
   978
     *
jaroslav@597
   979
     * @param o the Object to be compared for equality with this Vector
jaroslav@597
   980
     * @return true if the specified Object is equal to this Vector
jaroslav@597
   981
     */
jaroslav@597
   982
    public synchronized boolean equals(Object o) {
jaroslav@597
   983
        return super.equals(o);
jaroslav@597
   984
    }
jaroslav@597
   985
jaroslav@597
   986
    /**
jaroslav@597
   987
     * Returns the hash code value for this Vector.
jaroslav@597
   988
     */
jaroslav@597
   989
    public synchronized int hashCode() {
jaroslav@597
   990
        return super.hashCode();
jaroslav@597
   991
    }
jaroslav@597
   992
jaroslav@597
   993
    /**
jaroslav@597
   994
     * Returns a string representation of this Vector, containing
jaroslav@597
   995
     * the String representation of each element.
jaroslav@597
   996
     */
jaroslav@597
   997
    public synchronized String toString() {
jaroslav@597
   998
        return super.toString();
jaroslav@597
   999
    }
jaroslav@597
  1000
jaroslav@597
  1001
    /**
jaroslav@597
  1002
     * Returns a view of the portion of this List between fromIndex,
jaroslav@597
  1003
     * inclusive, and toIndex, exclusive.  (If fromIndex and toIndex are
jaroslav@597
  1004
     * equal, the returned List is empty.)  The returned List is backed by this
jaroslav@597
  1005
     * List, so changes in the returned List are reflected in this List, and
jaroslav@597
  1006
     * vice-versa.  The returned List supports all of the optional List
jaroslav@597
  1007
     * operations supported by this List.
jaroslav@597
  1008
     *
jaroslav@597
  1009
     * <p>This method eliminates the need for explicit range operations (of
jaroslav@597
  1010
     * the sort that commonly exist for arrays).  Any operation that expects
jaroslav@597
  1011
     * a List can be used as a range operation by operating on a subList view
jaroslav@597
  1012
     * instead of a whole List.  For example, the following idiom
jaroslav@597
  1013
     * removes a range of elements from a List:
jaroslav@597
  1014
     * <pre>
jaroslav@597
  1015
     *      list.subList(from, to).clear();
jaroslav@597
  1016
     * </pre>
jaroslav@597
  1017
     * Similar idioms may be constructed for indexOf and lastIndexOf,
jaroslav@597
  1018
     * and all of the algorithms in the Collections class can be applied to
jaroslav@597
  1019
     * a subList.
jaroslav@597
  1020
     *
jaroslav@597
  1021
     * <p>The semantics of the List returned by this method become undefined if
jaroslav@597
  1022
     * the backing list (i.e., this List) is <i>structurally modified</i> in
jaroslav@597
  1023
     * any way other than via the returned List.  (Structural modifications are
jaroslav@597
  1024
     * those that change the size of the List, or otherwise perturb it in such
jaroslav@597
  1025
     * a fashion that iterations in progress may yield incorrect results.)
jaroslav@597
  1026
     *
jaroslav@597
  1027
     * @param fromIndex low endpoint (inclusive) of the subList
jaroslav@597
  1028
     * @param toIndex high endpoint (exclusive) of the subList
jaroslav@597
  1029
     * @return a view of the specified range within this List
jaroslav@597
  1030
     * @throws IndexOutOfBoundsException if an endpoint index value is out of range
jaroslav@597
  1031
     *         {@code (fromIndex < 0 || toIndex > size)}
jaroslav@597
  1032
     * @throws IllegalArgumentException if the endpoint indices are out of order
jaroslav@597
  1033
     *         {@code (fromIndex > toIndex)}
jaroslav@597
  1034
     */
jaroslav@597
  1035
    public synchronized List<E> subList(int fromIndex, int toIndex) {
jaroslav@597
  1036
        return Collections.synchronizedList(super.subList(fromIndex, toIndex),
jaroslav@597
  1037
                                            this);
jaroslav@597
  1038
    }
jaroslav@597
  1039
jaroslav@597
  1040
    /**
jaroslav@597
  1041
     * Removes from this list all of the elements whose index is between
jaroslav@597
  1042
     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
jaroslav@597
  1043
     * Shifts any succeeding elements to the left (reduces their index).
jaroslav@597
  1044
     * This call shortens the list by {@code (toIndex - fromIndex)} elements.
jaroslav@597
  1045
     * (If {@code toIndex==fromIndex}, this operation has no effect.)
jaroslav@597
  1046
     */
jaroslav@597
  1047
    protected synchronized void removeRange(int fromIndex, int toIndex) {
jaroslav@597
  1048
        modCount++;
jaroslav@597
  1049
        int numMoved = elementCount - toIndex;
jaroslav@597
  1050
        System.arraycopy(elementData, toIndex, elementData, fromIndex,
jaroslav@597
  1051
                         numMoved);
jaroslav@597
  1052
jaroslav@597
  1053
        // Let gc do its work
jaroslav@597
  1054
        int newElementCount = elementCount - (toIndex-fromIndex);
jaroslav@597
  1055
        while (elementCount != newElementCount)
jaroslav@597
  1056
            elementData[--elementCount] = null;
jaroslav@597
  1057
    }
jaroslav@597
  1058
jaroslav@597
  1059
    /**
jaroslav@597
  1060
     * Returns a list iterator over the elements in this list (in proper
jaroslav@597
  1061
     * sequence), starting at the specified position in the list.
jaroslav@597
  1062
     * The specified index indicates the first element that would be
jaroslav@597
  1063
     * returned by an initial call to {@link ListIterator#next next}.
jaroslav@597
  1064
     * An initial call to {@link ListIterator#previous previous} would
jaroslav@597
  1065
     * return the element with the specified index minus one.
jaroslav@597
  1066
     *
jaroslav@597
  1067
     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
jaroslav@597
  1068
     *
jaroslav@597
  1069
     * @throws IndexOutOfBoundsException {@inheritDoc}
jaroslav@597
  1070
     */
jaroslav@597
  1071
    public synchronized ListIterator<E> listIterator(int index) {
jaroslav@597
  1072
        if (index < 0 || index > elementCount)
jaroslav@597
  1073
            throw new IndexOutOfBoundsException("Index: "+index);
jaroslav@597
  1074
        return new ListItr(index);
jaroslav@597
  1075
    }
jaroslav@597
  1076
jaroslav@597
  1077
    /**
jaroslav@597
  1078
     * Returns a list iterator over the elements in this list (in proper
jaroslav@597
  1079
     * sequence).
jaroslav@597
  1080
     *
jaroslav@597
  1081
     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
jaroslav@597
  1082
     *
jaroslav@597
  1083
     * @see #listIterator(int)
jaroslav@597
  1084
     */
jaroslav@597
  1085
    public synchronized ListIterator<E> listIterator() {
jaroslav@597
  1086
        return new ListItr(0);
jaroslav@597
  1087
    }
jaroslav@597
  1088
jaroslav@597
  1089
    /**
jaroslav@597
  1090
     * Returns an iterator over the elements in this list in proper sequence.
jaroslav@597
  1091
     *
jaroslav@597
  1092
     * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
jaroslav@597
  1093
     *
jaroslav@597
  1094
     * @return an iterator over the elements in this list in proper sequence
jaroslav@597
  1095
     */
jaroslav@597
  1096
    public synchronized Iterator<E> iterator() {
jaroslav@597
  1097
        return new Itr();
jaroslav@597
  1098
    }
jaroslav@597
  1099
jaroslav@597
  1100
    /**
jaroslav@597
  1101
     * An optimized version of AbstractList.Itr
jaroslav@597
  1102
     */
jaroslav@597
  1103
    private class Itr implements Iterator<E> {
jaroslav@597
  1104
        int cursor;       // index of next element to return
jaroslav@597
  1105
        int lastRet = -1; // index of last element returned; -1 if no such
jaroslav@597
  1106
        int expectedModCount = modCount;
jaroslav@597
  1107
jaroslav@597
  1108
        public boolean hasNext() {
jaroslav@597
  1109
            // Racy but within spec, since modifications are checked
jaroslav@597
  1110
            // within or after synchronization in next/previous
jaroslav@597
  1111
            return cursor != elementCount;
jaroslav@597
  1112
        }
jaroslav@597
  1113
jaroslav@597
  1114
        public E next() {
jaroslav@597
  1115
            synchronized (Vector.this) {
jaroslav@597
  1116
                checkForComodification();
jaroslav@597
  1117
                int i = cursor;
jaroslav@597
  1118
                if (i >= elementCount)
jaroslav@597
  1119
                    throw new NoSuchElementException();
jaroslav@597
  1120
                cursor = i + 1;
jaroslav@597
  1121
                return elementData(lastRet = i);
jaroslav@597
  1122
            }
jaroslav@597
  1123
        }
jaroslav@597
  1124
jaroslav@597
  1125
        public void remove() {
jaroslav@597
  1126
            if (lastRet == -1)
jaroslav@597
  1127
                throw new IllegalStateException();
jaroslav@597
  1128
            synchronized (Vector.this) {
jaroslav@597
  1129
                checkForComodification();
jaroslav@597
  1130
                Vector.this.remove(lastRet);
jaroslav@597
  1131
                expectedModCount = modCount;
jaroslav@597
  1132
            }
jaroslav@597
  1133
            cursor = lastRet;
jaroslav@597
  1134
            lastRet = -1;
jaroslav@597
  1135
        }
jaroslav@597
  1136
jaroslav@597
  1137
        final void checkForComodification() {
jaroslav@597
  1138
            if (modCount != expectedModCount)
jaroslav@597
  1139
                throw new ConcurrentModificationException();
jaroslav@597
  1140
        }
jaroslav@597
  1141
    }
jaroslav@597
  1142
jaroslav@597
  1143
    /**
jaroslav@597
  1144
     * An optimized version of AbstractList.ListItr
jaroslav@597
  1145
     */
jaroslav@597
  1146
    final class ListItr extends Itr implements ListIterator<E> {
jaroslav@597
  1147
        ListItr(int index) {
jaroslav@597
  1148
            super();
jaroslav@597
  1149
            cursor = index;
jaroslav@597
  1150
        }
jaroslav@597
  1151
jaroslav@597
  1152
        public boolean hasPrevious() {
jaroslav@597
  1153
            return cursor != 0;
jaroslav@597
  1154
        }
jaroslav@597
  1155
jaroslav@597
  1156
        public int nextIndex() {
jaroslav@597
  1157
            return cursor;
jaroslav@597
  1158
        }
jaroslav@597
  1159
jaroslav@597
  1160
        public int previousIndex() {
jaroslav@597
  1161
            return cursor - 1;
jaroslav@597
  1162
        }
jaroslav@597
  1163
jaroslav@597
  1164
        public E previous() {
jaroslav@597
  1165
            synchronized (Vector.this) {
jaroslav@597
  1166
                checkForComodification();
jaroslav@597
  1167
                int i = cursor - 1;
jaroslav@597
  1168
                if (i < 0)
jaroslav@597
  1169
                    throw new NoSuchElementException();
jaroslav@597
  1170
                cursor = i;
jaroslav@597
  1171
                return elementData(lastRet = i);
jaroslav@597
  1172
            }
jaroslav@597
  1173
        }
jaroslav@597
  1174
jaroslav@597
  1175
        public void set(E e) {
jaroslav@597
  1176
            if (lastRet == -1)
jaroslav@597
  1177
                throw new IllegalStateException();
jaroslav@597
  1178
            synchronized (Vector.this) {
jaroslav@597
  1179
                checkForComodification();
jaroslav@597
  1180
                Vector.this.set(lastRet, e);
jaroslav@597
  1181
            }
jaroslav@597
  1182
        }
jaroslav@597
  1183
jaroslav@597
  1184
        public void add(E e) {
jaroslav@597
  1185
            int i = cursor;
jaroslav@597
  1186
            synchronized (Vector.this) {
jaroslav@597
  1187
                checkForComodification();
jaroslav@597
  1188
                Vector.this.add(i, e);
jaroslav@597
  1189
                expectedModCount = modCount;
jaroslav@597
  1190
            }
jaroslav@597
  1191
            cursor = i + 1;
jaroslav@597
  1192
            lastRet = -1;
jaroslav@597
  1193
        }
jaroslav@597
  1194
    }
jaroslav@597
  1195
}