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 |
|
* |
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) |
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 this list. |
582 |
> |
* words, inserts the element at the front of this list. |
583 |
|
* |
584 |
|
* <p>This method is equivalent to {@link #addFirst}. |
585 |
|
* |
592 |
|
|
593 |
|
/** |
594 |
|
* Pops an element from the stack represented by this list. In other |
595 |
< |
* words, removes and returns the the first element of this list. |
595 |
> |
* words, removes and returns the first element of this list. |
596 |
|
* |
597 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
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) { |