rwaldhoff    2003/11/25 11:21:43

  Modified:    functor/src/java/org/apache/commons/functor Algorithms.java
               functor/src/test/org/apache/commons/functor
                        TestAlgorithms.java
  Log:
  iterate lazily, using TransformedIterator and FilteredIterator
  
  Revision  Changes    Path
  1.10      +27 -24    
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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Algorithms.java   25 Nov 2003 18:34:13 -0000      1.9
  +++ Algorithms.java   25 Nov 2003 19:21:43 -0000      1.10
  @@ -60,6 +60,9 @@
   import java.util.Iterator;
   import java.util.NoSuchElementException;
   
  +import org.apache.commons.functor.core.collection.FilteredIterator;
  +import org.apache.commons.functor.core.collection.TransformedIterator;
  +import org.apache.commons.functor.core.composite.UnaryNot;
   import org.apache.commons.functor.generator.BaseGenerator;
   import org.apache.commons.functor.generator.Generator;
   import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
  @@ -87,11 +90,11 @@
   public class Algorithms {
   
       /**
  -     * Equivalent to 
  -     * <code>[EMAIL PROTECTED] #apply(Generator,UnaryFunction) apply}(new [EMAIL 
PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter 
IteratorToGeneratorAdapter}(iter),func).toCollection().iterator()</code>.
  +     * Returns an [EMAIL PROTECTED] Iterator} that will apply the given [EMAIL 
PROTECTED] UnaryFunction} to each
  +     * element when accessed.
        */
       public static final Iterator apply(Iterator iter, UnaryFunction func) {
  -        return apply(new 
IteratorToGeneratorAdapter(iter),func).toCollection().iterator();
  +        return TransformedIterator.transform(iter,func);
       }
   
       /**
  @@ -259,13 +262,12 @@
           return result[0];
       }
   
  -
       /**
  -     * Equivalent to 
  -     * <code>[EMAIL PROTECTED] #reject(Generator,UnaryPredicate) reject}(new [EMAIL 
PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter 
IteratorToGeneratorAdapter}(iter),pred).toCollection().iterator()</code>.
  +     * Returns an [EMAIL PROTECTED] Iterator} that will only return elements that DO
  +     * NOT match the given predicate.
        */
       public static Iterator reject(Iterator iter, UnaryPredicate pred) {
  -        return reject(new IteratorToGeneratorAdapter(iter), 
pred).toCollection().iterator();
  +        return FilteredIterator.filter(iter,UnaryNot.not(pred));
       }
   
       /**
  @@ -287,11 +289,11 @@
       }
   
       /**
  -     * Equivalent to 
  -     * <code>[EMAIL PROTECTED] #select(Generator,UnaryPredicate) select}(new [EMAIL 
PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter 
IteratorToGeneratorAdapter}(iter),pred).toCollection().iterator()</code>.
  +     * Returns an [EMAIL PROTECTED] Iterator} that will only return elements that DO
  +     * match the given predicate.
        */
       public static final Iterator select(Iterator iter, UnaryPredicate pred) {
  -        return select(new IteratorToGeneratorAdapter(iter), 
pred).toCollection().iterator();
  +        return FilteredIterator.filter(iter,pred);
       }
   
       /**
  @@ -314,24 +316,24 @@
   
       /**
        * Equivalent to 
  -     * <code>[EMAIL PROTECTED] #until(Generator,UnaryPredicate) until}(new [EMAIL 
PROTECTED] org.apache.commons.functor.generator.IteratorToGeneratorAdapter 
IteratorToGeneratorAdapter}(iter),pred).toCollection().iterator()</code>.
  +     * <code>[EMAIL PROTECTED] #reject(Iterator,UnaryPredicate) 
reject}(iter,pred)</code>.
        */
  -    public static final Iterator until(Iterator iter, UnaryPredicate pred) {
  -        return until(new IteratorToGeneratorAdapter(iter), 
pred).toCollection().iterator();
  +    public static final Iterator until(final Iterator iter, final UnaryPredicate 
pred) {
  +        return reject(iter,pred);
       }
   
       /**
  -     * Returns a [EMAIL PROTECTED] Generator} that will stop when the predicate 
becomes
  -     * true. This is useful for imposing [EMAIL PROTECTED] Generator} limits. For 
example:
  -     *
  -     * <pre>
  -     *  EachLine.open(file).until(new Offset(1));
  -     * </pre>
  -     *
  -     * Would only "generate" 1 line from the file before [EMAIL PROTECTED] 
Generator#stop
  -     * stopping} the generator.
  +     * Equivalent to 
  +     * <code>[EMAIL PROTECTED] #reject(Generator,UnaryPredicate) 
reject}(gen,pred)</code>.
        */
       public static final Generator until(final Generator gen, final UnaryPredicate 
pred) {
  +        return reject(gen,pred);
  +        // here's the old version of this code
  +        // reject(gen,pred) doesn't call stop()
  +        // like this version does.
  +        // should reject call stop?  
  +        // is the stop call signficant here?
  +        /*
           return new BaseGenerator(gen) {
               public void run(final UnaryProcedure proc) {
                   gen.run(new UnaryProcedure() {
  @@ -345,6 +347,7 @@
                   });
               }
           };
  +        */
       }
   
       /**
  
  
  
  1.6       +2 -3      
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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestAlgorithms.java       25 Nov 2003 18:34:13 -0000      1.5
  +++ TestAlgorithms.java       25 Nov 2003 19:21:43 -0000      1.6
  @@ -218,7 +218,6 @@
   
       public void testLimit() {
           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]

Reply via email to