--- jsr166/src/main/java/util/LinkedList.java 2007/05/20 07:54:01 1.47 +++ jsr166/src/main/java/util/LinkedList.java 2008/05/18 23:47:56 1.48 @@ -82,9 +82,9 @@ package java.util; * * @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 */ @@ -112,8 +112,8 @@ public class LinkedList * @throws NullPointerException if the specified collection is null */ public LinkedList(Collection c) { - this(); - addAll(c); + this(); + addAll(c); } /** @@ -123,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; } /** @@ -136,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; } /** @@ -149,7 +149,7 @@ public class LinkedList * @throws NoSuchElementException if this list is empty */ public E removeFirst() { - return remove(header.next); + return remove(header.next); } /** @@ -159,7 +159,7 @@ public class LinkedList * @throws NoSuchElementException if this list is empty */ public E removeLast() { - return remove(header.previous); + return remove(header.previous); } /** @@ -168,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); } /** @@ -179,7 +179,7 @@ public class LinkedList * @param e the element to add */ public void addLast(E e) { - addBefore(e, header); + addBefore(e, header); } /** @@ -201,7 +201,7 @@ public class LinkedList * @return the number of elements in this list */ public int size() { - return size; + return size; } /** @@ -213,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; } @@ -288,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; @@ -316,7 +316,7 @@ public class LinkedList } header.next = header.previous = header; size = 0; - modCount++; + modCount++; } @@ -682,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 { @@ -751,67 +751,67 @@ 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; } @@ -825,13 +825,13 @@ public class LinkedList /** 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() { + public boolean hasNext() { + return itr.hasPrevious(); + } + public E next() { return itr.previous(); } - public void remove() { + public void remove() { itr.remove(); } } @@ -844,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); @@ -878,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; } /** @@ -928,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; @@ -950,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); } @@ -967,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(); @@ -977,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