Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4402118

Collections.replaceAll() doesn't raise UnsupportedOperationException

XMLWordPrintable

    • beta2
    • sparc
    • solaris_2.6
    • Verified



      Name: auR10023 Date: 01/05/2001



      The javadoc for java.util.Collections.replaceAll(List list, Object oldVal, Object newVal)
      method says:

      ...
      Throws:
                UnsupportedOperationException - if the specified list or its list-iterator does not
                support the set method.
      ...
      Following example shows that UnsupportedOperationException is not thrown by this method.
      Here is the example:


      import java.util.*;

          class TestList extends Vector {
              public TestList() {
                  super();
              }
              public TestList(Collection c) {
                  super(c);
              }
              public Object set(int index, Object element) {
                  throw new UnsupportedOperationException();
              }
              public List subList(int fromIndex, int toIndex) {
                  return new TestList(super.subList(fromIndex, toIndex));
              }
          }

          class TestList1 extends Vector {
              public TestList1() {
                  super();
              }
              public TestList1(Collection c) {
                  super(c);
              }
              public List subList(int fromIndex, int toIndex) {
                  return new TestList1(super.subList(fromIndex, toIndex));
              }
              public ListIterator listIterator() {
                  return new TestIter(super.listIterator());
              }
              public ListIterator listIterator(int index) {
                  return new TestIter(super.listIterator(index));
              }
          }

          class TestIter implements ListIterator {
              private ListIterator iter;
              public TestIter (ListIterator iter) {
                  this.iter = iter;
              }
              public boolean hasNext() {
                  return iter.hasNext();
              }
              public Object next() {
                  return iter.next();
              }
              public boolean hasPrevious() {
                  return iter.hasPrevious();
              }
              public Object previous() {
                  return iter.previous();
              }
              public int nextIndex() {
                  return iter.nextIndex();
              }
              public int previousIndex() {
                  return iter.previousIndex();
              }
              public void remove() {
                  iter.remove();
              }
              public void set(Object o) {
                  throw new UnsupportedOperationException();
              }
              public void add(Object o) {
                  iter.add(o);
              }
          }

      public class t {
          public static void main(String[] args){
              Vector v = new TestList();
              Vector v1 = new TestList1();
              v.add(new Integer(1));
              v1.add(new Integer(1));

              try {
                  Collections.replaceAll(v, new Integer(3), new Integer(2));
                  System.out.println("First exception is not raised");
              } catch (UnsupportedOperationException e) {
                  System.out.println("First exception raised");
              }

              try {
                  Collections.replaceAll(v1, new Integer(1), new Integer(2));
                  System.out.println("Second exception is not raised");
              } catch (UnsupportedOperationException e) {
                  System.out.println("Second exception raised");
              }
          }
      }

      -----Output----------

      First exception is not raised
      Second exception is not raised

      ======================================================================

      Name: auR10023 Date: 09/27/2001



      The javadoc for java.util.Collections.replaceAll() method contains following
      lines:

      ...
      The "destructive" algorithms contained in this class, that is, the algorithms
      that modify the collection on which they operate, are specified to throw
      UnsupportedOperationException if the collection does not support the appropriate
      mutation primitive(s), such as the set method. These algorithms may, but are not
      required to, throw this exception if an invocation would have no effect on the
      collection. For example, invoking the sort method on an unmodifiable list that is
      already sorted may or may not throw UnsupportedOperationException.
      ...

      This tests expected UnsupportedOperationException when container does not
      contain elements which should be replaced. So, they should be updated.

       

      ======================================================================

            jjb Josh Bloch
            avusunw Avu Avu (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: