ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractList.java
Revision: 1.14
Committed: Mon Jun 26 01:08:01 2006 UTC (17 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.13: +8 -9 lines
Log Message:
doc sync with mustang

File Contents

# User Rev Content
1 dl 1.1 /*
2 jsr166 1.10 * %W% %E%
3 dl 1.1 *
4 jsr166 1.5 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5 dl 1.1 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6     */
7    
8     package java.util;
9    
10     /**
11 jsr166 1.13 * This class provides a skeletal implementation of the {@link List}
12 dl 1.1 * interface to minimize the effort required to implement this interface
13     * backed by a "random access" data store (such as an array). For sequential
14 jsr166 1.13 * access data (such as a linked list), {@link AbstractSequentialList} should
15 jsr166 1.12 * be used in preference to this class.
16 dl 1.1 *
17 jsr166 1.13 * <p>To implement an unmodifiable list, the programmer needs only to extend
18     * this class and provide implementations for the {@link #get(int)} and
19     * {@link List#size() size()} methods.
20 dl 1.1 *
21 jsr166 1.13 * <p>To implement a modifiable list, the programmer must additionally
22     * override the {@link #set(int, Object) set(int, E)} method (which otherwise
23     * throws an {@code UnsupportedOperationException}). If the list is
24     * variable-size the programmer must additionally override the
25     * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
26 dl 1.1 *
27 jsr166 1.12 * <p>The programmer should generally provide a void (no argument) and collection
28 jsr166 1.13 * constructor, as per the recommendation in the {@link Collection} interface
29 jsr166 1.12 * specification.
30 dl 1.1 *
31 jsr166 1.12 * <p>Unlike the other abstract collection implementations, the programmer does
32 dl 1.1 * <i>not</i> have to provide an iterator implementation; the iterator and
33     * list iterator are implemented by this class, on top of the "random access"
34 jsr166 1.13 * methods:
35     * {@link #get(int)},
36     * {@link #set(int, Object) set(int, E)},
37     * {@link #add(int, Object) add(int, E)} and
38     * {@link #remove(int)}.
39 dl 1.1 *
40 jsr166 1.13 * <p>The documentation for each non-abstract method in this class describes its
41 dl 1.1 * implementation in detail. Each of these methods may be overridden if the
42 jsr166 1.12 * collection being implemented admits a more efficient implementation.
43 dl 1.1 *
44 jsr166 1.12 * <p>This class is a member of the
45 jsr166 1.11 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
46 dl 1.1 * Java Collections Framework</a>.
47     *
48     * @author Josh Bloch
49     * @author Neal Gafter
50 jsr166 1.7 * @version %I%, %G%
51 dl 1.1 * @since 1.2
52     */
53    
54     public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
55     /**
56     * Sole constructor. (For invocation by subclass constructors, typically
57     * implicit.)
58     */
59     protected AbstractList() {
60     }
61    
62     /**
63     * Appends the specified element to the end of this list (optional
64     * operation).
65     *
66     * <p>Lists that support this operation may place limitations on what
67     * elements may be added to this list. In particular, some
68     * lists will refuse to add null elements, and others will impose
69     * restrictions on the type of elements that may be added. List
70     * classes should clearly specify in their documentation any restrictions
71     * on what elements may be added.
72     *
73 jsr166 1.12 * <p>This implementation calls {@code add(size(), e)}.
74 dl 1.1 *
75     * <p>Note that this implementation throws an
76 jsr166 1.13 * {@code UnsupportedOperationException} unless
77     * {@link #add(int, Object) add(int, E)} is overridden.
78 dl 1.1 *
79     * @param e element to be appended to this list
80 jsr166 1.12 * @return {@code true} (as specified by {@link Collection#add})
81     * @throws UnsupportedOperationException if the {@code add} operation
82 dl 1.1 * is not supported by this list
83     * @throws ClassCastException if the class of the specified element
84     * prevents it from being added to this list
85     * @throws NullPointerException if the specified element is null and this
86     * list does not permit null elements
87     * @throws IllegalArgumentException if some property of this element
88     * prevents it from being added to this list
89     */
90     public boolean add(E e) {
91     add(size(), e);
92     return true;
93     }
94    
95     /**
96     * {@inheritDoc}
97     *
98     * @throws IndexOutOfBoundsException {@inheritDoc}
99     */
100     abstract public E get(int index);
101    
102     /**
103     * {@inheritDoc}
104     *
105     * <p>This implementation always throws an
106 jsr166 1.12 * {@code UnsupportedOperationException}.
107 dl 1.1 *
108     * @throws UnsupportedOperationException {@inheritDoc}
109     * @throws ClassCastException {@inheritDoc}
110     * @throws NullPointerException {@inheritDoc}
111     * @throws IllegalArgumentException {@inheritDoc}
112     * @throws IndexOutOfBoundsException {@inheritDoc}
113     */
114     public E set(int index, E element) {
115     throw new UnsupportedOperationException();
116     }
117    
118     /**
119     * {@inheritDoc}
120     *
121     * <p>This implementation always throws an
122 jsr166 1.12 * {@code UnsupportedOperationException}.
123 dl 1.1 *
124     * @throws UnsupportedOperationException {@inheritDoc}
125     * @throws ClassCastException {@inheritDoc}
126     * @throws NullPointerException {@inheritDoc}
127     * @throws IllegalArgumentException {@inheritDoc}
128     * @throws IndexOutOfBoundsException {@inheritDoc}
129     */
130     public void add(int index, E element) {
131     throw new UnsupportedOperationException();
132     }
133    
134     /**
135     * {@inheritDoc}
136     *
137     * <p>This implementation always throws an
138 jsr166 1.12 * {@code UnsupportedOperationException}.
139 dl 1.1 *
140     * @throws UnsupportedOperationException {@inheritDoc}
141     * @throws IndexOutOfBoundsException {@inheritDoc}
142     */
143     public E remove(int index) {
144     throw new UnsupportedOperationException();
145     }
146    
147    
148     // Search Operations
149    
150     /**
151     * {@inheritDoc}
152     *
153     * <p>This implementation first gets a list iterator (with
154 jsr166 1.12 * {@code listIterator()}). Then, it iterates over the list until the
155 dl 1.1 * specified element is found or the end of the list is reached.
156     *
157     * @throws ClassCastException {@inheritDoc}
158     * @throws NullPointerException {@inheritDoc}
159     */
160     public int indexOf(Object o) {
161     ListIterator<E> e = listIterator();
162     if (o==null) {
163     while (e.hasNext())
164     if (e.next()==null)
165     return e.previousIndex();
166     } else {
167     while (e.hasNext())
168     if (o.equals(e.next()))
169     return e.previousIndex();
170     }
171     return -1;
172     }
173    
174     /**
175     * {@inheritDoc}
176     *
177     * <p>This implementation first gets a list iterator that points to the end
178 jsr166 1.12 * of the list (with {@code listIterator(size())}). Then, it iterates
179 dl 1.1 * backwards over the list until the specified element is found, or the
180     * beginning of the list is reached.
181     *
182     * @throws ClassCastException {@inheritDoc}
183     * @throws NullPointerException {@inheritDoc}
184     */
185     public int lastIndexOf(Object o) {
186     ListIterator<E> e = listIterator(size());
187     if (o==null) {
188     while (e.hasPrevious())
189     if (e.previous()==null)
190     return e.nextIndex();
191     } else {
192     while (e.hasPrevious())
193     if (o.equals(e.previous()))
194     return e.nextIndex();
195     }
196     return -1;
197     }
198    
199    
200     // Bulk Operations
201    
202     /**
203     * Removes all of the elements from this list (optional operation).
204     * The list will be empty after this call returns.
205     *
206 jsr166 1.12 * <p>This implementation calls {@code removeRange(0, size())}.
207 dl 1.1 *
208     * <p>Note that this implementation throws an
209 jsr166 1.12 * {@code UnsupportedOperationException} unless {@code remove(int
210     * index)} or {@code removeRange(int fromIndex, int toIndex)} is
211 dl 1.1 * overridden.
212     *
213 jsr166 1.12 * @throws UnsupportedOperationException if the {@code clear} operation
214 dl 1.1 * is not supported by this list
215     */
216     public void clear() {
217     removeRange(0, size());
218     }
219    
220     /**
221     * {@inheritDoc}
222     *
223 jsr166 1.13 * <p>This implementation gets an iterator over the specified collection
224     * and iterates over it, inserting the elements obtained from the
225     * iterator into this list at the appropriate position, one at a time,
226     * using {@code add(int, E)}.
227     * Many implementations will override this method for efficiency.
228 dl 1.1 *
229     * <p>Note that this implementation throws an
230 jsr166 1.13 * {@code UnsupportedOperationException} unless
231     * {@link #add(int, Object) add(int, E)} is overridden.
232 dl 1.1 *
233     * @throws UnsupportedOperationException {@inheritDoc}
234     * @throws ClassCastException {@inheritDoc}
235     * @throws NullPointerException {@inheritDoc}
236     * @throws IllegalArgumentException {@inheritDoc}
237     * @throws IndexOutOfBoundsException {@inheritDoc}
238     */
239     public boolean addAll(int index, Collection<? extends E> c) {
240     boolean modified = false;
241     Iterator<? extends E> e = c.iterator();
242     while (e.hasNext()) {
243     add(index++, e.next());
244     modified = true;
245     }
246     return modified;
247     }
248    
249    
250     // Iterators
251    
252     /**
253 jsr166 1.14 * Returns an iterator over the elements in this list in proper sequence.
254 dl 1.1 *
255 jsr166 1.13 * <p>This implementation returns a straightforward implementation of the
256 jsr166 1.12 * iterator interface, relying on the backing list's {@code size()},
257 jsr166 1.13 * {@code get(int)}, and {@code remove(int)} methods.
258 dl 1.1 *
259 jsr166 1.13 * <p>Note that the iterator returned by this method will throw an
260 jsr166 1.12 * {@code UnsupportedOperationException} in response to its
261     * {@code remove} method unless the list's {@code remove(int)} method is
262 jsr166 1.13 * overridden.
263 dl 1.1 *
264 jsr166 1.13 * <p>This implementation can be made to throw runtime exceptions in the
265     * face of concurrent modification, as described in the specification
266     * for the (protected) {@code modCount} field.
267 dl 1.1 *
268     * @return an iterator over the elements in this list in proper sequence
269     *
270     * @see #modCount
271     */
272     public Iterator<E> iterator() {
273     return new Itr();
274     }
275    
276     /**
277     * {@inheritDoc}
278     *
279 jsr166 1.12 * <p>This implementation returns {@code listIterator(0)}.
280 dl 1.1 *
281     * @see #listIterator(int)
282     */
283     public ListIterator<E> listIterator() {
284     return listIterator(0);
285     }
286    
287     /**
288     * {@inheritDoc}
289     *
290     * <p>This implementation returns a straightforward implementation of the
291 jsr166 1.12 * {@code ListIterator} interface that extends the implementation of the
292     * {@code Iterator} interface returned by the {@code iterator()} method.
293     * The {@code ListIterator} implementation relies on the backing list's
294 jsr166 1.14 * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
295 jsr166 1.12 * and {@code remove(int)} methods.
296 dl 1.1 *
297     * <p>Note that the list iterator returned by this implementation will
298 jsr166 1.12 * throw an {@code UnsupportedOperationException} in response to its
299     * {@code remove}, {@code set} and {@code add} methods unless the
300 jsr166 1.13 * list's {@code remove(int)}, {@code set(int, E)}, and
301     * {@code add(int, E)} methods are overridden.
302 dl 1.1 *
303     * <p>This implementation can be made to throw runtime exceptions in the
304     * face of concurrent modification, as described in the specification for
305 jsr166 1.12 * the (protected) {@code modCount} field.
306 dl 1.1 *
307     * @throws IndexOutOfBoundsException {@inheritDoc}
308     *
309     * @see #modCount
310     */
311     public ListIterator<E> listIterator(final int index) {
312     if (index<0 || index>size())
313     throw new IndexOutOfBoundsException("Index: "+index);
314    
315     return new ListItr(index);
316     }
317    
318     private class Itr implements Iterator<E> {
319     /**
320     * Index of element to be returned by subsequent call to next.
321     */
322     int cursor = 0;
323    
324     /**
325     * Index of element returned by most recent call to next or
326     * previous. Reset to -1 if this element is deleted by a call
327     * to remove.
328     */
329     int lastRet = -1;
330    
331     /**
332     * The modCount value that the iterator believes that the backing
333     * List should have. If this expectation is violated, the iterator
334     * has detected concurrent modification.
335     */
336     int expectedModCount = modCount;
337    
338 dl 1.4 public boolean hasNext() {
339     return cursor != size();
340 dl 1.1 }
341    
342 dl 1.4 public E next() {
343 dl 1.8 checkForComodification();
344     try {
345     E next = get(cursor);
346     lastRet = cursor++;
347     return next;
348     } catch (IndexOutOfBoundsException e) {
349     checkForComodification();
350     throw new NoSuchElementException();
351     }
352 dl 1.1 }
353    
354 dl 1.4 public void remove() {
355 dl 1.1 if (lastRet == -1)
356     throw new IllegalStateException();
357 dl 1.8 checkForComodification();
358    
359 dl 1.1 try {
360     AbstractList.this.remove(lastRet);
361     if (lastRet < cursor)
362     cursor--;
363     lastRet = -1;
364     expectedModCount = modCount;
365     } catch (IndexOutOfBoundsException e) {
366     throw new ConcurrentModificationException();
367     }
368     }
369 dl 1.8
370     final void checkForComodification() {
371     if (modCount != expectedModCount)
372     throw new ConcurrentModificationException();
373     }
374 dl 1.1 }
375 jsr166 1.6
376 dl 1.4 private class ListItr extends Itr implements ListIterator<E> {
377 dl 1.1 ListItr(int index) {
378     cursor = index;
379     }
380    
381     public boolean hasPrevious() {
382 dl 1.4 return cursor != 0;
383 dl 1.1 }
384    
385 dl 1.8 public E previous() {
386     checkForComodification();
387     try {
388     int i = cursor - 1;
389     E previous = get(i);
390     lastRet = cursor = i;
391     return previous;
392     } catch (IndexOutOfBoundsException e) {
393     checkForComodification();
394     throw new NoSuchElementException();
395     }
396     }
397    
398 dl 1.1 public int nextIndex() {
399     return cursor;
400     }
401    
402     public int previousIndex() {
403 dl 1.8 return cursor-1;
404 dl 1.1 }
405    
406     public void set(E e) {
407     if (lastRet == -1)
408     throw new IllegalStateException();
409 dl 1.8 checkForComodification();
410    
411 dl 1.1 try {
412     AbstractList.this.set(lastRet, e);
413     expectedModCount = modCount;
414     } catch (IndexOutOfBoundsException ex) {
415     throw new ConcurrentModificationException();
416     }
417     }
418    
419     public void add(E e) {
420 dl 1.8 checkForComodification();
421    
422 dl 1.1 try {
423     int i = cursor;
424     AbstractList.this.add(i, e);
425     cursor = i + 1;
426     lastRet = -1;
427     expectedModCount = modCount;
428     } catch (IndexOutOfBoundsException ex) {
429     throw new ConcurrentModificationException();
430     }
431     }
432     }
433    
434     /**
435     * {@inheritDoc}
436     *
437     * <p>This implementation returns a list that subclasses
438 jsr166 1.12 * {@code AbstractList}. The subclass stores, in private fields, the
439 dl 1.1 * offset of the subList within the backing list, the size of the subList
440     * (which can change over its lifetime), and the expected
441 jsr166 1.12 * {@code modCount} value of the backing list. There are two variants
442     * of the subclass, one of which implements {@code RandomAccess}.
443     * If this list implements {@code RandomAccess} the returned list will
444     * be an instance of the subclass that implements {@code RandomAccess}.
445     *
446 jsr166 1.14 * <p>The subclass's {@code set(int, E)}, {@code get(int)},
447     * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
448 jsr166 1.12 * Collection)} and {@code removeRange(int, int)} methods all
449 dl 1.1 * delegate to the corresponding methods on the backing abstract list,
450     * after bounds-checking the index and adjusting for the offset. The
451 jsr166 1.12 * {@code addAll(Collection c)} method merely returns {@code addAll(size,
452     * c)}.
453 dl 1.1 *
454 jsr166 1.12 * <p>The {@code listIterator(int)} method returns a "wrapper object"
455 dl 1.1 * over a list iterator on the backing list, which is created with the
456 jsr166 1.12 * corresponding method on the backing list. The {@code iterator} method
457     * merely returns {@code listIterator()}, and the {@code size} method
458     * merely returns the subclass's {@code size} field.
459 dl 1.1 *
460 jsr166 1.12 * <p>All methods first check to see if the actual {@code modCount} of
461 dl 1.1 * the backing list is equal to its expected value, and throw a
462 jsr166 1.12 * {@code ConcurrentModificationException} if it is not.
463 dl 1.1 *
464     * @throws IndexOutOfBoundsException endpoint index value out of range
465 jsr166 1.14 * {@code (fromIndex < 0 || toIndex > size)}
466 dl 1.1 * @throws IllegalArgumentException if the endpoint indices are out of order
467 jsr166 1.14 * {@code (fromIndex > toIndex)}
468 dl 1.1 */
469     public List<E> subList(int fromIndex, int toIndex) {
470     return (this instanceof RandomAccess ?
471 dl 1.8 new RandomAccessSubList(this, this, fromIndex, fromIndex, toIndex) :
472     new SubList(this, this, fromIndex, fromIndex, toIndex));
473 dl 1.1 }
474    
475     // Comparison and hashing
476    
477     /**
478     * Compares the specified object with this list for equality. Returns
479 jsr166 1.12 * {@code true} if and only if the specified object is also a list, both
480 dl 1.1 * lists have the same size, and all corresponding pairs of elements in
481 jsr166 1.12 * the two lists are <i>equal</i>. (Two elements {@code e1} and
482     * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
483     * e1.equals(e2))}.) In other words, two lists are defined to be
484 jsr166 1.13 * equal if they contain the same elements in the same order.
485 dl 1.1 *
486 jsr166 1.13 * <p>This implementation first checks if the specified object is this
487 jsr166 1.12 * list. If so, it returns {@code true}; if not, it checks if the
488     * specified object is a list. If not, it returns {@code false}; if so,
489 dl 1.1 * it iterates over both lists, comparing corresponding pairs of elements.
490 jsr166 1.12 * If any comparison returns {@code false}, this method returns
491     * {@code false}. If either iterator runs out of elements before the
492     * other it returns {@code false} (as the lists are of unequal length);
493     * otherwise it returns {@code true} when the iterations complete.
494 dl 1.1 *
495     * @param o the object to be compared for equality with this list
496 jsr166 1.12 * @return {@code true} if the specified object is equal to this list
497 dl 1.1 */
498     public boolean equals(Object o) {
499     if (o == this)
500     return true;
501     if (!(o instanceof List))
502     return false;
503    
504     ListIterator<E> e1 = listIterator();
505     ListIterator e2 = ((List) o).listIterator();
506     while(e1.hasNext() && e2.hasNext()) {
507     E o1 = e1.next();
508     Object o2 = e2.next();
509     if (!(o1==null ? o2==null : o1.equals(o2)))
510     return false;
511     }
512     return !(e1.hasNext() || e2.hasNext());
513     }
514    
515     /**
516 jsr166 1.13 * Returns the hash code value for this list.
517 dl 1.1 *
518 jsr166 1.13 * <p>This implementation uses exactly the code that is used to define the
519 dl 1.1 * list hash function in the documentation for the {@link List#hashCode}
520     * method.
521     *
522     * @return the hash code value for this list
523     */
524     public int hashCode() {
525     int hashCode = 1;
526     Iterator<E> i = iterator();
527     while (i.hasNext()) {
528     E obj = i.next();
529     hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
530     }
531     return hashCode;
532     }
533    
534     /**
535     * Removes from this list all of the elements whose index is between
536 jsr166 1.12 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
537 dl 1.1 * Shifts any succeeding elements to the left (reduces their index).
538 jsr166 1.12 * This call shortens the ArrayList by {@code (toIndex - fromIndex)}
539     * elements. (If {@code toIndex==fromIndex}, this operation has no
540 jsr166 1.13 * effect.)
541 dl 1.1 *
542 jsr166 1.13 * <p>This method is called by the {@code clear} operation on this list
543 dl 1.1 * and its subLists. Overriding this method to take advantage of
544     * the internals of the list implementation can <i>substantially</i>
545 jsr166 1.12 * improve the performance of the {@code clear} operation on this list
546 jsr166 1.13 * and its subLists.
547 dl 1.1 *
548 jsr166 1.13 * <p>This implementation gets a list iterator positioned before
549 jsr166 1.12 * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
550     * followed by {@code ListIterator.remove} until the entire range has
551     * been removed. <b>Note: if {@code ListIterator.remove} requires linear
552 dl 1.1 * time, this implementation requires quadratic time.</b>
553     *
554     * @param fromIndex index of first element to be removed
555     * @param toIndex index after last element to be removed
556     */
557     protected void removeRange(int fromIndex, int toIndex) {
558     ListIterator<E> it = listIterator(fromIndex);
559     for (int i=0, n=toIndex-fromIndex; i<n; i++) {
560     it.next();
561     it.remove();
562     }
563     }
564    
565     /**
566     * The number of times this list has been <i>structurally modified</i>.
567     * Structural modifications are those that change the size of the
568     * list, or otherwise perturb it in such a fashion that iterations in
569 jsr166 1.13 * progress may yield incorrect results.
570 dl 1.1 *
571 jsr166 1.13 * <p>This field is used by the iterator and list iterator implementation
572 jsr166 1.12 * returned by the {@code iterator} and {@code listIterator} methods.
573 dl 1.1 * If the value of this field changes unexpectedly, the iterator (or list
574 jsr166 1.12 * iterator) will throw a {@code ConcurrentModificationException} in
575     * response to the {@code next}, {@code remove}, {@code previous},
576     * {@code set} or {@code add} operations. This provides
577 dl 1.1 * <i>fail-fast</i> behavior, rather than non-deterministic behavior in
578 jsr166 1.13 * the face of concurrent modification during iteration.
579 dl 1.1 *
580 jsr166 1.13 * <p><b>Use of this field by subclasses is optional.</b> If a subclass
581 dl 1.1 * wishes to provide fail-fast iterators (and list iterators), then it
582 jsr166 1.14 * merely has to increment this field in its {@code add(int, E)} and
583 jsr166 1.12 * {@code remove(int)} methods (and any other methods that it overrides
584 dl 1.1 * that result in structural modifications to the list). A single call to
585 jsr166 1.14 * {@code add(int, E)} or {@code remove(int)} must add no more than
586 dl 1.1 * one to this field, or the iterators (and list iterators) will throw
587 jsr166 1.12 * bogus {@code ConcurrentModificationExceptions}. If an implementation
588 dl 1.1 * does not wish to provide fail-fast iterators, this field may be
589     * ignored.
590     */
591     protected transient int modCount = 0;
592     }
593    
594 dl 1.8 /**
595     * Generic sublists. Non-nested to enable construction by other
596     * classes in this package.
597     */
598 dl 1.1 class SubList<E> extends AbstractList<E> {
599 dl 1.8 /*
600     * A SubList has both a "base", the ultimate backing list, as well
601     * as a "parent", which is the list or sublist creating this
602     * sublist. All methods that may cause structural modifications
603     * must propagate through the parent link, with O(k) performance
604     * where k is sublist depth. For example in the case of a
605     * sub-sub-list, invoking remove(x) will result in a chain of
606     * three remove calls. However, all other non-structurally
607     * modifying methods can bypass this chain, and relay directly to
608     * the base list. In particular, doing so signficantly speeds up
609     * the performance of iterators for deeply-nested sublists.
610     */
611     final AbstractList<E> base; // Backing list
612     final AbstractList<E> parent; // Parent list
613     final int baseOffset; // index wrt base
614     final int parentOffset; // index wrt parent
615     int length; // Number of elements in this sublist
616    
617     SubList(AbstractList<E> base,
618 jsr166 1.9 AbstractList<E> parent,
619     int baseIndex,
620     int fromIndex,
621 dl 1.8 int toIndex) {
622 dl 1.1 if (fromIndex < 0)
623     throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
624 dl 1.8 if (toIndex > parent.size())
625 dl 1.1 throw new IndexOutOfBoundsException("toIndex = " + toIndex);
626     if (fromIndex > toIndex)
627     throw new IllegalArgumentException("fromIndex(" + fromIndex +
628     ") > toIndex(" + toIndex + ")");
629 dl 1.8 this.base = base;
630     this.parent = parent;
631     this.baseOffset = baseIndex;
632     this.parentOffset = fromIndex;
633     this.length = toIndex - fromIndex;
634     this.modCount = base.modCount;
635     }
636    
637     /**
638     * Returns an IndexOutOfBoundsException with nicer message
639     */
640     private IndexOutOfBoundsException indexError(int index) {
641 jsr166 1.9 return new IndexOutOfBoundsException("Index: " + index +
642 dl 1.8 ", Size: " + length);
643 dl 1.1 }
644    
645     public E set(int index, E element) {
646 dl 1.8 if (index < 0 || index >= length)
647     throw indexError(index);
648     if (base.modCount != modCount)
649     throw new ConcurrentModificationException();
650     return base.set(index + baseOffset, element);
651 dl 1.1 }
652    
653     public E get(int index) {
654 dl 1.8 if (index < 0 || index >= length)
655     throw indexError(index);
656     if (base.modCount != modCount)
657     throw new ConcurrentModificationException();
658     return base.get(index + baseOffset);
659 dl 1.1 }
660    
661     public int size() {
662 dl 1.8 if (base.modCount != modCount)
663     throw new ConcurrentModificationException();
664     return length;
665 dl 1.1 }
666    
667     public void add(int index, E element) {
668 dl 1.8 if (index < 0 || index>length)
669     throw indexError(index);
670     if (base.modCount != modCount)
671     throw new ConcurrentModificationException();
672     parent.add(index + parentOffset, element);
673     length++;
674     modCount = base.modCount;
675 dl 1.1 }
676    
677     public E remove(int index) {
678 dl 1.8 if (index < 0 || index >= length)
679     throw indexError(index);
680     if (base.modCount != modCount)
681     throw new ConcurrentModificationException();
682     E result = parent.remove(index + parentOffset);
683     length--;
684     modCount = base.modCount;
685 dl 1.1 return result;
686     }
687    
688     protected void removeRange(int fromIndex, int toIndex) {
689 dl 1.8 if (base.modCount != modCount)
690     throw new ConcurrentModificationException();
691     parent.removeRange(fromIndex + parentOffset, toIndex + parentOffset);
692     length -= (toIndex-fromIndex);
693     modCount = base.modCount;
694 dl 1.1 }
695    
696     public boolean addAll(Collection<? extends E> c) {
697 dl 1.8 return addAll(length, c);
698 dl 1.1 }
699    
700     public boolean addAll(int index, Collection<? extends E> c) {
701 dl 1.8 if (index < 0 || index > length)
702     throw indexError(index);
703 dl 1.1 int cSize = c.size();
704     if (cSize==0)
705     return false;
706    
707 dl 1.8 if (base.modCount != modCount)
708     throw new ConcurrentModificationException();
709     parent.addAll(parentOffset + index, c);
710     length += cSize;
711     modCount = base.modCount;
712 dl 1.1 return true;
713     }
714    
715 dl 1.8 public List<E> subList(int fromIndex, int toIndex) {
716 jsr166 1.9 return new SubList(base, this, fromIndex + baseOffset,
717 dl 1.8 fromIndex, toIndex);
718     }
719    
720 dl 1.1 public Iterator<E> iterator() {
721 dl 1.8 return new SubListIterator(this, 0);
722     }
723    
724     public ListIterator<E> listIterator() {
725     return new SubListIterator(this, 0);
726 dl 1.1 }
727    
728 dl 1.8 public ListIterator<E> listIterator(int index) {
729     if (index < 0 || index>length)
730     throw indexError(index);
731     return new SubListIterator(this, index);
732     }
733 dl 1.1
734 dl 1.8 /**
735     * Generic sublist iterator obeying fastfail semantics via
736     * modCount. The hasNext and next methods locally check for
737     * in-range indices before relaying to backing list to get
738     * element. If this either encounters an unexpected modCount or
739     * fails, the backing list must have been concurrently modified,
740     * and is so reported. The add and remove methods performing
741     * structural modifications instead relay them through the
742     * sublist.
743     */
744     private static final class SubListIterator<E> implements ListIterator<E> {
745     final SubList<E> outer; // Sublist creating this iteraor
746     final AbstractList<E> base; // base list
747     final int offset; // Cursor offset wrt base
748     int cursor; // Current index
749     int fence; // Upper bound on cursor
750     int lastRet; // Index of returned element, or -1
751 jsr166 1.9 int expectedModCount; // Expected modCount of base
752 dl 1.8
753     SubListIterator(SubList<E> list, int index) {
754     this.lastRet = -1;
755     this.cursor = index;
756     this.outer = list;
757     this.offset = list.baseOffset;
758     this.fence = list.length;
759     this.base = list.base;
760     this.expectedModCount = base.modCount;
761     }
762 dl 1.1
763 dl 1.8 public boolean hasNext() {
764     return cursor < fence;
765     }
766 dl 1.1
767 dl 1.8 public boolean hasPrevious() {
768     return cursor > 0;
769     }
770 dl 1.1
771 dl 1.8 public int nextIndex() {
772     return cursor;
773     }
774 dl 1.1
775 dl 1.8 public int previousIndex() {
776     return cursor - 1;
777     }
778 dl 1.1
779 dl 1.8 public E next() {
780     int i = cursor;
781     if (cursor >= fence)
782     throw new NoSuchElementException();
783     if (expectedModCount == base.modCount) {
784     try {
785     Object next = base.get(i + offset);
786     lastRet = i;
787     cursor = i + 1;
788     return (E)next;
789     } catch (IndexOutOfBoundsException fallThrough) {
790     }
791 dl 1.1 }
792 dl 1.8 throw new ConcurrentModificationException();
793     }
794 dl 1.1
795 dl 1.8 public E previous() {
796     int i = cursor - 1;
797     if (i < 0)
798     throw new NoSuchElementException();
799     if (expectedModCount == base.modCount) {
800     try {
801     Object prev = base.get(i + offset);
802     lastRet = i;
803     cursor = i;
804     return (E)prev;
805     } catch (IndexOutOfBoundsException fallThrough) {
806     }
807 dl 1.1 }
808 dl 1.8 throw new ConcurrentModificationException();
809     }
810 dl 1.1
811 dl 1.8 public void set(E e) {
812     if (lastRet < 0)
813     throw new IllegalStateException();
814     if (expectedModCount != base.modCount)
815     throw new ConcurrentModificationException();
816     try {
817     outer.set(lastRet, e);
818     expectedModCount = base.modCount;
819     } catch (IndexOutOfBoundsException ex) {
820     throw new ConcurrentModificationException();
821 dl 1.1 }
822 dl 1.8 }
823 dl 1.1
824 dl 1.8 public void remove() {
825     int i = lastRet;
826     if (i < 0)
827     throw new IllegalStateException();
828     if (expectedModCount != base.modCount)
829     throw new ConcurrentModificationException();
830     try {
831     outer.remove(i);
832     if (i < cursor)
833     cursor--;
834     lastRet = -1;
835     fence = outer.length;
836     expectedModCount = base.modCount;
837     } catch (IndexOutOfBoundsException ex) {
838     throw new ConcurrentModificationException();
839 dl 1.1 }
840 dl 1.8 }
841 dl 1.1
842 dl 1.8 public void add(E e) {
843     if (expectedModCount != base.modCount)
844     throw new ConcurrentModificationException();
845     try {
846     int i = cursor;
847     outer.add(i, e);
848     cursor = i + 1;
849     lastRet = -1;
850     fence = outer.length;
851     expectedModCount = base.modCount;
852     } catch (IndexOutOfBoundsException ex) {
853     throw new ConcurrentModificationException();
854 dl 1.1 }
855 dl 1.8 }
856 dl 1.1 }
857    
858     }
859    
860     class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
861 dl 1.8 RandomAccessSubList(AbstractList<E> base,
862 jsr166 1.9 AbstractList<E> parent, int baseIndex,
863 dl 1.8 int fromIndex, int toIndex) {
864     super(base, parent, baseIndex, fromIndex, toIndex);
865 dl 1.1 }
866    
867     public List<E> subList(int fromIndex, int toIndex) {
868 dl 1.8 return new RandomAccessSubList(base, this, fromIndex + baseOffset,
869     fromIndex, toIndex);
870 dl 1.1 }
871     }
872 dl 1.8