--- jsr166/src/main/java/util/LinkedList.java 2005/05/21 17:33:09 1.33 +++ jsr166/src/main/java/util/LinkedList.java 2008/05/18 23:59:57 1.49 @@ -1,8 +1,26 @@ /* - * %W% %E% + * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * - * Copyright 2005 Sun Microsystems, Inc. All rights reserved. - * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. */ package java.util; @@ -25,18 +43,20 @@ package java.util; * list. Operations that index into the list will traverse the list from * the beginning or the end, whichever is closer to the specified index.

* - * Note that this implementation is not synchronized. If multiple - * threads access a list concurrently, and at least one of the threads - * modifies the list structurally, it must be synchronized - * externally. (A structural modification is any operation that adds or - * deletes one or more elements; merely setting the value of an element is not - * a structural modification.) This is typically accomplished by - * synchronizing on some object that naturally encapsulates the list. If no - * such object exists, the list should be "wrapped" using the - * Collections.synchronizedList method. This is best done at creation time, - * to prevent accidental unsynchronized access to the list:

- *     List list = Collections.synchronizedList(new LinkedList(...));
- * 
+ *

Note that this implementation is not synchronized. + * If multiple threads access a linked list concurrently, and at least + * one of the threads modifies the list structurally, it must be + * synchronized externally. (A structural modification is any operation + * that adds or deletes one or more elements; merely setting the value of + * an element is not a structural modification.) This is typically + * accomplished by synchronizing on some object that naturally + * encapsulates the list. + * + * If no such object exists, the list should be "wrapped" using the + * {@link Collections#synchronizedList Collections.synchronizedList} + * method. This is best done at creation time, to prevent accidental + * unsynchronized access to the list:

+ *   List list = Collections.synchronizedList(new LinkedList(...));
* *

The iterators returned by this class's iterator and * listIterator methods are fail-fast: if the list is @@ -57,15 +77,13 @@ package java.util; * should be used only to detect bugs. * *

This class is a member of the - * + * * Java Collections Framework. * * @author Josh Bloch - * @version %I%, %G% - * @see List - * @see ArrayList - * @see Vector - * @see Collections#synchronizedList(List) + * @see List + * @see ArrayList + * @see Vector * @since 1.2 * @param the type of elements held in this collection */ @@ -92,10 +110,10 @@ public class LinkedList * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ - public LinkedList(Collection c) { - this(); - addAll(c); - } + public LinkedList(Collection c) { + this(); + addAll(c); + } /** * Returns the first element in this list. @@ -104,10 +122,10 @@ public class LinkedList * @throws NoSuchElementException if this list is empty */ public E getFirst() { - if (size==0) - throw new NoSuchElementException(); + if (size==0) + throw new NoSuchElementException(); - return header.next.element; + return header.next.element; } /** @@ -117,10 +135,10 @@ public class LinkedList * @throws NoSuchElementException if this list is empty */ public E getLast() { - if (size==0) - throw new NoSuchElementException(); + if (size==0) + throw new NoSuchElementException(); - return header.previous.element; + return header.previous.element; } /** @@ -130,7 +148,7 @@ public class LinkedList * @throws NoSuchElementException if this list is empty */ public E removeFirst() { - return remove(header.next); + return remove(header.next); } /** @@ -140,26 +158,27 @@ public class LinkedList * @throws NoSuchElementException if this list is empty */ public E removeLast() { - return remove(header.previous); + return remove(header.previous); } /** - * Inserts the given element at the beginning of this list. + * Inserts the specified element at the beginning of this list. * - * @param e the element to be inserted at the beginning of this list + * @param e the element to add */ public void addFirst(E e) { - addBefore(e, header.next); + addBefore(e, header.next); } /** - * Appends the given element to the end of this list. (Identical in - * function to the add method; included only for consistency.) + * Appends the specified element to the end of this list. + * + *

This method is equivalent to {@link #add}. * - * @param e the element to be inserted at the end of this list + * @param e the element to add */ public void addLast(E e) { - addBefore(e, header); + addBefore(e, header); } /** @@ -181,17 +200,19 @@ public class LinkedList * @return the number of elements in this list */ public int size() { - return size; + return size; } /** * Appends the specified element to the end of this list. * + *

This method is equivalent to {@link #addLast}. + * * @param e element to be appended to this list - * @return true (as per the spec for {@link Collection#add}) + * @return true (as specified by {@link Collection#add}) */ public boolean add(E e) { - addBefore(e, header); + addBefore(e, header); return true; } @@ -266,11 +287,11 @@ public class LinkedList int numNew = a.length; if (numNew==0) return false; - modCount++; + modCount++; Entry successor = (index==size ? header : entry(index)); Entry predecessor = successor.previous; - for (int i=0; i e = new Entry((E)a[i], successor, predecessor); predecessor.next = e; predecessor = e; @@ -294,7 +315,7 @@ public class LinkedList } header.next = header.previous = header; size = 0; - modCount++; + modCount++; } @@ -481,7 +502,7 @@ public class LinkedList * Adds the specified element as the tail (last element) of this list. * * @param e the element to add - * @return true (as per the spec for {@link Queue#offer}) + * @return true (as specified by {@link Queue#offer}) * @since 1.5 */ public boolean offer(E e) { @@ -493,7 +514,7 @@ public class LinkedList * Inserts the specified element at the front of this list. * * @param e the element to insert - * @return true (as per the spec for {@link Deque#offerFirst}) + * @return true (as specified by {@link Deque#offerFirst}) * @since 1.6 */ public boolean offerFirst(E e) { @@ -505,7 +526,7 @@ public class LinkedList * Inserts the specified element at the end of this list. * * @param e the element to insert - * @return true (as per the spec for {@link Deque#offerLast}) + * @return true (as specified by {@link Deque#offerLast}) * @since 1.6 */ public boolean offerLast(E e) { @@ -542,8 +563,8 @@ public class LinkedList } /** - * Retrieves and removes the first element of this list, or - * null if this list is empty. + * Retrieves and removes the first element of this list, + * or returns null if this list is empty. * * @return the first element of this list, or null if * this list is empty @@ -556,8 +577,8 @@ public class LinkedList } /** - * Retrieves and removes the last element of this list, or - * null if this list is empty. + * Retrieves and removes the last element of this list, + * or returns null if this list is empty. * * @return the last element of this list, or null if * this list is empty @@ -621,14 +642,14 @@ public class LinkedList */ public boolean removeLastOccurrence(Object o) { if (o==null) { - for (Entry e = header.previous; e != header; e = e.previous) { + for (Entry e = header.previous; e != header; e = e.previous) { if (e.element==null) { remove(e); return true; } } } else { - for (Entry e = header.previous; e != header; e = e.previous) { + for (Entry e = header.previous; e != header; e = e.previous) { if (o.equals(e.element)) { remove(e); return true; @@ -660,68 +681,68 @@ public class LinkedList * @see List#listIterator(int) */ public ListIterator listIterator(int index) { - return new ListItr(index); + return new ListItr(index); } private class ListItr implements ListIterator { - private Entry lastReturned = header; - private Entry next; - private int nextIndex; - private int expectedModCount = modCount; - - ListItr(int index) { - if (index < 0 || index > size) - throw new IndexOutOfBoundsException("Index: "+index+ - ", Size: "+size); - if (index < (size >> 1)) { - next = header.next; - for (nextIndex=0; nextIndexindex; nextIndex--) - next = next.previous; - } - } - - public boolean hasNext() { - return nextIndex != size; - } - - public E next() { - checkForComodification(); - if (nextIndex == size) - throw new NoSuchElementException(); - - lastReturned = next; - next = next.next; - nextIndex++; - return lastReturned.element; - } - - public boolean hasPrevious() { - return nextIndex != 0; - } - - public E previous() { - if (nextIndex == 0) - throw new NoSuchElementException(); - - lastReturned = next = next.previous; - nextIndex--; - checkForComodification(); - return lastReturned.element; - } - - public int nextIndex() { - return nextIndex; - } - - public int previousIndex() { - return nextIndex-1; - } + private Entry lastReturned = header; + private Entry next; + private int nextIndex; + private int expectedModCount = modCount; + + ListItr(int index) { + if (index < 0 || index > size) + throw new IndexOutOfBoundsException("Index: "+index+ + ", Size: "+size); + if (index < (size >> 1)) { + next = header.next; + for (nextIndex=0; nextIndexindex; nextIndex--) + next = next.previous; + } + } + + public boolean hasNext() { + return nextIndex != size; + } + + public E next() { + checkForComodification(); + if (nextIndex == size) + throw new NoSuchElementException(); - public void remove() { + lastReturned = next; + next = next.next; + nextIndex++; + return lastReturned.element; + } + + public boolean hasPrevious() { + return nextIndex != 0; + } + + public E previous() { + if (nextIndex == 0) + throw new NoSuchElementException(); + + lastReturned = next = next.previous; + nextIndex--; + checkForComodification(); + return lastReturned.element; + } + + public int nextIndex() { + return nextIndex; + } + + public int previousIndex() { + return nextIndex-1; + } + + public void remove() { checkForComodification(); Entry lastNext = lastReturned.next; try { @@ -729,71 +750,92 @@ public class LinkedList } catch (NoSuchElementException e) { throw new IllegalStateException(); } - if (next==lastReturned) + if (next==lastReturned) next = lastNext; else - nextIndex--; - lastReturned = header; - expectedModCount++; - } - - public void set(E e) { - if (lastReturned == header) - throw new IllegalStateException(); - checkForComodification(); - lastReturned.element = e; - } - - public void add(E e) { - checkForComodification(); - lastReturned = header; - addBefore(e, next); - nextIndex++; - expectedModCount++; - } - - final void checkForComodification() { - if (modCount != expectedModCount) - throw new ConcurrentModificationException(); - } + nextIndex--; + lastReturned = header; + expectedModCount++; + } + + public void set(E e) { + if (lastReturned == header) + throw new IllegalStateException(); + checkForComodification(); + lastReturned.element = e; + } + + public void add(E e) { + checkForComodification(); + lastReturned = header; + addBefore(e, next); + nextIndex++; + expectedModCount++; + } + + final void checkForComodification() { + if (modCount != expectedModCount) + throw new ConcurrentModificationException(); + } } private static class Entry { - E element; - Entry next; - Entry previous; - - Entry(E element, Entry next, Entry previous) { - this.element = element; - this.next = next; - this.previous = previous; - } + E element; + Entry next; + Entry previous; + + Entry(E element, Entry next, Entry previous) { + this.element = element; + this.next = next; + this.previous = previous; + } } private Entry addBefore(E e, Entry entry) { - Entry newEntry = new Entry(e, entry, entry.previous); - newEntry.previous.next = newEntry; - newEntry.next.previous = newEntry; - size++; - modCount++; - return newEntry; + Entry newEntry = new Entry(e, entry, entry.previous); + newEntry.previous.next = newEntry; + newEntry.next.previous = newEntry; + size++; + modCount++; + return newEntry; } private E remove(Entry e) { - if (e == header) - throw new NoSuchElementException(); + if (e == header) + throw new NoSuchElementException(); E result = e.element; - e.previous.next = e.next; - e.next.previous = e.previous; + e.previous.next = e.next; + e.next.previous = e.previous; e.next = e.previous = null; e.element = null; - size--; - modCount++; + size--; + modCount++; return result; } /** + * @since 1.6 + */ + public Iterator descendingIterator() { + return new DescendingIterator(); + } + + /** Adapter to provide descending iterators via ListItr.previous */ + private class DescendingIterator implements Iterator { + final ListItr itr = new ListItr(size()); + public boolean hasNext() { + return itr.hasPrevious(); + } + public E next() { + return itr.previous(); + } + public void remove() { + itr.remove(); + } + } + + /** * Returns a shallow copy of this LinkedList. (The elements * themselves are not cloned.) * @@ -801,11 +843,11 @@ public class LinkedList */ public Object clone() { LinkedList clone = null; - try { - clone = (LinkedList) super.clone(); - } catch (CloneNotSupportedException e) { - throw new InternalError(); - } + try { + clone = (LinkedList) super.clone(); + } catch (CloneNotSupportedException e) { + throw new InternalError(); + } // Put clone into "virgin" state clone.header = new Entry(null, null, null); @@ -835,11 +877,11 @@ public class LinkedList * in proper sequence */ public Object[] toArray() { - Object[] result = new Object[size]; + Object[] result = new Object[size]; int i = 0; for (Entry e = header.next; e != header; e = e.next) result[i++] = e.element; - return result; + return result; } /** @@ -885,7 +927,7 @@ public class LinkedList a = (T[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), size); int i = 0; - Object[] result = a; + Object[] result = a; for (Entry e = header.next; e != header; e = e.next) result[i++] = e.element; @@ -907,13 +949,13 @@ public class LinkedList */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { - // Write out any hidden serialization magic - s.defaultWriteObject(); + // Write out any hidden serialization magic + s.defaultWriteObject(); // Write out size s.writeInt(size); - // Write out all elements in the proper order. + // Write out all elements in the proper order. for (Entry e = header.next; e != header; e = e.next) s.writeObject(e.element); } @@ -924,8 +966,8 @@ public class LinkedList */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { - // Read in any hidden serialization magic - s.defaultReadObject(); + // Read in any hidden serialization magic + s.defaultReadObject(); // Read in size int size = s.readInt(); @@ -934,8 +976,8 @@ public class LinkedList header = new Entry(null, null, null); header.next = header.previous = header; - // Read in all elements in the proper order. - for (int i=0; i