65 |
|
* |
66 |
|
* @author Josh Bloch |
67 |
|
* @author Neal Gafter |
68 |
– |
* @version %I%, %G% |
68 |
|
* @since 1.2 |
69 |
|
*/ |
70 |
|
|
105 |
|
* prevents it from being added to this list |
106 |
|
*/ |
107 |
|
public boolean add(E e) { |
108 |
< |
add(size(), e); |
109 |
< |
return true; |
108 |
> |
add(size(), e); |
109 |
> |
return true; |
110 |
|
} |
111 |
|
|
112 |
|
/** |
129 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
130 |
|
*/ |
131 |
|
public E set(int index, E element) { |
132 |
< |
throw new UnsupportedOperationException(); |
132 |
> |
throw new UnsupportedOperationException(); |
133 |
|
} |
134 |
|
|
135 |
|
/** |
145 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
146 |
|
*/ |
147 |
|
public void add(int index, E element) { |
148 |
< |
throw new UnsupportedOperationException(); |
148 |
> |
throw new UnsupportedOperationException(); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
158 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
159 |
|
*/ |
160 |
|
public E remove(int index) { |
161 |
< |
throw new UnsupportedOperationException(); |
161 |
> |
throw new UnsupportedOperationException(); |
162 |
|
} |
163 |
|
|
164 |
|
|
175 |
|
* @throws NullPointerException {@inheritDoc} |
176 |
|
*/ |
177 |
|
public int indexOf(Object o) { |
178 |
< |
ListIterator<E> e = listIterator(); |
179 |
< |
if (o==null) { |
180 |
< |
while (e.hasNext()) |
181 |
< |
if (e.next()==null) |
182 |
< |
return e.previousIndex(); |
183 |
< |
} else { |
184 |
< |
while (e.hasNext()) |
185 |
< |
if (o.equals(e.next())) |
186 |
< |
return e.previousIndex(); |
187 |
< |
} |
188 |
< |
return -1; |
178 |
> |
ListIterator<E> e = listIterator(); |
179 |
> |
if (o==null) { |
180 |
> |
while (e.hasNext()) |
181 |
> |
if (e.next()==null) |
182 |
> |
return e.previousIndex(); |
183 |
> |
} else { |
184 |
> |
while (e.hasNext()) |
185 |
> |
if (o.equals(e.next())) |
186 |
> |
return e.previousIndex(); |
187 |
> |
} |
188 |
> |
return -1; |
189 |
|
} |
190 |
|
|
191 |
|
/** |
200 |
|
* @throws NullPointerException {@inheritDoc} |
201 |
|
*/ |
202 |
|
public int lastIndexOf(Object o) { |
203 |
< |
ListIterator<E> e = listIterator(size()); |
204 |
< |
if (o==null) { |
205 |
< |
while (e.hasPrevious()) |
206 |
< |
if (e.previous()==null) |
207 |
< |
return e.nextIndex(); |
208 |
< |
} else { |
209 |
< |
while (e.hasPrevious()) |
210 |
< |
if (o.equals(e.previous())) |
211 |
< |
return e.nextIndex(); |
212 |
< |
} |
213 |
< |
return -1; |
203 |
> |
ListIterator<E> e = listIterator(size()); |
204 |
> |
if (o==null) { |
205 |
> |
while (e.hasPrevious()) |
206 |
> |
if (e.previous()==null) |
207 |
> |
return e.nextIndex(); |
208 |
> |
} else { |
209 |
> |
while (e.hasPrevious()) |
210 |
> |
if (o.equals(e.previous())) |
211 |
> |
return e.nextIndex(); |
212 |
> |
} |
213 |
> |
return -1; |
214 |
|
} |
215 |
|
|
216 |
|
|
254 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
255 |
|
*/ |
256 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
257 |
< |
rangeCheckForAdd(index); |
258 |
< |
boolean modified = false; |
259 |
< |
Iterator<? extends E> e = c.iterator(); |
260 |
< |
while (e.hasNext()) { |
261 |
< |
add(index++, e.next()); |
262 |
< |
modified = true; |
263 |
< |
} |
264 |
< |
return modified; |
257 |
> |
rangeCheckForAdd(index); |
258 |
> |
boolean modified = false; |
259 |
> |
Iterator<? extends E> e = c.iterator(); |
260 |
> |
while (e.hasNext()) { |
261 |
> |
add(index++, e.next()); |
262 |
> |
modified = true; |
263 |
> |
} |
264 |
> |
return modified; |
265 |
|
} |
266 |
|
|
267 |
|
|
286 |
|
* @return an iterator over the elements in this list in proper sequence |
287 |
|
*/ |
288 |
|
public Iterator<E> iterator() { |
289 |
< |
return new Itr(); |
289 |
> |
return new Itr(); |
290 |
|
} |
291 |
|
|
292 |
|
/** |
297 |
|
* @see #listIterator(int) |
298 |
|
*/ |
299 |
|
public ListIterator<E> listIterator() { |
300 |
< |
return listIterator(0); |
300 |
> |
return listIterator(0); |
301 |
|
} |
302 |
|
|
303 |
|
/** |
323 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
324 |
|
*/ |
325 |
|
public ListIterator<E> listIterator(final int index) { |
326 |
< |
rangeCheckForAdd(index); |
326 |
> |
rangeCheckForAdd(index); |
327 |
|
|
328 |
< |
return new ListItr(index); |
328 |
> |
return new ListItr(index); |
329 |
|
} |
330 |
|
|
331 |
|
private class Itr implements Iterator<E> { |
332 |
< |
/** |
333 |
< |
* Index of element to be returned by subsequent call to next. |
334 |
< |
*/ |
335 |
< |
int cursor = 0; |
336 |
< |
|
337 |
< |
/** |
338 |
< |
* Index of element returned by most recent call to next or |
339 |
< |
* previous. Reset to -1 if this element is deleted by a call |
340 |
< |
* to remove. |
341 |
< |
*/ |
342 |
< |
int lastRet = -1; |
343 |
< |
|
344 |
< |
/** |
345 |
< |
* The modCount value that the iterator believes that the backing |
346 |
< |
* List should have. If this expectation is violated, the iterator |
347 |
< |
* has detected concurrent modification. |
348 |
< |
*/ |
349 |
< |
int expectedModCount = modCount; |
332 |
> |
/** |
333 |
> |
* Index of element to be returned by subsequent call to next. |
334 |
> |
*/ |
335 |
> |
int cursor = 0; |
336 |
> |
|
337 |
> |
/** |
338 |
> |
* Index of element returned by most recent call to next or |
339 |
> |
* previous. Reset to -1 if this element is deleted by a call |
340 |
> |
* to remove. |
341 |
> |
*/ |
342 |
> |
int lastRet = -1; |
343 |
> |
|
344 |
> |
/** |
345 |
> |
* The modCount value that the iterator believes that the backing |
346 |
> |
* List should have. If this expectation is violated, the iterator |
347 |
> |
* has detected concurrent modification. |
348 |
> |
*/ |
349 |
> |
int expectedModCount = modCount; |
350 |
|
|
351 |
< |
public boolean hasNext() { |
351 |
> |
public boolean hasNext() { |
352 |
|
return cursor != size(); |
353 |
< |
} |
353 |
> |
} |
354 |
|
|
355 |
< |
public E next() { |
355 |
> |
public E next() { |
356 |
|
checkForComodification(); |
357 |
< |
try { |
358 |
< |
int i = cursor; |
359 |
< |
E next = get(i); |
360 |
< |
lastRet = i; |
361 |
< |
cursor = i + 1; |
362 |
< |
return next; |
363 |
< |
} catch (IndexOutOfBoundsException e) { |
364 |
< |
checkForComodification(); |
365 |
< |
throw new NoSuchElementException(); |
366 |
< |
} |
367 |
< |
} |
368 |
< |
|
369 |
< |
public void remove() { |
370 |
< |
if (lastRet < 0) |
371 |
< |
throw new IllegalStateException(); |
357 |
> |
try { |
358 |
> |
int i = cursor; |
359 |
> |
E next = get(i); |
360 |
> |
lastRet = i; |
361 |
> |
cursor = i + 1; |
362 |
> |
return next; |
363 |
> |
} catch (IndexOutOfBoundsException e) { |
364 |
> |
checkForComodification(); |
365 |
> |
throw new NoSuchElementException(); |
366 |
> |
} |
367 |
> |
} |
368 |
> |
|
369 |
> |
public void remove() { |
370 |
> |
if (lastRet < 0) |
371 |
> |
throw new IllegalStateException(); |
372 |
|
checkForComodification(); |
373 |
|
|
374 |
< |
try { |
375 |
< |
AbstractList.this.remove(lastRet); |
376 |
< |
if (lastRet < cursor) |
377 |
< |
cursor--; |
378 |
< |
lastRet = -1; |
379 |
< |
expectedModCount = modCount; |
380 |
< |
} catch (IndexOutOfBoundsException e) { |
381 |
< |
throw new ConcurrentModificationException(); |
382 |
< |
} |
383 |
< |
} |
384 |
< |
|
385 |
< |
final void checkForComodification() { |
386 |
< |
if (modCount != expectedModCount) |
387 |
< |
throw new ConcurrentModificationException(); |
388 |
< |
} |
374 |
> |
try { |
375 |
> |
AbstractList.this.remove(lastRet); |
376 |
> |
if (lastRet < cursor) |
377 |
> |
cursor--; |
378 |
> |
lastRet = -1; |
379 |
> |
expectedModCount = modCount; |
380 |
> |
} catch (IndexOutOfBoundsException e) { |
381 |
> |
throw new ConcurrentModificationException(); |
382 |
> |
} |
383 |
> |
} |
384 |
> |
|
385 |
> |
final void checkForComodification() { |
386 |
> |
if (modCount != expectedModCount) |
387 |
> |
throw new ConcurrentModificationException(); |
388 |
> |
} |
389 |
|
} |
390 |
|
|
391 |
|
private class ListItr extends Itr implements ListIterator<E> { |
392 |
< |
ListItr(int index) { |
393 |
< |
cursor = index; |
394 |
< |
} |
395 |
< |
|
396 |
< |
public boolean hasPrevious() { |
397 |
< |
return cursor != 0; |
398 |
< |
} |
392 |
> |
ListItr(int index) { |
393 |
> |
cursor = index; |
394 |
> |
} |
395 |
> |
|
396 |
> |
public boolean hasPrevious() { |
397 |
> |
return cursor != 0; |
398 |
> |
} |
399 |
|
|
400 |
|
public E previous() { |
401 |
|
checkForComodification(); |
410 |
|
} |
411 |
|
} |
412 |
|
|
413 |
< |
public int nextIndex() { |
414 |
< |
return cursor; |
415 |
< |
} |
416 |
< |
|
417 |
< |
public int previousIndex() { |
418 |
< |
return cursor-1; |
419 |
< |
} |
420 |
< |
|
421 |
< |
public void set(E e) { |
422 |
< |
if (lastRet < 0) |
423 |
< |
throw new IllegalStateException(); |
413 |
> |
public int nextIndex() { |
414 |
> |
return cursor; |
415 |
> |
} |
416 |
> |
|
417 |
> |
public int previousIndex() { |
418 |
> |
return cursor-1; |
419 |
> |
} |
420 |
> |
|
421 |
> |
public void set(E e) { |
422 |
> |
if (lastRet < 0) |
423 |
> |
throw new IllegalStateException(); |
424 |
|
checkForComodification(); |
425 |
|
|
426 |
< |
try { |
427 |
< |
AbstractList.this.set(lastRet, e); |
428 |
< |
expectedModCount = modCount; |
429 |
< |
} catch (IndexOutOfBoundsException ex) { |
430 |
< |
throw new ConcurrentModificationException(); |
431 |
< |
} |
432 |
< |
} |
426 |
> |
try { |
427 |
> |
AbstractList.this.set(lastRet, e); |
428 |
> |
expectedModCount = modCount; |
429 |
> |
} catch (IndexOutOfBoundsException ex) { |
430 |
> |
throw new ConcurrentModificationException(); |
431 |
> |
} |
432 |
> |
} |
433 |
|
|
434 |
< |
public void add(E e) { |
434 |
> |
public void add(E e) { |
435 |
|
checkForComodification(); |
436 |
|
|
437 |
< |
try { |
438 |
< |
int i = cursor; |
439 |
< |
AbstractList.this.add(i, e); |
440 |
< |
lastRet = -1; |
441 |
< |
cursor = i + 1; |
442 |
< |
expectedModCount = modCount; |
443 |
< |
} catch (IndexOutOfBoundsException ex) { |
444 |
< |
throw new ConcurrentModificationException(); |
445 |
< |
} |
446 |
< |
} |
437 |
> |
try { |
438 |
> |
int i = cursor; |
439 |
> |
AbstractList.this.add(i, e); |
440 |
> |
lastRet = -1; |
441 |
> |
cursor = i + 1; |
442 |
> |
expectedModCount = modCount; |
443 |
> |
} catch (IndexOutOfBoundsException ex) { |
444 |
> |
throw new ConcurrentModificationException(); |
445 |
> |
} |
446 |
> |
} |
447 |
|
} |
448 |
|
|
449 |
|
/** |
511 |
|
* @return {@code true} if the specified object is equal to this list |
512 |
|
*/ |
513 |
|
public boolean equals(Object o) { |
514 |
< |
if (o == this) |
515 |
< |
return true; |
516 |
< |
if (!(o instanceof List)) |
517 |
< |
return false; |
518 |
< |
|
519 |
< |
ListIterator<E> e1 = listIterator(); |
520 |
< |
ListIterator e2 = ((List) o).listIterator(); |
521 |
< |
while(e1.hasNext() && e2.hasNext()) { |
522 |
< |
E o1 = e1.next(); |
523 |
< |
Object o2 = e2.next(); |
524 |
< |
if (!(o1==null ? o2==null : o1.equals(o2))) |
525 |
< |
return false; |
526 |
< |
} |
527 |
< |
return !(e1.hasNext() || e2.hasNext()); |
514 |
> |
if (o == this) |
515 |
> |
return true; |
516 |
> |
if (!(o instanceof List)) |
517 |
> |
return false; |
518 |
> |
|
519 |
> |
ListIterator<E> e1 = listIterator(); |
520 |
> |
ListIterator e2 = ((List) o).listIterator(); |
521 |
> |
while(e1.hasNext() && e2.hasNext()) { |
522 |
> |
E o1 = e1.next(); |
523 |
> |
Object o2 = e2.next(); |
524 |
> |
if (!(o1==null ? o2==null : o1.equals(o2))) |
525 |
> |
return false; |
526 |
> |
} |
527 |
> |
return !(e1.hasNext() || e2.hasNext()); |
528 |
|
} |
529 |
|
|
530 |
|
/** |
537 |
|
* @return the hash code value for this list |
538 |
|
*/ |
539 |
|
public int hashCode() { |
540 |
< |
int hashCode = 1; |
541 |
< |
for (E e : this) |
542 |
< |
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode()); |
543 |
< |
return hashCode; |
540 |
> |
int hashCode = 1; |
541 |
> |
for (E e : this) |
542 |
> |
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode()); |
543 |
> |
return hashCode; |
544 |
|
} |
545 |
|
|
546 |
|
/** |
602 |
|
protected transient int modCount = 0; |
603 |
|
|
604 |
|
private void rangeCheckForAdd(int index) { |
605 |
< |
if (index < 0 || index > size()) |
606 |
< |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
605 |
> |
if (index < 0 || index > size()) |
606 |
> |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
607 |
|
} |
608 |
|
|
609 |
|
private String outOfBoundsMsg(int index) { |
610 |
< |
return "Index: "+index+", Size: "+size(); |
610 |
> |
return "Index: "+index+", Size: "+size(); |
611 |
|
} |
612 |
|
} |
613 |
|
|
648 |
|
} |
649 |
|
|
650 |
|
public void add(int index, E element) { |
651 |
< |
rangeCheckForAdd(index); |
651 |
> |
rangeCheckForAdd(index); |
652 |
|
checkForComodification(); |
653 |
|
l.add(index+offset, element); |
654 |
|
this.modCount = l.modCount; |
676 |
|
} |
677 |
|
|
678 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
679 |
< |
rangeCheckForAdd(index); |
679 |
> |
rangeCheckForAdd(index); |
680 |
|
int cSize = c.size(); |
681 |
|
if (cSize==0) |
682 |
|
return false; |
694 |
|
|
695 |
|
public ListIterator<E> listIterator(final int index) { |
696 |
|
checkForComodification(); |
697 |
< |
rangeCheckForAdd(index); |
697 |
> |
rangeCheckForAdd(index); |
698 |
|
|
699 |
|
return new ListIterator<E>() { |
700 |
|
private final ListIterator<E> i = l.listIterator(index+offset); |
757 |
|
} |
758 |
|
|
759 |
|
private void rangeCheckForAdd(int index) { |
760 |
< |
if (index < 0 || index > size) |
761 |
< |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
760 |
> |
if (index < 0 || index > size) |
761 |
> |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
762 |
|
} |
763 |
|
|
764 |
|
private String outOfBoundsMsg(int index) { |
765 |
< |
return "Index: "+index+", Size: "+size; |
765 |
> |
return "Index: "+index+", Size: "+size; |
766 |
|
} |
767 |
|
|
768 |
|
private void checkForComodification() { |