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

Collections.rotate() doesn't raise UnsupportedOperationException

XMLWordPrintable

    • beta2
    • sparc
    • solaris_2.6
    • Verified



      Name: auR10023 Date: 01/03/2001



      The javadoc for java.util.Collections.rotate(List,int) 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:

      ------t.java-----
      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.rotate(v,1);
                  System.out.println("First exception is not raised");
              } catch (UnsupportedOperationException e) {
                  System.out.println("First exception raised");
              }

              try {
                  Collections.rotate(v,1);
                  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

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

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: