ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/LinkedList.java
(Generate patch)

Comparing jsr166/src/main/java/util/LinkedList.java (file contents):
Revision 1.28 by jsr166, Mon May 16 05:17:07 2005 UTC vs.
Revision 1.44 by jsr166, Tue Feb 7 20:54:24 2006 UTC

# Line 1 | Line 1
1   /*
2   * %W% %E%
3   *
4 < * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5   * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6   */
7  
# Line 25 | Line 25 | package java.util;
25   * list.  Operations that index into the list will traverse the list from
26   * the beginning or the end, whichever is closer to the specified index.<p>
27   *
28 < * <b>Note that this implementation is not synchronized.</b> If multiple
29 < * threads access a list concurrently, and at least one of the threads
30 < * modifies the list structurally, it <i>must</i> be synchronized
31 < * externally.  (A structural modification is any operation that adds or
32 < * deletes one or more elements; merely setting the value of an element is not
33 < * a structural modification.)  This is typically accomplished by
34 < * synchronizing on some object that naturally encapsulates the list.  If no
35 < * such object exists, the list should be "wrapped" using the
36 < * Collections.synchronizedList method.  This is best done at creation time,
37 < * to prevent accidental unsynchronized access to the list: <pre>
38 < *     List list = Collections.synchronizedList(new LinkedList(...));
39 < * </pre>
28 > * <p><strong>Note that this implementation is not synchronized.</strong>
29 > * If multiple threads access a linked list concurrently, and at least
30 > * one of the threads modifies the list structurally, it <i>must</i> be
31 > * synchronized externally.  (A structural modification is any operation
32 > * that adds or deletes one or more elements; merely setting the value of
33 > * an element is not a structural modification.)  This is typically
34 > * accomplished by synchronizing on some object that naturally
35 > * encapsulates the list.
36 > *
37 > * If no such object exists, the list should be "wrapped" using the
38 > * {@link Collections#synchronizedList Collections.synchronizedList}
39 > * method.  This is best done at creation time, to prevent accidental
40 > * unsynchronized access to the list:<pre>
41 > *   List list = Collections.synchronizedList(new LinkedList(...));</pre>
42   *
43   * <p>The iterators returned by this class's <tt>iterator</tt> and
44   * <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is
# Line 65 | Line 67 | package java.util;
67   * @see     List
68   * @see     ArrayList
69   * @see     Vector
68 * @see     Collections#synchronizedList(List)
70   * @since 1.2
71   * @param <E> the type of elements held in this collection
72   */
# Line 92 | Line 93 | public class LinkedList<E>
93       * @param  c the collection whose elements are to be placed into this list
94       * @throws NullPointerException if the specified collection is null
95       */
96 <     public LinkedList(Collection<? extends E> c) {
97 <         this();
98 <         addAll(c);
99 <     }
96 >    public LinkedList(Collection<? extends E> c) {
97 >        this();
98 >        addAll(c);
99 >    }
100  
101      /**
102       * Returns the first element in this list.
# Line 144 | Line 145 | public class LinkedList<E>
145      }
146  
147      /**
148 <     * Inserts the given element at the beginning of this list.
148 >     * Inserts the specified element at the beginning of this list.
149       *
150 <     * @param e the element to be inserted at the beginning of this list
150 >     * @param e the element to add
151       */
152      public void addFirst(E e) {
153          addBefore(e, header.next);
154      }
155  
156      /**
157 <     * Appends the given element to the end of this list.  (Identical in
157 <     * function to the <tt>add</tt> method; included only for consistency.)
157 >     * Appends the specified element to the end of this list.
158       *
159 <     * @param e the element to be inserted at the end of this list
159 >     * <p>This method is equivalent to {@link #add}.
160 >     *
161 >     * @param e the element to add
162       */
163      public void addLast(E e) {
164          addBefore(e, header);
# Line 187 | Line 189 | public class LinkedList<E>
189      /**
190       * Appends the specified element to the end of this list.
191       *
192 +     * <p>This method is equivalent to {@link #addLast}.
193 +     *
194       * @param e element to be appended to this list
195 <     * @return <tt>true</tt> (as per the spec for {@link Collection#add})
195 >     * @return <tt>true</tt> (as specified by {@link Collection#add})
196       */
197      public boolean add(E e) {
198          addBefore(e, header);
# Line 232 | Line 236 | public class LinkedList<E>
236       * this list, in the order that they are returned by the specified
237       * collection's iterator.  The behavior of this operation is undefined if
238       * the specified collection is modified while the operation is in
239 <     * progress.  (This implies that the behavior of this call is undefined if
240 <     * the specified Collection is this list, and this list is nonempty.)
239 >     * progress.  (Note that this will occur if the specified collection is
240 >     * this list, and it's nonempty.)
241       *
242 <     * @param c the elements to be inserted into this list
242 >     * @param c collection containing elements to be added to this list
243       * @return <tt>true</tt> if this list changed as a result of the call
244       * @throws NullPointerException if the specified collection is null
245       */
# Line 253 | Line 257 | public class LinkedList<E>
257       *
258       * @param index index at which to insert the first element
259       *              from the specified collection
260 <     * @param c elements to be inserted into this list
260 >     * @param c collection containing elements to be added to this list
261       * @return <tt>true</tt> if this list changed as a result of the call
262       * @throws IndexOutOfBoundsException {@inheritDoc}
263       * @throws NullPointerException if the specified collection is null
# Line 375 | Line 379 | public class LinkedList<E>
379      // Search Operations
380  
381      /**
382 <     * Returns the index in this list of the first occurrence of the
383 <     * specified element, or -1 if the List does not contain this
384 <     * element.  More formally, returns the lowest index i such that
385 <     * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if
386 <     * there is no such index.
382 >     * Returns the index of the first occurrence of the specified element
383 >     * in this list, or -1 if this list does not contain the element.
384 >     * More formally, returns the lowest index <tt>i</tt> such that
385 >     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
386 >     * or -1 if there is no such index.
387       *
388       * @param o element to search for
389 <     * @return the index in this list of the first occurrence of the
390 <     *         specified element, or -1 if the list does not contain this
387 <     *         element
389 >     * @return the index of the first occurrence of the specified element in
390 >     *         this list, or -1 if this list does not contain the element
391       */
392      public int indexOf(Object o) {
393          int index = 0;
# Line 405 | Line 408 | public class LinkedList<E>
408      }
409  
410      /**
411 <     * Returns the index in this list of the last occurrence of the
412 <     * specified element, or -1 if the list does not contain this
413 <     * element.  More formally, returns the highest index i such that
414 <     * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if
415 <     * there is no such index.
411 >     * Returns the index of the last occurrence of the specified element
412 >     * in this list, or -1 if this list does not contain the element.
413 >     * More formally, returns the highest index <tt>i</tt> such that
414 >     * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
415 >     * or -1 if there is no such index.
416       *
417       * @param o element to search for
418 <     * @return the index in this list of the last occurrence of the
419 <     *         specified element, or -1 if the list does not contain this
417 <     *         element
418 >     * @return the index of the last occurrence of the specified element in
419 >     *         this list, or -1 if this list does not contain the element
420       */
421      public int lastIndexOf(Object o) {
422          int index = size;
# Line 483 | Line 485 | public class LinkedList<E>
485       * Adds the specified element as the tail (last element) of this list.
486       *
487       * @param e the element to add
488 <     * @return <tt>true</tt> (as per the spec for {@link Queue#offer})
488 >     * @return <tt>true</tt> (as specified by {@link Queue#offer})
489       * @since 1.5
490       */
491      public boolean offer(E e) {
# Line 495 | Line 497 | public class LinkedList<E>
497       * Inserts the specified element at the front of this list.
498       *
499       * @param e the element to insert
500 <     * @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst})
500 >     * @return <tt>true</tt> (as specified by {@link Deque#offerFirst})
501       * @since 1.6
502       */
503      public boolean offerFirst(E e) {
# Line 507 | Line 509 | public class LinkedList<E>
509       * Inserts the specified element at the end of this list.
510       *
511       * @param e the element to insert
512 <     * @return <tt>true</tt> (as per the spec for {@link Deque#offerLast})
512 >     * @return <tt>true</tt> (as specified by {@link Deque#offerLast})
513       * @since 1.6
514       */
515      public boolean offerLast(E e) {
# Line 544 | Line 546 | public class LinkedList<E>
546      }
547  
548      /**
549 <     * Retrieves and removes the first element of this list, or
550 <     * <tt>null</tt> if this list is empty.
549 >     * Retrieves and removes the first element of this list,
550 >     * or returns <tt>null</tt> if this list is empty.
551       *
552       * @return the first element of this list, or <tt>null</tt> if
553       *     this list is empty
# Line 558 | Line 560 | public class LinkedList<E>
560      }
561  
562      /**
563 <     * Retrieves and removes the last element of this list, or
564 <     * <tt>null</tt> if this list is empty.
563 >     * Retrieves and removes the last element of this list,
564 >     * or returns <tt>null</tt> if this list is empty.
565       *
566       * @return the last element of this list, or <tt>null</tt> if
567       *     this list is empty
# Line 623 | Line 625 | public class LinkedList<E>
625       */
626      public boolean removeLastOccurrence(Object o) {
627          if (o==null) {
628 <            for (Entry e = header.previous; e != header; e = e.previous) {
628 >            for (Entry<E> e = header.previous; e != header; e = e.previous) {
629                  if (e.element==null) {
630                      remove(e);
631                      return true;
632                  }
633              }
634          } else {
635 <            for (Entry e = header.previous; e != header; e = e.previous) {
635 >            for (Entry<E> e = header.previous; e != header; e = e.previous) {
636                  if (o.equals(e.element)) {
637                      remove(e);
638                      return true;
# Line 796 | Line 798 | public class LinkedList<E>
798      }
799  
800      /**
801 +     * @since 1.6
802 +     */
803 +    public Iterator<E> descendingIterator() {
804 +        return new DescendingIterator();
805 +    }
806 +
807 +    /** Adapter to provide descending iterators via ListItr.previous */
808 +    private class DescendingIterator implements Iterator {
809 +        final ListItr itr = new ListItr(size());
810 +        public boolean hasNext() {
811 +            return itr.hasPrevious();
812 +        }
813 +        public E next() {
814 +            return itr.previous();
815 +        }
816 +        public void remove() {
817 +            itr.remove();
818 +        }
819 +    }
820 +
821 +    /**
822       * Returns a shallow copy of this <tt>LinkedList</tt>. (The elements
823       * themselves are not cloned.)
824       *
# Line 824 | Line 847 | public class LinkedList<E>
847  
848      /**
849       * Returns an array containing all of the elements in this list
850 <     * in the correct order.
850 >     * in proper sequence (from first to last element).
851 >     *
852 >     * <p>The returned array will be "safe" in that no references to it are
853 >     * maintained by this list.  (In other words, this method must allocate
854 >     * a new array).  The caller is thus free to modify the returned array.
855 >     *
856 >     * <p>This method acts as bridge between array-based and collection-based
857 >     * APIs.
858       *
859       * @return an array containing all of the elements in this list
860 <     *         in the correct order
860 >     *         in proper sequence
861       */
862      public Object[] toArray() {
863          Object[] result = new Object[size];
# Line 839 | Line 869 | public class LinkedList<E>
869  
870      /**
871       * Returns an array containing all of the elements in this list in
872 <     * the correct order; the runtime type of the returned array is that of
873 <     * the specified array.  If the list fits in the specified array, it
874 <     * is returned therein.  Otherwise, a new array is allocated with the
875 <     * runtime type of the specified array and the size of this list.<p>
876 <     *
877 <     * If the list fits in the specified array with room to spare
878 <     * (i.e., the array has more elements than the list),
879 <     * the element in the array immediately following the end of the
880 <     * collection is set to null.  This is useful in determining the length
881 <     * of the list <i>only</i> if the caller knows that the list
882 <     * does not contain any null elements.
872 >     * proper sequence (from first to last element); the runtime type of
873 >     * the returned array is that of the specified array.  If the list fits
874 >     * in the specified array, it is returned therein.  Otherwise, a new
875 >     * array is allocated with the runtime type of the specified array and
876 >     * the size of this list.
877 >     *
878 >     * <p>If the list fits in the specified array with room to spare (i.e.,
879 >     * the array has more elements than the list), the element in the array
880 >     * immediately following the end of the list is set to <tt>null</tt>.
881 >     * (This is useful in determining the length of the list <i>only</i> if
882 >     * the caller knows that the list does not contain any null elements.)
883 >     *
884 >     * <p>Like the {@link #toArray()} method, this method acts as bridge between
885 >     * array-based and collection-based APIs.  Further, this method allows
886 >     * precise control over the runtime type of the output array, and may,
887 >     * under certain circumstances, be used to save allocation costs.
888 >     *
889 >     * <p>Suppose <tt>x</tt> is a list known to contain only strings.
890 >     * The following code can be used to dump the list into a newly
891 >     * allocated array of <tt>String</tt>:
892 >     *
893 >     * <pre>
894 >     *     String[] y = x.toArray(new String[0]);</pre>
895 >     *
896 >     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
897 >     * <tt>toArray()</tt>.
898       *
899       * @param a the array into which the elements of the list are to
900       *          be stored, if it is big enough; otherwise, a new array of the
901       *          same runtime type is allocated for this purpose.
902       * @return an array containing the elements of the list
903 <     * @throws ArrayStoreException if the runtime type of a is not a
904 <     *         supertype of the runtime type of every element in this list
903 >     * @throws ArrayStoreException if the runtime type of the specified array
904 >     *         is not a supertype of the runtime type of every element in
905 >     *         this list
906       * @throws NullPointerException if the specified array is null
907       */
908      public <T> T[] toArray(T[] a) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines