1 |
|
/* |
2 |
< |
* @(#)LinkedList.java 1.43 01/12/03 |
2 |
> |
* @(#)LinkedList.java 1.53 03/06/22 |
3 |
|
* |
4 |
< |
* Copyright 2002 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2003 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
8 |
|
package java.util; |
9 |
|
|
10 |
|
/** |
11 |
– |
* <b>JSR166: Added Queue operations</b>.<p> |
11 |
|
* Linked list implementation of the <tt>List</tt> interface. Implements all |
12 |
|
* optional list operations, and permits all elements (including |
13 |
|
* <tt>null</tt>). In addition to implementing the <tt>List</tt> interface, |
16 |
|
* beginning and end of the list. These operations allow linked lists to be |
17 |
|
* used as a stack, queue, or double-ended queue (deque).<p> |
18 |
|
* |
19 |
< |
* All of the stack/queue/deque operations could be easily recast in terms of |
20 |
< |
* the standard list operations. They're included here primarily for |
21 |
< |
* convenience, though they may run slightly faster than the equivalent List |
22 |
< |
* operations.<p> |
19 |
> |
* The class implements the <tt>Queue</tt> interface, providing |
20 |
> |
* first-in-first-out queue operations for <tt>add</tt>, |
21 |
> |
* <tt>poll</tt>, etc. Other stack and deque operations could be |
22 |
> |
* easily recast in terms of the standard list operations. They're |
23 |
> |
* included here primarily for convenience, though they may run |
24 |
> |
* slightly faster than the equivalent List operations.<p> |
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 |
52 |
|
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
53 |
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
54 |
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
55 |
< |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
55 |
> |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
56 |
|
* Therefore, it would be wrong to write a program that depended on this |
57 |
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
58 |
< |
* should be used only to detect bugs.</i> |
58 |
> |
* should be used only to detect bugs.</i><p> |
59 |
> |
* |
60 |
> |
* This class is a member of the |
61 |
> |
* <a href="{@docRoot}/../guide/collections/index.html"> |
62 |
> |
* Java Collections Framework</a>. |
63 |
|
* |
64 |
|
* @author Josh Bloch |
65 |
< |
* @version 1.43, 12/03/01 |
66 |
< |
* @see java.util.List |
67 |
< |
* @see java.util.ArrayList |
68 |
< |
* @see java.util.Vector |
69 |
< |
* @see java.util.Collections#synchronizedList(java.util.List) |
65 |
> |
* @version 1.53, 06/22/03 |
66 |
> |
* @see List |
67 |
> |
* @see ArrayList |
68 |
> |
* @see Vector |
69 |
> |
* @see Collections#synchronizedList(List) |
70 |
|
* @since 1.2 |
71 |
|
*/ |
72 |
|
|
73 |
< |
public class LinkedList extends AbstractSequentialList |
74 |
< |
implements List, Queue, Cloneable, java.io.Serializable |
73 |
> |
public class LinkedList<E> |
74 |
> |
extends AbstractSequentialList<E> |
75 |
> |
implements List<E>, Queue<E>, Cloneable, java.io.Serializable |
76 |
|
{ |
77 |
< |
private transient Entry header = new Entry(null, null, null); |
77 |
> |
private transient Entry<E> header = new Entry<E>(null, null, null); |
78 |
|
private transient int size = 0; |
79 |
|
|
80 |
|
/** |
92 |
|
* @param c the collection whose elements are to be placed into this list. |
93 |
|
* @throws NullPointerException if the specified collection is null. |
94 |
|
*/ |
95 |
< |
public LinkedList(Collection c) { |
96 |
< |
this(); |
97 |
< |
addAll(c); |
95 |
> |
public LinkedList(Collection<? extends E> c) { |
96 |
> |
this(); |
97 |
> |
addAll(c); |
98 |
|
} |
99 |
|
|
100 |
|
/** |
103 |
|
* @return the first element in this list. |
104 |
|
* @throws NoSuchElementException if this list is empty. |
105 |
|
*/ |
106 |
< |
public Object getFirst() { |
107 |
< |
if (size==0) |
108 |
< |
throw new NoSuchElementException(); |
106 |
> |
public E getFirst() { |
107 |
> |
if (size==0) |
108 |
> |
throw new NoSuchElementException(); |
109 |
|
|
110 |
< |
return header.next.element; |
110 |
> |
return header.next.element; |
111 |
|
} |
112 |
|
|
113 |
|
/** |
116 |
|
* @return the last element in this list. |
117 |
|
* @throws NoSuchElementException if this list is empty. |
118 |
|
*/ |
119 |
< |
public Object getLast() { |
120 |
< |
if (size==0) |
121 |
< |
throw new NoSuchElementException(); |
119 |
> |
public E getLast() { |
120 |
> |
if (size==0) |
121 |
> |
throw new NoSuchElementException(); |
122 |
|
|
123 |
< |
return header.previous.element; |
123 |
> |
return header.previous.element; |
124 |
|
} |
125 |
|
|
126 |
|
/** |
129 |
|
* @return the first element from this list. |
130 |
|
* @throws NoSuchElementException if this list is empty. |
131 |
|
*/ |
132 |
< |
public Object removeFirst() { |
133 |
< |
Object first = header.next.element; |
134 |
< |
remove(header.next); |
135 |
< |
return first; |
132 |
> |
public E removeFirst() { |
133 |
> |
E first = header.next.element; |
134 |
> |
remove(header.next); |
135 |
> |
return first; |
136 |
|
} |
137 |
|
|
138 |
|
/** |
141 |
|
* @return the last element from this list. |
142 |
|
* @throws NoSuchElementException if this list is empty. |
143 |
|
*/ |
144 |
< |
public Object removeLast() { |
145 |
< |
Object last = header.previous.element; |
146 |
< |
remove(header.previous); |
147 |
< |
return last; |
144 |
> |
public E removeLast() { |
145 |
> |
E last = header.previous.element; |
146 |
> |
remove(header.previous); |
147 |
> |
return last; |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
|
* Inserts the given element at the beginning of this list. |
152 |
< |
* |
152 |
> |
* |
153 |
|
* @param o the element to be inserted at the beginning of this list. |
154 |
|
*/ |
155 |
< |
public void addFirst(Object o) { |
156 |
< |
addBefore(o, header.next); |
155 |
> |
public void addFirst(E o) { |
156 |
> |
addBefore(o, header.next); |
157 |
|
} |
158 |
|
|
159 |
|
/** |
160 |
|
* Appends the given element to the end of this list. (Identical in |
161 |
|
* function to the <tt>add</tt> method; included only for consistency.) |
162 |
< |
* |
162 |
> |
* |
163 |
|
* @param o the element to be inserted at the end of this list. |
164 |
|
*/ |
165 |
< |
public void addLast(Object o) { |
166 |
< |
addBefore(o, header); |
165 |
> |
public void addLast(E o) { |
166 |
> |
addBefore(o, header); |
167 |
|
} |
168 |
|
|
169 |
|
/** |
185 |
|
* @return the number of elements in this list. |
186 |
|
*/ |
187 |
|
public int size() { |
188 |
< |
return size; |
188 |
> |
return size; |
189 |
|
} |
190 |
|
|
191 |
|
/** |
195 |
|
* @return <tt>true</tt> (as per the general contract of |
196 |
|
* <tt>Collection.add</tt>). |
197 |
|
*/ |
198 |
< |
public boolean add(Object o) { |
199 |
< |
addBefore(o, header); |
198 |
> |
public boolean add(E o) { |
199 |
> |
addBefore(o, header); |
200 |
|
return true; |
201 |
|
} |
202 |
|
|
212 |
|
*/ |
213 |
|
public boolean remove(Object o) { |
214 |
|
if (o==null) { |
215 |
< |
for (Entry e = header.next; e != header; e = e.next) { |
215 |
> |
for (Entry<E> e = header.next; e != header; e = e.next) { |
216 |
|
if (e.element==null) { |
217 |
|
remove(e); |
218 |
|
return true; |
219 |
|
} |
220 |
|
} |
221 |
|
} else { |
222 |
< |
for (Entry e = header.next; e != header; e = e.next) { |
222 |
> |
for (Entry<E> e = header.next; e != header; e = e.next) { |
223 |
|
if (o.equals(e.element)) { |
224 |
|
remove(e); |
225 |
|
return true; |
241 |
|
* @return <tt>true</tt> if this list changed as a result of the call. |
242 |
|
* @throws NullPointerException if the specified collection is null. |
243 |
|
*/ |
244 |
< |
public boolean addAll(Collection c) { |
244 |
> |
public boolean addAll(Collection<? extends E> c) { |
245 |
|
return addAll(size, c); |
246 |
|
} |
247 |
|
|
254 |
|
* specified collection's iterator. |
255 |
|
* |
256 |
|
* @param index index at which to insert first element |
257 |
< |
* from the specified collection. |
257 |
> |
* from the specified collection. |
258 |
|
* @param c elements to be inserted into this list. |
259 |
|
* @return <tt>true</tt> if this list changed as a result of the call. |
260 |
|
* @throws IndexOutOfBoundsException if the specified index is out of |
261 |
|
* range (<tt>index < 0 || index > size()</tt>). |
262 |
|
* @throws NullPointerException if the specified collection is null. |
263 |
|
*/ |
264 |
< |
public boolean addAll(int index, Collection c) { |
265 |
< |
int numNew = c.size(); |
266 |
< |
if (numNew==0) { |
267 |
< |
if (index < 0 || index >= size) |
268 |
< |
throw new IndexOutOfBoundsException(); |
269 |
< |
else |
270 |
< |
return false; |
271 |
< |
} |
272 |
< |
modCount++; |
273 |
< |
|
274 |
< |
Entry successor = (index==size ? header : entry(index)); |
275 |
< |
Entry predecessor = successor.previous; |
276 |
< |
Iterator it = c.iterator(); |
277 |
< |
for (int i=0; i<numNew; i++) { |
272 |
< |
Entry e = new Entry(it.next(), successor, predecessor); |
264 |
> |
public boolean addAll(int index, Collection<? extends E> c) { |
265 |
> |
if (index < 0 || index >= size) |
266 |
> |
throw new IndexOutOfBoundsException("Index: "+index+ |
267 |
> |
", Size: "+size); |
268 |
> |
Object[] a = c.toArray(); |
269 |
> |
int numNew = a.length; |
270 |
> |
if (numNew==0) |
271 |
> |
return false; |
272 |
> |
modCount++; |
273 |
> |
|
274 |
> |
Entry<E> successor = (index==size ? header : entry(index)); |
275 |
> |
Entry<E> predecessor = successor.previous; |
276 |
> |
for (int i=0; i<numNew; i++) { |
277 |
> |
Entry<E> e = new Entry<E>((E)a[i], successor, predecessor); |
278 |
|
predecessor.next = e; |
279 |
|
predecessor = e; |
280 |
|
} |
288 |
|
* Removes all of the elements from this list. |
289 |
|
*/ |
290 |
|
public void clear() { |
291 |
< |
modCount++; |
291 |
> |
modCount++; |
292 |
|
header.next = header.previous = header; |
293 |
< |
size = 0; |
293 |
> |
size = 0; |
294 |
|
} |
295 |
|
|
296 |
|
|
301 |
|
* |
302 |
|
* @param index index of element to return. |
303 |
|
* @return the element at the specified position in this list. |
304 |
< |
* |
304 |
> |
* |
305 |
|
* @throws IndexOutOfBoundsException if the specified index is is out of |
306 |
|
* range (<tt>index < 0 || index >= size()</tt>). |
307 |
|
*/ |
308 |
< |
public Object get(int index) { |
308 |
> |
public E get(int index) { |
309 |
|
return entry(index).element; |
310 |
|
} |
311 |
|
|
317 |
|
* @param element element to be stored at the specified position. |
318 |
|
* @return the element previously at the specified position. |
319 |
|
* @throws IndexOutOfBoundsException if the specified index is out of |
320 |
< |
* range (<tt>index < 0 || index >= size()</tt>). |
320 |
> |
* range (<tt>index < 0 || index >= size()</tt>). |
321 |
|
*/ |
322 |
< |
public Object set(int index, Object element) { |
323 |
< |
Entry e = entry(index); |
324 |
< |
Object oldVal = e.element; |
322 |
> |
public E set(int index, E element) { |
323 |
> |
Entry<E> e = entry(index); |
324 |
> |
E oldVal = e.element; |
325 |
|
e.element = element; |
326 |
|
return oldVal; |
327 |
|
} |
333 |
|
* |
334 |
|
* @param index index at which the specified element is to be inserted. |
335 |
|
* @param element element to be inserted. |
336 |
< |
* |
336 |
> |
* |
337 |
|
* @throws IndexOutOfBoundsException if the specified index is out of |
338 |
< |
* range (<tt>index < 0 || index > size()</tt>). |
338 |
> |
* range (<tt>index < 0 || index > size()</tt>). |
339 |
|
*/ |
340 |
< |
public void add(int index, Object element) { |
340 |
> |
public void add(int index, E element) { |
341 |
|
addBefore(element, (index==size ? header : entry(index))); |
342 |
|
} |
343 |
|
|
348 |
|
* |
349 |
|
* @param index the index of the element to removed. |
350 |
|
* @return the element previously at the specified position. |
351 |
< |
* |
351 |
> |
* |
352 |
|
* @throws IndexOutOfBoundsException if the specified index is out of |
353 |
< |
* range (<tt>index < 0 || index >= size()</tt>). |
353 |
> |
* range (<tt>index < 0 || index >= size()</tt>). |
354 |
|
*/ |
355 |
< |
public Object remove(int index) { |
356 |
< |
Entry e = entry(index); |
355 |
> |
public E remove(int index) { |
356 |
> |
Entry<E> e = entry(index); |
357 |
|
remove(e); |
358 |
|
return e.element; |
359 |
|
} |
361 |
|
/** |
362 |
|
* Return the indexed entry. |
363 |
|
*/ |
364 |
< |
private Entry entry(int index) { |
364 |
> |
private Entry<E> entry(int index) { |
365 |
|
if (index < 0 || index >= size) |
366 |
|
throw new IndexOutOfBoundsException("Index: "+index+ |
367 |
|
", Size: "+size); |
368 |
< |
Entry e = header; |
368 |
> |
Entry<E> e = header; |
369 |
|
if (index < (size >> 1)) { |
370 |
|
for (int i = 0; i <= index; i++) |
371 |
|
e = e.next; |
388 |
|
* |
389 |
|
* @param o element to search for. |
390 |
|
* @return the index in this list of the first occurrence of the |
391 |
< |
* specified element, or -1 if the list does not contain this |
392 |
< |
* element. |
391 |
> |
* specified element, or -1 if the list does not contain this |
392 |
> |
* element. |
393 |
|
*/ |
394 |
|
public int indexOf(Object o) { |
395 |
|
int index = 0; |
418 |
|
* |
419 |
|
* @param o element to search for. |
420 |
|
* @return the index in this list of the last occurrence of the |
421 |
< |
* specified element, or -1 if the list does not contain this |
422 |
< |
* element. |
421 |
> |
* specified element, or -1 if the list does not contain this |
422 |
> |
* element. |
423 |
|
*/ |
424 |
|
public int lastIndexOf(Object o) { |
425 |
|
int index = size; |
439 |
|
return -1; |
440 |
|
} |
441 |
|
|
442 |
+ |
// Queue operations. |
443 |
+ |
|
444 |
+ |
/** |
445 |
+ |
* Retrieves, but does not remove, the head (first element) of this list.. |
446 |
+ |
* @return the head of this queue, or <tt>null</tt> if this queue is empty. |
447 |
+ |
*/ |
448 |
+ |
public E peek() { |
449 |
+ |
if (size==0) |
450 |
+ |
return null; |
451 |
+ |
return getFirst(); |
452 |
+ |
} |
453 |
+ |
|
454 |
+ |
/** |
455 |
+ |
* Retrieves, but does not remove, the head (first element) of this list.. |
456 |
+ |
* @return the head of this queue. |
457 |
+ |
* @throws NoSuchElementException if this queue is empty. |
458 |
+ |
*/ |
459 |
+ |
public E element() { |
460 |
+ |
return getFirst(); |
461 |
+ |
} |
462 |
+ |
|
463 |
+ |
/** |
464 |
+ |
* Retrieves and removes the head (first element) of this list.. |
465 |
+ |
* @return the head of this queue, or <tt>null</tt> if this queue is empty. |
466 |
+ |
*/ |
467 |
+ |
public E poll() { |
468 |
+ |
if (size==0) |
469 |
+ |
return null; |
470 |
+ |
return removeFirst(); |
471 |
+ |
} |
472 |
+ |
|
473 |
+ |
/** |
474 |
+ |
* Retrieves and removes the head (first element) of this list.. |
475 |
+ |
* @return the head of this queue. |
476 |
+ |
* @throws NoSuchElementException if this queue is empty. |
477 |
+ |
*/ |
478 |
+ |
public E remove() { |
479 |
+ |
return removeFirst(); |
480 |
+ |
} |
481 |
+ |
|
482 |
+ |
/** |
483 |
+ |
* Adds the specified element as the tail (last element) of this list. |
484 |
+ |
* |
485 |
+ |
* @param o the element to add. |
486 |
+ |
* @return <tt>true</tt> (as per the general contract of |
487 |
+ |
* <tt>Queue.offer</tt>) |
488 |
+ |
*/ |
489 |
+ |
public boolean offer(E x) { |
490 |
+ |
return add(x); |
491 |
+ |
} |
492 |
+ |
|
493 |
|
/** |
494 |
|
* Returns a list-iterator of the elements in this list (in proper |
495 |
|
* sequence), starting at the specified position in the list. |
505 |
|
* time in the future. |
506 |
|
* |
507 |
|
* @param index index of first element to be returned from the |
508 |
< |
* list-iterator (by a call to <tt>next</tt>). |
508 |
> |
* list-iterator (by a call to <tt>next</tt>). |
509 |
|
* @return a ListIterator of the elements in this list (in proper |
510 |
< |
* sequence), starting at the specified position in the list. |
510 |
> |
* sequence), starting at the specified position in the list. |
511 |
|
* @throws IndexOutOfBoundsException if index is out of range |
512 |
< |
* (<tt>index < 0 || index > size()</tt>). |
513 |
< |
* @see java.util.List#listIterator(int) |
512 |
> |
* (<tt>index < 0 || index > size()</tt>). |
513 |
> |
* @see List#listIterator(int) |
514 |
|
*/ |
515 |
< |
public ListIterator listIterator(int index) { |
516 |
< |
return new ListItr(index); |
515 |
> |
public ListIterator<E> listIterator(int index) { |
516 |
> |
return new ListItr(index); |
517 |
|
} |
518 |
|
|
519 |
< |
private class ListItr implements ListIterator { |
520 |
< |
private Entry lastReturned = header; |
521 |
< |
private Entry next; |
522 |
< |
private int nextIndex; |
523 |
< |
private int expectedModCount = modCount; |
524 |
< |
|
525 |
< |
ListItr(int index) { |
526 |
< |
if (index < 0 || index > size) |
527 |
< |
throw new IndexOutOfBoundsException("Index: "+index+ |
528 |
< |
", Size: "+size); |
529 |
< |
if (index < (size >> 1)) { |
530 |
< |
next = header.next; |
531 |
< |
for (nextIndex=0; nextIndex<index; nextIndex++) |
532 |
< |
next = next.next; |
533 |
< |
} else { |
534 |
< |
next = header; |
535 |
< |
for (nextIndex=size; nextIndex>index; nextIndex--) |
536 |
< |
next = next.previous; |
537 |
< |
} |
538 |
< |
} |
539 |
< |
|
540 |
< |
public boolean hasNext() { |
541 |
< |
return nextIndex != size; |
542 |
< |
} |
519 |
> |
private class ListItr implements ListIterator<E> { |
520 |
> |
private Entry<E> lastReturned = header; |
521 |
> |
private Entry<E> next; |
522 |
> |
private int nextIndex; |
523 |
> |
private int expectedModCount = modCount; |
524 |
> |
|
525 |
> |
ListItr(int index) { |
526 |
> |
if (index < 0 || index > size) |
527 |
> |
throw new IndexOutOfBoundsException("Index: "+index+ |
528 |
> |
", Size: "+size); |
529 |
> |
if (index < (size >> 1)) { |
530 |
> |
next = header.next; |
531 |
> |
for (nextIndex=0; nextIndex<index; nextIndex++) |
532 |
> |
next = next.next; |
533 |
> |
} else { |
534 |
> |
next = header; |
535 |
> |
for (nextIndex=size; nextIndex>index; nextIndex--) |
536 |
> |
next = next.previous; |
537 |
> |
} |
538 |
> |
} |
539 |
> |
|
540 |
> |
public boolean hasNext() { |
541 |
> |
return nextIndex != size; |
542 |
> |
} |
543 |
> |
|
544 |
> |
public E next() { |
545 |
> |
checkForComodification(); |
546 |
> |
if (nextIndex == size) |
547 |
> |
throw new NoSuchElementException(); |
548 |
> |
|
549 |
> |
lastReturned = next; |
550 |
> |
next = next.next; |
551 |
> |
nextIndex++; |
552 |
> |
return lastReturned.element; |
553 |
> |
} |
554 |
> |
|
555 |
> |
public boolean hasPrevious() { |
556 |
> |
return nextIndex != 0; |
557 |
> |
} |
558 |
> |
|
559 |
> |
public E previous() { |
560 |
> |
if (nextIndex == 0) |
561 |
> |
throw new NoSuchElementException(); |
562 |
> |
|
563 |
> |
lastReturned = next = next.previous; |
564 |
> |
nextIndex--; |
565 |
> |
checkForComodification(); |
566 |
> |
return lastReturned.element; |
567 |
> |
} |
568 |
> |
|
569 |
> |
public int nextIndex() { |
570 |
> |
return nextIndex; |
571 |
> |
} |
572 |
> |
|
573 |
> |
public int previousIndex() { |
574 |
> |
return nextIndex-1; |
575 |
> |
} |
576 |
|
|
577 |
< |
public Object next() { |
489 |
< |
checkForComodification(); |
490 |
< |
if (nextIndex == size) |
491 |
< |
throw new NoSuchElementException(); |
492 |
< |
|
493 |
< |
lastReturned = next; |
494 |
< |
next = next.next; |
495 |
< |
nextIndex++; |
496 |
< |
return lastReturned.element; |
497 |
< |
} |
498 |
< |
|
499 |
< |
public boolean hasPrevious() { |
500 |
< |
return nextIndex != 0; |
501 |
< |
} |
502 |
< |
|
503 |
< |
public Object previous() { |
504 |
< |
if (nextIndex == 0) |
505 |
< |
throw new NoSuchElementException(); |
506 |
< |
|
507 |
< |
lastReturned = next = next.previous; |
508 |
< |
nextIndex--; |
509 |
< |
checkForComodification(); |
510 |
< |
return lastReturned.element; |
511 |
< |
} |
512 |
< |
|
513 |
< |
public int nextIndex() { |
514 |
< |
return nextIndex; |
515 |
< |
} |
516 |
< |
|
517 |
< |
public int previousIndex() { |
518 |
< |
return nextIndex-1; |
519 |
< |
} |
520 |
< |
|
521 |
< |
public void remove() { |
577 |
> |
public void remove() { |
578 |
|
checkForComodification(); |
579 |
|
try { |
580 |
|
LinkedList.this.remove(lastReturned); |
581 |
|
} catch (NoSuchElementException e) { |
582 |
|
throw new IllegalStateException(); |
583 |
|
} |
584 |
< |
if (next==lastReturned) |
584 |
> |
if (next==lastReturned) |
585 |
|
next = lastReturned.next; |
586 |
|
else |
587 |
< |
nextIndex--; |
588 |
< |
lastReturned = header; |
589 |
< |
expectedModCount++; |
590 |
< |
} |
591 |
< |
|
592 |
< |
public void set(Object o) { |
593 |
< |
if (lastReturned == header) |
594 |
< |
throw new IllegalStateException(); |
595 |
< |
checkForComodification(); |
596 |
< |
lastReturned.element = o; |
597 |
< |
} |
598 |
< |
|
599 |
< |
public void add(Object o) { |
600 |
< |
checkForComodification(); |
601 |
< |
lastReturned = header; |
602 |
< |
addBefore(o, next); |
603 |
< |
nextIndex++; |
604 |
< |
expectedModCount++; |
605 |
< |
} |
606 |
< |
|
607 |
< |
final void checkForComodification() { |
608 |
< |
if (modCount != expectedModCount) |
609 |
< |
throw new ConcurrentModificationException(); |
610 |
< |
} |
611 |
< |
} |
612 |
< |
|
613 |
< |
private static class Entry { |
614 |
< |
Object element; |
615 |
< |
Entry next; |
616 |
< |
Entry previous; |
617 |
< |
|
618 |
< |
Entry(Object element, Entry next, Entry previous) { |
619 |
< |
this.element = element; |
620 |
< |
this.next = next; |
621 |
< |
this.previous = previous; |
622 |
< |
} |
623 |
< |
} |
624 |
< |
|
625 |
< |
private Entry addBefore(Object o, Entry e) { |
626 |
< |
Entry newEntry = new Entry(o, e, e.previous); |
627 |
< |
newEntry.previous.next = newEntry; |
628 |
< |
newEntry.next.previous = newEntry; |
629 |
< |
size++; |
630 |
< |
modCount++; |
631 |
< |
return newEntry; |
632 |
< |
} |
633 |
< |
|
634 |
< |
private void remove(Entry e) { |
635 |
< |
if (e == header) |
636 |
< |
throw new NoSuchElementException(); |
637 |
< |
|
638 |
< |
e.previous.next = e.next; |
639 |
< |
e.next.previous = e.previous; |
640 |
< |
size--; |
641 |
< |
modCount++; |
587 |
> |
nextIndex--; |
588 |
> |
lastReturned = header; |
589 |
> |
expectedModCount++; |
590 |
> |
} |
591 |
> |
|
592 |
> |
public void set(E o) { |
593 |
> |
if (lastReturned == header) |
594 |
> |
throw new IllegalStateException(); |
595 |
> |
checkForComodification(); |
596 |
> |
lastReturned.element = o; |
597 |
> |
} |
598 |
> |
|
599 |
> |
public void add(E o) { |
600 |
> |
checkForComodification(); |
601 |
> |
lastReturned = header; |
602 |
> |
addBefore(o, next); |
603 |
> |
nextIndex++; |
604 |
> |
expectedModCount++; |
605 |
> |
} |
606 |
> |
|
607 |
> |
final void checkForComodification() { |
608 |
> |
if (modCount != expectedModCount) |
609 |
> |
throw new ConcurrentModificationException(); |
610 |
> |
} |
611 |
> |
} |
612 |
> |
|
613 |
> |
private static class Entry<E> { |
614 |
> |
E element; |
615 |
> |
Entry<E> next; |
616 |
> |
Entry<E> previous; |
617 |
> |
|
618 |
> |
Entry(E element, Entry<E> next, Entry<E> previous) { |
619 |
> |
this.element = element; |
620 |
> |
this.next = next; |
621 |
> |
this.previous = previous; |
622 |
> |
} |
623 |
> |
} |
624 |
> |
|
625 |
> |
private Entry<E> addBefore(E o, Entry<E> e) { |
626 |
> |
Entry<E> newEntry = new Entry<E>(o, e, e.previous); |
627 |
> |
newEntry.previous.next = newEntry; |
628 |
> |
newEntry.next.previous = newEntry; |
629 |
> |
size++; |
630 |
> |
modCount++; |
631 |
> |
return newEntry; |
632 |
> |
} |
633 |
> |
|
634 |
> |
private void remove(Entry<E> e) { |
635 |
> |
if (e == header) |
636 |
> |
throw new NoSuchElementException(); |
637 |
> |
|
638 |
> |
e.previous.next = e.next; |
639 |
> |
e.next.previous = e.previous; |
640 |
> |
size--; |
641 |
> |
modCount++; |
642 |
|
} |
643 |
|
|
644 |
|
/** |
648 |
|
* @return a shallow copy of this <tt>LinkedList</tt> instance. |
649 |
|
*/ |
650 |
|
public Object clone() { |
651 |
< |
LinkedList clone = null; |
652 |
< |
try { |
653 |
< |
clone = (LinkedList)super.clone(); |
654 |
< |
} catch (CloneNotSupportedException e) { |
655 |
< |
throw new InternalError(); |
656 |
< |
} |
651 |
> |
LinkedList<E> clone = null; |
652 |
> |
try { |
653 |
> |
clone = (LinkedList<E>) super.clone(); |
654 |
> |
} catch (CloneNotSupportedException e) { |
655 |
> |
throw new InternalError(); |
656 |
> |
} |
657 |
|
|
658 |
|
// Put clone into "virgin" state |
659 |
< |
clone.header = new Entry(null, null, null); |
659 |
> |
clone.header = new Entry<E>(null, null, null); |
660 |
|
clone.header.next = clone.header.previous = clone.header; |
661 |
|
clone.size = 0; |
662 |
|
clone.modCount = 0; |
663 |
|
|
664 |
|
// Initialize clone with our elements |
665 |
< |
for (Entry e = header.next; e != header; e = e.next) |
665 |
> |
for (Entry<E> e = header.next; e != header; e = e.next) |
666 |
|
clone.add(e.element); |
667 |
|
|
668 |
|
return clone; |
673 |
|
* in the correct order. |
674 |
|
* |
675 |
|
* @return an array containing all of the elements in this list |
676 |
< |
* in the correct order. |
676 |
> |
* in the correct order. |
677 |
|
*/ |
678 |
|
public Object[] toArray() { |
679 |
< |
Object[] result = new Object[size]; |
679 |
> |
Object[] result = new Object[size]; |
680 |
|
int i = 0; |
681 |
< |
for (Entry e = header.next; e != header; e = e.next) |
681 |
> |
for (Entry<E> e = header.next; e != header; e = e.next) |
682 |
|
result[i++] = e.element; |
683 |
< |
return result; |
683 |
> |
return result; |
684 |
|
} |
685 |
|
|
686 |
|
/** |
698 |
|
* does not contain any null elements. |
699 |
|
* |
700 |
|
* @param a the array into which the elements of the list are to |
701 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
702 |
< |
* same runtime type is allocated for this purpose. |
701 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
702 |
> |
* same runtime type is allocated for this purpose. |
703 |
|
* @return an array containing the elements of the list. |
704 |
|
* @throws ArrayStoreException if the runtime type of a is not a |
705 |
|
* supertype of the runtime type of every element in this list. |
706 |
|
* @throws NullPointerException if the specified array is null. |
707 |
|
*/ |
708 |
< |
public Object[] toArray(Object a[]) { |
708 |
> |
public <T> T[] toArray(T[] a) { |
709 |
|
if (a.length < size) |
710 |
< |
a = (Object[])java.lang.reflect.Array.newInstance( |
710 |
> |
a = (T[])java.lang.reflect.Array.newInstance( |
711 |
|
a.getClass().getComponentType(), size); |
712 |
|
int i = 0; |
713 |
< |
for (Entry e = header.next; e != header; e = e.next) |
714 |
< |
a[i++] = e.element; |
713 |
> |
Object[] result = a; |
714 |
> |
for (Entry<E> e = header.next; e != header; e = e.next) |
715 |
> |
result[i++] = e.element; |
716 |
|
|
717 |
|
if (a.length > size) |
718 |
|
a[size] = null; |
727 |
|
* is, serialize it). |
728 |
|
* |
729 |
|
* @serialData The size of the list (the number of elements it |
730 |
< |
* contains) is emitted (int), followed by all of its |
731 |
< |
* elements (each an Object) in the proper order. |
730 |
> |
* contains) is emitted (int), followed by all of its |
731 |
> |
* elements (each an Object) in the proper order. |
732 |
|
*/ |
733 |
< |
private synchronized void writeObject(java.io.ObjectOutputStream s) |
733 |
> |
private void writeObject(java.io.ObjectOutputStream s) |
734 |
|
throws java.io.IOException { |
735 |
< |
// Write out any hidden serialization magic |
736 |
< |
s.defaultWriteObject(); |
735 |
> |
// Write out any hidden serialization magic |
736 |
> |
s.defaultWriteObject(); |
737 |
|
|
738 |
|
// Write out size |
739 |
|
s.writeInt(size); |
740 |
|
|
741 |
< |
// Write out all elements in the proper order. |
741 |
> |
// Write out all elements in the proper order. |
742 |
|
for (Entry e = header.next; e != header; e = e.next) |
743 |
|
s.writeObject(e.element); |
744 |
|
} |
747 |
|
* Reconstitute this <tt>LinkedList</tt> instance from a stream (that is |
748 |
|
* deserialize it). |
749 |
|
*/ |
750 |
< |
private synchronized void readObject(java.io.ObjectInputStream s) |
750 |
> |
private void readObject(java.io.ObjectInputStream s) |
751 |
|
throws java.io.IOException, ClassNotFoundException { |
752 |
< |
// Read in any hidden serialization magic |
753 |
< |
s.defaultReadObject(); |
752 |
> |
// Read in any hidden serialization magic |
753 |
> |
s.defaultReadObject(); |
754 |
|
|
755 |
|
// Read in size |
756 |
|
int size = s.readInt(); |
757 |
|
|
758 |
|
// Initialize header |
759 |
< |
header = new Entry(null, null, null); |
759 |
> |
header = new Entry<E>(null, null, null); |
760 |
|
header.next = header.previous = header; |
761 |
|
|
762 |
< |
// Read in all elements in the proper order. |
763 |
< |
for (int i=0; i<size; i++) |
764 |
< |
add(s.readObject()); |
708 |
< |
} |
709 |
< |
|
710 |
< |
public Object peek() { |
711 |
< |
if (size==0) |
712 |
< |
return null; |
713 |
< |
return getFirst(); |
714 |
< |
} |
715 |
< |
|
716 |
< |
public Object element() { |
717 |
< |
return getFirst(); |
762 |
> |
// Read in all elements in the proper order. |
763 |
> |
for (int i=0; i<size; i++) |
764 |
> |
addBefore((E)s.readObject(), header); |
765 |
|
} |
719 |
– |
|
720 |
– |
public Object poll() { |
721 |
– |
if (size==0) |
722 |
– |
return null; |
723 |
– |
return removeFirst(); |
724 |
– |
} |
725 |
– |
|
726 |
– |
public Object remove() { |
727 |
– |
return removeFirst(); |
728 |
– |
} |
729 |
– |
|
730 |
– |
public boolean offer(Object x) { |
731 |
– |
return add(x); |
732 |
– |
} |
733 |
– |
|
766 |
|
} |