1 |
|
/* |
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 |
|
|
8 |
< |
package java.util; |
8 |
> |
package java.util; |
9 |
|
|
10 |
|
/** |
11 |
|
* Linked list implementation of the <tt>List</tt> interface. Implements all |
14 |
|
* the <tt>LinkedList</tt> class provides uniformly named methods to |
15 |
|
* <tt>get</tt>, <tt>remove</tt> and <tt>insert</tt> an element at the |
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> |
17 |
> |
* used as a stack, queue, or double-ended queue ({@link Deque}).<p> |
18 |
|
* |
19 |
< |
* The class implements the <tt>Queue</tt> interface, providing |
19 |
> |
* The class implements the <tt>Deque</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> |
21 |
> |
* <tt>poll</tt>, along with other stack and deque operations. |
22 |
|
* |
23 |
|
* All of the operations perform as could be expected for a doubly-linked |
24 |
|
* list. Operations that index into the list will traverse the list from |
60 |
|
* |
61 |
|
* @author Josh Bloch |
62 |
|
* @version %I%, %G% |
63 |
< |
* @see List |
64 |
< |
* @see ArrayList |
65 |
< |
* @see Vector |
66 |
< |
* @see Collections#synchronizedList(List) |
63 |
> |
* @see List |
64 |
> |
* @see ArrayList |
65 |
> |
* @see Vector |
66 |
> |
* @see Collections#synchronizedList(List) |
67 |
|
* @since 1.2 |
68 |
|
* @param <E> the type of elements held in this collection |
69 |
|
*/ |
70 |
|
|
71 |
|
public class LinkedList<E> |
72 |
|
extends AbstractSequentialList<E> |
73 |
< |
implements List<E>, Queue<E>, Cloneable, java.io.Serializable |
73 |
> |
implements List<E>, Deque<E>, Cloneable, java.io.Serializable |
74 |
|
{ |
75 |
|
private transient Entry<E> header = new Entry<E>(null, null, null); |
76 |
|
private transient int size = 0; |
90 |
|
* @param c the collection whose elements are to be placed into this list. |
91 |
|
* @throws NullPointerException if the specified collection is null. |
92 |
|
*/ |
93 |
< |
public LinkedList(Collection<? extends E> c) { |
94 |
< |
this(); |
95 |
< |
addAll(c); |
96 |
< |
} |
93 |
> |
public LinkedList(Collection<? extends E> c) { |
94 |
> |
this(); |
95 |
> |
addAll(c); |
96 |
> |
} |
97 |
|
|
98 |
|
/** |
99 |
|
* Returns the first element in this list. |
102 |
|
* @throws NoSuchElementException if this list is empty. |
103 |
|
*/ |
104 |
|
public E getFirst() { |
105 |
< |
if (size==0) |
106 |
< |
throw new NoSuchElementException(); |
105 |
> |
if (size==0) |
106 |
> |
throw new NoSuchElementException(); |
107 |
|
|
108 |
< |
return header.next.element; |
108 |
> |
return header.next.element; |
109 |
|
} |
110 |
|
|
111 |
|
/** |
115 |
|
* @throws NoSuchElementException if this list is empty. |
116 |
|
*/ |
117 |
|
public E getLast() { |
118 |
< |
if (size==0) |
119 |
< |
throw new NoSuchElementException(); |
118 |
> |
if (size==0) |
119 |
> |
throw new NoSuchElementException(); |
120 |
|
|
121 |
< |
return header.previous.element; |
121 |
> |
return header.previous.element; |
122 |
|
} |
123 |
|
|
124 |
|
/** |
128 |
|
* @throws NoSuchElementException if this list is empty. |
129 |
|
*/ |
130 |
|
public E removeFirst() { |
131 |
< |
E first = header.next.element; |
135 |
< |
remove(header.next); |
136 |
< |
return first; |
131 |
> |
return remove(header.next); |
132 |
|
} |
133 |
|
|
134 |
|
/** |
138 |
|
* @throws NoSuchElementException if this list is empty. |
139 |
|
*/ |
140 |
|
public E removeLast() { |
141 |
< |
E last = header.previous.element; |
147 |
< |
remove(header.previous); |
148 |
< |
return last; |
141 |
> |
return remove(header.previous); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
147 |
|
* @param o the element to be inserted at the beginning of this list. |
148 |
|
*/ |
149 |
|
public void addFirst(E o) { |
150 |
< |
addBefore(o, header.next); |
150 |
> |
addBefore(o, header.next); |
151 |
|
} |
152 |
|
|
153 |
|
/** |
157 |
|
* @param o the element to be inserted at the end of this list. |
158 |
|
*/ |
159 |
|
public void addLast(E o) { |
160 |
< |
addBefore(o, header); |
160 |
> |
addBefore(o, header); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
179 |
|
* @return the number of elements in this list. |
180 |
|
*/ |
181 |
|
public int size() { |
182 |
< |
return size; |
182 |
> |
return size; |
183 |
|
} |
184 |
|
|
185 |
|
/** |
190 |
|
* <tt>Collection.add</tt>). |
191 |
|
*/ |
192 |
|
public boolean add(E o) { |
193 |
< |
addBefore(o, header); |
193 |
> |
addBefore(o, header); |
194 |
|
return true; |
195 |
|
} |
196 |
|
|
248 |
|
* specified collection's iterator. |
249 |
|
* |
250 |
|
* @param index index at which to insert first element |
251 |
< |
* from the specified collection. |
251 |
> |
* from the specified collection. |
252 |
|
* @param c elements to be inserted into this list. |
253 |
|
* @return <tt>true</tt> if this list changed as a result of the call. |
254 |
|
* @throws IndexOutOfBoundsException if the specified index is out of |
263 |
|
int numNew = a.length; |
264 |
|
if (numNew==0) |
265 |
|
return false; |
266 |
< |
modCount++; |
266 |
> |
modCount++; |
267 |
|
|
268 |
|
Entry<E> successor = (index==size ? header : entry(index)); |
269 |
|
Entry<E> predecessor = successor.previous; |
270 |
< |
for (int i=0; i<numNew; i++) { |
270 |
> |
for (int i=0; i<numNew; i++) { |
271 |
|
Entry<E> e = new Entry<E>((E)a[i], successor, predecessor); |
272 |
|
predecessor.next = e; |
273 |
|
predecessor = e; |
282 |
|
* Removes all of the elements from this list. |
283 |
|
*/ |
284 |
|
public void clear() { |
285 |
< |
modCount++; |
285 |
> |
Entry<E> e = header.next; |
286 |
> |
while (e != header) { |
287 |
> |
Entry<E> next = e.next; |
288 |
> |
e.next = e.previous = null; |
289 |
> |
e.element = null; |
290 |
> |
e = next; |
291 |
> |
} |
292 |
|
header.next = header.previous = header; |
293 |
|
size = 0; |
294 |
+ |
modCount++; |
295 |
|
} |
296 |
|
|
297 |
|
|
303 |
|
* @param index index of element to return. |
304 |
|
* @return the element at the specified position in this list. |
305 |
|
* |
306 |
< |
* @throws IndexOutOfBoundsException if the specified index is is out of |
306 |
> |
* @throws IndexOutOfBoundsException if the specified index is out of |
307 |
|
* range (<tt>index < 0 || index >= size()</tt>). |
308 |
|
*/ |
309 |
|
public E get(int index) { |
318 |
|
* @param element element to be stored at the specified position. |
319 |
|
* @return the element previously at the specified position. |
320 |
|
* @throws IndexOutOfBoundsException if the specified index is out of |
321 |
< |
* range (<tt>index < 0 || index >= size()</tt>). |
321 |
> |
* range (<tt>index < 0 || index >= size()</tt>). |
322 |
|
*/ |
323 |
|
public E set(int index, E element) { |
324 |
|
Entry<E> e = entry(index); |
336 |
|
* @param element element to be inserted. |
337 |
|
* |
338 |
|
* @throws IndexOutOfBoundsException if the specified index is out of |
339 |
< |
* range (<tt>index < 0 || index > size()</tt>). |
339 |
> |
* range (<tt>index < 0 || index > size()</tt>). |
340 |
|
*/ |
341 |
|
public void add(int index, E element) { |
342 |
|
addBefore(element, (index==size ? header : entry(index))); |
351 |
|
* @return the element previously at the specified position. |
352 |
|
* |
353 |
|
* @throws IndexOutOfBoundsException if the specified index is out of |
354 |
< |
* range (<tt>index < 0 || index >= size()</tt>). |
354 |
> |
* range (<tt>index < 0 || index >= size()</tt>). |
355 |
|
*/ |
356 |
|
public E remove(int index) { |
357 |
< |
Entry<E> e = entry(index); |
358 |
< |
remove(e); |
359 |
< |
return e.element; |
357 |
> |
return remove(entry(index)); |
358 |
|
} |
359 |
|
|
360 |
|
/** |
387 |
|
* |
388 |
|
* @param o element to search for. |
389 |
|
* @return the index in this list of the first occurrence of the |
390 |
< |
* specified element, or -1 if the list does not contain this |
391 |
< |
* element. |
390 |
> |
* specified element, or -1 if the list does not contain this |
391 |
> |
* element. |
392 |
|
*/ |
393 |
|
public int indexOf(Object o) { |
394 |
|
int index = 0; |
417 |
|
* |
418 |
|
* @param o element to search for. |
419 |
|
* @return the index in this list of the last occurrence of the |
420 |
< |
* specified element, or -1 if the list does not contain this |
421 |
< |
* element. |
420 |
> |
* specified element, or -1 if the list does not contain this |
421 |
> |
* element. |
422 |
|
*/ |
423 |
|
public int lastIndexOf(Object o) { |
424 |
|
int index = size; |
494 |
|
return add(o); |
495 |
|
} |
496 |
|
|
497 |
+ |
// Deque operations |
498 |
+ |
/** |
499 |
+ |
* Inserts the specified element to the front this deque. |
500 |
+ |
* |
501 |
+ |
* @param e the element to insert |
502 |
+ |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
503 |
+ |
* @since 1.6 |
504 |
+ |
*/ |
505 |
+ |
public boolean offerFirst(E e) { |
506 |
+ |
addFirst(e); |
507 |
+ |
return true; |
508 |
+ |
} |
509 |
+ |
|
510 |
+ |
/** |
511 |
+ |
* Inserts the specified element to the end this deque. |
512 |
+ |
* |
513 |
+ |
* @param e the element to insert |
514 |
+ |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
515 |
+ |
* @since 1.6 |
516 |
+ |
*/ |
517 |
+ |
public boolean offerLast(E e) { |
518 |
+ |
addLast(e); |
519 |
+ |
return true; |
520 |
+ |
} |
521 |
+ |
|
522 |
+ |
/** |
523 |
+ |
* Retrieves, but does not remove, the first element of this deque, |
524 |
+ |
* returning <tt>null</tt> if this deque is empty. |
525 |
+ |
* |
526 |
+ |
* @return the first element of this deque, or <tt>null</tt> if |
527 |
+ |
* this deque is empty |
528 |
+ |
* @since 1.6 |
529 |
+ |
*/ |
530 |
+ |
public E peekFirst() { |
531 |
+ |
if (size==0) |
532 |
+ |
return null; |
533 |
+ |
return getFirst(); |
534 |
+ |
} |
535 |
+ |
|
536 |
+ |
/** |
537 |
+ |
* Retrieves, but does not remove, the last element of this deque, |
538 |
+ |
* returning <tt>null</tt> if this deque is empty. |
539 |
+ |
* |
540 |
+ |
* @return the last element of this deque, or <tt>null</tt> if this deque |
541 |
+ |
* is empty |
542 |
+ |
* @since 1.6 |
543 |
+ |
*/ |
544 |
+ |
public E peekLast() { |
545 |
+ |
if (size==0) |
546 |
+ |
return null; |
547 |
+ |
return getLast(); |
548 |
+ |
} |
549 |
+ |
|
550 |
+ |
/** |
551 |
+ |
* Retrieves and removes the first element of this deque, or |
552 |
+ |
* <tt>null</tt> if this deque is empty. |
553 |
+ |
* |
554 |
+ |
* @return the first element of this deque, or <tt>null</tt> if |
555 |
+ |
* this deque is empty |
556 |
+ |
* @since 1.6 |
557 |
+ |
*/ |
558 |
+ |
public E pollFirst() { |
559 |
+ |
if (size==0) |
560 |
+ |
return null; |
561 |
+ |
return removeFirst(); |
562 |
+ |
} |
563 |
+ |
|
564 |
+ |
/** |
565 |
+ |
* Retrieves and removes the last element of this deque, or |
566 |
+ |
* <tt>null</tt> if this deque is empty. |
567 |
+ |
* |
568 |
+ |
* @return the last element of this deque, or <tt>null</tt> if |
569 |
+ |
* this deque is empty |
570 |
+ |
* @since 1.6 |
571 |
+ |
*/ |
572 |
+ |
public E pollLast() { |
573 |
+ |
if (size==0) |
574 |
+ |
return null; |
575 |
+ |
return removeLast(); |
576 |
+ |
} |
577 |
+ |
|
578 |
+ |
/** |
579 |
+ |
* Pushes an element onto the stack represented by this deque. In other |
580 |
+ |
* words, inserts the element to the front this deque. |
581 |
+ |
* |
582 |
+ |
* <p>This method is equivalent to {@link #addFirst}. |
583 |
+ |
* |
584 |
+ |
* @param e the element to push |
585 |
+ |
* @since 1.6 |
586 |
+ |
*/ |
587 |
+ |
public void push(E e) { |
588 |
+ |
addFirst(e); |
589 |
+ |
} |
590 |
+ |
|
591 |
+ |
/** |
592 |
+ |
* Pops an element from the stack represented by this deque. In other |
593 |
+ |
* words, removes and returns the the first element of this deque. |
594 |
+ |
* |
595 |
+ |
* <p>This method is equivalent to {@link #removeFirst()}. |
596 |
+ |
* |
597 |
+ |
* @return the element at the front of this deque (which is the top |
598 |
+ |
* of the stack represented by this deque) |
599 |
+ |
* @throws NoSuchElementException if this deque is empty |
600 |
+ |
* @since 1.6 |
601 |
+ |
*/ |
602 |
+ |
public E pop() { |
603 |
+ |
return removeFirst(); |
604 |
+ |
} |
605 |
+ |
|
606 |
+ |
/** |
607 |
+ |
* Removes the first occurrence of the specified element in this |
608 |
+ |
* deque (when traversing the deque from head to tail). If the deque |
609 |
+ |
* does not contain the element, it is unchanged. |
610 |
+ |
* |
611 |
+ |
* @param e element to be removed from this deque, if present |
612 |
+ |
* @return <tt>true</tt> if the deque contained the specified element |
613 |
+ |
* @since 1.6 |
614 |
+ |
*/ |
615 |
+ |
public boolean removeFirstOccurrence(Object e) { |
616 |
+ |
return remove(e); |
617 |
+ |
} |
618 |
+ |
|
619 |
+ |
/** |
620 |
+ |
* Removes the last occurrence of the specified element in this |
621 |
+ |
* deque (when traversing the deque from head to tail). If the deque |
622 |
+ |
* does not contain the element, it is unchanged. |
623 |
+ |
* |
624 |
+ |
* @param o element to be removed from this deque, if present |
625 |
+ |
* @return <tt>true</tt> if the deque contained the specified element |
626 |
+ |
* @since 1.6 |
627 |
+ |
*/ |
628 |
+ |
public boolean removeLastOccurrence(Object o) { |
629 |
+ |
if (o==null) { |
630 |
+ |
for (Entry e = header.previous; e != header; e = e.previous) { |
631 |
+ |
if (e.element==null) { |
632 |
+ |
remove(e); |
633 |
+ |
return true; |
634 |
+ |
} |
635 |
+ |
} |
636 |
+ |
} else { |
637 |
+ |
for (Entry e = header.previous; e != header; e = e.previous) { |
638 |
+ |
if (o.equals(e.element)) { |
639 |
+ |
remove(e); |
640 |
+ |
return true; |
641 |
+ |
} |
642 |
+ |
} |
643 |
+ |
} |
644 |
+ |
return false; |
645 |
+ |
} |
646 |
+ |
|
647 |
|
/** |
648 |
|
* Returns a list-iterator of the elements in this list (in proper |
649 |
|
* sequence), starting at the specified position in the list. |
659 |
|
* time in the future. |
660 |
|
* |
661 |
|
* @param index index of first element to be returned from the |
662 |
< |
* list-iterator (by a call to <tt>next</tt>). |
662 |
> |
* list-iterator (by a call to <tt>next</tt>). |
663 |
|
* @return a ListIterator of the elements in this list (in proper |
664 |
< |
* sequence), starting at the specified position in the list. |
664 |
> |
* sequence), starting at the specified position in the list. |
665 |
|
* @throws IndexOutOfBoundsException if index is out of range |
666 |
< |
* (<tt>index < 0 || index > size()</tt>). |
666 |
> |
* (<tt>index < 0 || index > size()</tt>). |
667 |
|
* @see List#listIterator(int) |
668 |
|
*/ |
669 |
|
public ListIterator<E> listIterator(int index) { |
670 |
< |
return new ListItr(index); |
670 |
> |
return new ListItr(index); |
671 |
|
} |
672 |
|
|
673 |
|
private class ListItr implements ListIterator<E> { |
674 |
< |
private Entry<E> lastReturned = header; |
675 |
< |
private Entry<E> next; |
676 |
< |
private int nextIndex; |
677 |
< |
private int expectedModCount = modCount; |
678 |
< |
|
679 |
< |
ListItr(int index) { |
680 |
< |
if (index < 0 || index > size) |
681 |
< |
throw new IndexOutOfBoundsException("Index: "+index+ |
682 |
< |
", Size: "+size); |
683 |
< |
if (index < (size >> 1)) { |
684 |
< |
next = header.next; |
685 |
< |
for (nextIndex=0; nextIndex<index; nextIndex++) |
686 |
< |
next = next.next; |
687 |
< |
} else { |
688 |
< |
next = header; |
689 |
< |
for (nextIndex=size; nextIndex>index; nextIndex--) |
690 |
< |
next = next.previous; |
691 |
< |
} |
692 |
< |
} |
674 |
> |
private Entry<E> lastReturned = header; |
675 |
> |
private Entry<E> next; |
676 |
> |
private int nextIndex; |
677 |
> |
private int expectedModCount = modCount; |
678 |
> |
|
679 |
> |
ListItr(int index) { |
680 |
> |
if (index < 0 || index > size) |
681 |
> |
throw new IndexOutOfBoundsException("Index: "+index+ |
682 |
> |
", Size: "+size); |
683 |
> |
if (index < (size >> 1)) { |
684 |
> |
next = header.next; |
685 |
> |
for (nextIndex=0; nextIndex<index; nextIndex++) |
686 |
> |
next = next.next; |
687 |
> |
} else { |
688 |
> |
next = header; |
689 |
> |
for (nextIndex=size; nextIndex>index; nextIndex--) |
690 |
> |
next = next.previous; |
691 |
> |
} |
692 |
> |
} |
693 |
> |
|
694 |
> |
public boolean hasNext() { |
695 |
> |
return nextIndex != size; |
696 |
> |
} |
697 |
> |
|
698 |
> |
public E next() { |
699 |
> |
checkForComodification(); |
700 |
> |
if (nextIndex == size) |
701 |
> |
throw new NoSuchElementException(); |
702 |
> |
|
703 |
> |
lastReturned = next; |
704 |
> |
next = next.next; |
705 |
> |
nextIndex++; |
706 |
> |
return lastReturned.element; |
707 |
> |
} |
708 |
> |
|
709 |
> |
public boolean hasPrevious() { |
710 |
> |
return nextIndex != 0; |
711 |
> |
} |
712 |
> |
|
713 |
> |
public E previous() { |
714 |
> |
if (nextIndex == 0) |
715 |
> |
throw new NoSuchElementException(); |
716 |
> |
|
717 |
> |
lastReturned = next = next.previous; |
718 |
> |
nextIndex--; |
719 |
> |
checkForComodification(); |
720 |
> |
return lastReturned.element; |
721 |
> |
} |
722 |
> |
|
723 |
> |
public int nextIndex() { |
724 |
> |
return nextIndex; |
725 |
> |
} |
726 |
> |
|
727 |
> |
public int previousIndex() { |
728 |
> |
return nextIndex-1; |
729 |
> |
} |
730 |
|
|
731 |
< |
public boolean hasNext() { |
547 |
< |
return nextIndex != size; |
548 |
< |
} |
549 |
< |
|
550 |
< |
public E next() { |
551 |
< |
checkForComodification(); |
552 |
< |
if (nextIndex == size) |
553 |
< |
throw new NoSuchElementException(); |
554 |
< |
|
555 |
< |
lastReturned = next; |
556 |
< |
next = next.next; |
557 |
< |
nextIndex++; |
558 |
< |
return lastReturned.element; |
559 |
< |
} |
560 |
< |
|
561 |
< |
public boolean hasPrevious() { |
562 |
< |
return nextIndex != 0; |
563 |
< |
} |
564 |
< |
|
565 |
< |
public E previous() { |
566 |
< |
if (nextIndex == 0) |
567 |
< |
throw new NoSuchElementException(); |
568 |
< |
|
569 |
< |
lastReturned = next = next.previous; |
570 |
< |
nextIndex--; |
571 |
< |
checkForComodification(); |
572 |
< |
return lastReturned.element; |
573 |
< |
} |
574 |
< |
|
575 |
< |
public int nextIndex() { |
576 |
< |
return nextIndex; |
577 |
< |
} |
578 |
< |
|
579 |
< |
public int previousIndex() { |
580 |
< |
return nextIndex-1; |
581 |
< |
} |
582 |
< |
|
583 |
< |
public void remove() { |
731 |
> |
public void remove() { |
732 |
|
checkForComodification(); |
733 |
+ |
Entry<E> lastNext = lastReturned.next; |
734 |
|
try { |
735 |
|
LinkedList.this.remove(lastReturned); |
736 |
|
} catch (NoSuchElementException e) { |
737 |
|
throw new IllegalStateException(); |
738 |
|
} |
739 |
< |
if (next==lastReturned) |
740 |
< |
next = lastReturned.next; |
739 |
> |
if (next==lastReturned) |
740 |
> |
next = lastNext; |
741 |
|
else |
742 |
< |
nextIndex--; |
743 |
< |
lastReturned = header; |
744 |
< |
expectedModCount++; |
745 |
< |
} |
746 |
< |
|
747 |
< |
public void set(E o) { |
748 |
< |
if (lastReturned == header) |
749 |
< |
throw new IllegalStateException(); |
750 |
< |
checkForComodification(); |
751 |
< |
lastReturned.element = o; |
752 |
< |
} |
753 |
< |
|
754 |
< |
public void add(E o) { |
755 |
< |
checkForComodification(); |
756 |
< |
lastReturned = header; |
757 |
< |
addBefore(o, next); |
758 |
< |
nextIndex++; |
759 |
< |
expectedModCount++; |
760 |
< |
} |
761 |
< |
|
762 |
< |
final void checkForComodification() { |
763 |
< |
if (modCount != expectedModCount) |
764 |
< |
throw new ConcurrentModificationException(); |
765 |
< |
} |
742 |
> |
nextIndex--; |
743 |
> |
lastReturned = header; |
744 |
> |
expectedModCount++; |
745 |
> |
} |
746 |
> |
|
747 |
> |
public void set(E o) { |
748 |
> |
if (lastReturned == header) |
749 |
> |
throw new IllegalStateException(); |
750 |
> |
checkForComodification(); |
751 |
> |
lastReturned.element = o; |
752 |
> |
} |
753 |
> |
|
754 |
> |
public void add(E o) { |
755 |
> |
checkForComodification(); |
756 |
> |
lastReturned = header; |
757 |
> |
addBefore(o, next); |
758 |
> |
nextIndex++; |
759 |
> |
expectedModCount++; |
760 |
> |
} |
761 |
> |
|
762 |
> |
final void checkForComodification() { |
763 |
> |
if (modCount != expectedModCount) |
764 |
> |
throw new ConcurrentModificationException(); |
765 |
> |
} |
766 |
|
} |
767 |
|
|
768 |
|
private static class Entry<E> { |
769 |
< |
E element; |
770 |
< |
Entry<E> next; |
771 |
< |
Entry<E> previous; |
772 |
< |
|
773 |
< |
Entry(E element, Entry<E> next, Entry<E> previous) { |
774 |
< |
this.element = element; |
775 |
< |
this.next = next; |
776 |
< |
this.previous = previous; |
777 |
< |
} |
769 |
> |
E element; |
770 |
> |
Entry<E> next; |
771 |
> |
Entry<E> previous; |
772 |
> |
|
773 |
> |
Entry(E element, Entry<E> next, Entry<E> previous) { |
774 |
> |
this.element = element; |
775 |
> |
this.next = next; |
776 |
> |
this.previous = previous; |
777 |
> |
} |
778 |
|
} |
779 |
|
|
780 |
|
private Entry<E> addBefore(E o, Entry<E> e) { |
781 |
< |
Entry<E> newEntry = new Entry<E>(o, e, e.previous); |
782 |
< |
newEntry.previous.next = newEntry; |
783 |
< |
newEntry.next.previous = newEntry; |
784 |
< |
size++; |
785 |
< |
modCount++; |
786 |
< |
return newEntry; |
787 |
< |
} |
788 |
< |
|
789 |
< |
private void remove(Entry<E> e) { |
790 |
< |
if (e == header) |
791 |
< |
throw new NoSuchElementException(); |
792 |
< |
|
793 |
< |
e.previous.next = e.next; |
794 |
< |
e.next.previous = e.previous; |
795 |
< |
size--; |
796 |
< |
modCount++; |
781 |
> |
Entry<E> newEntry = new Entry<E>(o, e, e.previous); |
782 |
> |
newEntry.previous.next = newEntry; |
783 |
> |
newEntry.next.previous = newEntry; |
784 |
> |
size++; |
785 |
> |
modCount++; |
786 |
> |
return newEntry; |
787 |
> |
} |
788 |
> |
|
789 |
> |
private E remove(Entry<E> e) { |
790 |
> |
if (e == header) |
791 |
> |
throw new NoSuchElementException(); |
792 |
> |
|
793 |
> |
E result = e.element; |
794 |
> |
e.previous.next = e.next; |
795 |
> |
e.next.previous = e.previous; |
796 |
> |
e.next = e.previous = null; |
797 |
> |
e.element = null; |
798 |
> |
size--; |
799 |
> |
modCount++; |
800 |
> |
return result; |
801 |
|
} |
802 |
|
|
803 |
|
/** |
808 |
|
*/ |
809 |
|
public Object clone() { |
810 |
|
LinkedList<E> clone = null; |
811 |
< |
try { |
812 |
< |
clone = (LinkedList<E>) super.clone(); |
813 |
< |
} catch (CloneNotSupportedException e) { |
814 |
< |
throw new InternalError(); |
815 |
< |
} |
811 |
> |
try { |
812 |
> |
clone = (LinkedList<E>) super.clone(); |
813 |
> |
} catch (CloneNotSupportedException e) { |
814 |
> |
throw new InternalError(); |
815 |
> |
} |
816 |
|
|
817 |
|
// Put clone into "virgin" state |
818 |
|
clone.header = new Entry<E>(null, null, null); |
832 |
|
* in the correct order. |
833 |
|
* |
834 |
|
* @return an array containing all of the elements in this list |
835 |
< |
* in the correct order. |
835 |
> |
* in the correct order. |
836 |
|
*/ |
837 |
|
public Object[] toArray() { |
838 |
< |
Object[] result = new Object[size]; |
838 |
> |
Object[] result = new Object[size]; |
839 |
|
int i = 0; |
840 |
|
for (Entry<E> e = header.next; e != header; e = e.next) |
841 |
|
result[i++] = e.element; |
842 |
< |
return result; |
842 |
> |
return result; |
843 |
|
} |
844 |
|
|
845 |
|
/** |
857 |
|
* does not contain any null elements. |
858 |
|
* |
859 |
|
* @param a the array into which the elements of the list are to |
860 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
861 |
< |
* same runtime type is allocated for this purpose. |
860 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
861 |
> |
* same runtime type is allocated for this purpose. |
862 |
|
* @return an array containing the elements of the list. |
863 |
|
* @throws ArrayStoreException if the runtime type of a is not a |
864 |
|
* supertype of the runtime type of every element in this list. |
869 |
|
a = (T[])java.lang.reflect.Array.newInstance( |
870 |
|
a.getClass().getComponentType(), size); |
871 |
|
int i = 0; |
872 |
< |
Object[] result = a; |
872 |
> |
Object[] result = a; |
873 |
|
for (Entry<E> e = header.next; e != header; e = e.next) |
874 |
|
result[i++] = e.element; |
875 |
|
|
886 |
|
* is, serialize it). |
887 |
|
* |
888 |
|
* @serialData The size of the list (the number of elements it |
889 |
< |
* contains) is emitted (int), followed by all of its |
889 |
> |
* contains) is emitted (int), followed by all of its |
890 |
|
* elements (each an Object) in the proper order. |
891 |
|
*/ |
892 |
|
private void writeObject(java.io.ObjectOutputStream s) |
893 |
|
throws java.io.IOException { |
894 |
< |
// Write out any hidden serialization magic |
895 |
< |
s.defaultWriteObject(); |
894 |
> |
// Write out any hidden serialization magic |
895 |
> |
s.defaultWriteObject(); |
896 |
|
|
897 |
|
// Write out size |
898 |
|
s.writeInt(size); |
899 |
|
|
900 |
< |
// Write out all elements in the proper order. |
900 |
> |
// Write out all elements in the proper order. |
901 |
|
for (Entry e = header.next; e != header; e = e.next) |
902 |
|
s.writeObject(e.element); |
903 |
|
} |
908 |
|
*/ |
909 |
|
private void readObject(java.io.ObjectInputStream s) |
910 |
|
throws java.io.IOException, ClassNotFoundException { |
911 |
< |
// Read in any hidden serialization magic |
912 |
< |
s.defaultReadObject(); |
911 |
> |
// Read in any hidden serialization magic |
912 |
> |
s.defaultReadObject(); |
913 |
|
|
914 |
|
// Read in size |
915 |
|
int size = s.readInt(); |
918 |
|
header = new Entry<E>(null, null, null); |
919 |
|
header.next = header.previous = header; |
920 |
|
|
921 |
< |
// Read in all elements in the proper order. |
922 |
< |
for (int i=0; i<size; i++) |
921 |
> |
// Read in all elements in the proper order. |
922 |
> |
for (int i=0; i<size; i++) |
923 |
|
addBefore((E)s.readObject(), header); |
924 |
|
} |
925 |
|
} |