1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
8 |
< |
package java.util; |
8 |
> |
package java.util; |
9 |
|
|
10 |
|
/** |
11 |
|
* Linked list implementation of the <tt>List</tt> interface. Implements all |
19 |
|
* |
20 |
|
* The class implements the <tt>Deque</tt> interface, providing |
21 |
|
* first-in-first-out queue operations for <tt>add</tt>, |
22 |
< |
* <tt>poll</tt>, along with other stack and deque operations. |
22 |
> |
* <tt>poll</tt>, along with other stack and deque operations.<p> |
23 |
|
* |
24 |
|
* All of the operations perform as could be expected for a doubly-linked |
25 |
|
* list. Operations that index into the list will traverse the list from |
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><p> |
39 |
> |
* </pre> |
40 |
|
* |
41 |
< |
* The iterators returned by the this class's <tt>iterator</tt> and |
41 |
> |
* <p>The iterators returned by this class's <tt>iterator</tt> and |
42 |
|
* <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is |
43 |
< |
* structurally modified at any time after the iterator is created, in any way |
44 |
< |
* except through the Iterator's own <tt>remove</tt> or <tt>add</tt> methods, |
45 |
< |
* the iterator will throw a <tt>ConcurrentModificationException</tt>. Thus, |
46 |
< |
* in the face of concurrent modification, the iterator fails quickly and |
47 |
< |
* cleanly, rather than risking arbitrary, non-deterministic behavior at an |
48 |
< |
* undetermined time in the future. |
43 |
> |
* structurally modified at any time after the iterator is created, in |
44 |
> |
* any way except through the Iterator's own <tt>remove</tt> or |
45 |
> |
* <tt>add</tt> methods, the iterator will throw a {@link |
46 |
> |
* ConcurrentModificationException}. Thus, in the face of concurrent |
47 |
> |
* modification, the iterator fails quickly and cleanly, rather than |
48 |
> |
* risking arbitrary, non-deterministic behavior at an undetermined |
49 |
> |
* time in the future. |
50 |
|
* |
51 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
52 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
54 |
|
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
55 |
|
* Therefore, it would be wrong to write a program that depended on this |
56 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
57 |
< |
* should be used only to detect bugs.</i><p> |
57 |
> |
* should be used only to detect bugs.</i> |
58 |
|
* |
59 |
< |
* This class is a member of the |
59 |
> |
* <p>This class is a member of the |
60 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
61 |
|
* Java Collections Framework</a>. |
62 |
|
* |
146 |
|
/** |
147 |
|
* Inserts the given element at the beginning of this list. |
148 |
|
* |
149 |
< |
* @param o the element to be inserted at the beginning of this list. |
149 |
> |
* @param e the element to be inserted at the beginning of this list. |
150 |
|
*/ |
151 |
< |
public void addFirst(E o) { |
152 |
< |
addBefore(o, header.next); |
151 |
> |
public void addFirst(E e) { |
152 |
> |
addBefore(e, header.next); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
|
* Appends the given element to the end of this list. (Identical in |
157 |
|
* function to the <tt>add</tt> method; included only for consistency.) |
158 |
|
* |
159 |
< |
* @param o the element to be inserted at the end of this list. |
159 |
> |
* @param e the element to be inserted at the end of this list. |
160 |
|
*/ |
161 |
< |
public void addLast(E o) { |
162 |
< |
addBefore(o, header); |
161 |
> |
public void addLast(E e) { |
162 |
> |
addBefore(e, header); |
163 |
|
} |
164 |
|
|
165 |
|
/** |
187 |
|
/** |
188 |
|
* Appends the specified element to the end of this list. |
189 |
|
* |
190 |
< |
* @param o element to be appended to this list. |
190 |
> |
* @param e element to be appended to this list. |
191 |
|
* @return <tt>true</tt> (as per the general contract of |
192 |
|
* <tt>Collection.add</tt>). |
193 |
|
*/ |
194 |
< |
public boolean add(E o) { |
195 |
< |
addBefore(o, header); |
194 |
> |
public boolean add(E e) { |
195 |
> |
addBefore(e, header); |
196 |
|
return true; |
197 |
|
} |
198 |
|
|
253 |
|
* from the specified collection. |
254 |
|
* @param c elements to be inserted into this list. |
255 |
|
* @return <tt>true</tt> if this list changed as a result of the call. |
256 |
< |
* @throws IndexOutOfBoundsException if the specified index is out of |
257 |
< |
* range (<tt>index < 0 || index > size()</tt>). |
256 |
> |
* @throws IndexOutOfBoundsException if the index is out of range |
257 |
> |
* (<tt>index < 0 || index > size()</tt>). |
258 |
|
* @throws NullPointerException if the specified collection is null. |
259 |
|
*/ |
260 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
305 |
|
* @param index index of element to return. |
306 |
|
* @return the element at the specified position in this list. |
307 |
|
* |
308 |
< |
* @throws IndexOutOfBoundsException if the specified index is out of |
309 |
< |
* range (<tt>index < 0 || index >= size()</tt>). |
308 |
> |
* @throws IndexOutOfBoundsException if the index is out of range |
309 |
> |
* (<tt>index < 0 || index > size()</tt>). |
310 |
|
*/ |
311 |
|
public E get(int index) { |
312 |
|
return entry(index).element; |
319 |
|
* @param index index of element to replace. |
320 |
|
* @param element element to be stored at the specified position. |
321 |
|
* @return the element previously at the specified position. |
322 |
< |
* @throws IndexOutOfBoundsException if the specified index is out of |
323 |
< |
* range (<tt>index < 0 || index >= size()</tt>). |
322 |
> |
* @throws IndexOutOfBoundsException if the index is out of range |
323 |
> |
* (<tt>index < 0 || index > size()</tt>). |
324 |
|
*/ |
325 |
|
public E set(int index, E element) { |
326 |
|
Entry<E> e = entry(index); |
337 |
|
* @param index index at which the specified element is to be inserted. |
338 |
|
* @param element element to be inserted. |
339 |
|
* |
340 |
< |
* @throws IndexOutOfBoundsException if the specified index is out of |
341 |
< |
* range (<tt>index < 0 || index > size()</tt>). |
340 |
> |
* @throws IndexOutOfBoundsException if the index is out of range |
341 |
> |
* (<tt>index < 0 || index > size()</tt>). |
342 |
|
*/ |
343 |
|
public void add(int index, E element) { |
344 |
|
addBefore(element, (index==size ? header : entry(index))); |
352 |
|
* @param index the index of the element to removed. |
353 |
|
* @return the element previously at the specified position. |
354 |
|
* |
355 |
< |
* @throws IndexOutOfBoundsException if the specified index is out of |
356 |
< |
* range (<tt>index < 0 || index >= size()</tt>). |
355 |
> |
* @throws IndexOutOfBoundsException if the index is out of range |
356 |
> |
* (<tt>index < 0 || index > size()</tt>). |
357 |
|
*/ |
358 |
|
public E remove(int index) { |
359 |
|
return remove(entry(index)); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
< |
* Return the indexed entry. |
363 |
> |
* Returns the indexed entry. |
364 |
|
*/ |
365 |
|
private Entry<E> entry(int index) { |
366 |
|
if (index < 0 || index >= size) |
487 |
|
/** |
488 |
|
* Adds the specified element as the tail (last element) of this list. |
489 |
|
* |
490 |
< |
* @param o the element to add. |
490 |
> |
* @param e the element to add. |
491 |
|
* @return <tt>true</tt> (as per the general contract of |
492 |
|
* <tt>Queue.offer</tt>) |
493 |
|
* @since 1.5 |
494 |
|
*/ |
495 |
< |
public boolean offer(E o) { |
496 |
< |
return add(o); |
495 |
> |
public boolean offer(E e) { |
496 |
> |
return add(e); |
497 |
|
} |
498 |
|
|
499 |
|
// Deque operations |
500 |
|
/** |
501 |
< |
* Inserts the specified element to the front of this list. |
501 |
> |
* Inserts the specified element at the front of this list. |
502 |
|
* |
503 |
|
* @param e the element to insert |
504 |
|
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
510 |
|
} |
511 |
|
|
512 |
|
/** |
513 |
< |
* Inserts the specified element to the end of this list. |
513 |
> |
* Inserts the specified element at the end of this list. |
514 |
|
* |
515 |
|
* @param e the element to insert |
516 |
|
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
579 |
|
|
580 |
|
/** |
581 |
|
* Pushes an element onto the stack represented by this list. In other |
582 |
< |
* words, inserts the element to the front of this list. |
582 |
> |
* words, inserts the element at the front of this list. |
583 |
|
* |
584 |
|
* <p>This method is equivalent to {@link #addFirst}. |
585 |
|
* |
598 |
|
* |
599 |
|
* @return the element at the front of this list (which is the top |
600 |
|
* of the stack represented by this list) |
601 |
< |
* @throws NoSuchElementException if this list is empty |
601 |
> |
* @throws NoSuchElementException if this list is empty. |
602 |
|
* @since 1.6 |
603 |
|
*/ |
604 |
|
public E pop() { |
610 |
|
* list (when traversing the list from head to tail). If the list |
611 |
|
* does not contain the element, it is unchanged. |
612 |
|
* |
613 |
< |
* @param e element to be removed from this list, if present |
613 |
> |
* @param o element to be removed from this list, if present |
614 |
|
* @return <tt>true</tt> if the list contained the specified element |
615 |
|
* @since 1.6 |
616 |
|
*/ |
617 |
< |
public boolean removeFirstOccurrence(Object e) { |
618 |
< |
return remove(e); |
617 |
> |
public boolean removeFirstOccurrence(Object o) { |
618 |
> |
return remove(o); |
619 |
|
} |
620 |
|
|
621 |
|
/** |
664 |
|
* list-iterator (by a call to <tt>next</tt>). |
665 |
|
* @return a ListIterator of the elements in this list (in proper |
666 |
|
* sequence), starting at the specified position in the list. |
667 |
< |
* @throws IndexOutOfBoundsException if index is out of range |
668 |
< |
* (<tt>index < 0 || index > size()</tt>). |
667 |
> |
* @throws IndexOutOfBoundsException if the index is out of range |
668 |
> |
* (<tt>index < 0 || index > size()</tt>). |
669 |
|
* @see List#listIterator(int) |
670 |
|
*/ |
671 |
|
public ListIterator<E> listIterator(int index) { |
746 |
|
expectedModCount++; |
747 |
|
} |
748 |
|
|
749 |
< |
public void set(E o) { |
749 |
> |
public void set(E e) { |
750 |
|
if (lastReturned == header) |
751 |
|
throw new IllegalStateException(); |
752 |
|
checkForComodification(); |
753 |
< |
lastReturned.element = o; |
753 |
> |
lastReturned.element = e; |
754 |
|
} |
755 |
|
|
756 |
< |
public void add(E o) { |
756 |
> |
public void add(E e) { |
757 |
|
checkForComodification(); |
758 |
|
lastReturned = header; |
759 |
< |
addBefore(o, next); |
759 |
> |
addBefore(e, next); |
760 |
|
nextIndex++; |
761 |
|
expectedModCount++; |
762 |
|
} |
779 |
|
} |
780 |
|
} |
781 |
|
|
782 |
< |
private Entry<E> addBefore(E o, Entry<E> e) { |
783 |
< |
Entry<E> newEntry = new Entry<E>(o, e, e.previous); |
782 |
> |
private Entry<E> addBefore(E e, Entry<E> entry) { |
783 |
> |
Entry<E> newEntry = new Entry<E>(e, entry, entry.previous); |
784 |
|
newEntry.previous.next = newEntry; |
785 |
|
newEntry.next.previous = newEntry; |
786 |
|
size++; |