rwaldhoff 2003/11/25 10:34:14 Modified: functor/src/java/org/apache/commons/functor Algorithms.java functor/src/test/org/apache/commons/functor TestAlgorithms.java Log: when given an iterator, return an iterator Revision Changes Path 1.9 +24 -18 jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/Algorithms.java Index: Algorithms.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/Algorithms.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- Algorithms.java 25 Nov 2003 18:22:50 -0000 1.8 +++ Algorithms.java 25 Nov 2003 18:34:13 -0000 1.9 @@ -67,9 +67,9 @@ /** * Utility methods and algorithms for applying functors to [EMAIL PROTECTED] Generator}s. * [EMAIL PROTECTED] Generator}s also define these utility methods as instance methods. The - * [EMAIL PROTECTED] #apply}, [EMAIL PROTECTED] #select}, and [EMAIL PROTECTED] #reject} methods return new - * Generators. This becomes useful for constructing nested expressions. For - * example: + * [EMAIL PROTECTED] #apply apply}, [EMAIL PROTECTED] #select select}, and [EMAIL PROTECTED] #reject reject} methods + * return new Generators. This becomes useful for constructing nested expressions. + * For example: * * <pre> * Algorithms.apply(new EachElement(list), func1) @@ -82,14 +82,16 @@ * @since 1.0 * @version $Revision$ $Date$ * @author Jason Horman ([EMAIL PROTECTED]) + * @author Rodney Waldhoff */ public class Algorithms { /** - * Equivalent to <code>[EMAIL PROTECTED] #apply(Generator,UnaryFunction) apply}(new [EMAIL PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),func)</code>. + * Equivalent to + * <code>[EMAIL PROTECTED] #apply(Generator,UnaryFunction) apply}(new [EMAIL PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),func).toCollection().iterator()</code>. */ - public static final Generator apply(Iterator iter, UnaryFunction func) { - return apply(new IteratorToGeneratorAdapter(iter),func); + public static final Iterator apply(Iterator iter, UnaryFunction func) { + return apply(new IteratorToGeneratorAdapter(iter),func).toCollection().iterator(); } /** @@ -109,7 +111,8 @@ } /** - * Equivalent to <code>[EMAIL PROTECTED] #contains(Generator,UnaryPredicate) contains}(new [EMAIL PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred)</code>. + * Equivalent to + * <code>[EMAIL PROTECTED] #contains(Generator,UnaryPredicate) contains}(new [EMAIL PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred)</code>. */ public static final boolean contains(Iterator iter, UnaryPredicate pred) { return contains(new IteratorToGeneratorAdapter(iter),pred); @@ -258,10 +261,11 @@ /** - * Equivalent to <code>[EMAIL PROTECTED] #reject(Generator,UnaryPredicate) reject}(new [EMAIL PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred)</code>. + * Equivalent to + * <code>[EMAIL PROTECTED] #reject(Generator,UnaryPredicate) reject}(new [EMAIL PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred).toCollection().iterator()</code>. */ - public static Generator reject(Iterator iter, UnaryPredicate pred) { - return reject(new IteratorToGeneratorAdapter(iter), pred); + public static Iterator reject(Iterator iter, UnaryPredicate pred) { + return reject(new IteratorToGeneratorAdapter(iter), pred).toCollection().iterator(); } /** @@ -283,10 +287,11 @@ } /** - * Equivalent to <code>[EMAIL PROTECTED] #select(Generator,UnaryPredicate) select}(new [EMAIL PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred)</code>. + * Equivalent to + * <code>[EMAIL PROTECTED] #select(Generator,UnaryPredicate) select}(new [EMAIL PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred).toCollection().iterator()</code>. */ - public static final Generator select(Iterator iter, UnaryPredicate pred) { - return select(new IteratorToGeneratorAdapter(iter), pred); + public static final Iterator select(Iterator iter, UnaryPredicate pred) { + return select(new IteratorToGeneratorAdapter(iter), pred).toCollection().iterator(); } /** @@ -308,10 +313,11 @@ } /** - * Equivalent to <code>[EMAIL PROTECTED] #until(Generator,UnaryPredicate) until}(new [EMAIL PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred)</code>. + * Equivalent to + * <code>[EMAIL PROTECTED] #until(Generator,UnaryPredicate) until}(new [EMAIL PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred).toCollection().iterator()</code>. */ - public static final Generator until(Iterator iter, UnaryPredicate pred) { - return until(new IteratorToGeneratorAdapter(iter), pred); + public static final Iterator until(Iterator iter, UnaryPredicate pred) { + return until(new IteratorToGeneratorAdapter(iter), pred).toCollection().iterator(); } /** 1.5 +11 -10 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/TestAlgorithms.java Index: TestAlgorithms.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/TestAlgorithms.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- TestAlgorithms.java 25 Nov 2003 17:49:35 -0000 1.4 +++ TestAlgorithms.java 25 Nov 2003 18:34:13 -0000 1.5 @@ -72,6 +72,7 @@ import org.apache.commons.functor.core.IdentityFunction; import org.apache.commons.functor.core.IsEqual; import org.apache.commons.functor.core.Offset; +import org.apache.commons.functor.generator.IteratorToGeneratorAdapter; /** * @version $Revision$ $Date$ @@ -129,7 +130,7 @@ } public void testApply() { - Collection result = Algorithms.apply(list.iterator(),IdentityFunction.instance()).toCollection(); + Collection result = IteratorToGeneratorAdapter.adapt(Algorithms.apply(list.iterator(),IdentityFunction.instance())).toCollection(); assertNotNull(result); assertEquals(list.size(),result.size()); assertEquals(list,result); @@ -137,7 +138,7 @@ public void testApply2() { Set set = new HashSet(); - assertSame(set,Algorithms.apply(list.iterator(),IdentityFunction.instance()).to(set)); + assertSame(set,IteratorToGeneratorAdapter.adapt(Algorithms.apply(list.iterator(),IdentityFunction.instance())).to(set)); assertEquals(list.size(),set.size()); for(Iterator iter = list.iterator(); iter.hasNext(); ) { assertTrue(set.contains(iter.next())); @@ -146,7 +147,7 @@ public void testApply3() { Set set = new HashSet(); - assertSame(set,Algorithms.apply(listWithDuplicates.iterator(),IdentityFunction.instance()).to(set)); + assertSame(set,IteratorToGeneratorAdapter.adapt(Algorithms.apply(listWithDuplicates.iterator(),IdentityFunction.instance())).to(set)); assertTrue(listWithDuplicates.size() > set.size()); for(Iterator iter = listWithDuplicates.iterator(); iter.hasNext(); ) { assertTrue(set.contains(iter.next())); @@ -180,26 +181,26 @@ } public void testSelect1() { - Collection result = Algorithms.select(list.iterator(),isEven).toCollection(); + Collection result = IteratorToGeneratorAdapter.adapt(Algorithms.select(list.iterator(),isEven)).toCollection(); assertNotNull(result); assertEquals(evens,result); } public void testSelect2() { ArrayList result = new ArrayList(); - assertSame(result,Algorithms.select(list.iterator(),isEven).to(result)); + assertSame(result,IteratorToGeneratorAdapter.adapt(Algorithms.select(list.iterator(),isEven)).to(result)); assertEquals(evens,result); } public void testReject1() { - Collection result = Algorithms.reject(list.iterator(),isOdd).toCollection(); + Collection result = IteratorToGeneratorAdapter.adapt(Algorithms.reject(list.iterator(),isOdd)).toCollection(); assertNotNull(result); assertEquals(evens,result); } public void testReject2() { ArrayList result = new ArrayList(); - assertSame(result,Algorithms.reject(list.iterator(),isOdd).to(result)); + assertSame(result,IteratorToGeneratorAdapter.adapt(Algorithms.reject(list.iterator(),isOdd)).to(result)); assertEquals(evens,result); } @@ -216,7 +217,7 @@ } public void testLimit() { - Collection col = Algorithms.until(list.iterator(), new Offset(2)).toCollection(); + Collection col = IteratorToGeneratorAdapter.adapt(Algorithms.until(list.iterator(), new Offset(2))).toCollection(); System.out.println(col); assertEquals("[0, 1]", col.toString()); }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]