465 * {@code addAll(Collection c)} method merely returns {@code addAll(size,
466 * c)}.
467 *
468 * <p>The {@code listIterator(int)} method returns a "wrapper object"
469 * over a list iterator on the backing list, which is created with the
470 * corresponding method on the backing list. The {@code iterator} method
471 * merely returns {@code listIterator()}, and the {@code size} method
472 * merely returns the subclass's {@code size} field.
473 *
474 * <p>All methods first check to see if the actual {@code modCount} of
475 * the backing list is equal to its expected value, and throw a
476 * {@code ConcurrentModificationException} if it is not.
477 *
478 * @throws IndexOutOfBoundsException if an endpoint index value is out of range
479 * {@code (fromIndex < 0 || toIndex > size)}
480 * @throws IllegalArgumentException if the endpoint indices are out of order
481 * {@code (fromIndex > toIndex)}
482 */
483 public List<E> subList(int fromIndex, int toIndex) {
484 return (this instanceof RandomAccess ?
485 new RandomAccessSubList<E>(this, fromIndex, toIndex) :
486 new SubList<E>(this, fromIndex, toIndex));
487 }
488
489 // Comparison and hashing
490
491 /**
492 * Compares the specified object with this list for equality. Returns
493 * {@code true} if and only if the specified object is also a list, both
494 * lists have the same size, and all corresponding pairs of elements in
495 * the two lists are <i>equal</i>. (Two elements {@code e1} and
496 * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
497 * e1.equals(e2))}.) In other words, two lists are defined to be
498 * equal if they contain the same elements in the same order.<p>
499 *
500 * This implementation first checks if the specified object is this
501 * list. If so, it returns {@code true}; if not, it checks if the
502 * specified object is a list. If not, it returns {@code false}; if so,
503 * it iterates over both lists, comparing corresponding pairs of elements.
504 * If any comparison returns {@code false}, this method returns
505 * {@code false}. If either iterator runs out of elements before the
506 * other it returns {@code false} (as the lists are of unequal length);
730
731 public void remove() {
732 i.remove();
733 SubList.this.modCount = l.modCount;
734 size--;
735 }
736
737 public void set(E e) {
738 i.set(e);
739 }
740
741 public void add(E e) {
742 i.add(e);
743 SubList.this.modCount = l.modCount;
744 size++;
745 }
746 };
747 }
748
749 public List<E> subList(int fromIndex, int toIndex) {
750 return new SubList<E>(this, fromIndex, toIndex);
751 }
752
753 private void rangeCheck(int index) {
754 if (index < 0 || index >= size)
755 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
756 }
757
758 private void rangeCheckForAdd(int 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;
765 }
766
767 private void checkForComodification() {
768 if (this.modCount != l.modCount)
769 throw new ConcurrentModificationException();
770 }
771 }
772
773 class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
774 RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
775 super(list, fromIndex, toIndex);
776 }
777
778 public List<E> subList(int fromIndex, int toIndex) {
779 return new RandomAccessSubList<E>(this, fromIndex, toIndex);
780 }
781 }
|
465 * {@code addAll(Collection c)} method merely returns {@code addAll(size,
466 * c)}.
467 *
468 * <p>The {@code listIterator(int)} method returns a "wrapper object"
469 * over a list iterator on the backing list, which is created with the
470 * corresponding method on the backing list. The {@code iterator} method
471 * merely returns {@code listIterator()}, and the {@code size} method
472 * merely returns the subclass's {@code size} field.
473 *
474 * <p>All methods first check to see if the actual {@code modCount} of
475 * the backing list is equal to its expected value, and throw a
476 * {@code ConcurrentModificationException} if it is not.
477 *
478 * @throws IndexOutOfBoundsException if an endpoint index value is out of range
479 * {@code (fromIndex < 0 || toIndex > size)}
480 * @throws IllegalArgumentException if the endpoint indices are out of order
481 * {@code (fromIndex > toIndex)}
482 */
483 public List<E> subList(int fromIndex, int toIndex) {
484 return (this instanceof RandomAccess ?
485 new RandomAccessSubList<>(this, fromIndex, toIndex) :
486 new SubList<>(this, fromIndex, toIndex));
487 }
488
489 // Comparison and hashing
490
491 /**
492 * Compares the specified object with this list for equality. Returns
493 * {@code true} if and only if the specified object is also a list, both
494 * lists have the same size, and all corresponding pairs of elements in
495 * the two lists are <i>equal</i>. (Two elements {@code e1} and
496 * {@code e2} are <i>equal</i> if {@code (e1==null ? e2==null :
497 * e1.equals(e2))}.) In other words, two lists are defined to be
498 * equal if they contain the same elements in the same order.<p>
499 *
500 * This implementation first checks if the specified object is this
501 * list. If so, it returns {@code true}; if not, it checks if the
502 * specified object is a list. If not, it returns {@code false}; if so,
503 * it iterates over both lists, comparing corresponding pairs of elements.
504 * If any comparison returns {@code false}, this method returns
505 * {@code false}. If either iterator runs out of elements before the
506 * other it returns {@code false} (as the lists are of unequal length);
730
731 public void remove() {
732 i.remove();
733 SubList.this.modCount = l.modCount;
734 size--;
735 }
736
737 public void set(E e) {
738 i.set(e);
739 }
740
741 public void add(E e) {
742 i.add(e);
743 SubList.this.modCount = l.modCount;
744 size++;
745 }
746 };
747 }
748
749 public List<E> subList(int fromIndex, int toIndex) {
750 return new SubList<>(this, fromIndex, toIndex);
751 }
752
753 private void rangeCheck(int index) {
754 if (index < 0 || index >= size)
755 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
756 }
757
758 private void rangeCheckForAdd(int 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;
765 }
766
767 private void checkForComodification() {
768 if (this.modCount != l.modCount)
769 throw new ConcurrentModificationException();
770 }
771 }
772
773 class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
774 RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
775 super(list, fromIndex, toIndex);
776 }
777
778 public List<E> subList(int fromIndex, int toIndex) {
779 return new RandomAccessSubList<>(this, fromIndex, toIndex);
780 }
781 }
|