0N/A/*
2362N/A * Copyright (c) 2007, 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 sun.awt.util;
0N/A
0N/Aimport java.util.AbstractList;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.Collection;
0N/Aimport java.util.ConcurrentModificationException;
0N/Aimport java.util.List;
0N/Aimport java.util.RandomAccess;
0N/A
0N/A/**
0N/A * Resizable-array implementation of the <tt>List</tt> interface. Implements
0N/A * all optional list operations, and permits all elements, including
0N/A * <tt>null</tt>. In addition to implementing the <tt>List</tt> interface,
0N/A * this class provides methods to manipulate the size of the array that is
0N/A * used internally to store the list. (This class is roughly equivalent to
0N/A * <tt>Vector</tt>, except that it is unsynchronized.)<p>
0N/A *
0N/A * The <tt>size</tt>, <tt>isEmpty</tt>, <tt>get</tt>, <tt>set</tt>,
0N/A * <tt>iterator</tt>, and <tt>listIterator</tt> operations run in constant
0N/A * time. The <tt>add</tt> operation runs in <i>amortized constant time</i>,
0N/A * that is, adding n elements requires O(n) time. All of the other operations
0N/A * run in linear time (roughly speaking). The constant factor is low compared
0N/A * to that for the <tt>LinkedList</tt> implementation.<p>
0N/A *
0N/A * Each <tt>IdentityArrayList</tt> instance has a <i>capacity</i>. The capacity is
0N/A * the size of the array used to store the elements in the list. It is always
0N/A * at least as large as the list size. As elements are added to an IdentityArrayList,
0N/A * its capacity grows automatically. The details of the growth policy are not
0N/A * specified beyond the fact that adding an element has constant amortized
0N/A * time cost.<p>
0N/A *
0N/A * An application can increase the capacity of an <tt>IdentityArrayList</tt> instance
0N/A * before adding a large number of elements using the <tt>ensureCapacity</tt>
0N/A * operation. This may reduce the amount of incremental reallocation.
0N/A *
0N/A * <p><strong>Note that this implementation is not synchronized.</strong>
0N/A * If multiple threads access an <tt>IdentityArrayList</tt> instance concurrently,
0N/A * and at least one of the threads modifies the list structurally, it
0N/A * <i>must</i> be synchronized externally. (A structural modification is
0N/A * any operation that adds or deletes one or more elements, or explicitly
0N/A * resizes the backing array; merely setting the value of an element is not
0N/A * a structural modification.) This is typically accomplished by
0N/A * synchronizing on some object that naturally encapsulates the list.
0N/A *
0N/A * If no such object exists, the list should be "wrapped" using the
0N/A * {@link Collections#synchronizedList Collections.synchronizedList}
0N/A * method. This is best done at creation time, to prevent accidental
0N/A * unsynchronized access to the list:<pre>
0N/A * List list = Collections.synchronizedList(new IdentityArrayList(...));</pre>
0N/A *
0N/A * <p>The iterators returned by this class's <tt>iterator</tt> and
0N/A * <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is
0N/A * structurally modified at any time after the iterator is created, in any way
0N/A * except through the iterator's own <tt>remove</tt> or <tt>add</tt> methods,
0N/A * the iterator will throw a {@link ConcurrentModificationException}. Thus, in
0N/A * the face of concurrent modification, the iterator fails quickly and cleanly,
0N/A * rather than risking arbitrary, non-deterministic behavior at an undetermined
0N/A * time in the future.<p>
0N/A *
0N/A * 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 <tt>ConcurrentModificationException</tt> 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><p>
0N/A *
0N/A */
0N/A
0N/Apublic class IdentityArrayList<E> extends AbstractList<E>
0N/A implements List<E>, RandomAccess
0N/A{
0N/A
0N/A /**
0N/A * The array buffer into which the elements of the IdentityArrayList are stored.
0N/A * The capacity of the IdentityArrayList is the length of this array buffer.
0N/A */
0N/A private transient Object[] elementData;
0N/A
0N/A /**
0N/A * The size of the IdentityArrayList (the number of elements it contains).
0N/A *
0N/A * @serial
0N/A */
0N/A private int size;
0N/A
0N/A /**
0N/A * Constructs an empty list with the specified initial capacity.
0N/A *
0N/A * @param initialCapacity the initial capacity of the list
0N/A * @exception IllegalArgumentException if the specified initial capacity
0N/A * is negative
0N/A */
0N/A public IdentityArrayList(int initialCapacity) {
0N/A super();
0N/A if (initialCapacity < 0)
0N/A throw new IllegalArgumentException("Illegal Capacity: "+
0N/A initialCapacity);
0N/A this.elementData = new Object[initialCapacity];
0N/A }
0N/A
0N/A /**
0N/A * Constructs an empty list with an initial capacity of ten.
0N/A */
0N/A public IdentityArrayList() {
0N/A this(10);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a list 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 list
0N/A * @throws NullPointerException if the specified collection is null
0N/A */
0N/A public IdentityArrayList(Collection<? extends E> c) {
0N/A elementData = c.toArray();
0N/A size = 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, size, Object[].class);
0N/A }
0N/A
0N/A /**
0N/A * Trims the capacity of this <tt>IdentityArrayList</tt> instance to be the
0N/A * list's current size. An application can use this operation to minimize
0N/A * the storage of an <tt>IdentityArrayList</tt> instance.
0N/A */
0N/A public void trimToSize() {
0N/A modCount++;
0N/A int oldCapacity = elementData.length;
0N/A if (size < oldCapacity) {
0N/A elementData = Arrays.copyOf(elementData, size);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Increases the capacity of this <tt>IdentityArrayList</tt> instance, if
0N/A * necessary, to ensure that it can hold at least the number of elements
0N/A * specified by the minimum capacity argument.
0N/A *
0N/A * @param minCapacity the desired minimum capacity
0N/A */
0N/A public void ensureCapacity(int minCapacity) {
0N/A modCount++;
0N/A int oldCapacity = elementData.length;
0N/A if (minCapacity > oldCapacity) {
0N/A Object oldData[] = elementData;
0N/A int newCapacity = (oldCapacity * 3)/2 + 1;
0N/A if (newCapacity < minCapacity)
0N/A newCapacity = minCapacity;
0N/A // minCapacity is usually close to size, so this is a win:
0N/A elementData = Arrays.copyOf(elementData, newCapacity);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of elements in this list.
0N/A *
0N/A * @return the number of elements in this list
0N/A */
0N/A public int size() {
0N/A return size;
0N/A }
0N/A
0N/A /**
0N/A * Returns <tt>true</tt> if this list contains no elements.
0N/A *
0N/A * @return <tt>true</tt> if this list contains no elements
0N/A */
0N/A public boolean isEmpty() {
0N/A return size == 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns <tt>true</tt> if this list contains the specified element.
0N/A * More formally, returns <tt>true</tt> if and only if this list contains
0N/A * at least one element <tt>e</tt> such that
0N/A * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o == e)</tt>.
0N/A *
0N/A * @param o element whose presence in this list is to be tested
0N/A * @return <tt>true</tt> if this list contains the specified element
0N/A */
0N/A public boolean contains(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
0N/A * in this list, or -1 if this list does not contain the element.
0N/A * More formally, returns the lowest index <tt>i</tt> such that
0N/A * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o == get(i))</tt>,
0N/A * or -1 if there is no such index.
0N/A */
0N/A public int indexOf(Object o) {
0N/A for (int i = 0; i < size; i++) {
0N/A if (o == elementData[i]) {
0N/A return i;
0N/A }
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 list, or -1 if this list does not contain the element.
0N/A * More formally, returns the highest index <tt>i</tt> such that
0N/A * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o == get(i))</tt>,
0N/A * or -1 if there is no such index.
0N/A */
0N/A public int lastIndexOf(Object o) {
0N/A for (int i = size-1; i >= 0; i--) {
0N/A if (o == elementData[i]) {
0N/A return i;
0N/A }
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns an array containing all of the elements in this list
0N/A * in proper sequence (from first to last element).
0N/A *
0N/A * <p>The returned array will be "safe" in that no references to it are
0N/A * maintained by this list. (In other words, this method must allocate
0N/A * a new array). The caller is thus free to modify the returned array.
0N/A *
0N/A * <p>This method acts as bridge between array-based and collection-based
0N/A * APIs.
0N/A *
0N/A * @return an array containing all of the elements in this list in
0N/A * proper sequence
0N/A */
0N/A public Object[] toArray() {
0N/A return Arrays.copyOf(elementData, size);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array containing all of the elements in this list in proper
0N/A * sequence (from first to last element); the runtime type of the returned
0N/A * array is that of the specified array. If the list fits in the
0N/A * specified array, it is returned therein. Otherwise, a new array is
0N/A * allocated with the runtime type of the specified array and the size of
0N/A * this list.
0N/A *
0N/A * <p>If the list fits in the specified array with room to spare
0N/A * (i.e., the array has more elements than the list), the element in
0N/A * the array immediately following the end of the collection is set to
0N/A * <tt>null</tt>. (This is useful in determining the length of the
0N/A * list <i>only</i> if the caller knows that the list does not contain
0N/A * any null elements.)
0N/A *
0N/A * @param a the array into which the elements of the list 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 list
0N/A * @throws ArrayStoreException if the runtime type of the specified array
0N/A * is not a supertype of the runtime type of every element in
0N/A * this list
0N/A * @throws NullPointerException if the specified array is null
0N/A */
0N/A public <T> T[] toArray(T[] a) {
0N/A if (a.length < size)
0N/A // Make a new array of a's runtime type, but my contents:
0N/A return (T[]) Arrays.copyOf(elementData, size, a.getClass());
0N/A System.arraycopy(elementData, 0, a, 0, size);
0N/A if (a.length > size)
0N/A a[size] = null;
0N/A return a;
0N/A }
0N/A
0N/A // Positional Access Operations
0N/A
0N/A /**
0N/A * Returns the element at the specified position in this list.
0N/A *
0N/A * @param index index of the element to return
0N/A * @return the element at the specified position in this list
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public E get(int index) {
0N/A rangeCheck(index);
0N/A
0N/A return (E) elementData[index];
0N/A }
0N/A
0N/A /**
0N/A * Replaces the element at the specified position in this list with
0N/A * the 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 IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public E set(int index, E element) {
0N/A rangeCheck(index);
0N/A
0N/A E oldValue = (E) 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 list.
0N/A *
0N/A * @param e element to be appended to this list
0N/A * @return <tt>true</tt> (as specified by {@link Collection#add})
0N/A */
0N/A public boolean add(E e) {
0N/A ensureCapacity(size + 1); // Increments modCount!!
0N/A elementData[size++] = e;
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Inserts the specified element at the specified position in this
0N/A * list. Shifts the element currently at that position (if any) and
0N/A * any 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 IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public void add(int index, E element) {
0N/A rangeCheckForAdd(index);
0N/A
0N/A ensureCapacity(size+1); // Increments modCount!!
0N/A System.arraycopy(elementData, index, elementData, index + 1,
0N/A size - index);
0N/A elementData[index] = element;
0N/A size++;
0N/A }
0N/A
0N/A /**
0N/A * Removes the element at the specified position in this list.
0N/A * Shifts any subsequent elements to the left (subtracts one from their
0N/A * indices).
0N/A *
0N/A * @param index the index of the element to be removed
0N/A * @return the element that was removed from the list
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public E remove(int index) {
0N/A rangeCheck(index);
0N/A
0N/A modCount++;
0N/A E oldValue = (E) elementData[index];
0N/A
0N/A int numMoved = size - index - 1;
0N/A if (numMoved > 0)
0N/A System.arraycopy(elementData, index+1, elementData, index,
0N/A numMoved);
0N/A elementData[--size] = null; // Let gc do its work
0N/A
0N/A return oldValue;
0N/A }
0N/A
0N/A /**
0N/A * Removes the first occurrence of the specified element from this list,
0N/A * if it is present. If the list does not contain the element, it is
0N/A * unchanged. More formally, removes the element with the lowest index
0N/A * <tt>i</tt> such that
0N/A * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o == get(i))</tt>
0N/A * (if such an element exists). Returns <tt>true</tt> if this list
0N/A * contained the specified element (or equivalently, if this list
0N/A * changed as a result of the call).
0N/A *
0N/A * @param o element to be removed from this list, if present
0N/A * @return <tt>true</tt> if this list contained the specified element
0N/A */
0N/A public boolean remove(Object o) {
0N/A for (int index = 0; index < size; index++) {
0N/A if (o == elementData[index]) {
0N/A fastRemove(index);
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /*
0N/A * Private remove method that skips bounds checking and does not
0N/A * return the value removed.
0N/A */
0N/A private void fastRemove(int index) {
0N/A modCount++;
0N/A int numMoved = size - index - 1;
0N/A if (numMoved > 0)
0N/A System.arraycopy(elementData, index+1, elementData, index,
0N/A numMoved);
0N/A elementData[--size] = null; // Let gc do its work
0N/A }
0N/A
0N/A /**
0N/A * Removes all of the elements from this list. The list will
0N/A * be empty after this call returns.
0N/A */
0N/A public void clear() {
0N/A modCount++;
0N/A
0N/A // Let gc do its work
0N/A for (int i = 0; i < size; i++)
0N/A elementData[i] = null;
0N/A
0N/A size = 0;
0N/A }
0N/A
0N/A /**
0N/A * Appends all of the elements in the specified collection to the end of
0N/A * this list, in the order that they are returned by the
0N/A * specified collection's Iterator. The behavior of this operation is
0N/A * undefined if the specified collection is modified while the operation
0N/A * is in progress. (This implies that the behavior of this call is
0N/A * undefined if the specified collection is this list, and this
0N/A * list is nonempty.)
0N/A *
0N/A * @param c collection containing elements to be added to this list
0N/A * @return <tt>true</tt> if this list changed as a result of the call
0N/A * @throws NullPointerException if the specified collection is null
0N/A */
0N/A public boolean addAll(Collection<? extends E> c) {
0N/A Object[] a = c.toArray();
0N/A int numNew = a.length;
0N/A ensureCapacity(size + numNew); // Increments modCount
0N/A System.arraycopy(a, 0, elementData, size, numNew);
0N/A size += numNew;
0N/A return numNew != 0;
0N/A }
0N/A
0N/A /**
0N/A * Inserts all of the elements in the specified collection into this
0N/A * list, starting at the specified position. Shifts the element
0N/A * currently at that position (if any) and any subsequent elements to
0N/A * the right (increases their indices). The new elements will appear
0N/A * in the list in the order that they are returned by the
0N/A * specified collection's 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 collection containing elements to be added to this list
0N/A * @return <tt>true</tt> if this list changed as a result of the call
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A * @throws NullPointerException if the specified collection is null
0N/A */
0N/A public boolean addAll(int index, Collection<? extends E> c) {
0N/A rangeCheckForAdd(index);
0N/A
0N/A Object[] a = c.toArray();
0N/A int numNew = a.length;
0N/A ensureCapacity(size + numNew); // Increments modCount
0N/A
0N/A int numMoved = size - index;
0N/A if (numMoved > 0) {
0N/A System.arraycopy(elementData, index, elementData, index + numNew, numMoved);
0N/A }
0N/A
0N/A System.arraycopy(a, 0, elementData, index, numNew);
0N/A size += numNew;
0N/A return numNew != 0;
0N/A }
0N/A
0N/A /**
0N/A * Removes from this list all of the elements whose index is between
0N/A * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.
0N/A * Shifts any succeeding elements to the left (reduces their index).
0N/A * This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements.
0N/A * (If <tt>toIndex==fromIndex</tt>, this operation has no effect.)
0N/A *
0N/A * @param fromIndex index of first element to be removed
0N/A * @param toIndex index after last element to be removed
0N/A * @throws IndexOutOfBoundsException if fromIndex or toIndex out of
0N/A * range (fromIndex &lt; 0 || fromIndex &gt;= size() || toIndex
0N/A * &gt; size() || toIndex &lt; fromIndex)
0N/A */
0N/A protected void removeRange(int fromIndex, int toIndex) {
0N/A modCount++;
0N/A int numMoved = size - toIndex;
0N/A System.arraycopy(elementData, toIndex, elementData, fromIndex,
0N/A numMoved);
0N/A
0N/A // Let gc do its work
0N/A int newSize = size - (toIndex-fromIndex);
0N/A while (size != newSize)
0N/A elementData[--size] = null;
0N/A }
0N/A
0N/A /**
0N/A * Checks if the given index is in range. If not, throws an appropriate
0N/A * runtime exception. This method does *not* check if the index is
0N/A * negative: It is always used immediately prior to an array access,
0N/A * which throws an ArrayIndexOutOfBoundsException if index is negative.
0N/A */
0N/A private void rangeCheck(int index) {
0N/A if (index >= size)
0N/A throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
0N/A }
0N/A
0N/A /**
0N/A * A version of rangeCheck used by add and addAll.
0N/A */
0N/A private void rangeCheckForAdd(int index) {
0N/A if (index > size || index < 0)
0N/A throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
0N/A }
0N/A
0N/A /**
0N/A * Constructs an IndexOutOfBoundsException detail message.
0N/A * Of the many possible refactorings of the error handling code,
0N/A * this "outlining" performs best with both server and client VMs.
0N/A */
0N/A private String outOfBoundsMsg(int index) {
0N/A return "Index: "+index+", Size: "+size;
0N/A }
0N/A}