rwaldhoff    2003/11/24 13:29:28

  Modified:    functor/src/java/org/apache/commons/functor/core/collection
                        Size.java
               functor/src/test/org/apache/commons/functor/core/collection
                        TestSize.java
  Log:
  support Strings and arrays in Size function, add tests
  
  Revision  Changes    Path
  1.3       +29 -4     
jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/Size.java
  
  Index: Size.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/Size.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Size.java 24 Nov 2003 20:12:17 -0000      1.2
  +++ Size.java 24 Nov 2003 21:29:28 -0000      1.3
  @@ -57,11 +57,14 @@
   package org.apache.commons.functor.core.collection;
   
   import java.io.Serializable;
  +import java.lang.reflect.Array;
   import java.util.Collection;
   
   import org.apache.commons.functor.UnaryFunction;
   
   /**
  + * Returns the size of the specified Collection, or the length
  + * of the specified array or String.
    * @version $Revision$ $Date$
    * @author Rodney Waldhoff
    */
  @@ -73,7 +76,17 @@
       public Size() { }
       
       public Object evaluate(Object obj) {
  -        return new Integer(((Collection)obj).size());
  +        if(obj instanceof Collection) {
  +            return evaluate((Collection)obj);
  +        } else if(obj instanceof String) {
  +            return evaluate((String)obj);
  +        } else if(null != obj && obj.getClass().isArray()) {
  +            return evaluateArray(obj);
  +        } else if(null == obj){
  +            throw new NullPointerException("Argument must not be null");
  +        } else {
  +            throw new ClassCastException("Expected Collection, String or Array, 
found " + obj);
  +        } 
       }
   
       /**
  @@ -100,7 +113,19 @@
       public static final Size instance() {
           return INSTANCE;
       }
  +
  +    private Object evaluate(Collection col) {
  +        return new Integer(col.size());
  +    }
  +    
  +    private Object evaluate(String str) {
  +        return new Integer(str.length());
  +    }
  +    
  +    private Object evaluateArray(Object array) {
  +        return new Integer(Array.getLength(array));
  +    }
       
       private static final Size INSTANCE = new Size();
  -
  +    
   }
  
  
  
  1.3       +9 -3      
jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestSize.java
  
  Index: TestSize.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestSize.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestSize.java     24 Nov 2003 20:12:17 -0000      1.2
  +++ TestSize.java     24 Nov 2003 21:29:28 -0000      1.3
  @@ -139,7 +139,7 @@
           }
       }
       
  -    public void testTestNonCollection() throws Exception {
  +    public void testEvaluateNonCollection() throws Exception {
           try {
               Size.instance().evaluate(new Integer(3));
               fail("Expected ClassCastException");
  @@ -148,6 +148,12 @@
           }
       }
       
  +    public void testEvaluateArray() throws Exception {
  +        assertEquals(new Integer(10),Size.instance().evaluate(new int[10]));
  +        assertEquals(new Integer(7),Size.instance().evaluate(new String[7]));
  +        assertEquals(new 
Integer("xyzzy".length()),Size.instance().evaluate("xyzzy"));
  +    }
  +
       public void testEquals() throws Exception {
           UnaryFunction f = new Size();
           assertEquals(f,f);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to