jaroslav@597: /* jaroslav@597: * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. jaroslav@597: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@597: * jaroslav@597: * This code is free software; you can redistribute it and/or modify it jaroslav@597: * under the terms of the GNU General Public License version 2 only, as jaroslav@597: * published by the Free Software Foundation. Oracle designates this jaroslav@597: * particular file as subject to the "Classpath" exception as provided jaroslav@597: * by Oracle in the LICENSE file that accompanied this code. jaroslav@597: * jaroslav@597: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@597: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@597: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@597: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@597: * accompanied this code). jaroslav@597: * jaroslav@597: * You should have received a copy of the GNU General Public License version jaroslav@597: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@597: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@597: * jaroslav@597: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@597: * or visit www.oracle.com if you need additional information or have any jaroslav@597: * questions. jaroslav@597: */ jaroslav@597: jaroslav@597: package java.util; jaroslav@597: jaroslav@599: import org.apidesign.bck2brwsr.emul.lang.System; jaroslav@599: jaroslav@597: /** jaroslav@597: * The {@code Vector} class implements a growable array of jaroslav@597: * objects. Like an array, it contains components that can be jaroslav@597: * accessed using an integer index. However, the size of a jaroslav@597: * {@code Vector} can grow or shrink as needed to accommodate jaroslav@597: * adding and removing items after the {@code Vector} has been created. jaroslav@597: * jaroslav@597: *

Each vector tries to optimize storage management by maintaining a jaroslav@597: * {@code capacity} and a {@code capacityIncrement}. The jaroslav@597: * {@code capacity} is always at least as large as the vector jaroslav@597: * size; it is usually larger because as components are added to the jaroslav@597: * vector, the vector's storage increases in chunks the size of jaroslav@597: * {@code capacityIncrement}. An application can increase the jaroslav@597: * capacity of a vector before inserting a large number of jaroslav@597: * components; this reduces the amount of incremental reallocation. jaroslav@597: * jaroslav@597: *

jaroslav@597: * The iterators returned by this class's {@link #iterator() iterator} and jaroslav@597: * {@link #listIterator(int) listIterator} methods are fail-fast: jaroslav@597: * if the vector is structurally modified at any time after the iterator is jaroslav@597: * created, in any way except through the iterator's own jaroslav@597: * {@link ListIterator#remove() remove} or jaroslav@597: * {@link ListIterator#add(Object) add} methods, the iterator will throw a jaroslav@597: * {@link ConcurrentModificationException}. Thus, in the face of jaroslav@597: * concurrent modification, the iterator fails quickly and cleanly, rather jaroslav@597: * than risking arbitrary, non-deterministic behavior at an undetermined jaroslav@597: * time in the future. The {@link Enumeration Enumerations} returned by jaroslav@597: * the {@link #elements() elements} method are not fail-fast. jaroslav@597: * jaroslav@597: *

Note that the fail-fast behavior of an iterator cannot be guaranteed jaroslav@597: * as it is, generally speaking, impossible to make any hard guarantees in the jaroslav@597: * presence of unsynchronized concurrent modification. Fail-fast iterators jaroslav@597: * throw {@code ConcurrentModificationException} on a best-effort basis. jaroslav@597: * Therefore, it would be wrong to write a program that depended on this jaroslav@597: * exception for its correctness: the fail-fast behavior of iterators jaroslav@597: * should be used only to detect bugs. jaroslav@597: * jaroslav@597: *

As of the Java 2 platform v1.2, this class was retrofitted to jaroslav@597: * implement the {@link List} interface, making it a member of the jaroslav@597: * jaroslav@597: * Java Collections Framework. Unlike the new collection jaroslav@597: * implementations, {@code Vector} is synchronized. If a thread-safe jaroslav@597: * implementation is not needed, it is recommended to use {@link jaroslav@597: * ArrayList} in place of {@code Vector}. jaroslav@597: * jaroslav@597: * @author Lee Boynton jaroslav@597: * @author Jonathan Payne jaroslav@597: * @see Collection jaroslav@597: * @see LinkedList jaroslav@597: * @since JDK1.0 jaroslav@597: */ jaroslav@597: public class Vector jaroslav@597: extends AbstractList jaroslav@597: implements List, RandomAccess, Cloneable, java.io.Serializable jaroslav@597: { jaroslav@597: /** jaroslav@597: * The array buffer into which the components of the vector are jaroslav@597: * stored. The capacity of the vector is the length of this array buffer, jaroslav@597: * and is at least large enough to contain all the vector's elements. jaroslav@597: * jaroslav@597: *

Any array elements following the last element in the Vector are null. jaroslav@597: * jaroslav@597: * @serial jaroslav@597: */ jaroslav@597: protected Object[] elementData; jaroslav@597: jaroslav@597: /** jaroslav@597: * The number of valid components in this {@code Vector} object. jaroslav@597: * Components {@code elementData[0]} through jaroslav@597: * {@code elementData[elementCount-1]} are the actual items. jaroslav@597: * jaroslav@597: * @serial jaroslav@597: */ jaroslav@597: protected int elementCount; jaroslav@597: jaroslav@597: /** jaroslav@597: * The amount by which the capacity of the vector is automatically jaroslav@597: * incremented when its size becomes greater than its capacity. If jaroslav@597: * the capacity increment is less than or equal to zero, the capacity jaroslav@597: * of the vector is doubled each time it needs to grow. jaroslav@597: * jaroslav@597: * @serial jaroslav@597: */ jaroslav@597: protected int capacityIncrement; jaroslav@597: jaroslav@597: /** use serialVersionUID from JDK 1.0.2 for interoperability */ jaroslav@597: private static final long serialVersionUID = -2767605614048989439L; jaroslav@597: jaroslav@597: /** jaroslav@597: * Constructs an empty vector with the specified initial capacity and jaroslav@597: * capacity increment. jaroslav@597: * jaroslav@597: * @param initialCapacity the initial capacity of the vector jaroslav@597: * @param capacityIncrement the amount by which the capacity is jaroslav@597: * increased when the vector overflows jaroslav@597: * @throws IllegalArgumentException if the specified initial capacity jaroslav@597: * is negative jaroslav@597: */ jaroslav@597: public Vector(int initialCapacity, int capacityIncrement) { jaroslav@597: super(); jaroslav@597: if (initialCapacity < 0) jaroslav@597: throw new IllegalArgumentException("Illegal Capacity: "+ jaroslav@597: initialCapacity); jaroslav@597: this.elementData = new Object[initialCapacity]; jaroslav@597: this.capacityIncrement = capacityIncrement; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Constructs an empty vector with the specified initial capacity and jaroslav@597: * with its capacity increment equal to zero. jaroslav@597: * jaroslav@597: * @param initialCapacity the initial capacity of the vector jaroslav@597: * @throws IllegalArgumentException if the specified initial capacity jaroslav@597: * is negative jaroslav@597: */ jaroslav@597: public Vector(int initialCapacity) { jaroslav@597: this(initialCapacity, 0); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Constructs an empty vector so that its internal data array jaroslav@597: * has size {@code 10} and its standard capacity increment is jaroslav@597: * zero. jaroslav@597: */ jaroslav@597: public Vector() { jaroslav@597: this(10); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Constructs a vector containing the elements of the specified jaroslav@597: * collection, in the order they are returned by the collection's jaroslav@597: * iterator. jaroslav@597: * jaroslav@597: * @param c the collection whose elements are to be placed into this jaroslav@597: * vector jaroslav@597: * @throws NullPointerException if the specified collection is null jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public Vector(Collection c) { jaroslav@597: elementData = c.toArray(); jaroslav@597: elementCount = elementData.length; jaroslav@597: // c.toArray might (incorrectly) not return Object[] (see 6260652) jaroslav@597: if (elementData.getClass() != Object[].class) jaroslav@597: elementData = Arrays.copyOf(elementData, elementCount, Object[].class); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Copies the components of this vector into the specified array. jaroslav@597: * The item at index {@code k} in this vector is copied into jaroslav@597: * component {@code k} of {@code anArray}. jaroslav@597: * jaroslav@597: * @param anArray the array into which the components get copied jaroslav@597: * @throws NullPointerException if the given array is null jaroslav@597: * @throws IndexOutOfBoundsException if the specified array is not jaroslav@597: * large enough to hold all the components of this vector jaroslav@597: * @throws ArrayStoreException if a component of this vector is not of jaroslav@597: * a runtime type that can be stored in the specified array jaroslav@597: * @see #toArray(Object[]) jaroslav@597: */ jaroslav@597: public synchronized void copyInto(Object[] anArray) { jaroslav@597: System.arraycopy(elementData, 0, anArray, 0, elementCount); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Trims the capacity of this vector to be the vector's current jaroslav@597: * size. If the capacity of this vector is larger than its current jaroslav@597: * size, then the capacity is changed to equal the size by replacing jaroslav@597: * its internal data array, kept in the field {@code elementData}, jaroslav@597: * with a smaller one. An application can use this operation to jaroslav@597: * minimize the storage of a vector. jaroslav@597: */ jaroslav@597: public synchronized void trimToSize() { jaroslav@597: modCount++; jaroslav@597: int oldCapacity = elementData.length; jaroslav@597: if (elementCount < oldCapacity) { jaroslav@597: elementData = Arrays.copyOf(elementData, elementCount); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Increases the capacity of this vector, if necessary, to ensure jaroslav@597: * that it can hold at least the number of components specified by jaroslav@597: * the minimum capacity argument. jaroslav@597: * jaroslav@597: *

If the current capacity of this vector is less than jaroslav@597: * {@code minCapacity}, then its capacity is increased by replacing its jaroslav@597: * internal data array, kept in the field {@code elementData}, with a jaroslav@597: * larger one. The size of the new data array will be the old size plus jaroslav@597: * {@code capacityIncrement}, unless the value of jaroslav@597: * {@code capacityIncrement} is less than or equal to zero, in which case jaroslav@597: * the new capacity will be twice the old capacity; but if this new size jaroslav@597: * is still smaller than {@code minCapacity}, then the new capacity will jaroslav@597: * be {@code minCapacity}. jaroslav@597: * jaroslav@597: * @param minCapacity the desired minimum capacity jaroslav@597: */ jaroslav@597: public synchronized void ensureCapacity(int minCapacity) { jaroslav@597: if (minCapacity > 0) { jaroslav@597: modCount++; jaroslav@597: ensureCapacityHelper(minCapacity); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * This implements the unsynchronized semantics of ensureCapacity. jaroslav@597: * Synchronized methods in this class can internally call this jaroslav@597: * method for ensuring capacity without incurring the cost of an jaroslav@597: * extra synchronization. jaroslav@597: * jaroslav@597: * @see #ensureCapacity(int) jaroslav@597: */ jaroslav@597: private void ensureCapacityHelper(int minCapacity) { jaroslav@597: // overflow-conscious code jaroslav@597: if (minCapacity - elementData.length > 0) jaroslav@597: grow(minCapacity); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * The maximum size of array to allocate. jaroslav@597: * Some VMs reserve some header words in an array. jaroslav@597: * Attempts to allocate larger arrays may result in jaroslav@597: * OutOfMemoryError: Requested array size exceeds VM limit jaroslav@597: */ jaroslav@597: private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; jaroslav@597: jaroslav@597: private void grow(int minCapacity) { jaroslav@597: // overflow-conscious code jaroslav@597: int oldCapacity = elementData.length; jaroslav@597: int newCapacity = oldCapacity + ((capacityIncrement > 0) ? jaroslav@597: capacityIncrement : oldCapacity); jaroslav@597: if (newCapacity - minCapacity < 0) jaroslav@597: newCapacity = minCapacity; jaroslav@597: if (newCapacity - MAX_ARRAY_SIZE > 0) jaroslav@597: newCapacity = hugeCapacity(minCapacity); jaroslav@597: elementData = Arrays.copyOf(elementData, newCapacity); jaroslav@597: } jaroslav@597: jaroslav@597: private static int hugeCapacity(int minCapacity) { jaroslav@597: if (minCapacity < 0) // overflow jaroslav@597: throw new OutOfMemoryError(); jaroslav@597: return (minCapacity > MAX_ARRAY_SIZE) ? jaroslav@597: Integer.MAX_VALUE : jaroslav@597: MAX_ARRAY_SIZE; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Sets the size of this vector. If the new size is greater than the jaroslav@597: * current size, new {@code null} items are added to the end of jaroslav@597: * the vector. If the new size is less than the current size, all jaroslav@597: * components at index {@code newSize} and greater are discarded. jaroslav@597: * jaroslav@597: * @param newSize the new size of this vector jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the new size is negative jaroslav@597: */ jaroslav@597: public synchronized void setSize(int newSize) { jaroslav@597: modCount++; jaroslav@597: if (newSize > elementCount) { jaroslav@597: ensureCapacityHelper(newSize); jaroslav@597: } else { jaroslav@597: for (int i = newSize ; i < elementCount ; i++) { jaroslav@597: elementData[i] = null; jaroslav@597: } jaroslav@597: } jaroslav@597: elementCount = newSize; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the current capacity of this vector. jaroslav@597: * jaroslav@597: * @return the current capacity (the length of its internal jaroslav@597: * data array, kept in the field {@code elementData} jaroslav@597: * of this vector) jaroslav@597: */ jaroslav@597: public synchronized int capacity() { jaroslav@597: return elementData.length; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the number of components in this vector. jaroslav@597: * jaroslav@597: * @return the number of components in this vector jaroslav@597: */ jaroslav@597: public synchronized int size() { jaroslav@597: return elementCount; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Tests if this vector has no components. jaroslav@597: * jaroslav@597: * @return {@code true} if and only if this vector has jaroslav@597: * no components, that is, its size is zero; jaroslav@597: * {@code false} otherwise. jaroslav@597: */ jaroslav@597: public synchronized boolean isEmpty() { jaroslav@597: return elementCount == 0; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns an enumeration of the components of this vector. The jaroslav@597: * returned {@code Enumeration} object will generate all items in jaroslav@597: * this vector. The first item generated is the item at index {@code 0}, jaroslav@597: * then the item at index {@code 1}, and so on. jaroslav@597: * jaroslav@597: * @return an enumeration of the components of this vector jaroslav@597: * @see Iterator jaroslav@597: */ jaroslav@597: public Enumeration elements() { jaroslav@597: return new Enumeration() { jaroslav@597: int count = 0; jaroslav@597: jaroslav@597: public boolean hasMoreElements() { jaroslav@597: return count < elementCount; jaroslav@597: } jaroslav@597: jaroslav@597: public E nextElement() { jaroslav@597: synchronized (Vector.this) { jaroslav@597: if (count < elementCount) { jaroslav@597: return elementData(count++); jaroslav@597: } jaroslav@597: } jaroslav@597: throw new NoSuchElementException("Vector Enumeration"); jaroslav@597: } jaroslav@597: }; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns {@code true} if this vector contains the specified element. jaroslav@597: * More formally, returns {@code true} if and only if this vector jaroslav@597: * contains at least one element {@code e} such that jaroslav@597: * (o==null ? e==null : o.equals(e)). jaroslav@597: * jaroslav@597: * @param o element whose presence in this vector is to be tested jaroslav@597: * @return {@code true} if this vector contains the specified element jaroslav@597: */ jaroslav@597: public boolean contains(Object o) { jaroslav@597: return indexOf(o, 0) >= 0; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the index of the first occurrence of the specified element jaroslav@597: * in this vector, or -1 if this vector does not contain the element. jaroslav@597: * More formally, returns the lowest index {@code i} such that jaroslav@597: * (o==null ? get(i)==null : o.equals(get(i))), jaroslav@597: * or -1 if there is no such index. jaroslav@597: * jaroslav@597: * @param o element to search for jaroslav@597: * @return the index of the first occurrence of the specified element in jaroslav@597: * this vector, or -1 if this vector does not contain the element jaroslav@597: */ jaroslav@597: public int indexOf(Object o) { jaroslav@597: return indexOf(o, 0); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the index of the first occurrence of the specified element in jaroslav@597: * this vector, searching forwards from {@code index}, or returns -1 if jaroslav@597: * the element is not found. jaroslav@597: * More formally, returns the lowest index {@code i} such that jaroslav@597: * (i >= index && (o==null ? get(i)==null : o.equals(get(i)))), jaroslav@597: * or -1 if there is no such index. jaroslav@597: * jaroslav@597: * @param o element to search for jaroslav@597: * @param index index to start searching from jaroslav@597: * @return the index of the first occurrence of the element in jaroslav@597: * this vector at position {@code index} or later in the vector; jaroslav@597: * {@code -1} if the element is not found. jaroslav@597: * @throws IndexOutOfBoundsException if the specified index is negative jaroslav@597: * @see Object#equals(Object) jaroslav@597: */ jaroslav@597: public synchronized int indexOf(Object o, int index) { jaroslav@597: if (o == null) { jaroslav@597: for (int i = index ; i < elementCount ; i++) jaroslav@597: if (elementData[i]==null) jaroslav@597: return i; jaroslav@597: } else { jaroslav@597: for (int i = index ; i < elementCount ; i++) jaroslav@597: if (o.equals(elementData[i])) jaroslav@597: return i; jaroslav@597: } jaroslav@597: return -1; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the index of the last occurrence of the specified element jaroslav@597: * in this vector, or -1 if this vector does not contain the element. jaroslav@597: * More formally, returns the highest index {@code i} such that jaroslav@597: * (o==null ? get(i)==null : o.equals(get(i))), jaroslav@597: * or -1 if there is no such index. jaroslav@597: * jaroslav@597: * @param o element to search for jaroslav@597: * @return the index of the last occurrence of the specified element in jaroslav@597: * this vector, or -1 if this vector does not contain the element jaroslav@597: */ jaroslav@597: public synchronized int lastIndexOf(Object o) { jaroslav@597: return lastIndexOf(o, elementCount-1); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the index of the last occurrence of the specified element in jaroslav@597: * this vector, searching backwards from {@code index}, or returns -1 if jaroslav@597: * the element is not found. jaroslav@597: * More formally, returns the highest index {@code i} such that jaroslav@597: * (i <= index && (o==null ? get(i)==null : o.equals(get(i)))), jaroslav@597: * or -1 if there is no such index. jaroslav@597: * jaroslav@597: * @param o element to search for jaroslav@597: * @param index index to start searching backwards from jaroslav@597: * @return the index of the last occurrence of the element at position jaroslav@597: * less than or equal to {@code index} in this vector; jaroslav@597: * -1 if the element is not found. jaroslav@597: * @throws IndexOutOfBoundsException if the specified index is greater jaroslav@597: * than or equal to the current size of this vector jaroslav@597: */ jaroslav@597: public synchronized int lastIndexOf(Object o, int index) { jaroslav@597: if (index >= elementCount) jaroslav@597: throw new IndexOutOfBoundsException(index + " >= "+ elementCount); jaroslav@597: jaroslav@597: if (o == null) { jaroslav@597: for (int i = index; i >= 0; i--) jaroslav@597: if (elementData[i]==null) jaroslav@597: return i; jaroslav@597: } else { jaroslav@597: for (int i = index; i >= 0; i--) jaroslav@597: if (o.equals(elementData[i])) jaroslav@597: return i; jaroslav@597: } jaroslav@597: return -1; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the component at the specified index. jaroslav@597: * jaroslav@597: *

This method is identical in functionality to the {@link #get(int)} jaroslav@597: * method (which is part of the {@link List} interface). jaroslav@597: * jaroslav@597: * @param index an index into this vector jaroslav@597: * @return the component at the specified index jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range jaroslav@597: * ({@code index < 0 || index >= size()}) jaroslav@597: */ jaroslav@597: public synchronized E elementAt(int index) { jaroslav@597: if (index >= elementCount) { jaroslav@597: throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); jaroslav@597: } jaroslav@597: jaroslav@597: return elementData(index); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the first component (the item at index {@code 0}) of jaroslav@597: * this vector. jaroslav@597: * jaroslav@597: * @return the first component of this vector jaroslav@597: * @throws NoSuchElementException if this vector has no components jaroslav@597: */ jaroslav@597: public synchronized E firstElement() { jaroslav@597: if (elementCount == 0) { jaroslav@597: throw new NoSuchElementException(); jaroslav@597: } jaroslav@597: return elementData(0); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the last component of the vector. jaroslav@597: * jaroslav@597: * @return the last component of the vector, i.e., the component at index jaroslav@597: * size() - 1. jaroslav@597: * @throws NoSuchElementException if this vector is empty jaroslav@597: */ jaroslav@597: public synchronized E lastElement() { jaroslav@597: if (elementCount == 0) { jaroslav@597: throw new NoSuchElementException(); jaroslav@597: } jaroslav@597: return elementData(elementCount - 1); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Sets the component at the specified {@code index} of this jaroslav@597: * vector to be the specified object. The previous component at that jaroslav@597: * position is discarded. jaroslav@597: * jaroslav@597: *

The index must be a value greater than or equal to {@code 0} jaroslav@597: * and less than the current size of the vector. jaroslav@597: * jaroslav@597: *

This method is identical in functionality to the jaroslav@597: * {@link #set(int, Object) set(int, E)} jaroslav@597: * method (which is part of the {@link List} interface). Note that the jaroslav@597: * {@code set} method reverses the order of the parameters, to more closely jaroslav@597: * match array usage. Note also that the {@code set} method returns the jaroslav@597: * old value that was stored at the specified position. jaroslav@597: * jaroslav@597: * @param obj what the component is to be set to jaroslav@597: * @param index the specified index jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range jaroslav@597: * ({@code index < 0 || index >= size()}) jaroslav@597: */ jaroslav@597: public synchronized void setElementAt(E obj, int index) { jaroslav@597: if (index >= elementCount) { jaroslav@597: throw new ArrayIndexOutOfBoundsException(index + " >= " + jaroslav@597: elementCount); jaroslav@597: } jaroslav@597: elementData[index] = obj; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Deletes the component at the specified index. Each component in jaroslav@597: * this vector with an index greater or equal to the specified jaroslav@597: * {@code index} is shifted downward to have an index one jaroslav@597: * smaller than the value it had previously. The size of this vector jaroslav@597: * is decreased by {@code 1}. jaroslav@597: * jaroslav@597: *

The index must be a value greater than or equal to {@code 0} jaroslav@597: * and less than the current size of the vector. jaroslav@597: * jaroslav@597: *

This method is identical in functionality to the {@link #remove(int)} jaroslav@597: * method (which is part of the {@link List} interface). Note that the jaroslav@597: * {@code remove} method returns the old value that was stored at the jaroslav@597: * specified position. jaroslav@597: * jaroslav@597: * @param index the index of the object to remove jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range jaroslav@597: * ({@code index < 0 || index >= size()}) jaroslav@597: */ jaroslav@597: public synchronized void removeElementAt(int index) { jaroslav@597: modCount++; jaroslav@597: if (index >= elementCount) { jaroslav@597: throw new ArrayIndexOutOfBoundsException(index + " >= " + jaroslav@597: elementCount); jaroslav@597: } jaroslav@597: else if (index < 0) { jaroslav@597: throw new ArrayIndexOutOfBoundsException(index); jaroslav@597: } jaroslav@597: int j = elementCount - index - 1; jaroslav@597: if (j > 0) { jaroslav@597: System.arraycopy(elementData, index + 1, elementData, index, j); jaroslav@597: } jaroslav@597: elementCount--; jaroslav@597: elementData[elementCount] = null; /* to let gc do its work */ jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Inserts the specified object as a component in this vector at the jaroslav@597: * specified {@code index}. Each component in this vector with jaroslav@597: * an index greater or equal to the specified {@code index} is jaroslav@597: * shifted upward to have an index one greater than the value it had jaroslav@597: * previously. jaroslav@597: * jaroslav@597: *

The index must be a value greater than or equal to {@code 0} jaroslav@597: * and less than or equal to the current size of the vector. (If the jaroslav@597: * index is equal to the current size of the vector, the new element jaroslav@597: * is appended to the Vector.) jaroslav@597: * jaroslav@597: *

This method is identical in functionality to the jaroslav@597: * {@link #add(int, Object) add(int, E)} jaroslav@597: * method (which is part of the {@link List} interface). Note that the jaroslav@597: * {@code add} method reverses the order of the parameters, to more closely jaroslav@597: * match array usage. jaroslav@597: * jaroslav@597: * @param obj the component to insert jaroslav@597: * @param index where to insert the new component jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range jaroslav@597: * ({@code index < 0 || index > size()}) jaroslav@597: */ jaroslav@597: public synchronized void insertElementAt(E obj, int index) { jaroslav@597: modCount++; jaroslav@597: if (index > elementCount) { jaroslav@597: throw new ArrayIndexOutOfBoundsException(index jaroslav@597: + " > " + elementCount); jaroslav@597: } jaroslav@597: ensureCapacityHelper(elementCount + 1); jaroslav@597: System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); jaroslav@597: elementData[index] = obj; jaroslav@597: elementCount++; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Adds the specified component to the end of this vector, jaroslav@597: * increasing its size by one. The capacity of this vector is jaroslav@597: * increased if its size becomes greater than its capacity. jaroslav@597: * jaroslav@597: *

This method is identical in functionality to the jaroslav@597: * {@link #add(Object) add(E)} jaroslav@597: * method (which is part of the {@link List} interface). jaroslav@597: * jaroslav@597: * @param obj the component to be added jaroslav@597: */ jaroslav@597: public synchronized void addElement(E obj) { jaroslav@597: modCount++; jaroslav@597: ensureCapacityHelper(elementCount + 1); jaroslav@597: elementData[elementCount++] = obj; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Removes the first (lowest-indexed) occurrence of the argument jaroslav@597: * from this vector. If the object is found in this vector, each jaroslav@597: * component in the vector with an index greater or equal to the jaroslav@597: * object's index is shifted downward to have an index one smaller jaroslav@597: * than the value it had previously. jaroslav@597: * jaroslav@597: *

This method is identical in functionality to the jaroslav@597: * {@link #remove(Object)} method (which is part of the jaroslav@597: * {@link List} interface). jaroslav@597: * jaroslav@597: * @param obj the component to be removed jaroslav@597: * @return {@code true} if the argument was a component of this jaroslav@597: * vector; {@code false} otherwise. jaroslav@597: */ jaroslav@597: public synchronized boolean removeElement(Object obj) { jaroslav@597: modCount++; jaroslav@597: int i = indexOf(obj); jaroslav@597: if (i >= 0) { jaroslav@597: removeElementAt(i); jaroslav@597: return true; jaroslav@597: } jaroslav@597: return false; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Removes all components from this vector and sets its size to zero. jaroslav@597: * jaroslav@597: *

This method is identical in functionality to the {@link #clear} jaroslav@597: * method (which is part of the {@link List} interface). jaroslav@597: */ jaroslav@597: public synchronized void removeAllElements() { jaroslav@597: modCount++; jaroslav@597: // Let gc do its work jaroslav@597: for (int i = 0; i < elementCount; i++) jaroslav@597: elementData[i] = null; jaroslav@597: jaroslav@597: elementCount = 0; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a clone of this vector. The copy will contain a jaroslav@597: * reference to a clone of the internal data array, not a reference jaroslav@597: * to the original internal data array of this {@code Vector} object. jaroslav@597: * jaroslav@597: * @return a clone of this vector jaroslav@597: */ jaroslav@597: public synchronized Object clone() { jaroslav@597: try { jaroslav@597: @SuppressWarnings("unchecked") jaroslav@597: Vector v = (Vector) super.clone(); jaroslav@597: v.elementData = Arrays.copyOf(elementData, elementCount); jaroslav@597: v.modCount = 0; jaroslav@597: return v; jaroslav@597: } catch (CloneNotSupportedException e) { jaroslav@597: // this shouldn't happen, since we are Cloneable jaroslav@597: throw new InternalError(); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns an array containing all of the elements in this Vector jaroslav@597: * in the correct order. jaroslav@597: * jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public synchronized Object[] toArray() { jaroslav@597: return Arrays.copyOf(elementData, elementCount); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns an array containing all of the elements in this Vector in the jaroslav@597: * correct order; the runtime type of the returned array is that of the jaroslav@597: * specified array. If the Vector fits in the specified array, it is jaroslav@597: * returned therein. Otherwise, a new array is allocated with the runtime jaroslav@597: * type of the specified array and the size of this Vector. jaroslav@597: * jaroslav@597: *

If the Vector fits in the specified array with room to spare jaroslav@597: * (i.e., the array has more elements than the Vector), jaroslav@597: * the element in the array immediately following the end of the jaroslav@597: * Vector is set to null. (This is useful in determining the length jaroslav@597: * of the Vector only if the caller knows that the Vector jaroslav@597: * does not contain any null elements.) jaroslav@597: * jaroslav@597: * @param a the array into which the elements of the Vector are to jaroslav@597: * be stored, if it is big enough; otherwise, a new array of the jaroslav@597: * same runtime type is allocated for this purpose. jaroslav@597: * @return an array containing the elements of the Vector jaroslav@597: * @throws ArrayStoreException if the runtime type of a is not a supertype jaroslav@597: * of the runtime type of every element in this Vector jaroslav@597: * @throws NullPointerException if the given array is null jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: @SuppressWarnings("unchecked") jaroslav@597: public synchronized T[] toArray(T[] a) { jaroslav@597: if (a.length < elementCount) jaroslav@597: return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass()); jaroslav@597: jaroslav@597: System.arraycopy(elementData, 0, a, 0, elementCount); jaroslav@597: jaroslav@597: if (a.length > elementCount) jaroslav@597: a[elementCount] = null; jaroslav@597: jaroslav@597: return a; jaroslav@597: } jaroslav@597: jaroslav@597: // Positional Access Operations jaroslav@597: jaroslav@597: @SuppressWarnings("unchecked") jaroslav@597: E elementData(int index) { jaroslav@597: return (E) elementData[index]; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the element at the specified position in this Vector. jaroslav@597: * jaroslav@597: * @param index index of the element to return jaroslav@597: * @return object at the specified index jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range jaroslav@597: * ({@code index < 0 || index >= size()}) jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public synchronized E get(int index) { jaroslav@597: if (index >= elementCount) jaroslav@597: throw new ArrayIndexOutOfBoundsException(index); jaroslav@597: jaroslav@597: return elementData(index); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Replaces the element at the specified position in this Vector with the jaroslav@597: * specified element. jaroslav@597: * jaroslav@597: * @param index index of the element to replace jaroslav@597: * @param element element to be stored at the specified position jaroslav@597: * @return the element previously at the specified position jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range jaroslav@597: * ({@code index < 0 || index >= size()}) jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public synchronized E set(int index, E element) { jaroslav@597: if (index >= elementCount) jaroslav@597: throw new ArrayIndexOutOfBoundsException(index); jaroslav@597: jaroslav@597: E oldValue = elementData(index); jaroslav@597: elementData[index] = element; jaroslav@597: return oldValue; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Appends the specified element to the end of this Vector. jaroslav@597: * jaroslav@597: * @param e element to be appended to this Vector jaroslav@597: * @return {@code true} (as specified by {@link Collection#add}) jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public synchronized boolean add(E e) { jaroslav@597: modCount++; jaroslav@597: ensureCapacityHelper(elementCount + 1); jaroslav@597: elementData[elementCount++] = e; jaroslav@597: return true; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Removes the first occurrence of the specified element in this Vector jaroslav@597: * If the Vector does not contain the element, it is unchanged. More jaroslav@597: * formally, removes the element with the lowest index i such that jaroslav@597: * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such jaroslav@597: * an element exists). jaroslav@597: * jaroslav@597: * @param o element to be removed from this Vector, if present jaroslav@597: * @return true if the Vector contained the specified element jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public boolean remove(Object o) { jaroslav@597: return removeElement(o); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Inserts the specified element at the specified position in this Vector. jaroslav@597: * Shifts the element currently at that position (if any) and any jaroslav@597: * subsequent elements to the right (adds one to their indices). jaroslav@597: * jaroslav@597: * @param index index at which the specified element is to be inserted jaroslav@597: * @param element element to be inserted jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range jaroslav@597: * ({@code index < 0 || index > size()}) jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public void add(int index, E element) { jaroslav@597: insertElementAt(element, index); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Removes the element at the specified position in this Vector. jaroslav@597: * Shifts any subsequent elements to the left (subtracts one from their jaroslav@597: * indices). Returns the element that was removed from the Vector. jaroslav@597: * jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range jaroslav@597: * ({@code index < 0 || index >= size()}) jaroslav@597: * @param index the index of the element to be removed jaroslav@597: * @return element that was removed jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public synchronized E remove(int index) { jaroslav@597: modCount++; jaroslav@597: if (index >= elementCount) jaroslav@597: throw new ArrayIndexOutOfBoundsException(index); jaroslav@597: E oldValue = elementData(index); jaroslav@597: jaroslav@597: int numMoved = elementCount - index - 1; jaroslav@597: if (numMoved > 0) jaroslav@597: System.arraycopy(elementData, index+1, elementData, index, jaroslav@597: numMoved); jaroslav@597: elementData[--elementCount] = null; // Let gc do its work jaroslav@597: jaroslav@597: return oldValue; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Removes all of the elements from this Vector. The Vector will jaroslav@597: * be empty after this call returns (unless it throws an exception). jaroslav@597: * jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public void clear() { jaroslav@597: removeAllElements(); jaroslav@597: } jaroslav@597: jaroslav@597: // Bulk Operations jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns true if this Vector contains all of the elements in the jaroslav@597: * specified Collection. jaroslav@597: * jaroslav@597: * @param c a collection whose elements will be tested for containment jaroslav@597: * in this Vector jaroslav@597: * @return true if this Vector contains all of the elements in the jaroslav@597: * specified collection jaroslav@597: * @throws NullPointerException if the specified collection is null jaroslav@597: */ jaroslav@597: public synchronized boolean containsAll(Collection c) { jaroslav@597: return super.containsAll(c); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Appends all of the elements in the specified Collection to the end of jaroslav@597: * this Vector, in the order that they are returned by the specified jaroslav@597: * Collection's Iterator. The behavior of this operation is undefined if jaroslav@597: * the specified Collection is modified while the operation is in progress. jaroslav@597: * (This implies that the behavior of this call is undefined if the jaroslav@597: * specified Collection is this Vector, and this Vector is nonempty.) jaroslav@597: * jaroslav@597: * @param c elements to be inserted into this Vector jaroslav@597: * @return {@code true} if this Vector changed as a result of the call jaroslav@597: * @throws NullPointerException if the specified collection is null jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public synchronized boolean addAll(Collection c) { jaroslav@597: modCount++; jaroslav@597: Object[] a = c.toArray(); jaroslav@597: int numNew = a.length; jaroslav@597: ensureCapacityHelper(elementCount + numNew); jaroslav@597: System.arraycopy(a, 0, elementData, elementCount, numNew); jaroslav@597: elementCount += numNew; jaroslav@597: return numNew != 0; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Removes from this Vector all of its elements that are contained in the jaroslav@597: * specified Collection. jaroslav@597: * jaroslav@597: * @param c a collection of elements to be removed from the Vector jaroslav@597: * @return true if this Vector changed as a result of the call jaroslav@597: * @throws ClassCastException if the types of one or more elements jaroslav@597: * in this vector are incompatible with the specified jaroslav@597: * collection jaroslav@597: * (optional) jaroslav@597: * @throws NullPointerException if this vector contains one or more null jaroslav@597: * elements and the specified collection does not support null jaroslav@597: * elements jaroslav@597: * (optional), jaroslav@597: * or if the specified collection is null jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public synchronized boolean removeAll(Collection c) { jaroslav@597: return super.removeAll(c); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Retains only the elements in this Vector that are contained in the jaroslav@597: * specified Collection. In other words, removes from this Vector all jaroslav@597: * of its elements that are not contained in the specified Collection. jaroslav@597: * jaroslav@597: * @param c a collection of elements to be retained in this Vector jaroslav@597: * (all other elements are removed) jaroslav@597: * @return true if this Vector changed as a result of the call jaroslav@597: * @throws ClassCastException if the types of one or more elements jaroslav@597: * in this vector are incompatible with the specified jaroslav@597: * collection jaroslav@597: * (optional) jaroslav@597: * @throws NullPointerException if this vector contains one or more null jaroslav@597: * elements and the specified collection does not support null jaroslav@597: * elements jaroslav@597: * (optional), jaroslav@597: * or if the specified collection is null jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public synchronized boolean retainAll(Collection c) { jaroslav@597: return super.retainAll(c); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Inserts all of the elements in the specified Collection into this jaroslav@597: * Vector at the specified position. Shifts the element currently at jaroslav@597: * that position (if any) and any subsequent elements to the right jaroslav@597: * (increases their indices). The new elements will appear in the Vector jaroslav@597: * in the order that they are returned by the specified Collection's jaroslav@597: * iterator. jaroslav@597: * jaroslav@597: * @param index index at which to insert the first element from the jaroslav@597: * specified collection jaroslav@597: * @param c elements to be inserted into this Vector jaroslav@597: * @return {@code true} if this Vector changed as a result of the call jaroslav@597: * @throws ArrayIndexOutOfBoundsException if the index is out of range jaroslav@597: * ({@code index < 0 || index > size()}) jaroslav@597: * @throws NullPointerException if the specified collection is null jaroslav@597: * @since 1.2 jaroslav@597: */ jaroslav@597: public synchronized boolean addAll(int index, Collection c) { jaroslav@597: modCount++; jaroslav@597: if (index < 0 || index > elementCount) jaroslav@597: throw new ArrayIndexOutOfBoundsException(index); jaroslav@597: jaroslav@597: Object[] a = c.toArray(); jaroslav@597: int numNew = a.length; jaroslav@597: ensureCapacityHelper(elementCount + numNew); jaroslav@597: jaroslav@597: int numMoved = elementCount - index; jaroslav@597: if (numMoved > 0) jaroslav@597: System.arraycopy(elementData, index, elementData, index + numNew, jaroslav@597: numMoved); jaroslav@597: jaroslav@597: System.arraycopy(a, 0, elementData, index, numNew); jaroslav@597: elementCount += numNew; jaroslav@597: return numNew != 0; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Compares the specified Object with this Vector for equality. Returns jaroslav@597: * true if and only if the specified Object is also a List, both Lists jaroslav@597: * have the same size, and all corresponding pairs of elements in the two jaroslav@597: * Lists are equal. (Two elements {@code e1} and jaroslav@597: * {@code e2} are equal if {@code (e1==null ? e2==null : jaroslav@597: * e1.equals(e2))}.) In other words, two Lists are defined to be jaroslav@597: * equal if they contain the same elements in the same order. jaroslav@597: * jaroslav@597: * @param o the Object to be compared for equality with this Vector jaroslav@597: * @return true if the specified Object is equal to this Vector jaroslav@597: */ jaroslav@597: public synchronized boolean equals(Object o) { jaroslav@597: return super.equals(o); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns the hash code value for this Vector. jaroslav@597: */ jaroslav@597: public synchronized int hashCode() { jaroslav@597: return super.hashCode(); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a string representation of this Vector, containing jaroslav@597: * the String representation of each element. jaroslav@597: */ jaroslav@597: public synchronized String toString() { jaroslav@597: return super.toString(); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a view of the portion of this List between fromIndex, jaroslav@597: * inclusive, and toIndex, exclusive. (If fromIndex and toIndex are jaroslav@597: * equal, the returned List is empty.) The returned List is backed by this jaroslav@597: * List, so changes in the returned List are reflected in this List, and jaroslav@597: * vice-versa. The returned List supports all of the optional List jaroslav@597: * operations supported by this List. jaroslav@597: * jaroslav@597: *

This method eliminates the need for explicit range operations (of jaroslav@597: * the sort that commonly exist for arrays). Any operation that expects jaroslav@597: * a List can be used as a range operation by operating on a subList view jaroslav@597: * instead of a whole List. For example, the following idiom jaroslav@597: * removes a range of elements from a List: jaroslav@597: *

jaroslav@597:      *      list.subList(from, to).clear();
jaroslav@597:      * 
jaroslav@597: * Similar idioms may be constructed for indexOf and lastIndexOf, jaroslav@597: * and all of the algorithms in the Collections class can be applied to jaroslav@597: * a subList. jaroslav@597: * jaroslav@597: *

The semantics of the List returned by this method become undefined if jaroslav@597: * the backing list (i.e., this List) is structurally modified in jaroslav@597: * any way other than via the returned List. (Structural modifications are jaroslav@597: * those that change the size of the List, or otherwise perturb it in such jaroslav@597: * a fashion that iterations in progress may yield incorrect results.) jaroslav@597: * jaroslav@597: * @param fromIndex low endpoint (inclusive) of the subList jaroslav@597: * @param toIndex high endpoint (exclusive) of the subList jaroslav@597: * @return a view of the specified range within this List jaroslav@597: * @throws IndexOutOfBoundsException if an endpoint index value is out of range jaroslav@597: * {@code (fromIndex < 0 || toIndex > size)} jaroslav@597: * @throws IllegalArgumentException if the endpoint indices are out of order jaroslav@597: * {@code (fromIndex > toIndex)} jaroslav@597: */ jaroslav@597: public synchronized List subList(int fromIndex, int toIndex) { jaroslav@597: return Collections.synchronizedList(super.subList(fromIndex, toIndex), jaroslav@597: this); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Removes from this list all of the elements whose index is between jaroslav@597: * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. jaroslav@597: * Shifts any succeeding elements to the left (reduces their index). jaroslav@597: * This call shortens the list by {@code (toIndex - fromIndex)} elements. jaroslav@597: * (If {@code toIndex==fromIndex}, this operation has no effect.) jaroslav@597: */ jaroslav@597: protected synchronized void removeRange(int fromIndex, int toIndex) { jaroslav@597: modCount++; jaroslav@597: int numMoved = elementCount - toIndex; jaroslav@597: System.arraycopy(elementData, toIndex, elementData, fromIndex, jaroslav@597: numMoved); jaroslav@597: jaroslav@597: // Let gc do its work jaroslav@597: int newElementCount = elementCount - (toIndex-fromIndex); jaroslav@597: while (elementCount != newElementCount) jaroslav@597: elementData[--elementCount] = null; jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a list iterator over the elements in this list (in proper jaroslav@597: * sequence), starting at the specified position in the list. jaroslav@597: * The specified index indicates the first element that would be jaroslav@597: * returned by an initial call to {@link ListIterator#next next}. jaroslav@597: * An initial call to {@link ListIterator#previous previous} would jaroslav@597: * return the element with the specified index minus one. jaroslav@597: * jaroslav@597: *

The returned list iterator is fail-fast. jaroslav@597: * jaroslav@597: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@597: */ jaroslav@597: public synchronized ListIterator listIterator(int index) { jaroslav@597: if (index < 0 || index > elementCount) jaroslav@597: throw new IndexOutOfBoundsException("Index: "+index); jaroslav@597: return new ListItr(index); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns a list iterator over the elements in this list (in proper jaroslav@597: * sequence). jaroslav@597: * jaroslav@597: *

The returned list iterator is fail-fast. jaroslav@597: * jaroslav@597: * @see #listIterator(int) jaroslav@597: */ jaroslav@597: public synchronized ListIterator listIterator() { jaroslav@597: return new ListItr(0); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * Returns an iterator over the elements in this list in proper sequence. jaroslav@597: * jaroslav@597: *

The returned iterator is fail-fast. jaroslav@597: * jaroslav@597: * @return an iterator over the elements in this list in proper sequence jaroslav@597: */ jaroslav@597: public synchronized Iterator iterator() { jaroslav@597: return new Itr(); jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * An optimized version of AbstractList.Itr jaroslav@597: */ jaroslav@597: private class Itr implements Iterator { jaroslav@597: int cursor; // index of next element to return jaroslav@597: int lastRet = -1; // index of last element returned; -1 if no such jaroslav@597: int expectedModCount = modCount; jaroslav@597: jaroslav@597: public boolean hasNext() { jaroslav@597: // Racy but within spec, since modifications are checked jaroslav@597: // within or after synchronization in next/previous jaroslav@597: return cursor != elementCount; jaroslav@597: } jaroslav@597: jaroslav@597: public E next() { jaroslav@597: synchronized (Vector.this) { jaroslav@597: checkForComodification(); jaroslav@597: int i = cursor; jaroslav@597: if (i >= elementCount) jaroslav@597: throw new NoSuchElementException(); jaroslav@597: cursor = i + 1; jaroslav@597: return elementData(lastRet = i); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: public void remove() { jaroslav@597: if (lastRet == -1) jaroslav@597: throw new IllegalStateException(); jaroslav@597: synchronized (Vector.this) { jaroslav@597: checkForComodification(); jaroslav@597: Vector.this.remove(lastRet); jaroslav@597: expectedModCount = modCount; jaroslav@597: } jaroslav@597: cursor = lastRet; jaroslav@597: lastRet = -1; jaroslav@597: } jaroslav@597: jaroslav@597: final void checkForComodification() { jaroslav@597: if (modCount != expectedModCount) jaroslav@597: throw new ConcurrentModificationException(); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: /** jaroslav@597: * An optimized version of AbstractList.ListItr jaroslav@597: */ jaroslav@597: final class ListItr extends Itr implements ListIterator { jaroslav@597: ListItr(int index) { jaroslav@597: super(); jaroslav@597: cursor = index; jaroslav@597: } jaroslav@597: jaroslav@597: public boolean hasPrevious() { jaroslav@597: return cursor != 0; jaroslav@597: } jaroslav@597: jaroslav@597: public int nextIndex() { jaroslav@597: return cursor; jaroslav@597: } jaroslav@597: jaroslav@597: public int previousIndex() { jaroslav@597: return cursor - 1; jaroslav@597: } jaroslav@597: jaroslav@597: public E previous() { jaroslav@597: synchronized (Vector.this) { jaroslav@597: checkForComodification(); jaroslav@597: int i = cursor - 1; jaroslav@597: if (i < 0) jaroslav@597: throw new NoSuchElementException(); jaroslav@597: cursor = i; jaroslav@597: return elementData(lastRet = i); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: public void set(E e) { jaroslav@597: if (lastRet == -1) jaroslav@597: throw new IllegalStateException(); jaroslav@597: synchronized (Vector.this) { jaroslav@597: checkForComodification(); jaroslav@597: Vector.this.set(lastRet, e); jaroslav@597: } jaroslav@597: } jaroslav@597: jaroslav@597: public void add(E e) { jaroslav@597: int i = cursor; jaroslav@597: synchronized (Vector.this) { jaroslav@597: checkForComodification(); jaroslav@597: Vector.this.add(i, e); jaroslav@597: expectedModCount = modCount; jaroslav@597: } jaroslav@597: cursor = i + 1; jaroslav@597: lastRet = -1; jaroslav@597: } jaroslav@597: } jaroslav@597: }