--- jsr166/src/main/java/util/LinkedList.java 2005/05/16 05:17:07 1.28 +++ jsr166/src/main/java/util/LinkedList.java 2006/01/10 21:32:09 1.43 @@ -1,11 +1,12 @@ /* * %W% %E% * - * Copyright 2005 Sun Microsystems, Inc. All rights reserved. + * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util; +import java.util.*; // for javadoc (till 6280605 is fixed) /** * Linked list implementation of the List interface. Implements all @@ -25,18 +26,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 @@ -65,7 +68,6 @@ package java.util; * @see List * @see ArrayList * @see Vector - * @see Collections#synchronizedList(List) * @since 1.2 * @param the type of elements held in this collection */ @@ -92,10 +94,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. @@ -144,19 +146,20 @@ public class LinkedList } /** - * 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); } /** - * 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. * - * @param e the element to be inserted at the end of this list + *

This method is equivalent to {@link #add}. + * + * @param e the element to add */ public void addLast(E e) { addBefore(e, header); @@ -187,8 +190,10 @@ public class LinkedList /** * 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); @@ -232,10 +237,10 @@ public class LinkedList * this list, in the order that they are returned by the specified * collection's iterator. The behavior of this operation is undefined if * the specified collection is modified while the operation is in - * progress. (This implies that the behavior of this call is undefined if - * the specified Collection is this list, and this list is nonempty.) + * progress. (Note that this will occur if the specified collection is + * this list, and it's nonempty.) * - * @param c the elements to be inserted into this list + * @param c collection containing elements to be added to this list * @return true if this list changed as a result of the call * @throws NullPointerException if the specified collection is null */ @@ -253,7 +258,7 @@ public class LinkedList * * @param index index at which to insert the first element * from the specified collection - * @param c elements to be inserted into this list + * @param c collection containing elements to be added to this list * @return true if this list changed as a result of the call * @throws IndexOutOfBoundsException {@inheritDoc} * @throws NullPointerException if the specified collection is null @@ -375,16 +380,15 @@ public class LinkedList // Search Operations /** - * Returns the index in this list of the first occurrence of the - * specified element, or -1 if the List does not contain this - * element. More formally, returns the lowest index i such that - * (o==null ? get(i)==null : o.equals(get(i))), or -1 if - * there is no such index. + * Returns the index of the first occurrence of the specified element + * in this list, or -1 if this list does not contain the element. + * More formally, returns the lowest index i such that + * (o==null ? get(i)==null : o.equals(get(i))), + * or -1 if there is no such index. * * @param o element to search for - * @return the index in this list of the first occurrence of the - * specified element, or -1 if the list does not contain this - * element + * @return the index of the first occurrence of the specified element in + * this list, or -1 if this list does not contain the element */ public int indexOf(Object o) { int index = 0; @@ -405,16 +409,15 @@ public class LinkedList } /** - * Returns the index in this list of the last occurrence of the - * specified element, or -1 if the list does not contain this - * element. More formally, returns the highest index i such that - * (o==null ? get(i)==null : o.equals(get(i))), or -1 if - * there is no such index. + * Returns the index of the last occurrence of the specified element + * in this list, or -1 if this list does not contain the element. + * More formally, returns the highest index i such that + * (o==null ? get(i)==null : o.equals(get(i))), + * or -1 if there is no such index. * * @param o element to search for - * @return the index in this list of the last occurrence of the - * specified element, or -1 if the list does not contain this - * element + * @return the index of the last occurrence of the specified element in + * this list, or -1 if this list does not contain the element */ public int lastIndexOf(Object o) { int index = size; @@ -483,7 +486,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) { @@ -495,7 +498,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) { @@ -507,7 +510,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) { @@ -544,8 +547,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 @@ -558,8 +561,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 @@ -623,14 +626,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; @@ -796,6 +799,27 @@ public class LinkedList } /** + * @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.) * @@ -824,10 +848,17 @@ public class LinkedList /** * Returns an array containing all of the elements in this list - * in the correct order. + * in proper sequence (from first to last element). + * + *

The returned array will be "safe" in that no references to it are + * maintained by this list. (In other words, this method must allocate + * a new array). The caller is thus free to modify the returned array. + * + *

This method acts as bridge between array-based and collection-based + * APIs. * * @return an array containing all of the elements in this list - * in the correct order + * in proper sequence */ public Object[] toArray() { Object[] result = new Object[size]; @@ -839,24 +870,40 @@ public class LinkedList /** * Returns an array containing all of the elements in this list in - * the correct order; the runtime type of the returned array is that of - * the specified array. If the list fits in the specified array, it - * is returned therein. Otherwise, a new array is allocated with the - * runtime type of the specified array and the size of this list.

- * - * If the list fits in the specified array with room to spare - * (i.e., the array has more elements than the list), - * the element in the array immediately following the end of the - * collection is set to null. This is useful in determining the length - * of the list only if the caller knows that the list - * does not contain any null elements. + * proper sequence (from first to last element); the runtime type of + * the returned array is that of the specified array. If the list fits + * in the specified array, it is returned therein. Otherwise, a new + * array is allocated with the runtime type of the specified array and + * the size of this list. + * + *

If the list fits in the specified array with room to spare (i.e., + * the array has more elements than the list), the element in the array + * immediately following the end of the list is set to null. + * (This is useful in determining the length of the list only if + * the caller knows that the list does not contain any null elements.) + * + *

Like the {@link #toArray()} method, this method acts as bridge between + * array-based and collection-based APIs. Further, this method allows + * precise control over the runtime type of the output array, and may, + * under certain circumstances, be used to save allocation costs. + * + *

Suppose x is a list known to contain only strings. + * The following code can be used to dump the list into a newly + * allocated array of String: + * + *

+     *     String[] y = x.toArray(new String[0]);
+ * + * Note that toArray(new Object[0]) is identical in function to + * toArray(). * * @param a the array into which the elements of the list are to * be stored, if it is big enough; otherwise, a new array of the * same runtime type is allocated for this purpose. * @return an array containing the elements of the list - * @throws ArrayStoreException if the runtime type of a is not a - * supertype of the runtime type of every element in this list + * @throws ArrayStoreException if the runtime type of the specified array + * is not a supertype of the runtime type of every element in + * this list * @throws NullPointerException if the specified array is null */ public T[] toArray(T[] a) {