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 |
|
|
8 |
|
package java.util; |
9 |
+ |
import java.util.*; // for javadoc (till 6280605 is fixed) |
10 |
|
|
11 |
|
/** |
12 |
|
* Linked list implementation of the <tt>List</tt> interface. Implements all |
26 |
|
* list. Operations that index into the list will traverse the list from |
27 |
|
* the beginning or the end, whichever is closer to the specified index.<p> |
28 |
|
* |
29 |
< |
* <b>Note that this implementation is not synchronized.</b> If multiple |
30 |
< |
* threads access a list concurrently, and at least one of the threads |
31 |
< |
* modifies the list structurally, it <i>must</i> be synchronized |
32 |
< |
* externally. (A structural modification is any operation that adds or |
33 |
< |
* deletes one or more elements; merely setting the value of an element is not |
34 |
< |
* a structural modification.) This is typically accomplished by |
35 |
< |
* synchronizing on some object that naturally encapsulates the list. If no |
36 |
< |
* such object exists, the list should be "wrapped" using the |
37 |
< |
* Collections.synchronizedList method. This is best done at creation time, |
38 |
< |
* to prevent accidental unsynchronized access to the list: <pre> |
39 |
< |
* List list = Collections.synchronizedList(new LinkedList(...)); |
40 |
< |
* </pre> |
29 |
> |
* <p><strong>Note that this implementation is not synchronized.</strong> |
30 |
> |
* If multiple threads access a linked list concurrently, and at least |
31 |
> |
* one of the threads modifies the list structurally, it <i>must</i> be |
32 |
> |
* synchronized externally. (A structural modification is any operation |
33 |
> |
* that adds or deletes one or more elements; merely setting the value of |
34 |
> |
* an element is not a structural modification.) This is typically |
35 |
> |
* accomplished by synchronizing on some object that naturally |
36 |
> |
* encapsulates the list. |
37 |
> |
* |
38 |
> |
* If no such object exists, the list should be "wrapped" using the |
39 |
> |
* {@link Collections#synchronizedList Collections.synchronizedList} |
40 |
> |
* method. This is best done at creation time, to prevent accidental |
41 |
> |
* unsynchronized access to the list:<pre> |
42 |
> |
* List list = Collections.synchronizedList(new LinkedList(...));</pre> |
43 |
|
* |
44 |
|
* <p>The iterators returned by this class's <tt>iterator</tt> and |
45 |
|
* <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is |
68 |
|
* @see List |
69 |
|
* @see ArrayList |
70 |
|
* @see Vector |
68 |
– |
* @see Collections#synchronizedList(List) |
71 |
|
* @since 1.2 |
72 |
|
* @param <E> the type of elements held in this collection |
73 |
|
*/ |
94 |
|
* @param c the collection whose elements are to be placed into this list |
95 |
|
* @throws NullPointerException if the specified collection is null |
96 |
|
*/ |
97 |
< |
public LinkedList(Collection<? extends E> c) { |
98 |
< |
this(); |
99 |
< |
addAll(c); |
100 |
< |
} |
97 |
> |
public LinkedList(Collection<? extends E> c) { |
98 |
> |
this(); |
99 |
> |
addAll(c); |
100 |
> |
} |
101 |
|
|
102 |
|
/** |
103 |
|
* Returns the first element in this list. |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
< |
* Inserts the given element at the beginning of this list. |
149 |
> |
* Inserts the specified element at the beginning of this list. |
150 |
|
* |
151 |
< |
* @param e the element to be inserted at the beginning of this list |
151 |
> |
* @param e the element to add |
152 |
|
*/ |
153 |
|
public void addFirst(E e) { |
154 |
|
addBefore(e, header.next); |
155 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
< |
* Appends the given element to the end of this list. (Identical in |
159 |
< |
* function to the <tt>add</tt> method; included only for consistency.) |
158 |
> |
* Appends the specified element to the end of this list. |
159 |
> |
* |
160 |
> |
* <p>This method is equivalent to {@link #add}. |
161 |
|
* |
162 |
< |
* @param e the element to be inserted at the end of this list |
162 |
> |
* @param e the element to add |
163 |
|
*/ |
164 |
|
public void addLast(E e) { |
165 |
|
addBefore(e, header); |
190 |
|
/** |
191 |
|
* Appends the specified element to the end of this list. |
192 |
|
* |
193 |
+ |
* <p>This method is equivalent to {@link #addLast}. |
194 |
+ |
* |
195 |
|
* @param e element to be appended to this list |
196 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
196 |
> |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
197 |
|
*/ |
198 |
|
public boolean add(E e) { |
199 |
|
addBefore(e, header); |
237 |
|
* this list, in the order that they are returned by the specified |
238 |
|
* collection's iterator. The behavior of this operation is undefined if |
239 |
|
* the specified collection is modified while the operation is in |
240 |
< |
* progress. (This implies that the behavior of this call is undefined if |
241 |
< |
* the specified Collection is this list, and this list is nonempty.) |
240 |
> |
* progress. (Note that this will occur if the specified collection is |
241 |
> |
* this list, and it's nonempty.) |
242 |
|
* |
243 |
|
* @param c collection containing elements to be added to this list |
244 |
|
* @return <tt>true</tt> if this list changed as a result of the call |
486 |
|
* Adds the specified element as the tail (last element) of this list. |
487 |
|
* |
488 |
|
* @param e the element to add |
489 |
< |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
489 |
> |
* @return <tt>true</tt> (as specified by {@link Queue#offer}) |
490 |
|
* @since 1.5 |
491 |
|
*/ |
492 |
|
public boolean offer(E e) { |
498 |
|
* Inserts the specified element at the front of this list. |
499 |
|
* |
500 |
|
* @param e the element to insert |
501 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
501 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerFirst}) |
502 |
|
* @since 1.6 |
503 |
|
*/ |
504 |
|
public boolean offerFirst(E e) { |
510 |
|
* Inserts the specified element at the end of this list. |
511 |
|
* |
512 |
|
* @param e the element to insert |
513 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
513 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerLast}) |
514 |
|
* @since 1.6 |
515 |
|
*/ |
516 |
|
public boolean offerLast(E e) { |
547 |
|
} |
548 |
|
|
549 |
|
/** |
550 |
< |
* Retrieves and removes the first element of this list, or |
551 |
< |
* <tt>null</tt> if this list is empty. |
550 |
> |
* Retrieves and removes the first element of this list, |
551 |
> |
* or returns <tt>null</tt> if this list is empty. |
552 |
|
* |
553 |
|
* @return the first element of this list, or <tt>null</tt> if |
554 |
|
* this list is empty |
561 |
|
} |
562 |
|
|
563 |
|
/** |
564 |
< |
* Retrieves and removes the last element of this list, or |
565 |
< |
* <tt>null</tt> if this list is empty. |
564 |
> |
* Retrieves and removes the last element of this list, |
565 |
> |
* or returns <tt>null</tt> if this list is empty. |
566 |
|
* |
567 |
|
* @return the last element of this list, or <tt>null</tt> if |
568 |
|
* this list is empty |
626 |
|
*/ |
627 |
|
public boolean removeLastOccurrence(Object o) { |
628 |
|
if (o==null) { |
629 |
< |
for (Entry e = header.previous; e != header; e = e.previous) { |
629 |
> |
for (Entry<E> e = header.previous; e != header; e = e.previous) { |
630 |
|
if (e.element==null) { |
631 |
|
remove(e); |
632 |
|
return true; |
633 |
|
} |
634 |
|
} |
635 |
|
} else { |
636 |
< |
for (Entry e = header.previous; e != header; e = e.previous) { |
636 |
> |
for (Entry<E> e = header.previous; e != header; e = e.previous) { |
637 |
|
if (o.equals(e.element)) { |
638 |
|
remove(e); |
639 |
|
return true; |
798 |
|
return result; |
799 |
|
} |
800 |
|
|
801 |
+ |
public Iterator<E> descendingIterator() { |
802 |
+ |
return new DescendingIterator(); |
803 |
+ |
} |
804 |
+ |
|
805 |
+ |
/** Adapter to provide descending iterators via ListItr.previous */ |
806 |
+ |
private class DescendingIterator implements Iterator { |
807 |
+ |
final ListItr itr = new ListItr(size()); |
808 |
+ |
public boolean hasNext() { |
809 |
+ |
return itr.hasPrevious(); |
810 |
+ |
} |
811 |
+ |
public E next() { |
812 |
+ |
return itr.previous(); |
813 |
+ |
} |
814 |
+ |
public void remove() { |
815 |
+ |
itr.remove(); |
816 |
+ |
} |
817 |
+ |
} |
818 |
+ |
|
819 |
|
/** |
820 |
|
* Returns a shallow copy of this <tt>LinkedList</tt>. (The elements |
821 |
|
* themselves are not cloned.) |
850 |
|
* <p>The returned array will be "safe" in that no references to it are |
851 |
|
* maintained by this list. (In other words, this method must allocate |
852 |
|
* a new array). The caller is thus free to modify the returned array. |
853 |
< |
* |
853 |
> |
* |
854 |
|
* <p>This method acts as bridge between array-based and collection-based |
855 |
|
* APIs. |
856 |
|
* |