--- jsr166/src/main/java/util/LinkedList.java 2005/07/18 19:14:17 1.38
+++ jsr166/src/main/java/util/LinkedList.java 2008/05/18 23:47:56 1.48
@@ -1,12 +1,29 @@
/*
- * %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;
-import java.util.*; // for javadoc (till 6280605 is fixed)
/**
* Linked list implementation of the List interface. Implements all
@@ -60,14 +77,14 @@ import java.util.*; // for javadoc (till
* 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 List
+ * @see ArrayList
+ * @see Vector
* @since 1.2
* @param the type of elements held in this collection
*/
@@ -95,8 +112,8 @@ public class LinkedList
* @throws NullPointerException if the specified collection is null
*/
public LinkedList(Collection extends E> c) {
- this();
- addAll(c);
+ this();
+ addAll(c);
}
/**
@@ -106,10 +123,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;
}
/**
@@ -119,10 +136,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;
}
/**
@@ -132,7 +149,7 @@ public class LinkedList
* @throws NoSuchElementException if this list is empty
*/
public E removeFirst() {
- return remove(header.next);
+ return remove(header.next);
}
/**
@@ -142,7 +159,7 @@ public class LinkedList
* @throws NoSuchElementException if this list is empty
*/
public E removeLast() {
- return remove(header.previous);
+ return remove(header.previous);
}
/**
@@ -151,7 +168,7 @@ public class LinkedList
* @param e the element to add
*/
public void addFirst(E e) {
- addBefore(e, header.next);
+ addBefore(e, header.next);
}
/**
@@ -162,7 +179,7 @@ public class LinkedList
* @param e the element to add
*/
public void addLast(E e) {
- addBefore(e, header);
+ addBefore(e, header);
}
/**
@@ -184,7 +201,7 @@ public class LinkedList
* @return the number of elements in this list
*/
public int size() {
- return size;
+ return size;
}
/**
@@ -196,7 +213,7 @@ public class LinkedList
* @return true (as specified by {@link Collection#add})
*/
public boolean add(E e) {
- addBefore(e, header);
+ addBefore(e, header);
return true;
}
@@ -271,11 +288,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;
@@ -299,7 +316,7 @@ public class LinkedList
}
header.next = header.previous = header;
size = 0;
- modCount++;
+ modCount++;
}
@@ -510,7 +527,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) {
@@ -547,8 +564,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
@@ -561,8 +578,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
@@ -665,68 +682,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();
+
+ 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() {
+ public void remove() {
checkForComodification();
Entry lastNext = lastReturned.next;
try {
@@ -734,71 +751,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.)
*
@@ -806,11 +844,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);
@@ -840,11 +878,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;
}
/**
@@ -890,7 +928,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;
@@ -912,13 +950,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);
}
@@ -929,8 +967,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();
@@ -939,8 +977,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