rwaldhoff    2003/11/24 13:56:43

  Modified:    functor/src/test/org/apache/commons/functor/core/collection
                        TestIsElementOf.java
               functor/src/java/org/apache/commons/functor/core/collection
                        IsElementOf.java
  Log:
  support Arrays in isElementOf, add tests
  
  Revision  Changes    Path
  1.6       +50 -5     
jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestIsElementOf.java
  
  Index: TestIsElementOf.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/collection/TestIsElementOf.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestIsElementOf.java      24 Nov 2003 20:31:20 -0000      1.5
  +++ TestIsElementOf.java      24 Nov 2003 21:56:43 -0000      1.6
  @@ -96,7 +96,7 @@
       // Tests
       // ------------------------------------------------------------------------
   
  -    public void testTest() throws Exception {
  +    public void testTestCollection() throws Exception {
           ArrayList list = new ArrayList();
           list.add(new Integer(5));
           list.add(new Integer(10));
  @@ -112,12 +112,57 @@
   
       }
   
  -    public void testNullWrapper() {
  +    public void testTestArray() throws Exception {
  +        int[] list = new int[] { 5, 10, 15 };
  +
  +        UnaryPredicate p = IsElementOf.instance(list);
  +        assertTrue(p.test(new Integer(5)));
  +        assertTrue(p.test(new Integer(10)));
  +        assertTrue(p.test(new Integer(15)));
  +
  +        assertTrue(!p.test(new Integer(4)));
  +        assertTrue(!p.test(new Integer(11)));
  +    }
  +
  +    public void testTestArrayWithNull() throws Exception {
  +        assertTrue(! IsElementOf.instance().test(null,new int[] { 5, 10, 15 }));
  +        assertTrue(IsElementOf.instance().test(null,new Integer[] { new Integer(5), 
null, new Integer(15) }));
  +        assertTrue(IsElementOf.instance().test(new Integer(15),new Integer[] { new 
Integer(5), null, new Integer(15) }));
  +    }
  +
  +    public void testWrapNull() {
           try {
               IsElementOf.instance(null);
  +            fail("expected NullPointerException");
  +        } catch (NullPointerException e) {
  +            // expected
  +        }
  +    }
  +
  +    public void testWrapNonCollection() {
  +        try {
  +            IsElementOf.instance(new Integer(3));
  +            fail("expected IllegalArgumentException");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +    }
  +
  +    public void testTestNull() {
  +        try {
  +            IsElementOf.instance().test(new Integer(5),null);
  +            fail("expected NullPointerException");
  +        } catch (NullPointerException e) {
  +            // expected
  +        }
  +    }
  +
  +    public void testTestNonCollection() {
  +        try {
  +            IsElementOf.instance().test(new Integer(5),new Long(5));
               fail("expected IllegalArgumentException");
           } catch (IllegalArgumentException e) {
  -            // good
  +            // expected
           }
       }
   
  
  
  
  1.4       +40 -11    
jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/IsElementOf.java
  
  Index: IsElementOf.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/collection/IsElementOf.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- IsElementOf.java  24 Nov 2003 20:29:23 -0000      1.3
  +++ IsElementOf.java  24 Nov 2003 21:56:43 -0000      1.4
  @@ -58,6 +58,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.BinaryPredicate;
  @@ -85,13 +86,17 @@
       //---------------------------------------------------------------
       
       public boolean test(Object obj, Object col) {
  -        return test(obj,(Collection)col);
  +        if(col instanceof Collection) {
  +            return testCollection(obj,(Collection)col);
  +        } else if(null != col && col.getClass().isArray()) {
  +            return testArray(obj,col);
  +        } else if(null == col) {
  +            throw new NullPointerException("Right side argument must not be null.");
  +        } else {
  +            throw new IllegalArgumentException("Expected Collection or Array, found 
" + col.getClass());
  +        }
       }
       
  -    public boolean test(Object obj, Collection col) {
  -        return col.contains(obj);
  -    }
  -
       public boolean equals(Object obj) {
           return (obj instanceof IsElementOf);
       }
  @@ -104,6 +109,25 @@
           return "IsElementOf";
       }
   
  +    private boolean testCollection(Object obj, Collection col) {
  +        return col.contains(obj);
  +    }
  +
  +    private boolean testArray(Object obj, Object array) {
  +        for(int i=0,m=Array.getLength(array);i<m;i++) {
  +            Object value = Array.get(array,i);            
  +            if(null == obj) {
  +                if(null == value) {
  +                    return true;
  +                }
  +            } else if(obj.equals(value)) {
  +                return true;
  +            }
  +        }
  +        return false;
  +    }
  +
  +
       // class methods
       //---------------------------------------------------------------
       
  @@ -111,11 +135,16 @@
           return INSTANCE;
       }
       
  -    public static UnaryPredicate instance(Collection col) {
  -        if(null == col) {
  -            throw new IllegalArgumentException("Collection must not be null");
  +    public static UnaryPredicate instance(Object obj) {
  +        if(null == obj) {
  +            throw new NullPointerException("Argument must not be null");
  +        } else if(obj instanceof Collection) {
  +            return new RightBoundPredicate(instance(),obj);
  +        } else if(obj.getClass().isArray()) {
  +            return new RightBoundPredicate(instance(),obj);
  +        } else {
  +            throw new IllegalArgumentException("Expected Collection or Array, found 
" + obj.getClass());
           }
  -        return new RightBoundPredicate(instance(),col);
       }
       
       // class variables
  
  
  

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

Reply via email to