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/11/24 21:29:23 UTC

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

rwaldhoff    2003/11/24 12:29:23

  Modified:    functor/src/test/org/apache/commons/functor/core/collection
                        TestIsElementOf.java
               functor/src/java/org/apache/commons/functor/core/collection
                        IsElementOf.java
  Log:
  convert ElementOf to a BinaryPredicate
  use ElementOf.instance(Collection) for the previous behavior of new ElementOf(Collection)
  
  Revision  Changes    Path
  1.4       +15 -39    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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestIsElementOf.java	29 Jun 2003 21:46:12 -0000	1.3
  +++ TestIsElementOf.java	24 Nov 2003 20:29:23 -0000	1.4
  @@ -62,6 +62,8 @@
   import junit.framework.TestSuite;
   
   import org.apache.commons.functor.BaseFunctorTest;
  +import org.apache.commons.functor.UnaryPredicate;
  +import org.apache.commons.functor.core.ConstantPredicate;
   
   /**
    * @version $Revision$ $Date$
  @@ -85,20 +87,12 @@
       // ------------------------------------------------------------------------
   
       protected Object makeFunctor() {
  -        return new IsElementOf(new ArrayList());
  +        return new IsElementOf();
       }
   
       // Lifecycle
       // ------------------------------------------------------------------------
   
  -    public void setUp() throws Exception {
  -        super.setUp();
  -    }
  -
  -    public void tearDown() throws Exception {
  -        super.tearDown();
  -    }
  -
       // Tests
       // ------------------------------------------------------------------------
   
  @@ -108,7 +102,7 @@
           list.add(new Integer(10));
           list.add(new Integer(15));
   
  -        IsElementOf p = new IsElementOf(list);
  +        UnaryPredicate p = IsElementOf.instance(list);
           assertTrue(p.test(new Integer(5)));
           assertTrue(p.test(new Integer(10)));
           assertTrue(p.test(new Integer(15)));
  @@ -118,39 +112,21 @@
   
       }
   
  -    public void testNullConstructor() {
  +    public void testNullWrapper() {
           try {
  -            new IsElementOf(null);
  -            fail("should have thrown IllegalArgumentException");
  +            IsElementOf.instance(null);
  +            fail("expected IllegalArgumentException");
           } catch (IllegalArgumentException e) {
               // good
  -        } catch (Exception e) {
  -            fail("should have thrown IllegalArgumentException, not " + e);
           }
       }
   
       public void testEquals() throws Exception {
  -        ArrayList list1 = new ArrayList();
  -        list1.add(new Integer(5));
  -
  -        IsElementOf p1 = new IsElementOf(list1);
  -        IsElementOf p2 = new IsElementOf(list1);
  -
  -        assertEquals(p1, p2);
  -
  -        ArrayList list2 = new ArrayList();
  -        list2.add(new Integer(5));
  -        p2 = new IsElementOf(list2);
  -
  -        assertEquals(p1, p2);
  -
  -        list1.add(new Integer(6));
  -        assertTrue(!p1.equals(p2));
  -
  -        list2.add(new Integer(6));
  -        assertEquals(p1, p2);
  -
  -        list2.add(new Integer(7));
  -        assertTrue(!p1.equals(p2));
  +        IsElementOf p1 = new IsElementOf();
  +        assertObjectsAreEqual(p1, p1);
  +        assertObjectsAreEqual(p1, new IsElementOf());
  +        assertObjectsAreEqual(p1, IsElementOf.instance());
  +        assertSame(IsElementOf.instance(), IsElementOf.instance());
  +        assertObjectsAreNotEqual(p1, ConstantPredicate.getFalsePredicate());
       }
   }
  
  
  
  1.3       +48 -43    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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- IsElementOf.java	24 Jun 2003 15:49:58 -0000	1.2
  +++ IsElementOf.java	24 Nov 2003 20:29:23 -0000	1.3
  @@ -57,65 +57,70 @@
   
   package org.apache.commons.functor.core.collection;
   
  -import org.apache.commons.functor.UnaryPredicate;
  -
  -import java.util.Collection;
   import java.io.Serializable;
  +import java.util.Collection;
  +
  +import org.apache.commons.functor.BinaryPredicate;
  +import org.apache.commons.functor.UnaryPredicate;
  +import org.apache.commons.functor.adapter.RightBoundPredicate;
   
   /**
  - * A {@link UnaryPredicate} that checks to see if elements are
  - * part of a Collection.
  + * A {@link BinaryPredicate} that checks to see if the
  + * specified object is an element of the specified
  + * Collection.
    *
    * @since 1.0
    * @version $Revision$ $Date$
    * @author  Jason Horman (jason@jhorman.org)
  + * @author  Rodney Waldhoff
    */
  +public final class IsElementOf implements BinaryPredicate, Serializable {
   
  -public class IsElementOf implements UnaryPredicate, Serializable {
  -
  -    /***************************************************
  -     *  Instance variables
  -     ***************************************************/
  -
  -    /** The collection that will be checked, .contains'd */
  -    private Collection c = null;
  -
  -    /** Hashcode of the name of this Predicate. */
  -    private static final int nameHashCode = "IsElementOf".hashCode();
  -
  -    /***************************************************
  -     *  Constructors
  -     ***************************************************/
  -
  -    public IsElementOf(Collection c) {
  -        if (c == null) {
  -            throw new IllegalArgumentException("collection must not be null");
  -        }
  -
  -        this.c = c;
  +    // constructors
  +    //---------------------------------------------------------------
  +    public IsElementOf() {
       }
   
  -    /***************************************************
  -     *  Instance methods
  -     ***************************************************/
  -
  -    public boolean test(Object o) {
  -        return c.contains(o);
  +    // instance methods
  +    //---------------------------------------------------------------
  +    
  +    public boolean test(Object obj, Object col) {
  +        return test(obj,(Collection)col);
  +    }
  +    
  +    public boolean test(Object obj, Collection col) {
  +        return col.contains(obj);
       }
   
  -    public boolean equals(Object o) {
  -        if (this == o) return true;
  -        if (!(o instanceof IsElementOf)) return false;
  -        final IsElementOf isElementOf = (IsElementOf) o;
  -        if (!c.equals(isElementOf.c)) return false;
  -        return true;
  +    public boolean equals(Object obj) {
  +        return (obj instanceof IsElementOf);
       }
   
       public int hashCode() {
  -        return 29 * c.hashCode() + nameHashCode;
  +        return "IsElementOf".hashCode();
       }
   
       public String toString() {
  -        return "IsElementOf(" + c + ")";
  +        return "IsElementOf";
       }
  +
  +    // class methods
  +    //---------------------------------------------------------------
  +    
  +    public static IsElementOf instance() {
  +        return INSTANCE;
  +    }
  +    
  +    public static UnaryPredicate instance(Collection col) {
  +        if(null == col) {
  +            throw new IllegalArgumentException("Collection must not be null");
  +        }
  +        return new RightBoundPredicate(instance(),col);
  +    }
  +    
  +    // class variables
  +    //---------------------------------------------------------------
  +    
  +    private static IsElementOf INSTANCE = new IsElementOf();
  +
   }
  
  
  

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