0N/A/*
2362N/A * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/A/**
0N/A * The {@code Vector} class implements a growable array of
0N/A * objects. Like an array, it contains components that can be
0N/A * accessed using an integer index. However, the size of a
0N/A * {@code Vector} can grow or shrink as needed to accommodate
0N/A * adding and removing items after the {@code Vector} has been created.
0N/A *
0N/A * <p>Each vector tries to optimize storage management by maintaining a
0N/A * {@code capacity} and a {@code capacityIncrement}. The
0N/A * {@code capacity} is always at least as large as the vector
0N/A * size; it is usually larger because as components are added to the
0N/A * vector, the vector's storage increases in chunks the size of
0N/A * {@code capacityIncrement}. An application can increase the
0N/A * capacity of a vector before inserting a large number of
0N/A * components; this reduces the amount of incremental reallocation.
0N/A *
0N/A * <p><a name="fail-fast"/>
528N/A * The iterators returned by this class's {@link #iterator() iterator} and
0N/A * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
0N/A * if the vector is structurally modified at any time after the iterator is
1790N/A * created, in any way except through the iterator's own
0N/A * {@link ListIterator#remove() remove} or
0N/A * {@link ListIterator#add(Object) add} methods, the iterator will throw a
0N/A * {@link ConcurrentModificationException}. Thus, in the face of
0N/A * concurrent modification, the iterator fails quickly and cleanly, rather
0N/A * than risking arbitrary, non-deterministic behavior at an undetermined
0N/A * time in the future. The {@link Enumeration Enumerations} returned by
0N/A * the {@link #elements() elements} method are <em>not</em> fail-fast.
0N/A *
0N/A * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
0N/A * as it is, generally speaking, impossible to make any hard guarantees in the
0N/A * presence of unsynchronized concurrent modification. Fail-fast iterators
0N/A * throw {@code ConcurrentModificationException} on a best-effort basis.
0N/A * Therefore, it would be wrong to write a program that depended on this
0N/A * exception for its correctness: <i>the fail-fast behavior of iterators
0N/A * should be used only to detect bugs.</i>
0N/A *
0N/A * <p>As of the Java 2 platform v1.2, this class was retrofitted to
0N/A * implement the {@link List} interface, making it a member of the
0N/A * <a href="{@docRoot}/../technotes/guides/collections/index.html">
0N/A * Java Collections Framework</a>. Unlike the new collection
0N/A * implementations, {@code Vector} is synchronized. If a thread-safe
0N/A * implementation is not needed, it is recommended to use {@link
0N/A * ArrayList} in place of {@code Vector}.
0N/A *
0N/A * @author Lee Boynton
0N/A * @author Jonathan Payne
0N/A * @see Collection
0N/A * @see LinkedList
0N/A * @since JDK1.0
0N/A */
0N/Apublic class Vector<E>
0N/A extends AbstractList<E>
0N/A implements List<E>, RandomAccess, Cloneable, java.io.Serializable
0N/A{
0N/A /**
0N/A * The array buffer into which the components of the vector are
0N/A * stored. The capacity of the vector is the length of this array buffer,
0N/A * and is at least large enough to contain all the vector's elements.
0N/A *
0N/A * <p>Any array elements following the last element in the Vector are null.
0N/A *
0N/A * @serial
528N/A */
0N/A protected Object[] elementData;
0N/A
0N/A /**
0N/A * The number of valid components in this {@code Vector} object.
0N/A * Components {@code elementData[0]} through
0N/A * {@code elementData[elementCount-1]} are the actual items.
0N/A *
0N/A * @serial
0N/A */
0N/A protected int elementCount;
0N/A
0N/A /**
0N/A * The amount by which the capacity of the vector is automatically
0N/A * incremented when its size becomes greater than its capacity. If
1790N/A * the capacity increment is less than or equal to zero, the capacity
0N/A * of the vector is doubled each time it needs to grow.
1790N/A *
1790N/A * @serial
0N/A */
0N/A protected int capacityIncrement;
0N/A
0N/A /** use serialVersionUID from JDK 1.0.2 for interoperability */
1790N/A private static final long serialVersionUID = -2767605614048989439L;
0N/A
0N/A /**
0N/A * Constructs an empty vector with the specified initial capacity and
0N/A * capacity increment.
0N/A *
0N/A * @param initialCapacity the initial capacity of the vector
0N/A * @param capacityIncrement the amount by which the capacity is
1790N/A * increased when the vector overflows
1790N/A * @throws IllegalArgumentException if the specified initial capacity
1790N/A * is negative
0N/A */
0N/A public Vector(int initialCapacity, int capacityIncrement) {
1790N/A super();
0N/A if (initialCapacity < 0)
0N/A throw new IllegalArgumentException("Illegal Capacity: "+
1790N/A initialCapacity);
0N/A this.elementData = new Object[initialCapacity];
0N/A this.capacityIncrement = capacityIncrement;
0N/A }
0N/A
0N/A /**
0N/A * Constructs an empty vector with the specified initial capacity and
0N/A * with its capacity increment equal to zero.
0N/A *
0N/A * @param initialCapacity the initial capacity of the vector
0N/A * @throws IllegalArgumentException if the specified initial capacity
0N/A * is negative
0N/A */
0N/A public Vector(int initialCapacity) {
0N/A this(initialCapacity, 0);
0N/A }
0N/A
0N/A /**
0N/A * Constructs an empty vector so that its internal data array
0N/A * has size {@code 10} and its standard capacity increment is
0N/A * zero.
0N/A */
0N/A public Vector() {
0N/A this(10);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a vector containing the elements of the specified
0N/A * collection, in the order they are returned by the collection's
0N/A * iterator.
0N/A *
0N/A * @param c the collection whose elements are to be placed into this
0N/A * vector
0N/A * @throws NullPointerException if the specified collection is null
0N/A * @since 1.2
0N/A */
0N/A public Vector(Collection<? extends E> c) {
0N/A elementData = c.toArray();
0N/A elementCount = elementData.length;
0N/A // c.toArray might (incorrectly) not return Object[] (see 6260652)
0N/A if (elementData.getClass() != Object[].class)
0N/A elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
0N/A }
0N/A
0N/A /**
0N/A * Copies the components of this vector into the specified array.
0N/A * The item at index {@code k} in this vector is copied into
0N/A * component {@code k} of {@code anArray}.
0N/A *
0N/A * @param anArray the array into which the components get copied
0N/A * @throws NullPointerException if the given array is null
0N/A * @throws IndexOutOfBoundsException if the specified array is not
0N/A * large enough to hold all the components of this vector
0N/A * @throws ArrayStoreException if a component of this vector is not of
0N/A * a runtime type that can be stored in the specified array
0N/A * @see #toArray(Object[])
0N/A */
0N/A public synchronized void copyInto(Object[] anArray) {
0N/A System.arraycopy(elementData, 0, anArray, 0, elementCount);
0N/A }
0N/A
0N/A /**
0N/A * Trims the capacity of this vector to be the vector's current
0N/A * size. If the capacity of this vector is larger than its current
0N/A * size, then the capacity is changed to equal the size by replacing
0N/A * its internal data array, kept in the field {@code elementData},
0N/A * with a smaller one. An application can use this operation to
0N/A * minimize the storage of a vector.
0N/A */
0N/A public synchronized void trimToSize() {
0N/A modCount++;
0N/A int oldCapacity = elementData.length;
0N/A if (elementCount < oldCapacity) {
0N/A elementData = Arrays.copyOf(elementData, elementCount);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Increases the capacity of this vector, if necessary, to ensure
0N/A * that it can hold at least the number of components specified by
0N/A * the minimum capacity argument.
0N/A *
0N/A * <p>If the current capacity of this vector is less than
0N/A * {@code minCapacity}, then its capacity is increased by replacing its
0N/A * internal data array, kept in the field {@code elementData}, with a
0N/A * larger one. The size of the new data array will be the old size plus
0N/A * {@code capacityIncrement}, unless the value of
0N/A * {@code capacityIncrement} is less than or equal to zero, in which case
0N/A * the new capacity will be twice the old capacity; but if this new size
0N/A * is still smaller than {@code minCapacity}, then the new capacity will
0N/A * be {@code minCapacity}.
0N/A *
0N/A * @param minCapacity the desired minimum capacity
0N/A */
0N/A public synchronized void ensureCapacity(int minCapacity) {
0N/A if (minCapacity > 0) {
0N/A modCount++;
0N/A ensureCapacityHelper(minCapacity);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This implements the unsynchronized semantics of ensureCapacity.
0N/A * Synchronized methods in this class can internally call this
0N/A * method for ensuring capacity without incurring the cost of an
0N/A * extra synchronization.
0N/A *
0N/A * @see #ensureCapacity(int)
0N/A */
0N/A private void ensureCapacityHelper(int minCapacity) {
0N/A // overflow-conscious code
0N/A if (minCapacity - elementData.length > 0)
0N/A grow(minCapacity);
0N/A }
0N/A
0N/A /**
0N/A * The maximum size of array to allocate.
0N/A * Some VMs reserve some header words in an array.
0N/A * Attempts to allocate larger arrays may result in
0N/A * OutOfMemoryError: Requested array size exceeds VM limit
0N/A */
0N/A private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
0N/A
0N/A private void grow(int minCapacity) {
0N/A // overflow-conscious code
0N/A int oldCapacity = elementData.length;
0N/A int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
0N/A capacityIncrement : oldCapacity);
0N/A if (newCapacity - minCapacity < 0)
0N/A newCapacity = minCapacity;
0N/A if (newCapacity - MAX_ARRAY_SIZE > 0)
0N/A newCapacity = hugeCapacity(minCapacity);
0N/A elementData = Arrays.copyOf(elementData, newCapacity);
0N/A }
0N/A
0N/A private static int hugeCapacity(int minCapacity) {
0N/A if (minCapacity < 0) // overflow
0N/A throw new OutOfMemoryError();
0N/A return (minCapacity > MAX_ARRAY_SIZE) ?
0N/A Integer.MAX_VALUE :
0N/A MAX_ARRAY_SIZE;
0N/A }
0N/A
0N/A /**
0N/A * Sets the size of this vector. If the new size is greater than the
0N/A * current size, new {@code null} items are added to the end of
0N/A * the vector. If the new size is less than the current size, all
0N/A * components at index {@code newSize} and greater are discarded.
0N/A *
0N/A * @param newSize the new size of this vector
0N/A * @throws ArrayIndexOutOfBoundsException if the new size is negative
0N/A */
0N/A public synchronized void setSize(int newSize) {
0N/A modCount++;
0N/A if (newSize > elementCount) {
0N/A ensureCapacityHelper(newSize);
0N/A } else {
0N/A for (int i = newSize ; i < elementCount ; i++) {
0N/A elementData[i] = null;
0N/A }
0N/A }
0N/A elementCount = newSize;
0N/A }
1790N/A
0N/A /**
0N/A * Returns the current capacity of this vector.
0N/A *
0N/A * @return the current capacity (the length of its internal
0N/A * data array, kept in the field {@code elementData}
0N/A * of this vector)
0N/A */
0N/A public synchronized int capacity() {
0N/A return elementData.length;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of components in this vector.
0N/A *
0N/A * @return the number of components in this vector
0N/A */
0N/A public synchronized int size() {
1790N/A return elementCount;
1790N/A }
0N/A
0N/A /**
0N/A * Tests if this vector has no components.
0N/A *
0N/A * @return {@code true} if and only if this vector has
0N/A * no components, that is, its size is zero;
0N/A * {@code false} otherwise.
0N/A */
0N/A public synchronized boolean isEmpty() {
0N/A return elementCount == 0;
528N/A }
0N/A
0N/A /**
0N/A * Returns an enumeration of the components of this vector. The
1790N/A * returned {@code Enumeration} object will generate all items in
0N/A * this vector. The first item generated is the item at index {@code 0},
0N/A * then the item at index {@code 1}, and so on.
0N/A *
0N/A * @return an enumeration of the components of this vector
0N/A * @see Iterator
0N/A */
0N/A public Enumeration<E> elements() {
0N/A return new Enumeration<E>() {
0N/A int count = 0;
0N/A
0N/A public boolean hasMoreElements() {
0N/A return count < elementCount;
0N/A }
0N/A
0N/A public E nextElement() {
0N/A synchronized (Vector.this) {
0N/A if (count < elementCount) {
0N/A return elementData(count++);
0N/A }
0N/A }
0N/A throw new NoSuchElementException("Vector Enumeration");
0N/A }
0N/A };
1790N/A }
1790N/A
0N/A /**
0N/A * Returns {@code true} if this vector contains the specified element.
0N/A * More formally, returns {@code true} if and only if this vector
0N/A * contains at least one element {@code e} such that
0N/A * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
0N/A *
0N/A * @param o element whose presence in this vector is to be tested
0N/A * @return {@code true} if this vector contains the specified element
0N/A */
0N/A public boolean contains(Object o) {
0N/A return indexOf(o, 0) >= 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns the index of the first occurrence of the specified element
0N/A * in this vector, or -1 if this vector does not contain the element.
0N/A * More formally, returns the lowest index {@code i} such that
0N/A * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
0N/A * or -1 if there is no such index.
0N/A *
0N/A * @param o element to search for
0N/A * @return the index of the first occurrence of the specified element in
0N/A * this vector, or -1 if this vector does not contain the element
0N/A */
0N/A public int indexOf(Object o) {
0N/A return indexOf(o, 0);
0N/A }
0N/A
0N/A /**
0N/A * Returns the index of the first occurrence of the specified element in
0N/A * this vector, searching forwards from {@code index}, or returns -1 if
0N/A * the element is not found.
0N/A * More formally, returns the lowest index {@code i} such that
0N/A * <tt>(i&nbsp;&gt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</tt>,
0N/A * or -1 if there is no such index.
0N/A *
1790N/A * @param o element to search for
1790N/A * @param index index to start searching from
0N/A * @return the index of the first occurrence of the element in
0N/A * this vector at position {@code index} or later in the vector;
0N/A * {@code -1} if the element is not found.
0N/A * @throws IndexOutOfBoundsException if the specified index is negative
0N/A * @see Object#equals(Object)
1790N/A */
0N/A public synchronized int indexOf(Object o, int index) {
0N/A if (o == null) {
0N/A for (int i = index ; i < elementCount ; i++)
0N/A if (elementData[i]==null)
0N/A return i;
0N/A } else {
0N/A for (int i = index ; i < elementCount ; i++)
0N/A if (o.equals(elementData[i]))
0N/A return i;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the index of the last occurrence of the specified element
0N/A * in this vector, or -1 if this vector does not contain the element.
0N/A * More formally, returns the highest index {@code i} such that
0N/A * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
0N/A * or -1 if there is no such index.
0N/A *
0N/A * @param o element to search for
0N/A * @return the index of the last occurrence of the specified element in
0N/A * this vector, or -1 if this vector does not contain the element
0N/A */
0N/A public synchronized int lastIndexOf(Object o) {
0N/A return lastIndexOf(o, elementCount-1);
0N/A }
0N/A
0N/A /**
0N/A * Returns the index of the last occurrence of the specified element in
0N/A * this vector, searching backwards from {@code index}, or returns -1 if
0N/A * the element is not found.
0N/A * More formally, returns the highest index {@code i} such that
0N/A * <tt>(i&nbsp;&lt;=&nbsp;index&nbsp;&amp;&amp;&nbsp;(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i))))</tt>,
0N/A * or -1 if there is no such index.
0N/A *
0N/A * @param o element to search for
0N/A * @param index index to start searching backwards from
0N/A * @return the index of the last occurrence of the element at position
0N/A * less than or equal to {@code index} in this vector;
0N/A * -1 if the element is not found.
0N/A * @throws IndexOutOfBoundsException if the specified index is greater
0N/A * than or equal to the current size of this vector
0N/A */
0N/A public synchronized int lastIndexOf(Object o, int index) {
0N/A if (index >= elementCount)
1790N/A throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
1790N/A
0N/A if (o == null) {
0N/A for (int i = index; i >= 0; i--)
0N/A if (elementData[i]==null)
0N/A return i;
0N/A } else {
1790N/A for (int i = index; i >= 0; i--)
0N/A if (o.equals(elementData[i]))
0N/A return i;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the component at the specified index.
0N/A *
0N/A * <p>This method is identical in functionality to the {@link #get(int)}
0N/A * method (which is part of the {@link List} interface).
0N/A *
0N/A * @param index an index into this vector
0N/A * @return the component at the specified index
0N/A * @throws ArrayIndexOutOfBoundsException if the index is out of range
0N/A * ({@code index < 0 || index >= size()})
0N/A */
0N/A public synchronized E elementAt(int index) {
0N/A if (index >= elementCount) {
0N/A throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
0N/A }
0N/A
0N/A return elementData(index);
0N/A }
0N/A
0N/A /**
0N/A * Returns the first component (the item at index {@code 0}) of
0N/A * this vector.
0N/A *
0N/A * @return the first component of this vector
0N/A * @throws NoSuchElementException if this vector has no components
0N/A */
0N/A public synchronized E firstElement() {
0N/A if (elementCount == 0) {
0N/A throw new NoSuchElementException();
0N/A }
0N/A return elementData(0);
0N/A }
0N/A
0N/A /**
0N/A * Returns the last component of the vector.
0N/A *
0N/A * @return the last component of the vector, i.e., the component at index
0N/A * <code>size()&nbsp;-&nbsp;1</code>.
0N/A * @throws NoSuchElementException if this vector is empty
0N/A */
0N/A public synchronized E lastElement() {
0N/A if (elementCount == 0) {
0N/A throw new NoSuchElementException();
0N/A }
0N/A return elementData(elementCount - 1);
0N/A }
0N/A
0N/A /**
0N/A * Sets the component at the specified {@code index} of this
0N/A * vector to be the specified object. The previous component at that
0N/A * position is discarded.
0N/A *
0N/A * <p>The index must be a value greater than or equal to {@code 0}
0N/A * and less than the current size of the vector.
0N/A *
0N/A * <p>This method is identical in functionality to the
0N/A * {@link #set(int, Object) set(int, E)}
0N/A * method (which is part of the {@link List} interface). Note that the
0N/A * {@code set} method reverses the order of the parameters, to more closely
0N/A * match array usage. Note also that the {@code set} method returns the
0N/A * old value that was stored at the specified position.
0N/A *
0N/A * @param obj what the component is to be set to
0N/A * @param index the specified index
0N/A * @throws ArrayIndexOutOfBoundsException if the index is out of range
0N/A * ({@code index < 0 || index >= size()})
0N/A */
0N/A public synchronized void setElementAt(E obj, int index) {
0N/A if (index >= elementCount) {
0N/A throw new ArrayIndexOutOfBoundsException(index + " >= " +
0N/A elementCount);
0N/A }
0N/A elementData[index] = obj;
0N/A }
0N/A
0N/A /**
0N/A * Deletes the component at the specified index. Each component in
0N/A * this vector with an index greater or equal to the specified
0N/A * {@code index} is shifted downward to have an index one
0N/A * smaller than the value it had previously. The size of this vector
0N/A * is decreased by {@code 1}.
0N/A *
0N/A * <p>The index must be a value greater than or equal to {@code 0}
0N/A * and less than the current size of the vector.
0N/A *
0N/A * <p>This method is identical in functionality to the {@link #remove(int)}
0N/A * method (which is part of the {@link List} interface). Note that the
0N/A * {@code remove} method returns the old value that was stored at the
0N/A * specified position.
0N/A *
0N/A * @param index the index of the object to remove
0N/A * @throws ArrayIndexOutOfBoundsException if the index is out of range
0N/A * ({@code index < 0 || index >= size()})
0N/A */
0N/A public synchronized void removeElementAt(int index) {
0N/A modCount++;
0N/A if (index >= elementCount) {
0N/A throw new ArrayIndexOutOfBoundsException(index + " >= " +
0N/A elementCount);
0N/A }
0N/A else if (index < 0) {
0N/A throw new ArrayIndexOutOfBoundsException(index);
0N/A }
0N/A int j = elementCount - index - 1;
0N/A if (j > 0) {
0N/A System.arraycopy(elementData, index + 1, elementData, index, j);
0N/A }
0N/A elementCount--;
0N/A elementData[elementCount] = null; /* to let gc do its work */
0N/A }
0N/A
0N/A /**
0N/A * Inserts the specified object as a component in this vector at the
0N/A * specified {@code index}. Each component in this vector with
0N/A * an index greater or equal to the specified {@code index} is
0N/A * shifted upward to have an index one greater than the value it had
0N/A * previously.
0N/A *
0N/A * <p>The index must be a value greater than or equal to {@code 0}
0N/A * and less than or equal to the current size of the vector. (If the
0N/A * index is equal to the current size of the vector, the new element
0N/A * is appended to the Vector.)
0N/A *
0N/A * <p>This method is identical in functionality to the
0N/A * {@link #add(int, Object) add(int, E)}
0N/A * method (which is part of the {@link List} interface). Note that the
0N/A * {@code add} method reverses the order of the parameters, to more closely
0N/A * match array usage.
0N/A *
0N/A * @param obj the component to insert
0N/A * @param index where to insert the new component
0N/A * @throws ArrayIndexOutOfBoundsException if the index is out of range
0N/A * ({@code index < 0 || index > size()})
0N/A */
0N/A public synchronized void insertElementAt(E obj, int index) {
0N/A modCount++;
0N/A if (index > elementCount) {
0N/A throw new ArrayIndexOutOfBoundsException(index
0N/A + " > " + elementCount);
0N/A }
0N/A ensureCapacityHelper(elementCount + 1);
0N/A System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
0N/A elementData[index] = obj;
0N/A elementCount++;
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified component to the end of this vector,
0N/A * increasing its size by one. The capacity of this vector is
0N/A * increased if its size becomes greater than its capacity.
0N/A *
0N/A * <p>This method is identical in functionality to the
0N/A * {@link #add(Object) add(E)}
0N/A * method (which is part of the {@link List} interface).
0N/A *
0N/A * @param obj the component to be added
0N/A */
0N/A public synchronized void addElement(E obj) {
0N/A modCount++;
0N/A ensureCapacityHelper(elementCount + 1);
0N/A elementData[elementCount++] = obj;
0N/A }
0N/A
0N/A /**
0N/A * Removes the first (lowest-indexed) occurrence of the argument
0N/A * from this vector. If the object is found in this vector, each
0N/A * component in the vector with an index greater or equal to the
0N/A * object's index is shifted downward to have an index one smaller
0N/A * than the value it had previously.
0N/A *
0N/A * <p>This method is identical in functionality to the
0N/A * {@link #remove(Object)} method (which is part of the
0N/A * {@link List} interface).
0N/A *
0N/A * @param obj the component to be removed
0N/A * @return {@code true} if the argument was a component of this
0N/A * vector; {@code false} otherwise.
0N/A */
0N/A public synchronized boolean removeElement(Object obj) {
0N/A modCount++;
0N/A int i = indexOf(obj);
0N/A if (i >= 0) {
0N/A removeElementAt(i);
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Removes all components from this vector and sets its size to zero.
0N/A *
0N/A * <p>This method is identical in functionality to the {@link #clear}
0N/A * method (which is part of the {@link List} interface).
0N/A */
0N/A public synchronized void removeAllElements() {
0N/A modCount++;
0N/A // Let gc do its work
0N/A for (int i = 0; i < elementCount; i++)
0N/A elementData[i] = null;
0N/A
0N/A elementCount = 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns a clone of this vector. The copy will contain a
0N/A * reference to a clone of the internal data array, not a reference
0N/A * to the original internal data array of this {@code Vector} object.
0N/A *
0N/A * @return a clone of this vector
0N/A */
0N/A public synchronized Object clone() {
0N/A try {
0N/A @SuppressWarnings("unchecked")
0N/A Vector<E> v = (Vector<E>) super.clone();
0N/A v.elementData = Arrays.copyOf(elementData, elementCount);
0N/A v.modCount = 0;
0N/A return v;
0N/A } catch (CloneNotSupportedException e) {
0N/A // this shouldn't happen, since we are Cloneable
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an array containing all of the elements in this Vector
0N/A * in the correct order.
0N/A *
0N/A * @since 1.2
0N/A */
0N/A public synchronized Object[] toArray() {
0N/A return Arrays.copyOf(elementData, elementCount);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array containing all of the elements in this Vector in the
0N/A * correct order; the runtime type of the returned array is that of the
0N/A * specified array. If the Vector fits in the specified array, it is
0N/A * returned therein. Otherwise, a new array is allocated with the runtime
0N/A * type of the specified array and the size of this Vector.
0N/A *
0N/A * <p>If the Vector fits in the specified array with room to spare
0N/A * (i.e., the array has more elements than the Vector),
0N/A * the element in the array immediately following the end of the
0N/A * Vector is set to null. (This is useful in determining the length
0N/A * of the Vector <em>only</em> if the caller knows that the Vector
0N/A * does not contain any null elements.)
0N/A *
0N/A * @param a the array into which the elements of the Vector are to
0N/A * be stored, if it is big enough; otherwise, a new array of the
0N/A * same runtime type is allocated for this purpose.
0N/A * @return an array containing the elements of the Vector
0N/A * @throws ArrayStoreException if the runtime type of a is not a supertype
0N/A * of the runtime type of every element in this Vector
0N/A * @throws NullPointerException if the given array is null
0N/A * @since 1.2
0N/A */
0N/A @SuppressWarnings("unchecked")
0N/A public synchronized <T> T[] toArray(T[] a) {
0N/A if (a.length < elementCount)
0N/A return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
0N/A
0N/A System.arraycopy(elementData, 0, a, 0, elementCount);
0N/A
0N/A if (a.length > elementCount)
0N/A a[elementCount] = null;
0N/A
0N/A return a;
0N/A }
0N/A
0N/A // Positional Access Operations
0N/A
0N/A @SuppressWarnings("unchecked")
0N/A E elementData(int index) {
0N/A return (E) elementData[index];
0N/A }
0N/A
0N/A /**
0N/A * Returns the element at the specified position in this Vector.
0N/A *
0N/A * @param index index of the element to return
0N/A * @return object at the specified index
0N/A * @throws ArrayIndexOutOfBoundsException if the index is out of range
0N/A * ({@code index < 0 || index >= size()})
0N/A * @since 1.2
0N/A */
0N/A public synchronized E get(int index) {
0N/A if (index >= elementCount)
0N/A throw new ArrayIndexOutOfBoundsException(index);
0N/A
0N/A return elementData(index);
0N/A }
0N/A
0N/A /**
0N/A * Replaces the element at the specified position in this Vector with the
0N/A * specified element.
0N/A *
0N/A * @param index index of the element to replace
0N/A * @param element element to be stored at the specified position
0N/A * @return the element previously at the specified position
0N/A * @throws ArrayIndexOutOfBoundsException if the index is out of range
0N/A * ({@code index < 0 || index >= size()})
0N/A * @since 1.2
0N/A */
0N/A public synchronized E set(int index, E element) {
0N/A if (index >= elementCount)
0N/A throw new ArrayIndexOutOfBoundsException(index);
0N/A
0N/A E oldValue = elementData(index);
0N/A elementData[index] = element;
0N/A return oldValue;
0N/A }
0N/A
0N/A /**
0N/A * Appends the specified element to the end of this Vector.
0N/A *
0N/A * @param e element to be appended to this Vector
0N/A * @return {@code true} (as specified by {@link Collection#add})
0N/A * @since 1.2
0N/A */
0N/A public synchronized boolean add(E e) {
0N/A modCount++;
0N/A ensureCapacityHelper(elementCount + 1);
0N/A elementData[elementCount++] = e;
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Removes the first occurrence of the specified element in this Vector
0N/A * If the Vector does not contain the element, it is unchanged. More
0N/A * formally, removes the element with the lowest index i such that
0N/A * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such
0N/A * an element exists).
0N/A *
0N/A * @param o element to be removed from this Vector, if present
0N/A * @return true if the Vector contained the specified element
0N/A * @since 1.2
0N/A */
0N/A public boolean remove(Object o) {
0N/A return removeElement(o);
0N/A }
0N/A
0N/A /**
0N/A * Inserts the specified element at the specified position in this Vector.
0N/A * Shifts the element currently at that position (if any) and any
0N/A * subsequent elements to the right (adds one to their indices).
0N/A *
0N/A * @param index index at which the specified element is to be inserted
0N/A * @param element element to be inserted
0N/A * @throws ArrayIndexOutOfBoundsException if the index is out of range
0N/A * ({@code index < 0 || index > size()})
0N/A * @since 1.2
0N/A */
0N/A public void add(int index, E element) {
0N/A insertElementAt(element, index);
0N/A }
0N/A
0N/A /**
0N/A * Removes the element at the specified position in this Vector.
0N/A * Shifts any subsequent elements to the left (subtracts one from their
0N/A * indices). Returns the element that was removed from the Vector.
0N/A *
0N/A * @throws ArrayIndexOutOfBoundsException if the index is out of range
0N/A * ({@code index < 0 || index >= size()})
0N/A * @param index the index of the element to be removed
0N/A * @return element that was removed
0N/A * @since 1.2
0N/A */
0N/A public synchronized E remove(int index) {
0N/A modCount++;
0N/A if (index >= elementCount)
0N/A throw new ArrayIndexOutOfBoundsException(index);
0N/A E oldValue = elementData(index);
0N/A
0N/A int numMoved = elementCount - index - 1;
0N/A if (numMoved > 0)
0N/A System.arraycopy(elementData, index+1, elementData, index,
0N/A numMoved);
0N/A elementData[--elementCount] = null; // Let gc do its work
0N/A
0N/A return oldValue;
0N/A }
0N/A
0N/A /**
0N/A * Removes all of the elements from this Vector. The Vector will
0N/A * be empty after this call returns (unless it throws an exception).
0N/A *
0N/A * @since 1.2
0N/A */
0N/A public void clear() {
0N/A removeAllElements();
0N/A }
0N/A
0N/A // Bulk Operations
0N/A
0N/A /**
0N/A * Returns true if this Vector contains all of the elements in the
0N/A * specified Collection.
0N/A *
0N/A * @param c a collection whose elements will be tested for containment
0N/A * in this Vector
0N/A * @return true if this Vector contains all of the elements in the
0N/A * specified collection
0N/A * @throws NullPointerException if the specified collection is null
0N/A */
0N/A public synchronized boolean containsAll(Collection<?> c) {
0N/A return super.containsAll(c);
0N/A }
0N/A
0N/A /**
0N/A * Appends all of the elements in the specified Collection to the end of
0N/A * this Vector, in the order that they are returned by the specified
0N/A * Collection's Iterator. The behavior of this operation is undefined if
0N/A * the specified Collection is modified while the operation is in progress.
0N/A * (This implies that the behavior of this call is undefined if the
0N/A * specified Collection is this Vector, and this Vector is nonempty.)
0N/A *
0N/A * @param c elements to be inserted into this Vector
0N/A * @return {@code true} if this Vector changed as a result of the call
0N/A * @throws NullPointerException if the specified collection is null
0N/A * @since 1.2
0N/A */
0N/A public synchronized boolean addAll(Collection<? extends E> c) {
0N/A modCount++;
0N/A Object[] a = c.toArray();
0N/A int numNew = a.length;
0N/A ensureCapacityHelper(elementCount + numNew);
0N/A System.arraycopy(a, 0, elementData, elementCount, numNew);
0N/A elementCount += numNew;
0N/A return numNew != 0;
0N/A }
0N/A
0N/A /**
0N/A * Removes from this Vector all of its elements that are contained in the
0N/A * specified Collection.
0N/A *
0N/A * @param c a collection of elements to be removed from the Vector
0N/A * @return true if this Vector changed as a result of the call
0N/A * @throws ClassCastException if the types of one or more elements
0N/A * in this vector are incompatible with the specified
0N/A * collection
0N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws NullPointerException if this vector contains one or more null
0N/A * elements and the specified collection does not support null
0N/A * elements
0N/A * (<a href="Collection.html#optional-restrictions">optional</a>),
0N/A * or if the specified collection is null
0N/A * @since 1.2
0N/A */
0N/A public synchronized boolean removeAll(Collection<?> c) {
0N/A return super.removeAll(c);
0N/A }
0N/A
0N/A /**
0N/A * Retains only the elements in this Vector that are contained in the
0N/A * specified Collection. In other words, removes from this Vector all
0N/A * of its elements that are not contained in the specified Collection.
0N/A *
0N/A * @param c a collection of elements to be retained in this Vector
0N/A * (all other elements are removed)
0N/A * @return true if this Vector changed as a result of the call
0N/A * @throws ClassCastException if the types of one or more elements
0N/A * in this vector are incompatible with the specified
0N/A * collection
0N/A * (<a href="Collection.html#optional-restrictions">optional</a>)
0N/A * @throws NullPointerException if this vector contains one or more null
0N/A * elements and the specified collection does not support null
0N/A * elements
0N/A * (<a href="Collection.html#optional-restrictions">optional</a>),
0N/A * or if the specified collection is null
0N/A * @since 1.2
0N/A */
0N/A public synchronized boolean retainAll(Collection<?> c) {
0N/A return super.retainAll(c);
0N/A }
0N/A
0N/A /**
0N/A * Inserts all of the elements in the specified Collection into this
0N/A * Vector at the specified position. Shifts the element currently at
0N/A * that position (if any) and any subsequent elements to the right
0N/A * (increases their indices). The new elements will appear in the Vector
0N/A * in the order that they are returned by the specified Collection's
0N/A * iterator.
0N/A *
0N/A * @param index index at which to insert the first element from the
0N/A * specified collection
0N/A * @param c elements to be inserted into this Vector
0N/A * @return {@code true} if this Vector changed as a result of the call
0N/A * @throws ArrayIndexOutOfBoundsException if the index is out of range
0N/A * ({@code index < 0 || index > size()})
0N/A * @throws NullPointerException if the specified collection is null
0N/A * @since 1.2
0N/A */
0N/A public synchronized boolean addAll(int index, Collection<? extends E> c) {
0N/A modCount++;
0N/A if (index < 0 || index > elementCount)
0N/A throw new ArrayIndexOutOfBoundsException(index);
0N/A
0N/A Object[] a = c.toArray();
0N/A int numNew = a.length;
0N/A ensureCapacityHelper(elementCount + numNew);
0N/A
0N/A int numMoved = elementCount - index;
0N/A if (numMoved > 0)
0N/A System.arraycopy(elementData, index, elementData, index + numNew,
0N/A numMoved);
0N/A
0N/A System.arraycopy(a, 0, elementData, index, numNew);
0N/A elementCount += numNew;
0N/A return numNew != 0;
0N/A }
0N/A
0N/A /**
0N/A * Compares the specified Object with this Vector for equality. Returns
0N/A * true if and only if the specified Object is also a List, both Lists
0N/A * have the same size, and all corresponding pairs of elements in the two
0N/A * Lists are <em>equal</em>. (Two elements {@code e1} and
0N/A * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
0N/A * e1.equals(e2))}.) In other words, two Lists are defined to be
0N/A * equal if they contain the same elements in the same order.
0N/A *
0N/A * @param o the Object to be compared for equality with this Vector
0N/A * @return true if the specified Object is equal to this Vector
0N/A */
0N/A public synchronized boolean equals(Object o) {
0N/A return super.equals(o);
0N/A }
0N/A
0N/A /**
0N/A * Returns the hash code value for this Vector.
0N/A */
0N/A public synchronized int hashCode() {
0N/A return super.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this Vector, containing
0N/A * the String representation of each element.
0N/A */
0N/A public synchronized String toString() {
0N/A return super.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns a view of the portion of this List between fromIndex,
0N/A * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are
0N/A * equal, the returned List is empty.) The returned List is backed by this
0N/A * List, so changes in the returned List are reflected in this List, and
0N/A * vice-versa. The returned List supports all of the optional List
0N/A * operations supported by this List.
0N/A *
0N/A * <p>This method eliminates the need for explicit range operations (of
0N/A * the sort that commonly exist for arrays). Any operation that expects
0N/A * a List can be used as a range operation by operating on a subList view
0N/A * instead of a whole List. For example, the following idiom
0N/A * removes a range of elements from a List:
0N/A * <pre>
0N/A * list.subList(from, to).clear();
0N/A * </pre>
0N/A * Similar idioms may be constructed for indexOf and lastIndexOf,
0N/A * and all of the algorithms in the Collections class can be applied to
0N/A * a subList.
0N/A *
0N/A * <p>The semantics of the List returned by this method become undefined if
0N/A * the backing list (i.e., this List) is <i>structurally modified</i> in
0N/A * any way other than via the returned List. (Structural modifications are
0N/A * those that change the size of the List, or otherwise perturb it in such
0N/A * a fashion that iterations in progress may yield incorrect results.)
0N/A *
0N/A * @param fromIndex low endpoint (inclusive) of the subList
0N/A * @param toIndex high endpoint (exclusive) of the subList
0N/A * @return a view of the specified range within this List
0N/A * @throws IndexOutOfBoundsException if an endpoint index value is out of range
0N/A * {@code (fromIndex < 0 || toIndex > size)}
0N/A * @throws IllegalArgumentException if the endpoint indices are out of order
0N/A * {@code (fromIndex > toIndex)}
0N/A */
0N/A public synchronized List<E> subList(int fromIndex, int toIndex) {
0N/A return Collections.synchronizedList(super.subList(fromIndex, toIndex),
0N/A this);
0N/A }
0N/A
0N/A /**
0N/A * Removes from this list all of the elements whose index is between
0N/A * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
0N/A * Shifts any succeeding elements to the left (reduces their index).
0N/A * This call shortens the list by {@code (toIndex - fromIndex)} elements.
0N/A * (If {@code toIndex==fromIndex}, this operation has no effect.)
0N/A */
0N/A protected synchronized void removeRange(int fromIndex, int toIndex) {
0N/A modCount++;
0N/A int numMoved = elementCount - toIndex;
0N/A System.arraycopy(elementData, toIndex, elementData, fromIndex,
0N/A numMoved);
0N/A
0N/A // Let gc do its work
0N/A int newElementCount = elementCount - (toIndex-fromIndex);
0N/A while (elementCount != newElementCount)
0N/A elementData[--elementCount] = null;
0N/A }
0N/A
0N/A /**
0N/A * Save the state of the {@code Vector} instance to a stream (that
0N/A * is, serialize it).
0N/A * This method performs synchronization to ensure the consistency
0N/A * of the serialized data.
0N/A */
0N/A private void writeObject(java.io.ObjectOutputStream s)
0N/A throws java.io.IOException {
0N/A final java.io.ObjectOutputStream.PutField fields = s.putFields();
0N/A final Object[] data;
0N/A synchronized (this) {
0N/A fields.put("capacityIncrement", capacityIncrement);
0N/A fields.put("elementCount", elementCount);
0N/A data = elementData.clone();
0N/A }
0N/A fields.put("elementData", data);
0N/A s.writeFields();
0N/A }
0N/A
0N/A /**
0N/A * Returns a list iterator over the elements in this list (in proper
0N/A * sequence), starting at the specified position in the list.
0N/A * The specified index indicates the first element that would be
0N/A * returned by an initial call to {@link ListIterator#next next}.
0N/A * An initial call to {@link ListIterator#previous previous} would
0N/A * return the element with the specified index minus one.
0N/A *
0N/A * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
0N/A *
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public synchronized ListIterator<E> listIterator(int index) {
0N/A if (index < 0 || index > elementCount)
0N/A throw new IndexOutOfBoundsException("Index: "+index);
0N/A return new ListItr(index);
0N/A }
0N/A
0N/A /**
0N/A * Returns a list iterator over the elements in this list (in proper
0N/A * sequence).
0N/A *
0N/A * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
0N/A *
0N/A * @see #listIterator(int)
0N/A */
0N/A public synchronized ListIterator<E> listIterator() {
0N/A return new ListItr(0);
0N/A }
0N/A
0N/A /**
0N/A * Returns an iterator over the elements in this list in proper sequence.
0N/A *
0N/A * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
0N/A *
0N/A * @return an iterator over the elements in this list in proper sequence
0N/A */
0N/A public synchronized Iterator<E> iterator() {
0N/A return new Itr();
0N/A }
0N/A
0N/A /**
0N/A * An optimized version of AbstractList.Itr
0N/A */
0N/A private class Itr implements Iterator<E> {
0N/A int cursor; // index of next element to return
0N/A int lastRet = -1; // index of last element returned; -1 if no such
0N/A int expectedModCount = modCount;
0N/A
0N/A public boolean hasNext() {
0N/A // Racy but within spec, since modifications are checked
0N/A // within or after synchronization in next/previous
0N/A return cursor != elementCount;
0N/A }
0N/A
0N/A public E next() {
0N/A synchronized (Vector.this) {
0N/A checkForComodification();
0N/A int i = cursor;
0N/A if (i >= elementCount)
0N/A throw new NoSuchElementException();
0N/A cursor = i + 1;
0N/A return elementData(lastRet = i);
0N/A }
0N/A }
0N/A
0N/A public void remove() {
0N/A if (lastRet == -1)
0N/A throw new IllegalStateException();
0N/A synchronized (Vector.this) {
0N/A checkForComodification();
0N/A Vector.this.remove(lastRet);
0N/A expectedModCount = modCount;
0N/A }
0N/A cursor = lastRet;
0N/A lastRet = -1;
0N/A }
0N/A
0N/A final void checkForComodification() {
0N/A if (modCount != expectedModCount)
0N/A throw new ConcurrentModificationException();
0N/A }
0N/A }
0N/A
/**
* An optimized version of AbstractList.ListItr
*/
final class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
public E previous() {
synchronized (Vector.this) {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
cursor = i;
return elementData(lastRet = i);
}
}
public void set(E e) {
if (lastRet == -1)
throw new IllegalStateException();
synchronized (Vector.this) {
checkForComodification();
Vector.this.set(lastRet, e);
}
}
public void add(E e) {
int i = cursor;
synchronized (Vector.this) {
checkForComodification();
Vector.this.add(i, e);
expectedModCount = modCount;
}
cursor = i + 1;
lastRet = -1;
}
}
}