src/share/classes/java/util/AbstractCollection.java

Print this page
rev 7046 : 4802647: Throw required NPEs from removeAll()/retainAll()
Reviewed-by: mduigou, chegar, dholmes
Contributed-by: Brandon Passanisi <brandon.passanisi@oracle.com>


 351      *
 352      * <p>This implementation iterates over this collection, checking each
 353      * element returned by the iterator in turn to see if it's contained
 354      * in the specified collection.  If it's so contained, it's removed from
 355      * this collection with the iterator's <tt>remove</tt> method.
 356      *
 357      * <p>Note that this implementation will throw an
 358      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 359      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 360      * and this collection contains one or more elements in common with the
 361      * specified collection.
 362      *
 363      * @throws UnsupportedOperationException {@inheritDoc}
 364      * @throws ClassCastException            {@inheritDoc}
 365      * @throws NullPointerException          {@inheritDoc}
 366      *
 367      * @see #remove(Object)
 368      * @see #contains(Object)
 369      */
 370     public boolean removeAll(Collection<?> c) {

 371         boolean modified = false;
 372         Iterator<?> it = iterator();
 373         while (it.hasNext()) {
 374             if (c.contains(it.next())) {
 375                 it.remove();
 376                 modified = true;
 377             }
 378         }
 379         return modified;
 380     }
 381 
 382     /**
 383      * {@inheritDoc}
 384      *
 385      * <p>This implementation iterates over this collection, checking each
 386      * element returned by the iterator in turn to see if it's contained
 387      * in the specified collection.  If it's not so contained, it's removed
 388      * from this collection with the iterator's <tt>remove</tt> method.
 389      *
 390      * <p>Note that this implementation will throw an
 391      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 392      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 393      * and this collection contains one or more elements not present in the
 394      * specified collection.
 395      *
 396      * @throws UnsupportedOperationException {@inheritDoc}
 397      * @throws ClassCastException            {@inheritDoc}
 398      * @throws NullPointerException          {@inheritDoc}
 399      *
 400      * @see #remove(Object)
 401      * @see #contains(Object)
 402      */
 403     public boolean retainAll(Collection<?> c) {

 404         boolean modified = false;
 405         Iterator<E> it = iterator();
 406         while (it.hasNext()) {
 407             if (!c.contains(it.next())) {
 408                 it.remove();
 409                 modified = true;
 410             }
 411         }
 412         return modified;
 413     }
 414 
 415     /**
 416      * {@inheritDoc}
 417      *
 418      * <p>This implementation iterates over this collection, removing each
 419      * element using the <tt>Iterator.remove</tt> operation.  Most
 420      * implementations will probably choose to override this method for
 421      * efficiency.
 422      *
 423      * <p>Note that this implementation will throw an




 351      *
 352      * <p>This implementation iterates over this collection, checking each
 353      * element returned by the iterator in turn to see if it's contained
 354      * in the specified collection.  If it's so contained, it's removed from
 355      * this collection with the iterator's <tt>remove</tt> method.
 356      *
 357      * <p>Note that this implementation will throw an
 358      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 359      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 360      * and this collection contains one or more elements in common with the
 361      * specified collection.
 362      *
 363      * @throws UnsupportedOperationException {@inheritDoc}
 364      * @throws ClassCastException            {@inheritDoc}
 365      * @throws NullPointerException          {@inheritDoc}
 366      *
 367      * @see #remove(Object)
 368      * @see #contains(Object)
 369      */
 370     public boolean removeAll(Collection<?> c) {
 371         Objects.requireNonNull(c);
 372         boolean modified = false;
 373         Iterator<?> it = iterator();
 374         while (it.hasNext()) {
 375             if (c.contains(it.next())) {
 376                 it.remove();
 377                 modified = true;
 378             }
 379         }
 380         return modified;
 381     }
 382 
 383     /**
 384      * {@inheritDoc}
 385      *
 386      * <p>This implementation iterates over this collection, checking each
 387      * element returned by the iterator in turn to see if it's contained
 388      * in the specified collection.  If it's not so contained, it's removed
 389      * from this collection with the iterator's <tt>remove</tt> method.
 390      *
 391      * <p>Note that this implementation will throw an
 392      * <tt>UnsupportedOperationException</tt> if the iterator returned by the
 393      * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
 394      * and this collection contains one or more elements not present in the
 395      * specified collection.
 396      *
 397      * @throws UnsupportedOperationException {@inheritDoc}
 398      * @throws ClassCastException            {@inheritDoc}
 399      * @throws NullPointerException          {@inheritDoc}
 400      *
 401      * @see #remove(Object)
 402      * @see #contains(Object)
 403      */
 404     public boolean retainAll(Collection<?> c) {
 405         Objects.requireNonNull(c);
 406         boolean modified = false;
 407         Iterator<E> it = iterator();
 408         while (it.hasNext()) {
 409             if (!c.contains(it.next())) {
 410                 it.remove();
 411                 modified = true;
 412             }
 413         }
 414         return modified;
 415     }
 416 
 417     /**
 418      * {@inheritDoc}
 419      *
 420      * <p>This implementation iterates over this collection, removing each
 421      * element using the <tt>Iterator.remove</tt> operation.  Most
 422      * implementations will probably choose to override this method for
 423      * efficiency.
 424      *
 425      * <p>Note that this implementation will throw an