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 |
< |
} |
265 |
< |
return modified; |
257 |
> |
rangeCheckForAdd(index); |
258 |
> |
boolean modified = false; |
259 |
> |
for (E e : c) { |
260 |
> |
add(index++, e); |
261 |
> |
modified = true; |
262 |
> |
} |
263 |
> |
return modified; |
264 |
|
} |
265 |
|
|
266 |
|
|
285 |
|
* @return an iterator over the elements in this list in proper sequence |
286 |
|
*/ |
287 |
|
public Iterator<E> iterator() { |
288 |
< |
return new Itr(); |
288 |
> |
return new Itr(); |
289 |
|
} |
290 |
|
|
291 |
|
/** |
296 |
|
* @see #listIterator(int) |
297 |
|
*/ |
298 |
|
public ListIterator<E> listIterator() { |
299 |
< |
return listIterator(0); |
299 |
> |
return listIterator(0); |
300 |
|
} |
301 |
|
|
302 |
|
/** |
322 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
323 |
|
*/ |
324 |
|
public ListIterator<E> listIterator(final int index) { |
325 |
< |
rangeCheckForAdd(index); |
325 |
> |
rangeCheckForAdd(index); |
326 |
|
|
327 |
< |
return new ListItr(index); |
327 |
> |
return new ListItr(index); |
328 |
|
} |
329 |
|
|
330 |
|
private class Itr implements Iterator<E> { |
331 |
< |
/** |
332 |
< |
* Index of element to be returned by subsequent call to next. |
333 |
< |
*/ |
334 |
< |
int cursor = 0; |
335 |
< |
|
336 |
< |
/** |
337 |
< |
* Index of element returned by most recent call to next or |
338 |
< |
* previous. Reset to -1 if this element is deleted by a call |
339 |
< |
* to remove. |
340 |
< |
*/ |
341 |
< |
int lastRet = -1; |
342 |
< |
|
343 |
< |
/** |
344 |
< |
* The modCount value that the iterator believes that the backing |
345 |
< |
* List should have. If this expectation is violated, the iterator |
346 |
< |
* has detected concurrent modification. |
347 |
< |
*/ |
348 |
< |
int expectedModCount = modCount; |
331 |
> |
/** |
332 |
> |
* Index of element to be returned by subsequent call to next. |
333 |
> |
*/ |
334 |
> |
int cursor = 0; |
335 |
> |
|
336 |
> |
/** |
337 |
> |
* Index of element returned by most recent call to next or |
338 |
> |
* previous. Reset to -1 if this element is deleted by a call |
339 |
> |
* to remove. |
340 |
> |
*/ |
341 |
> |
int lastRet = -1; |
342 |
> |
|
343 |
> |
/** |
344 |
> |
* The modCount value that the iterator believes that the backing |
345 |
> |
* List should have. If this expectation is violated, the iterator |
346 |
> |
* has detected concurrent modification. |
347 |
> |
*/ |
348 |
> |
int expectedModCount = modCount; |
349 |
|
|
350 |
< |
public boolean hasNext() { |
350 |
> |
public boolean hasNext() { |
351 |
|
return cursor != size(); |
352 |
< |
} |
352 |
> |
} |
353 |
|
|
354 |
< |
public E next() { |
354 |
> |
public E next() { |
355 |
|
checkForComodification(); |
356 |
< |
try { |
357 |
< |
int i = cursor; |
358 |
< |
E next = get(i); |
359 |
< |
lastRet = i; |
360 |
< |
cursor = i + 1; |
361 |
< |
return next; |
362 |
< |
} catch (IndexOutOfBoundsException e) { |
363 |
< |
checkForComodification(); |
364 |
< |
throw new NoSuchElementException(); |
365 |
< |
} |
366 |
< |
} |
367 |
< |
|
368 |
< |
public void remove() { |
369 |
< |
if (lastRet < 0) |
370 |
< |
throw new IllegalStateException(); |
356 |
> |
try { |
357 |
> |
int i = cursor; |
358 |
> |
E next = get(i); |
359 |
> |
lastRet = i; |
360 |
> |
cursor = i + 1; |
361 |
> |
return next; |
362 |
> |
} catch (IndexOutOfBoundsException e) { |
363 |
> |
checkForComodification(); |
364 |
> |
throw new NoSuchElementException(); |
365 |
> |
} |
366 |
> |
} |
367 |
> |
|
368 |
> |
public void remove() { |
369 |
> |
if (lastRet < 0) |
370 |
> |
throw new IllegalStateException(); |
371 |
|
checkForComodification(); |
372 |
|
|
373 |
< |
try { |
374 |
< |
AbstractList.this.remove(lastRet); |
375 |
< |
if (lastRet < cursor) |
376 |
< |
cursor--; |
377 |
< |
lastRet = -1; |
378 |
< |
expectedModCount = modCount; |
379 |
< |
} catch (IndexOutOfBoundsException e) { |
380 |
< |
throw new ConcurrentModificationException(); |
381 |
< |
} |
382 |
< |
} |
383 |
< |
|
384 |
< |
final void checkForComodification() { |
385 |
< |
if (modCount != expectedModCount) |
386 |
< |
throw new ConcurrentModificationException(); |
387 |
< |
} |
373 |
> |
try { |
374 |
> |
AbstractList.this.remove(lastRet); |
375 |
> |
if (lastRet < cursor) |
376 |
> |
cursor--; |
377 |
> |
lastRet = -1; |
378 |
> |
expectedModCount = modCount; |
379 |
> |
} catch (IndexOutOfBoundsException e) { |
380 |
> |
throw new ConcurrentModificationException(); |
381 |
> |
} |
382 |
> |
} |
383 |
> |
|
384 |
> |
final void checkForComodification() { |
385 |
> |
if (modCount != expectedModCount) |
386 |
> |
throw new ConcurrentModificationException(); |
387 |
> |
} |
388 |
|
} |
389 |
|
|
390 |
|
private class ListItr extends Itr implements ListIterator<E> { |
391 |
< |
ListItr(int index) { |
392 |
< |
cursor = index; |
393 |
< |
} |
394 |
< |
|
395 |
< |
public boolean hasPrevious() { |
396 |
< |
return cursor != 0; |
397 |
< |
} |
391 |
> |
ListItr(int index) { |
392 |
> |
cursor = index; |
393 |
> |
} |
394 |
> |
|
395 |
> |
public boolean hasPrevious() { |
396 |
> |
return cursor != 0; |
397 |
> |
} |
398 |
|
|
399 |
|
public E previous() { |
400 |
|
checkForComodification(); |
409 |
|
} |
410 |
|
} |
411 |
|
|
412 |
< |
public int nextIndex() { |
413 |
< |
return cursor; |
414 |
< |
} |
415 |
< |
|
416 |
< |
public int previousIndex() { |
417 |
< |
return cursor-1; |
418 |
< |
} |
419 |
< |
|
420 |
< |
public void set(E e) { |
421 |
< |
if (lastRet < 0) |
422 |
< |
throw new IllegalStateException(); |
412 |
> |
public int nextIndex() { |
413 |
> |
return cursor; |
414 |
> |
} |
415 |
> |
|
416 |
> |
public int previousIndex() { |
417 |
> |
return cursor-1; |
418 |
> |
} |
419 |
> |
|
420 |
> |
public void set(E e) { |
421 |
> |
if (lastRet < 0) |
422 |
> |
throw new IllegalStateException(); |
423 |
|
checkForComodification(); |
424 |
|
|
425 |
< |
try { |
426 |
< |
AbstractList.this.set(lastRet, e); |
427 |
< |
expectedModCount = modCount; |
428 |
< |
} catch (IndexOutOfBoundsException ex) { |
429 |
< |
throw new ConcurrentModificationException(); |
430 |
< |
} |
431 |
< |
} |
425 |
> |
try { |
426 |
> |
AbstractList.this.set(lastRet, e); |
427 |
> |
expectedModCount = modCount; |
428 |
> |
} catch (IndexOutOfBoundsException ex) { |
429 |
> |
throw new ConcurrentModificationException(); |
430 |
> |
} |
431 |
> |
} |
432 |
|
|
433 |
< |
public void add(E e) { |
433 |
> |
public void add(E e) { |
434 |
|
checkForComodification(); |
435 |
|
|
436 |
< |
try { |
437 |
< |
int i = cursor; |
438 |
< |
AbstractList.this.add(i, e); |
439 |
< |
lastRet = -1; |
440 |
< |
cursor = i + 1; |
441 |
< |
expectedModCount = modCount; |
442 |
< |
} catch (IndexOutOfBoundsException ex) { |
443 |
< |
throw new ConcurrentModificationException(); |
444 |
< |
} |
445 |
< |
} |
436 |
> |
try { |
437 |
> |
int i = cursor; |
438 |
> |
AbstractList.this.add(i, e); |
439 |
> |
lastRet = -1; |
440 |
> |
cursor = i + 1; |
441 |
> |
expectedModCount = modCount; |
442 |
> |
} catch (IndexOutOfBoundsException ex) { |
443 |
> |
throw new ConcurrentModificationException(); |
444 |
> |
} |
445 |
> |
} |
446 |
|
} |
447 |
|
|
448 |
|
/** |
510 |
|
* @return {@code true} if the specified object is equal to this list |
511 |
|
*/ |
512 |
|
public boolean equals(Object o) { |
513 |
< |
if (o == this) |
514 |
< |
return true; |
515 |
< |
if (!(o instanceof List)) |
516 |
< |
return false; |
517 |
< |
|
518 |
< |
ListIterator<E> e1 = listIterator(); |
519 |
< |
ListIterator e2 = ((List) o).listIterator(); |
520 |
< |
while(e1.hasNext() && e2.hasNext()) { |
521 |
< |
E o1 = e1.next(); |
522 |
< |
Object o2 = e2.next(); |
523 |
< |
if (!(o1==null ? o2==null : o1.equals(o2))) |
524 |
< |
return false; |
525 |
< |
} |
526 |
< |
return !(e1.hasNext() || e2.hasNext()); |
513 |
> |
if (o == this) |
514 |
> |
return true; |
515 |
> |
if (!(o instanceof List)) |
516 |
> |
return false; |
517 |
> |
|
518 |
> |
ListIterator<E> e1 = listIterator(); |
519 |
> |
ListIterator e2 = ((List) o).listIterator(); |
520 |
> |
while(e1.hasNext() && e2.hasNext()) { |
521 |
> |
E o1 = e1.next(); |
522 |
> |
Object o2 = e2.next(); |
523 |
> |
if (!(o1==null ? o2==null : o1.equals(o2))) |
524 |
> |
return false; |
525 |
> |
} |
526 |
> |
return !(e1.hasNext() || e2.hasNext()); |
527 |
|
} |
528 |
|
|
529 |
|
/** |
536 |
|
* @return the hash code value for this list |
537 |
|
*/ |
538 |
|
public int hashCode() { |
539 |
< |
int hashCode = 1; |
540 |
< |
for (E e : this) |
541 |
< |
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode()); |
542 |
< |
return hashCode; |
539 |
> |
int hashCode = 1; |
540 |
> |
for (E e : this) |
541 |
> |
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode()); |
542 |
> |
return hashCode; |
543 |
|
} |
544 |
|
|
545 |
|
/** |
601 |
|
protected transient int modCount = 0; |
602 |
|
|
603 |
|
private void rangeCheckForAdd(int index) { |
604 |
< |
if (index < 0 || index > size()) |
605 |
< |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
604 |
> |
if (index < 0 || index > size()) |
605 |
> |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
606 |
|
} |
607 |
|
|
608 |
|
private String outOfBoundsMsg(int index) { |
609 |
< |
return "Index: "+index+", Size: "+size(); |
609 |
> |
return "Index: "+index+", Size: "+size(); |
610 |
|
} |
611 |
|
} |
612 |
|
|
647 |
|
} |
648 |
|
|
649 |
|
public void add(int index, E element) { |
650 |
< |
rangeCheckForAdd(index); |
650 |
> |
rangeCheckForAdd(index); |
651 |
|
checkForComodification(); |
652 |
|
l.add(index+offset, element); |
653 |
|
this.modCount = l.modCount; |
675 |
|
} |
676 |
|
|
677 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
678 |
< |
rangeCheckForAdd(index); |
678 |
> |
rangeCheckForAdd(index); |
679 |
|
int cSize = c.size(); |
680 |
|
if (cSize==0) |
681 |
|
return false; |
693 |
|
|
694 |
|
public ListIterator<E> listIterator(final int index) { |
695 |
|
checkForComodification(); |
696 |
< |
rangeCheckForAdd(index); |
696 |
> |
rangeCheckForAdd(index); |
697 |
|
|
698 |
|
return new ListIterator<E>() { |
699 |
|
private final ListIterator<E> i = l.listIterator(index+offset); |
756 |
|
} |
757 |
|
|
758 |
|
private void rangeCheckForAdd(int index) { |
759 |
< |
if (index < 0 || index > size) |
760 |
< |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
759 |
> |
if (index < 0 || index > size) |
760 |
> |
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); |
761 |
|
} |
762 |
|
|
763 |
|
private String outOfBoundsMsg(int index) { |
764 |
< |
return "Index: "+index+", Size: "+size; |
764 |
> |
return "Index: "+index+", Size: "+size; |
765 |
|
} |
766 |
|
|
767 |
|
private void checkForComodification() { |