ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/AbstractList.java
(Generate patch)

Comparing jsr166/src/main/java/util/AbstractList.java (file contents):
Revision 1.18 by jsr166, Tue Sep 11 15:24:16 2007 UTC vs.
Revision 1.19 by jsr166, Sun May 18 23:47:55 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines