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/02/19 13:09:04 UTC

cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection TestCollectionAlgorithms.java

rwaldhoff    2003/02/19 04:09:04

  Modified:    functor/src/java/org/apache/commons/functor/core/collection
                        CollectionAlgorithms.java
               functor/src/test/org/apache/commons/functor/core/collection
                        TestCollectionAlgorithms.java
  Log:
  add inject [into] and tests
  
  Revision  Changes    Path
  1.2       +33 -7     jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/CollectionAlgorithms.java
  
  Index: CollectionAlgorithms.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/CollectionAlgorithms.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CollectionAlgorithms.java	19 Feb 2003 00:54:36 -0000	1.1
  +++ CollectionAlgorithms.java	19 Feb 2003 12:09:04 -0000	1.2
  @@ -61,11 +61,15 @@
   import java.util.Iterator;
   import java.util.NoSuchElementException;
   
  +import org.apache.commons.functor.BinaryFunction;
   import org.apache.commons.functor.UnaryFunction;
   import org.apache.commons.functor.UnaryPredicate;
   import org.apache.commons.functor.UnaryProcedure;
   
   /**
  + * Utility methods and algorithms for applying functors 
  + * to {@link Collection Collections}.
  + * 
    * @version $Revision$ $Date$
    * @author Rodney Waldhoff
    */
  @@ -91,7 +95,7 @@
        * and {@link Collection#add add} the result to a
        * new {@link Collection}.
        * 
  -     * @see #collect(java.util.Iterator,org.apache.commons.functor.UnaryFunction,java.util.Collection}
  +     * @see #collect(java.util.Iterator,org.apache.commons.functor.UnaryFunction,java.util.Collection)
        */
       public static Collection collect(Iterator iter, UnaryFunction func) {
           return collect(iter,func,new ArrayList());
  @@ -105,7 +109,7 @@
        * given {@link Collection}.
        * 
        * @return the given {@link Collection}
  -     * @see #collect(java.util.Iterator,org.apache.commons.functor.UnaryFunction}
  +     * @see #collect(java.util.Iterator,org.apache.commons.functor.UnaryFunction)
        */
       public static Collection collect(Iterator iter, UnaryFunction func, Collection col) {
           while(iter.hasNext()) {
  @@ -120,7 +124,7 @@
        * {@link Iterator Iterator} that matches the given
        * {@link UnaryPredicate UnaryPredicate}.
        * 
  -     * @see #detect(java.util.Iterator,org.apache.commons.functor.UnaryPredicate}
  +     * @see #detect(java.util.Iterator,org.apache.commons.functor.UnaryPredicate)
        */
       public static boolean contains(Iterator iter, UnaryPredicate pred) {
           while(iter.hasNext()) {
  @@ -138,7 +142,7 @@
        * {@link NoSuchElementException NoSuchElementException} if no
        * matching element can be found.
        * 
  -     * @see #detect(java.util.Iterator,org.apache.commons.functor.UnaryPredicate,java.lang.Object}
  +     * @see #detect(java.util.Iterator,org.apache.commons.functor.UnaryPredicate,java.lang.Object)
        */
       public static Object detect(Iterator iter, UnaryPredicate pred) {
           while(iter.hasNext()) {
  @@ -157,7 +161,7 @@
        * the given (possibly <code>null</code> <code>Object</code>
        * if no matching element can be found.
        * 
  -     * @see #detect(java.util.Iterator,org.apache.commons.functor.UnaryPredicate}
  +     * @see #detect(java.util.Iterator,org.apache.commons.functor.UnaryPredicate)
        */
       public static Object detect(Iterator iter, UnaryPredicate pred, Object ifNone) {
           while(iter.hasNext()) {
  @@ -178,6 +182,28 @@
           while(iter.hasNext()) {
               proc.run(iter.next());
           }
  +    }
  +
  +    /**
  +     * {@link BinaryFunction#evaluate Evaluate} the pair
  +     * <i>( previousResult, element )</i> for each element 
  +     * in the given {@link Iterator Iterator} where 
  +     * previousResult is initially <i>seed</i>, and thereafter
  +     * the result of the evaluation of the previous element
  +     * in the iterator.  Returns the result of the final
  +     * evaluation.
  +     * <p>
  +     * In code:
  +     * <pre>while(iter.hasNext()) { 
  +     *   seed = func.evaluate(seed,iter.next());
  +     * }
  +     * return seed;</pre>
  +     */
  +    public static Object inject(Iterator iter, Object seed, BinaryFunction func) {        
  +        while(iter.hasNext()) {
  +            seed = func.evaluate(seed,iter.next());
  +        }
  +        return seed;
       }
   
       /**
  
  
  
  1.2       +15 -4     jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestCollectionAlgorithms.java
  
  Index: TestCollectionAlgorithms.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestCollectionAlgorithms.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestCollectionAlgorithms.java	19 Feb 2003 00:54:37 -0000	1.1
  +++ TestCollectionAlgorithms.java	19 Feb 2003 12:09:04 -0000	1.2
  @@ -68,6 +68,7 @@
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
  +import org.apache.commons.functor.BinaryFunction;
   import org.apache.commons.functor.UnaryPredicate;
   import org.apache.commons.functor.UnaryProcedure;
   import org.apache.commons.functor.adapter.LeftBoundPredicate;
  @@ -202,6 +203,18 @@
           assertEquals(evens,result);
       }    
   
  +    public void testInject() {
  +        Object result = CollectionAlgorithms.inject(
  +            list.iterator(),
  +            new Integer(0),
  +            new BinaryFunction() {
  +                public Object evaluate(Object a, Object b) {
  +                    return new Integer(((Number)a).intValue() + ((Number)b).intValue());
  +                }
  +            });
  +        assertEquals(new Integer(sum),result);
  +    }    
  +
       // Attributes
       // ------------------------------------------------------------------------
       private List list = null;    
  @@ -230,6 +243,4 @@
           }        
           public int sum = 0;
       }
  -    
  -
   }
  
  
  

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