ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractList.java
Revision: 1.1
Committed: Sun Nov 27 14:54:23 2005 UTC (18 years, 5 months ago) by dl
Branch: MAIN
Log Message:
Iterator performance improvements

File Contents

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