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.4 by dl, Mon Aug 11 17:44:24 2003 UTC vs.
Revision 1.15 by jsr166, Sun Apr 11 04:50:24 2004 UTC

# Line 1 | Line 1
1   /*
2 < * @(#)LinkedList.java  1.53 03/06/22
2 > * %W% %E%
3   *
4 < * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2004 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   *
26   * All of the operations perform as could be expected for a doubly-linked
27   * list.  Operations that index into the list will traverse the list from
28 < * the begining or the end, whichever is closer to the specified index.<p>
28 > * the beginning or the end, whichever is closer to the specified index.<p>
29   *
30   * <b>Note that this implementation is not synchronized.</b> If multiple
31   * threads access a list concurrently, and at least one of the threads
# Line 62 | Line 62 | package java.util;
62   * Java Collections Framework</a>.
63   *
64   * @author  Josh Bloch
65 < * @version 1.53, 06/22/03
65 > * @version %I%, %G%
66   * @see     List
67   * @see     ArrayList
68   * @see     Vector
69   * @see     Collections#synchronizedList(List)
70   * @since 1.2
71 + * @param <E> the type of elements held in this collection
72   */
73  
74   public class LinkedList<E>
# Line 130 | Line 131 | public class LinkedList<E>
131       * @throws    NoSuchElementException if this list is empty.
132       */
133      public E removeFirst() {
134 <        E first = header.next.element;
134 <        remove(header.next);
135 <        return first;
134 >        return remove(header.next);
135      }
136  
137      /**
# Line 142 | Line 141 | public class LinkedList<E>
141       * @throws    NoSuchElementException if this list is empty.
142       */
143      public E removeLast() {
144 <        E last = header.previous.element;
146 <        remove(header.previous);
147 <        return last;
144 >        return remove(header.previous);
145      }
146  
147      /**
# Line 288 | Line 285 | public class LinkedList<E>
285       * Removes all of the elements from this list.
286       */
287      public void clear() {
288 <        modCount++;
288 >        Entry<E> e = header.next;
289 >        while (e != header) {
290 >            Entry<E> next = e.next;
291 >            e.next = e.previous = null;
292 >            e.element = null;
293 >            e = next;
294 >        }
295          header.next = header.previous = header;
296 <        size = 0;
296 >        size = 0;
297 >        modCount++;
298      }
299  
300  
# Line 302 | Line 306 | public class LinkedList<E>
306       * @param index index of element to return.
307       * @return the element at the specified position in this list.
308       *
309 <     * @throws IndexOutOfBoundsException if the specified index is is out of
309 >     * @throws IndexOutOfBoundsException if the specified index is out of
310       * range (<tt>index &lt; 0 || index &gt;= size()</tt>).
311       */
312      public E get(int index) {
# Line 353 | Line 357 | public class LinkedList<E>
357       *            range (<tt>index &lt; 0 || index &gt;= size()</tt>).
358       */
359      public E remove(int index) {
360 <        Entry<E> e = entry(index);
357 <        remove(e);
358 <        return e.element;
360 >        return remove(entry(index));
361      }
362  
363      /**
# Line 442 | Line 444 | public class LinkedList<E>
444      // Queue operations.
445  
446      /**
447 <     * Retrieves, but does not remove, the head (first element) of this list..
447 >     * Retrieves, but does not remove, the head (first element) of this list.
448       * @return the head of this queue, or <tt>null</tt> if this queue is empty.
449 +     * @since 1.5
450       */
451      public E peek() {
452          if (size==0)
# Line 452 | Line 455 | public class LinkedList<E>
455      }
456  
457      /**
458 <     * Retrieves, but does not remove, the head (first element) of this list..
458 >     * Retrieves, but does not remove, the head (first element) of this list.
459       * @return the head of this queue.
460       * @throws NoSuchElementException if this queue is empty.
461 +     * @since 1.5
462       */
463      public E element() {
464          return getFirst();
465      }
466  
467      /**
468 <     * Retrieves and removes the head (first element) of this list..
468 >     * Retrieves and removes the head (first element) of this list.
469       * @return the head of this queue, or <tt>null</tt> if this queue is empty.
470 +     * @since 1.5
471       */
472      public E poll() {
473          if (size==0)
# Line 471 | Line 476 | public class LinkedList<E>
476      }
477  
478      /**
479 <     * Retrieves and removes the head (first element) of this list..
479 >     * Retrieves and removes the head (first element) of this list.
480       * @return the head of this queue.
481       * @throws NoSuchElementException if this queue is empty.
482 +     * @since 1.5
483       */
484      public E remove() {
485          return removeFirst();
# Line 485 | Line 491 | public class LinkedList<E>
491       * @param o the element to add.
492       * @return <tt>true</tt> (as per the general contract of
493       * <tt>Queue.offer</tt>)
494 +     * @since 1.5
495       */
496 <    public boolean offer(E x) {
497 <        return add(x);
496 >    public boolean offer(E o) {
497 >        return add(o);
498      }
499  
500      /**
# Line 576 | Line 583 | public class LinkedList<E>
583  
584          public void remove() {
585              checkForComodification();
586 +            Entry<E> lastNext = lastReturned.next;
587              try {
588                  LinkedList.this.remove(lastReturned);
589              } catch (NoSuchElementException e) {
590                  throw new IllegalStateException();
591              }
592              if (next==lastReturned)
593 <                next = lastReturned.next;
593 >                next = lastNext;
594              else
595                  nextIndex--;
596              lastReturned = header;
# Line 631 | Line 639 | public class LinkedList<E>
639          return newEntry;
640      }
641  
642 <    private void remove(Entry<E> e) {
642 >    private E remove(Entry<E> e) {
643          if (e == header)
644              throw new NoSuchElementException();
645  
646 +        E result = e.element;
647          e.previous.next = e.next;
648          e.next.previous = e.previous;
649 +        e.next = e.previous = null;
650 +        e.element = null;
651          size--;
652          modCount++;
653 +        return result;
654      }
655  
656      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines