You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/11/02 18:26:36 UTC

cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/iterators UnmodifiableListIterator.java UnmodifiableIterator.java

scolebourne    2003/11/02 09:26:36

  Modified:    collections/src/test/org/apache/commons/collections/iterators
                        TestAll.java
  Added:       collections/src/test/org/apache/commons/collections/iterators
                        TestUnmodifiableIterator.java
                        TestUnmodifiableListIterator.java
               collections/src/java/org/apache/commons/collections/iterators
                        UnmodifiableListIterator.java
                        UnmodifiableIterator.java
  Log:
  Add UnmodifiableIterator classes to iterators package
  
  Revision  Changes    Path
  1.8       +4 -2      jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestAll.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestAll.java	5 Oct 2003 21:26:46 -0000	1.7
  +++ TestAll.java	2 Nov 2003 17:26:36 -0000	1.8
  @@ -92,6 +92,8 @@
           suite.addTest(TestSingletonIterator.suite());
           suite.addTest(TestSingletonListIterator.suite());
           suite.addTest(TestUniqueFilterIterator.suite());
  +        suite.addTest(TestUnmodifiableIterator.suite());
  +        suite.addTest(TestUnmodifiableListIterator.suite());
           return suite;
       }
           
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestUnmodifiableIterator.java
  
  Index: TestUnmodifiableIterator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestUnmodifiableIterator.java,v 1.1 2003/11/02 17:26:36 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.iterators;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.Iterator;
  import java.util.List;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.collections.Unmodifiable;
  
  /**
   * Tests the UnmodifiableIterator.
   * 
   * @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:36 $
   * 
   * @author Stephen Colebourne
   */
  public class TestUnmodifiableIterator extends AbstractTestIterator {
  
      protected String[] testArray = { "One", "Two", "Three" };
      protected List testList = new ArrayList(Arrays.asList(testArray));
  
      public static Test suite() {
          return new TestSuite(TestUnmodifiableIterator.class);
      }
  
      public TestUnmodifiableIterator(String testName) {
          super(testName);
      }
  
      public Iterator makeEmptyIterator() {
          return UnmodifiableIterator.decorate(Collections.EMPTY_LIST.iterator());
      }
  
      public Iterator makeFullIterator() {
          return UnmodifiableIterator.decorate(testList.iterator());
      }
  
      public boolean supportsRemove() {
          return false;
      }
  
      //-----------------------------------------------------------------------
      public void testIterator() {
          assertTrue(makeEmptyIterator() instanceof Unmodifiable);
      }
      
      public void testDecorateFactory() {
          Iterator it = makeFullIterator();
          assertSame(it, UnmodifiableIterator.decorate(it));
          
          it = testList.iterator();
          assertTrue(it != UnmodifiableIterator.decorate(it));
          
          try {
              UnmodifiableIterator.decorate(null);
              fail();
          } catch (IllegalArgumentException ex) {}
      }
  
  }
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestUnmodifiableListIterator.java
  
  Index: TestUnmodifiableListIterator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestUnmodifiableListIterator.java,v 1.1 2003/11/02 17:26:36 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.iterators;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.List;
  import java.util.ListIterator;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.collections.Unmodifiable;
  
  /**
   * Tests the UnmodifiableListIterator.
   * 
   * @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:36 $
   * 
   * @author Stephen Colebourne
   */
  public class TestUnmodifiableListIterator extends AbstractTestListIterator {
  
      protected String[] testArray = { "One", "Two", "Three" };
      protected List testList = new ArrayList(Arrays.asList(testArray));
  
      public static Test suite() {
          return new TestSuite(TestUnmodifiableListIterator.class);
      }
  
      public TestUnmodifiableListIterator(String testName) {
          super(testName);
      }
  
      public ListIterator makeEmptyListIterator() {
          return UnmodifiableListIterator.decorate(Collections.EMPTY_LIST.listIterator());
      }
  
      public ListIterator makeFullListIterator() {
          return UnmodifiableListIterator.decorate(testList.listIterator());
      }
  
      public boolean supportsRemove() {
          return false;
      }
  
      public boolean supportsAdd() {
          return false;
      }
  
      public boolean supportsSet() {
          return false;
      }
  
      //-----------------------------------------------------------------------
      public void testListIterator() {
          assertTrue(makeEmptyListIterator() instanceof Unmodifiable);
      }
      
      public void testDecorateFactory() {
          ListIterator it = makeFullListIterator();
          assertSame(it, UnmodifiableListIterator.decorate(it));
          
          it = testList.listIterator();
          assertTrue(it != UnmodifiableListIterator.decorate(it));
          
          try {
              UnmodifiableListIterator.decorate(null);
              fail();
          } catch (IllegalArgumentException ex) {}
      }
  
  }
  
  
  
  1.1                  jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/UnmodifiableListIterator.java
  
  Index: UnmodifiableListIterator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/UnmodifiableListIterator.java,v 1.1 2003/11/02 17:26:36 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.iterators;
  
  import java.util.ListIterator;
  
  import org.apache.commons.collections.Unmodifiable;
  
  /** 
   * Decorates a list iterator such that it cannot be modified.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:36 $
   * 
   * @author Stephen Colebourne
   */
  public final class UnmodifiableListIterator implements ListIterator, Unmodifiable {
  
      /** The iterator being decorated */
      private ListIterator iterator;
  
      //-----------------------------------------------------------------------
      /**
       * Decorates the specified iterator such that it cannot be modified.
       *
       * @param iterator  the iterator to decoarate
       * @throws IllegalArgumentException if the iterator is null
       */
      public static ListIterator decorate(ListIterator iterator) {
          if (iterator == null) {
              throw new IllegalArgumentException("ListIterator must not be null");
          }
          if (iterator instanceof Unmodifiable) {
              return iterator;
          }
          return new UnmodifiableListIterator(iterator);
      }
      
      //-----------------------------------------------------------------------
      /**
       * Constructor.
       *
       * @param iterator  the iterator to decoarate
       */
      protected UnmodifiableListIterator(ListIterator iterator) {
          super();
          this.iterator = iterator;
      }
  
      //-----------------------------------------------------------------------
      public boolean hasNext() {
          return iterator.hasNext();
      }
  
      public Object next() {
          return iterator.next();
      }
  
      public int nextIndex() {
          return iterator.nextIndex();
      }
  
      public boolean hasPrevious() {
          return iterator.hasPrevious();
      }
  
      public Object previous() {
          return iterator.previous();
      }
  
      public int previousIndex() {
          return iterator.previousIndex();
      }
  
      public void remove() {
          throw new UnsupportedOperationException("remove() is not supported");
      }
  
      public void set(Object obj) {
          throw new UnsupportedOperationException("set() is not supported");
      }
  
      public void add(Object obj) {
          throw new UnsupportedOperationException("add() is not supported");
      }
  
  }
  
  
  
  1.1                  jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/UnmodifiableIterator.java
  
  Index: UnmodifiableIterator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/iterators/UnmodifiableIterator.java,v 1.1 2003/11/02 17:26:36 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.iterators;
  
  import java.util.Iterator;
  
  import org.apache.commons.collections.Unmodifiable;
  
  /** 
   * Decorates an iterator such that it cannot be modified.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:36 $
   * 
   * @author Stephen Colebourne
   */
  public final class UnmodifiableIterator implements Iterator, Unmodifiable {
  
      /** The iterator being decorated */
      private Iterator iterator;
  
      //-----------------------------------------------------------------------
      /**
       * Decorates the specified iterator such that it cannot be modified.
       * <p>
       * If the iterator is already unmodifiable it is returned directly.
       *
       * @param iterator  the iterator to decoarate
       * @throws IllegalArgumentException if the iterator is null
       */
      public static Iterator decorate(Iterator iterator) {
          if (iterator == null) {
              throw new IllegalArgumentException("Iterator must not be null");
          }
          if (iterator instanceof Unmodifiable) {
              return iterator;
          }
          return new UnmodifiableIterator(iterator);
      }
      
      //-----------------------------------------------------------------------
      /**
       * Constructor.
       *
       * @param iterator  the iterator to decoarate
       */
      protected UnmodifiableIterator(Iterator iterator) {
          super();
          this.iterator = iterator;
      }
  
      //-----------------------------------------------------------------------
      public boolean hasNext() {
          return iterator.hasNext();
      }
  
      public Object next() {
          return iterator.next();
      }
  
      public void remove() {
          throw new UnsupportedOperationException("remove() is not supported");
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org