rwaldhoff    2003/11/24 13:38:39

  Modified:    functor/src/test/org/apache/commons/functor/core/collection
                        TestIsEmpty.java
               functor/src/java/org/apache/commons/functor/core/collection
                        IsEmpty.java
  Log:
  support Strings and arrays in IsEmpty predicate, add tests
  
  Revision  Changes    Path
  1.4       +14 -2     
jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestIsEmpty.java
  
  Index: TestIsEmpty.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestIsEmpty.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestIsEmpty.java  24 Nov 2003 20:12:17 -0000      1.3
  +++ TestIsEmpty.java  24 Nov 2003 21:38:39 -0000      1.4
  @@ -143,6 +143,18 @@
           }
       }
       
  +    public void testTestArray() throws Exception {
  +        assertTrue(! IsEmpty.instance().test(new int[10]));
  +        assertTrue(! IsEmpty.instance().test(new Object[10]));
  +        assertTrue(IsEmpty.instance().test(new int[0]));
  +        assertTrue(IsEmpty.instance().test(new Object[0]));
  +    }
  +
  +    public void testTestString() throws Exception {
  +        assertTrue(! IsEmpty.instance().test("xyzzy"));
  +        assertTrue(IsEmpty.instance().test(""));
  +    }
  +
       public void testEquals() throws Exception {
           UnaryPredicate p = new IsEmpty();
           assertEquals(p,p);
  
  
  
  1.3       +35 -3     
jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/IsEmpty.java
  
  Index: IsEmpty.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/IsEmpty.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- IsEmpty.java      24 Nov 2003 20:12:17 -0000      1.2
  +++ IsEmpty.java      24 Nov 2003 21:38:39 -0000      1.3
  @@ -57,6 +57,7 @@
   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.UnaryPredicate;
  @@ -71,9 +72,22 @@
       // ------------------------------------------------------------------------
       
       public IsEmpty() { }
  +
  +    // instance methods
  +    // ------------------------------------------------------------------------
       
       public boolean test(Object obj) {
  -        return ((Collection)obj).isEmpty();
  +        if(obj instanceof Collection) {
  +            return test((Collection)obj);
  +        } else if(obj instanceof String) {
  +            return test((String)obj);
  +        } else if(null != obj && obj.getClass().isArray()) {
  +            return testArray(obj);
  +        } else if(null == obj){
  +            throw new NullPointerException("Argument must not be null");
  +        } else {
  +            throw new ClassCastException("Expected Collection, String or Array, 
found " + obj);
  +        } 
       }
   
       /**
  @@ -97,9 +111,27 @@
           return "IsEmpty()";
       }
   
  +    private boolean test(Collection col) {
  +        return col.isEmpty();
  +    }
  +    
  +    private boolean test(String str) {
  +        return 0 == str.length();
  +    }
  +    
  +    private boolean testArray(Object array) {
  +        return 0 == Array.getLength(array);
  +    }
  +
  +    // class methods
  +    // ------------------------------------------------------------------------
  +    
       public static final IsEmpty instance() {
           return INSTANCE;
       }
  +    
  +    // class variables
  +    // ------------------------------------------------------------------------
       
       private static final IsEmpty INSTANCE = new IsEmpty();
   
  
  
  

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

Reply via email to