src/share/classes/java/util/ArrayList.java

Print this page
rev 3977 : 6546713: link the word (optional) in exception specifications to the text which provides explanation and context.


 611         if (index > size || index < 0)
 612             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 613     }
 614 
 615     /**
 616      * Constructs an IndexOutOfBoundsException detail message.
 617      * Of the many possible refactorings of the error handling code,
 618      * this "outlining" performs best with both server and client VMs.
 619      */
 620     private String outOfBoundsMsg(int index) {
 621         return "Index: "+index+", Size: "+size;
 622     }
 623 
 624     /**
 625      * Removes from this list all of its elements that are contained in the
 626      * specified collection.
 627      *
 628      * @param c collection containing elements to be removed from this list
 629      * @return {@code true} if this list changed as a result of the call
 630      * @throws ClassCastException if the class of an element of this list
 631      *         is incompatible with the specified collection (optional)

 632      * @throws NullPointerException if this list contains a null element and the
 633      *         specified collection does not permit null elements (optional),

 634      *         or if the specified collection is null
 635      * @see Collection#contains(Object)
 636      */
 637     public boolean removeAll(Collection<?> c) {
 638         return batchRemove(c, false);
 639     }
 640 
 641     /**
 642      * Retains only the elements in this list that are contained in the
 643      * specified collection.  In other words, removes from this list all
 644      * of its elements that are not contained in the specified collection.
 645      *
 646      * @param c collection containing elements to be retained in this list
 647      * @return {@code true} if this list changed as a result of the call
 648      * @throws ClassCastException if the class of an element of this list
 649      *         is incompatible with the specified collection (optional)

 650      * @throws NullPointerException if this list contains a null element and the
 651      *         specified collection does not permit null elements (optional),

 652      *         or if the specified collection is null
 653      * @see Collection#contains(Object)
 654      */
 655     public boolean retainAll(Collection<?> c) {
 656         return batchRemove(c, true);
 657     }
 658 
 659     private boolean batchRemove(Collection<?> c, boolean complement) {
 660         final Object[] elementData = this.elementData;
 661         int r = 0, w = 0;
 662         boolean modified = false;
 663         try {
 664             for (; r < size; r++)
 665                 if (c.contains(elementData[r]) == complement)
 666                     elementData[w++] = elementData[r];
 667         } finally {
 668             // Preserve behavioral compatibility with AbstractCollection,
 669             // even if c.contains() throws.
 670             if (r != size) {
 671                 System.arraycopy(elementData, r,




 611         if (index > size || index < 0)
 612             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 613     }
 614 
 615     /**
 616      * Constructs an IndexOutOfBoundsException detail message.
 617      * Of the many possible refactorings of the error handling code,
 618      * this "outlining" performs best with both server and client VMs.
 619      */
 620     private String outOfBoundsMsg(int index) {
 621         return "Index: "+index+", Size: "+size;
 622     }
 623 
 624     /**
 625      * Removes from this list all of its elements that are contained in the
 626      * specified collection.
 627      *
 628      * @param c collection containing elements to be removed from this list
 629      * @return {@code true} if this list changed as a result of the call
 630      * @throws ClassCastException if the class of an element of this list
 631      *         is incompatible with the specified collection
 632      * (<a href="Collection.html#optional-restrictions">optional</a>)
 633      * @throws NullPointerException if this list contains a null element and the
 634      *         specified collection does not permit null elements
 635      * (<a href="Collection.html#optional-restrictions">optional</a>),
 636      *         or if the specified collection is null
 637      * @see Collection#contains(Object)
 638      */
 639     public boolean removeAll(Collection<?> c) {
 640         return batchRemove(c, false);
 641     }
 642 
 643     /**
 644      * Retains only the elements in this list that are contained in the
 645      * specified collection.  In other words, removes from this list all
 646      * of its elements that are not contained in the specified collection.
 647      *
 648      * @param c collection containing elements to be retained in this list
 649      * @return {@code true} if this list changed as a result of the call
 650      * @throws ClassCastException if the class of an element of this list
 651      *         is incompatible with the specified collection
 652      * (<a href="Collection.html#optional-restrictions">optional</a>)
 653      * @throws NullPointerException if this list contains a null element and the
 654      *         specified collection does not permit null elements
 655      * (<a href="Collection.html#optional-restrictions">optional</a>),
 656      *         or if the specified collection is null
 657      * @see Collection#contains(Object)
 658      */
 659     public boolean retainAll(Collection<?> c) {
 660         return batchRemove(c, true);
 661     }
 662 
 663     private boolean batchRemove(Collection<?> c, boolean complement) {
 664         final Object[] elementData = this.elementData;
 665         int r = 0, w = 0;
 666         boolean modified = false;
 667         try {
 668             for (; r < size; r++)
 669                 if (c.contains(elementData[r]) == complement)
 670                     elementData[w++] = elementData[r];
 671         } finally {
 672             // Preserve behavioral compatibility with AbstractCollection,
 673             // even if c.contains() throws.
 674             if (r != size) {
 675                 System.arraycopy(elementData, r,