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

cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection TestTransformedIterator.java TestFilteredIterator.java TestAll.java TestPredicatedIterator.java

rwaldhoff    2003/11/25 11:02:42

  Modified:    functor/src/test/org/apache/commons/functor/core/collection
                        TestAll.java
  Added:       functor/src/java/org/apache/commons/functor/core/collection
                        FilteredIterator.java TransformedIterator.java
               functor/src/test/org/apache/commons/functor/core/collection
                        TestTransformedIterator.java
                        TestFilteredIterator.java
  Removed:     functor/src/java/org/apache/commons/functor/core/collection
                        PredicatedIterator.java
               functor/src/test/org/apache/commons/functor/core/collection
                        TestPredicatedIterator.java
  Log:
  * rename PredicatedIterator to FilteredIterator
  * add functions and teststo FilteredIterator
  * add TransformedIterator and tests
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/FilteredIterator.java
  
  Index: FilteredIterator.java
  ===================================================================
  /* 
   * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/FilteredIterator.java,v 1.1 2003/11/25 19:02:42 rwaldhoff Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments 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 name, 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.functor.core.collection;
  
  import java.util.Iterator;
  import java.util.NoSuchElementException;
  
  import org.apache.commons.functor.UnaryPredicate;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/11/25 19:02:42 $
   * @author Rodney Waldhoff
   */
  public final class FilteredIterator implements Iterator {
  
      // constructor
      // ------------------------------------------------------------------------
      
      public FilteredIterator(UnaryPredicate predicate, Iterator iterator) {
          if(null == iterator || null == predicate) {
              throw new NullPointerException();
          } else {
              this.predicate = predicate;
              this.iterator = iterator;
          }
      }
      
      // iterator methods
      // ------------------------------------------------------------------------
      
      /**
       * @see java.util.Iterator#hasNext()
       */
      public boolean hasNext() {
          if(nextSet) {
              return true;
          } else {
              return setNext();
          }
      }
  
      /**
       * @see java.util.Iterator#next()
       */
      public Object next() {
          if(hasNext()) {            
              return returnNext();
          } else {
              throw new NoSuchElementException();
          }
      }
  
      /**
       * @see java.util.Iterator#remove()
       */
      public void remove() {
          if(canRemove) {
              canRemove = false;
              iterator.remove();
          } else {
              throw new IllegalStateException();
          }
      }
      
  
      public boolean equals(Object obj) {
          if(obj instanceof FilteredIterator) {
              FilteredIterator that = (FilteredIterator)obj;
              return predicate.equals(that.predicate) && iterator.equals(that.iterator);  
          } else {
              return false;
          }
      }
  
      public int hashCode() {
          int hash = "FilteredIterator".hashCode();
          hash <<= 2;
          hash ^= predicate.hashCode();
          hash <<= 2;
          hash ^= iterator.hashCode();
          return hash;
      }
  
      public String toString() {
          return "FilteredIterator<" + predicate + "," + iterator + ">";
      }
      
      // class methods
      // ------------------------------------------------------------------------
      
      public static Iterator filter(UnaryPredicate pred, Iterator iter) {
          return null == pred ? iter : (null == iter ? null : new FilteredIterator(pred,iter));
      }
   
      // private
      // ------------------------------------------------------------------------
      
      private boolean setNext() {
          while(iterator.hasNext()) {
              canRemove = false;
              Object obj = iterator.next();
              if(predicate.test(obj)) {
                  next = obj;
                  nextSet = true;
                  return true;
              }
          }
          next = null;
          nextSet = false;
          return false;
      }
   
      private Object returnNext() {
          Object temp = next;
          canRemove = true;
          next = null;
          nextSet = false;
          return temp;
      }
   
      // attributes
      // ------------------------------------------------------------------------
      
      private UnaryPredicate predicate = null;
      private Iterator iterator = null;
      private Object next = null;
      private boolean nextSet = false;
      private boolean canRemove = false;
      
  
  }
  
  
  
  1.1                  jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/TransformedIterator.java
  
  Index: TransformedIterator.java
  ===================================================================
  /* 
   * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/TransformedIterator.java,v 1.1 2003/11/25 19:02:42 rwaldhoff Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments 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 name, 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.functor.core.collection;
  
  import java.util.Iterator;
  
  import org.apache.commons.functor.UnaryFunction;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/11/25 19:02:42 $
   * @author Rodney Waldhoff
   */
  public final class TransformedIterator implements Iterator {
  
      // constructor
      // ------------------------------------------------------------------------
      
      public TransformedIterator(UnaryFunction function, Iterator iterator) {
          if(null == iterator || null == function) {
              throw new NullPointerException();
          } else {
              this.function = function;
              this.iterator = iterator;
          }
      }
      
      // iterator methods
      // ------------------------------------------------------------------------
      
      /**
       * @see java.util.Iterator#hasNext()
       */
      public boolean hasNext() {
          return iterator.hasNext();
      }
  
      /**
       * @see java.util.Iterator#next()
       */
      public Object next() {
          return function.evaluate(iterator.next());
      }
  
      /**
       * @see java.util.Iterator#remove()
       */
      public void remove() {
          iterator.remove();
      }
  
      public boolean equals(Object obj) {
          if(obj instanceof TransformedIterator) {
              TransformedIterator that = (TransformedIterator)obj;
              return function.equals(that.function) && iterator.equals(that.iterator);  
          } else {
              return false;
          }
      }
  
      public int hashCode() {
          int hash = "TransformedIterator".hashCode();
          hash <<= 2;
          hash ^= function.hashCode();
          hash <<= 2;
          hash ^= iterator.hashCode();
          return hash;
      }
  
      public String toString() {
          return "TransformedIterator<" + function + "," + iterator + ">";
      }
      
      // class methods
      // ------------------------------------------------------------------------
      
      public static Iterator transform(UnaryFunction func, Iterator iter) {
          return null == func ? iter : (null == iter ? null : new TransformedIterator(func,iter));
      }
   
   
      // attributes
      // ------------------------------------------------------------------------
      
      private UnaryFunction function = null;
      private Iterator iterator = null;
      
  
  }
  
  
  
  1.4       +4 -3      jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestAll.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestAll.java	29 Jun 2003 21:46:12 -0000	1.3
  +++ TestAll.java	25 Nov 2003 19:02:42 -0000	1.4
  @@ -73,7 +73,8 @@
           TestSuite suite = new TestSuite();
           
           suite.addTest(TestCollectionAlgorithms.suite());
  -        suite.addTest(TestPredicatedIterator.suite());
  +        suite.addTest(TestFilteredIterator.suite());
  +        suite.addTest(TestTransformedIterator.suite());
           suite.addTest(TestIsEmpty.suite());
           suite.addTest(TestIsElementOf.suite());
           suite.addTest(TestSize.suite());
  
  
  
  1.1                  jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestTransformedIterator.java
  
  Index: TestTransformedIterator.java
  ===================================================================
  /* 
   * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestTransformedIterator.java,v 1.1 2003/11/25 19:02:42 rwaldhoff Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments 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 name, 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.functor.core.collection;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Iterator;
  import java.util.List;
  import java.util.NoSuchElementException;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.functor.BaseFunctorTest;
  import org.apache.commons.functor.UnaryFunction;
  import org.apache.commons.functor.core.IdentityFunction;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/11/25 19:02:42 $
   * @author Rodney Waldhoff
   */
  public class TestTransformedIterator extends BaseFunctorTest {
  
      // Conventional
      // ------------------------------------------------------------------------
  
      public TestTransformedIterator(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestTransformedIterator.class);
      }
      
      public Object makeFunctor() {
          List list = new ArrayList();
          list.add("xyzzy");        
          return TransformedIterator.transform(IdentityFunction.instance(),list.iterator());
      }
  
      // Lifecycle
      // ------------------------------------------------------------------------
  
      public void setUp() throws Exception {
          super.setUp();
          list = new ArrayList();
          negatives = new ArrayList();
          for(int i=0;i<10;i++) {
              list.add(new Integer(i));
              negatives.add(new Integer(i*-1));
          }
      }
  
      public void tearDown() throws Exception {
          super.tearDown();
          list = null;
          negatives = null;
      }
  
      // Tests
      // ------------------------------------------------------------------------
      
      public void testBasicTransform() {
          Iterator expected = negatives.iterator();
          Iterator testing = new TransformedIterator(negate,list.iterator());
          while(expected.hasNext()) {
              assertTrue(testing.hasNext());
              assertEquals(expected.next(),testing.next());
          }
          assertTrue(!testing.hasNext());
      }
  
      public void testEmptyList() {
          Iterator testing = new TransformedIterator(negate,Collections.EMPTY_LIST.iterator());
          assertTrue(!testing.hasNext());
      }
  
      public void testNextWithoutHasNext() {
          Iterator testing = new TransformedIterator(negate,list.iterator());
          Iterator expected = negatives.iterator();
          while(expected.hasNext()) {
              assertEquals(expected.next(),testing.next());
          }
          assertTrue(!(testing.hasNext()));
      }
  
      public void testNextAfterEndOfList() {
          Iterator testing = new TransformedIterator(negate,list.iterator());
          Iterator expected = negatives.iterator();
          while(expected.hasNext()) {
              assertEquals(expected.next(),testing.next());
          }
          try {
              testing.next();
              fail("ExpectedNoSuchElementException");
          } catch(NoSuchElementException e) {
              // expected
          }
      }
  
      public void testNextOnEmptyList() {
          Iterator testing = new TransformedIterator(negate,Collections.EMPTY_LIST.iterator());
          try {
              testing.next();
              fail("ExpectedNoSuchElementException");
          } catch(NoSuchElementException e) {
              // expected
          }
      }
      
      public void testRemoveBeforeNext() {
          Iterator testing = new TransformedIterator(negate,list.iterator());
          try {
              testing.remove();
              fail("IllegalStateException");
          } catch(IllegalStateException e) {
              // expected
          }
      }
      
      public void testRemoveAfterNext() {
          Iterator testing = new TransformedIterator(negate,list.iterator());
          testing.next();
          testing.remove();
          try {
              testing.remove();
              fail("IllegalStateException");
          } catch(IllegalStateException e) {
              // expected
          }
      }
      
      public void testRemoveAll() {
          Iterator testing = new TransformedIterator(negate,list.iterator());
          while(testing.hasNext()) {
              testing.next();
              testing.remove();
          }
          assertTrue(list.isEmpty());
      }
  
      public void testRemoveWithoutHasNext() {
          Iterator testing = new TransformedIterator(negate,list.iterator());
          for(int i=0,m = list.size();i<m;i++) {
              testing.next();
              testing.remove();
          }
          assertTrue(list.isEmpty());
      }
      
      public void testTransformWithNullIteratorReturnsNull() {
          assertNull(TransformedIterator.transform(negate,null));
      }
      
      public void testTransformWithNullPredicateReturnsIdentity() {
          Iterator iter = list.iterator();
          assertSame(iter,TransformedIterator.transform(null,iter));
      }
  
      public void testConstructorProhibitsNull() {
          try {
              new TransformedIterator(null,null);
              fail("ExpectedNullPointerException");
          } catch(NullPointerException e) {
              // expected
          }
          try {
              new TransformedIterator(negate,null);
              fail("ExpectedNullPointerException");
          } catch(NullPointerException e) {
              // expected
          }
          try {
              new TransformedIterator(null,list.iterator());
              fail("ExpectedNullPointerException");
          } catch(NullPointerException e) {
              // expected
          }
      }
      
  
      // Attributes
      // ------------------------------------------------------------------------
      private List list = null;    
      private List negatives = null;
      private UnaryFunction negate = new UnaryFunction() { 
          public Object evaluate(Object obj) {
              return new Integer(((Number)obj).intValue() * -1);
          }
      };
      
  }
  
  
  
  1.1                  jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestFilteredIterator.java
  
  Index: TestFilteredIterator.java
  ===================================================================
  /* 
   * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestFilteredIterator.java,v 1.1 2003/11/25 19:02:42 rwaldhoff Exp $
   * ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 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 acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments 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 name, 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.functor.core.collection;
  
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Iterator;
  import java.util.List;
  import java.util.NoSuchElementException;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.functor.BaseFunctorTest;
  import org.apache.commons.functor.UnaryPredicate;
  import org.apache.commons.functor.core.ConstantPredicate;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/11/25 19:02:42 $
   * @author Rodney Waldhoff
   */
  public class TestFilteredIterator extends BaseFunctorTest {
  
      // Conventional
      // ------------------------------------------------------------------------
  
      public TestFilteredIterator(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestFilteredIterator.class);
      }
  
      public Object makeFunctor() {
          List list = new ArrayList();
          list.add("xyzzy");        
          return FilteredIterator.filter(ConstantPredicate.trueInstance(),list.iterator());
      }
  
      // Lifecycle
      // ------------------------------------------------------------------------
  
      public void setUp() throws Exception {
          super.setUp();
          list = new ArrayList();
          evens = new ArrayList();
          for(int i=0;i<10;i++) {
              list.add(new Integer(i));
              if(i%2 == 0) {
                  evens.add(new Integer(i));
              }
          }
      }
  
      public void tearDown() throws Exception {
          super.tearDown();
          list = null;
          evens = null;
      }
  
      // Tests
      // ------------------------------------------------------------------------
      
      public void testSomePass() {
          Iterator expected = evens.iterator();
          Iterator testing = new FilteredIterator(isEven,list.iterator());
          while(expected.hasNext()) {
              assertTrue(testing.hasNext());
              assertEquals(expected.next(),testing.next());
          }
          assertTrue(!testing.hasNext());
      }
  
      public void testAllPass() {
          Iterator expected = evens.iterator();
          Iterator testing = new FilteredIterator(isEven,evens.iterator());
          while(expected.hasNext()) {
              assertTrue(testing.hasNext());
              assertEquals(expected.next(),testing.next());
          }
          assertTrue(!testing.hasNext());
      }
  
      public void testAllPass2() {
          Iterator expected = list.iterator();
          Iterator testing = new FilteredIterator(ConstantPredicate.trueInstance(),list.iterator());
          while(expected.hasNext()) {
              assertTrue(testing.hasNext());
              assertEquals(expected.next(),testing.next());
          }
          assertTrue(!testing.hasNext());
      }
  
      public void testEmptyList() {
          Iterator testing = new FilteredIterator(isEven,Collections.EMPTY_LIST.iterator());
          assertTrue(!testing.hasNext());
      }
  
      public void testNonePass() {
          Iterator testing = new FilteredIterator(ConstantPredicate.falseInstance(),Collections.EMPTY_LIST.iterator());
          assertTrue(!testing.hasNext());
      }
  
      public void testNextWithoutHasNext() {
          Iterator testing = new FilteredIterator(isEven,list.iterator());
          Iterator expected = evens.iterator();
          while(expected.hasNext()) {
              assertEquals(expected.next(),testing.next());
          }
          assertTrue(!(testing.hasNext()));
      }
  
      public void testNextAfterEndOfList() {
          Iterator testing = new FilteredIterator(isEven,list.iterator());
          Iterator expected = evens.iterator();
          while(expected.hasNext()) {
              assertEquals(expected.next(),testing.next());
          }
          try {
              testing.next();
              fail("ExpectedNoSuchElementException");
          } catch(NoSuchElementException e) {
              // expected
          }
      }
  
      public void testNextOnEmptyList() {
          Iterator testing = new FilteredIterator(isEven,Collections.EMPTY_LIST.iterator());
          try {
              testing.next();
              fail("ExpectedNoSuchElementException");
          } catch(NoSuchElementException e) {
              // expected
          }
      }
      
      public void testRemoveBeforeNext() {
          Iterator testing = new FilteredIterator(isEven,list.iterator());
          try {
              testing.remove();
              fail("IllegalStateException");
          } catch(IllegalStateException e) {
              // expected
          }
      }
      
      public void testRemoveAfterNext() {
          Iterator testing = new FilteredIterator(isEven,list.iterator());
          testing.next();
          testing.remove();
          try {
              testing.remove();
              fail("IllegalStateException");
          } catch(IllegalStateException e) {
              // expected
          }
      }
      
      public void testRemoveSome() {
          Iterator testing = new FilteredIterator(isEven,list.iterator());
          while(testing.hasNext()) {
              testing.next();
              testing.remove();
          }
          for(Iterator iter = list.iterator(); iter.hasNext();) {
              assertTrue(! isEven.test(iter.next()) );
          }
      }
  
      public void testRemoveAll() {
          Iterator testing = new FilteredIterator(ConstantPredicate.trueInstance(),list.iterator());
          while(testing.hasNext()) {
              testing.next();
              testing.remove();
          }
          assertTrue(list.isEmpty());
      }
  
      public void testRemoveWithoutHasNext() {
          Iterator testing = new FilteredIterator(ConstantPredicate.trueInstance(),list.iterator());
          for(int i=0,m = list.size();i<m;i++) {
              testing.next();
              testing.remove();
          }
          assertTrue(list.isEmpty());
      }
      
      public void testFilterWithNullIteratorReturnsNull() {
          assertNull(FilteredIterator.filter(ConstantPredicate.trueInstance(),null));
      }
      
      public void testFilterWithNullPredicateReturnsIdentity() {
          Iterator iter = list.iterator();
          assertSame(iter,FilteredIterator.filter(null,iter));
      }
  
      public void testConstructorProhibitsNull() {
          try {
              new FilteredIterator(null,null);
              fail("ExpectedNullPointerException");
          } catch(NullPointerException e) {
              // expected
          }
          try {
              new FilteredIterator(ConstantPredicate.trueInstance(),null);
              fail("ExpectedNullPointerException");
          } catch(NullPointerException e) {
              // expected
          }
          try {
              new FilteredIterator(null,list.iterator());
              fail("ExpectedNullPointerException");
          } catch(NullPointerException e) {
              // expected
          }
      }
      
  
      // Attributes
      // ------------------------------------------------------------------------
      private List list = null;    
      private List evens = null;
      private UnaryPredicate isEven = new UnaryPredicate() { 
          public boolean test(Object obj) {
              return ((Number)obj).intValue() % 2 == 0;
          }
      };
      
  }
  
  
  

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