commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwaldh...@apache.org
Subject cvs commit: jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection CollectionAlgorithms.java
Date Tue, 25 Nov 2003 19:39:45 GMT
rwaldhoff    2003/11/25 11:39:45

  Modified:    functor/src/java/org/apache/commons/functor Algorithms.java
               functor/src/test/org/apache/commons/functor/core/collection
                        TestCollectionAlgorithms.java
               functor/src/test/org/apache/commons/functor/example
                        QuicksortExample.java
  Removed:     functor/src/java/org/apache/commons/functor/core/collection
                        CollectionAlgorithms.java
  Log:
  move functionality of CollectionAlgorithms into Algorithms, fix clients, remove CollectionAlgorithms
  
  Revision  Changes    Path
  1.11      +66 -3     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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Algorithms.java	25 Nov 2003 19:21:43 -0000	1.10
  +++ Algorithms.java	25 Nov 2003 19:39:44 -0000	1.11
  @@ -57,7 +57,10 @@
   
   package org.apache.commons.functor;
   
  +import java.util.ArrayList;
  +import java.util.Collection;
   import java.util.Iterator;
  +import java.util.ListIterator;
   import java.util.NoSuchElementException;
   
   import org.apache.commons.functor.core.collection.FilteredIterator;
  @@ -87,8 +90,68 @@
    * @author Jason Horman (jason@jhorman.org)
    * @author Rodney Waldhoff
    */
  -public class Algorithms {
  +public final class Algorithms {
   
  +    /** Public constructor for bean-ish APIs */
  +    public Algorithms() {        
  +    }
  +
  +    public static Collection collect(Iterator iter) {
  +        return collect(iter,new ArrayList());
  +    }
  +    
  +    public static Collection collect(Iterator iter, Collection col) {
  +        while(iter.hasNext()) {
  +            col.add(iter.next());
  +        }
  +        return col;
  +    }
  +    
  +    /**
  +     * {@link ListIterator#set Set} each element of the
  +     * given {@link ListIterator ListIterator} to
  +     * the result of applying the 
  +     * given {@link UnaryFunction UnaryFunction} to
  +     * its original value.
  +     */
  +    public static void transform(ListIterator iter, UnaryFunction func) {
  +        while(iter.hasNext()) {
  +            iter.set(func.evaluate(iter.next()));
  +        }
  +    }
  +
  +    /**
  +     * {@link Iterator#remove Remove} from the
  +     * given {@link Iterator Iterator} all elements 
  +     * that fail to match the
  +     * given {@link UnaryPredicate UnaryPredicate}.
  +     * 
  +     * @see #remove(Iterator,UnaryPredicate)
  +     */
  +    public static void retain(Iterator iter, UnaryPredicate pred) {
  +        while(iter.hasNext()) {
  +            if(!(pred.test(iter.next()))) {
  +                iter.remove();
  +            }
  +        }
  +    }
  +
  +    /**
  +     * {@link Iterator#remove Renmove} from the
  +     * given {@link Iterator Iterator} all elements 
  +     * that match the
  +     * given {@link UnaryPredicate UnaryPredicate}.
  +     * 
  +     * @see #retain(Iterator,UnaryPredicate)
  +     */
  +    public static void remove(Iterator iter, UnaryPredicate pred) {
  +        while(iter.hasNext()) {
  +            if(pred.test(iter.next())) {
  +                iter.remove();
  +            }
  +        }        
  +    }
  +    
       /**
        * Returns an {@link Iterator} that will apply the given {@link UnaryFunction} to each
        * element when accessed.
  
  
  
  1.9       +22 -22    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TestCollectionAlgorithms.java	24 Nov 2003 23:09:13 -0000	1.8
  +++ TestCollectionAlgorithms.java	25 Nov 2003 19:39:45 -0000	1.9
  @@ -68,12 +68,12 @@
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
  +import org.apache.commons.functor.Algorithms;
   import org.apache.commons.functor.BinaryFunction;
   import org.apache.commons.functor.UnaryFunction;
   import org.apache.commons.functor.UnaryPredicate;
   import org.apache.commons.functor.UnaryProcedure;
   import org.apache.commons.functor.adapter.LeftBoundPredicate;
  -import org.apache.commons.functor.core.IdentityFunction;
   import org.apache.commons.functor.core.IsEqual;
   
   /**
  @@ -128,11 +128,11 @@
       
       public void testHasPublicConstructor() {
           // some frameworks work best with instantiable classes
  -        assertNotNull(new CollectionAlgorithms());
  +        assertNotNull(new Algorithms());
       }    
       
       public void testCollect() {
  -        Collection result = CollectionAlgorithms.collect(list.iterator(),IdentityFunction.instance());
  +        Collection result = Algorithms.collect(list.iterator());
           assertNotNull(result);
           assertEquals(list.size(),result.size());
           assertEquals(list,result);
  @@ -140,7 +140,7 @@
   
       public void testCollect2() {
           Set set = new HashSet();
  -        assertSame(set,CollectionAlgorithms.collect(list.iterator(),IdentityFunction.instance(),set));
  +        assertSame(set,Algorithms.collect(list.iterator(),set));
           assertEquals(list.size(),set.size());
           for(Iterator iter = list.iterator(); iter.hasNext(); ) {
               assertTrue(set.contains(iter.next()));
  @@ -149,7 +149,7 @@
   
       public void testCollect3() {
           Set set = new HashSet();
  -        assertSame(set,CollectionAlgorithms.collect(listWithDuplicates.iterator(),IdentityFunction.instance(),set));
  +        assertSame(set,Algorithms.collect(listWithDuplicates.iterator(),set));
           assertTrue(listWithDuplicates.size() > set.size());
           for(Iterator iter = listWithDuplicates.iterator(); iter.hasNext(); ) {
               assertTrue(set.contains(iter.next()));
  @@ -157,14 +157,14 @@
       }    
   
       public void testContains() {
  -        assertTrue(CollectionAlgorithms.contains(list.iterator(),equalsThree));
  -        assertTrue(!CollectionAlgorithms.contains(list.iterator(),equalsTwentyThree));
  +        assertTrue(Algorithms.contains(list.iterator(),equalsThree));
  +        assertTrue(!Algorithms.contains(list.iterator(),equalsTwentyThree));
       }    
   
       public void testDetect() {
  -        assertEquals(new Integer(3),CollectionAlgorithms.detect(list.iterator(),equalsThree));
  +        assertEquals(new Integer(3),Algorithms.detect(list.iterator(),equalsThree));
           try {
  -            CollectionAlgorithms.detect(list.iterator(),equalsTwentyThree);
  +            Algorithms.detect(list.iterator(),equalsTwentyThree);
               fail("Expected NoSuchElementException");
           } catch(NoSuchElementException e) {
               // expected
  @@ -172,42 +172,42 @@
       }    
   
       public void testDetectIfNone() {
  -        assertEquals(new Integer(3),CollectionAlgorithms.detect(list.iterator(),equalsThree,"Xyzzy"));
  -        assertEquals("Xyzzy",CollectionAlgorithms.detect(list.iterator(),equalsTwentyThree,"Xyzzy"));
  +        assertEquals(new Integer(3),Algorithms.detect(list.iterator(),equalsThree,"Xyzzy"));
  +        assertEquals("Xyzzy",Algorithms.detect(list.iterator(),equalsTwentyThree,"Xyzzy"));
       }    
   
       public void testForEach() {
           Summer summer = new Summer();
  -        CollectionAlgorithms.foreach(list.iterator(),summer);
  +        Algorithms.foreach(list.iterator(),summer);
           assertEquals(sum,summer.sum);
       }    
   
       public void testSelect1() {
  -        Collection result = CollectionAlgorithms.select(list.iterator(),isEven);
  +        Collection result = Algorithms.collect(Algorithms.select(list.iterator(),isEven));
           assertNotNull(result);
           assertEquals(evens,result);
       }    
   
       public void testSelect2() {
           ArrayList result = new ArrayList();
  -        assertSame(result,CollectionAlgorithms.select(list.iterator(),isEven,result));
  +        assertSame(result,Algorithms.collect(Algorithms.select(list.iterator(),isEven),result));
           assertEquals(evens,result);
       }    
   
       public void testReject1() {
  -        Collection result = CollectionAlgorithms.reject(list.iterator(),isOdd);
  +        Collection result = Algorithms.collect(Algorithms.reject(list.iterator(),isOdd));
           assertNotNull(result);
           assertEquals(evens,result);
       }    
   
       public void testReject2() {
           ArrayList result = new ArrayList();
  -        assertSame(result,CollectionAlgorithms.reject(list.iterator(),isOdd,result));
  +        assertSame(result,Algorithms.collect(Algorithms.reject(list.iterator(),isOdd),result));
           assertEquals(evens,result);
       }    
   
       public void testInject() {
  -        Object result = CollectionAlgorithms.inject(
  +        Object result = Algorithms.inject(
               list.iterator(),
               new Integer(0),
               new BinaryFunction() {
  @@ -219,17 +219,17 @@
       }    
   
       public void testRetain() {
  -        CollectionAlgorithms.retain(list.iterator(),isEven);
  +        Algorithms.retain(list.iterator(),isEven);
           assertEquals(evens,list);
       }
   
       public void testRemove() {
  -        CollectionAlgorithms.remove(list.iterator(),isOdd);
  +        Algorithms.remove(list.iterator(),isOdd);
           assertEquals(evens,list);
       }
   
       public void testTransform() {
  -        CollectionAlgorithms.transform(
  +        Algorithms.transform(
               list.listIterator(),
               new UnaryFunction() {
                   public Object evaluate(Object obj) {
  
  
  
  1.7       +7 -7      jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/QuicksortExample.java
  
  Index: QuicksortExample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/QuicksortExample.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- QuicksortExample.java	24 Nov 2003 23:09:13 -0000	1.6
  +++ QuicksortExample.java	25 Nov 2003 19:39:45 -0000	1.7
  @@ -65,11 +65,11 @@
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
  +import org.apache.commons.functor.Algorithms;
   import org.apache.commons.functor.BinaryFunction;
   import org.apache.commons.functor.UnaryFunction;
   import org.apache.commons.functor.adapter.RightBoundPredicate;
   import org.apache.commons.functor.core.ConstantFunction;
  -import org.apache.commons.functor.core.collection.CollectionAlgorithms;
   import org.apache.commons.functor.core.collection.IsEmpty;
   import org.apache.commons.functor.core.comparator.IsGreaterThanOrEqual;
   import org.apache.commons.functor.core.comparator.IsLessThan;
  @@ -501,11 +501,11 @@
    */
       private BinaryFunction lesserTail = new ObjectListFunction() {
           public Object evaluate(Object head, List tail) {
  -            return CollectionAlgorithms.select(
  +            return Algorithms.collect(Algorithms.select(
                   tail.iterator(),
                   RightBoundPredicate.bind(
                       IsLessThan.getIsLessThan(), 
  -                    head));
  +                    head)));
           }
       };
   
  @@ -516,11 +516,11 @@
    */
       private BinaryFunction greaterTail = new BinaryFunction() {
           public Object evaluate(Object head, Object tail) {
  -            return CollectionAlgorithms.select(
  +            return Algorithms.collect(Algorithms.select(
                   ((List)tail).iterator(),
                   RightBoundPredicate.bind(
                       IsGreaterThanOrEqual.instance(), 
  -                    head));
  +                    head)));
           }
       };
   
  
  
  

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


Mime
View raw message