Dear All,

I have started using the sandbox functors for my projects. It seems that parts of the code may still need some streamlining.

I gave a shot at "IsInstanceOf".

Please receive attached a patch which changes IsInstanceOf from a UnaryPredicate to a BinaryPredicate (as the binary operator "instanceof" )

I tried to structure it in the same way as the Predicate "IsElementOf". In this way the code becomes more compact and more coherent in addition of providing all three kinds of Predicates.

Unfortunately, the changes are not backward compatible since it has become now a BinaryPredicate, which I hope it not of show-stopper importance at this stage of project.

Can the maintainers please consider the application of this patch.

I plan to regularly contribute to this projects in the future and I really hope that this project is not doomed, which the lack of exchange on its behalf may suggest.

Cheers,
Arnd.
Index: src/java/org/apache/commons/functor/core/IsInstanceOf.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/IsInstanceOf.java,v
retrieving revision 1.2
diff -u -r1.2 IsInstanceOf.java
--- src/java/org/apache/commons/functor/core/IsInstanceOf.java  28 Feb 2004 03:35:24 
-0000      1.2
+++ src/java/org/apache/commons/functor/core/IsInstanceOf.java  28 Apr 2004 19:11:20 
-0000
@@ -15,9 +15,13 @@
  */
 package org.apache.commons.functor.core;
 
-import java.io.Serializable;
-
+import org.apache.commons.functor.BinaryPredicate;
+import org.apache.commons.functor.Predicate;
 import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.adapter.BoundPredicate;
+import org.apache.commons.functor.adapter.RightBoundPredicate;
+
+import java.io.Serializable;
 
 /**
  * [EMAIL PROTECTED] #test Tests} 
@@ -28,47 +32,55 @@
  * @version $Revision: 1.2 $ $Date: 2004/02/28 03:35:24 $
  * @author Rodney Waldhoff
  */
-public final class IsInstanceOf implements UnaryPredicate, Serializable {
+public final class IsInstanceOf implements BinaryPredicate, Serializable {
 
-    // constructor
-    // ------------------------------------------------------------------------
-    public IsInstanceOf(Class klass) {
-        this.klass = klass;
+    /** Public constructor not needed, since one instance should always suffice. */
+    private IsInstanceOf() {
     }
  
-    // predicate interface
-    // ------------------------------------------------------------------------
 
-    public boolean test(Object obj) {
-        return klass.isInstance(obj);
+    /** Tests if an object is an instance of the type given. 
+     * @param instance The candidate of which the type is under question.
+     * @param klass The class.
+     */
+    public boolean test(Object instance, Object klass) {
+        return ((Class)klass).isInstance(instance);
     }
-
-    public boolean equals(Object that) {
-        if(that instanceof IsInstanceOf) {
-            return equals((IsInstanceOf)that);
-        } else {
-            return false;
-        }
+    
+    public String toString() {
+        return "IsInstanceOf";
+    }
+    
+    /** The singleton instance. */
+    private static final IsInstanceOf INSTANCE = new IsInstanceOf();
+    
+    /** Returns the IsInstanceOf instance. */
+    public static BinaryPredicate instance(){
+        return INSTANCE;
     }
     
-    public boolean equals(IsInstanceOf that) {
-        return (null != that && (null == this.klass ? null == that.klass : 
this.klass.equals(that.klass)));
+    /** Returns an UnaryPredicate which tests if an argument is of the specified 
class. */ 
+    public static UnaryPredicate instance(Class klass){
+        return new RightBoundPredicate(instance(),klass); 
     }
     
-    public int hashCode() {
-        int hash = "IsInstanceOf".hashCode();
-        if(null != klass) {
-            hash ^= klass.hashCode();
-        }
-        return hash;
+    /** Returns a Predicate which tests if a specified argument is of the specified 
class. */
+    public static Predicate instance(Object instance, Class klass){
+        return new BoundPredicate(instance(klass),instance); 
     }
     
-    public String toString() {
-        return "IsInstanceOf<" + klass + ">";
+    /**
+     * @see java.lang.Object#hashCode()
+     */
+    public boolean equals(Object that) {
+        return that instanceof IsInstanceOf;
     }
     
-    // attributes
-    // ------------------------------------------------------------------------
-    private Class klass;
-
+    
+    /**
+     * @see java.lang.Object#hashCode()
+     */
+    public int hashCode() {
+        return "IsInstanceOf".hashCode();
+    }
 }
Index: src/test/org/apache/commons/functor/core/TestIsInstanceOf.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/TestIsInstanceOf.java,v
retrieving revision 1.5
diff -u -r1.5 TestIsInstanceOf.java
--- src/test/org/apache/commons/functor/core/TestIsInstanceOf.java      28 Feb 2004 
03:35:27 -0000      1.5
+++ src/test/org/apache/commons/functor/core/TestIsInstanceOf.java      28 Apr 2004 
19:11:20 -0000
@@ -42,7 +42,7 @@
     // ------------------------------------------------------------------------
 
     protected Object makeFunctor() {
-        return new IsInstanceOf(String.class);
+        return IsInstanceOf.instance();
     }
     
     // Lifecycle
@@ -60,7 +60,7 @@
     // ------------------------------------------------------------------------
     
     public void testTest() throws Exception {
-        UnaryPredicate p = new IsInstanceOf(Number.class);
+        UnaryPredicate p = IsInstanceOf.instance(Number.class);
         assertTrue(!p.test(null));
         assertTrue(!p.test("foo"));
         assertTrue(p.test(new Integer(3)));
@@ -69,7 +69,7 @@
     }
     
     public void testInstanceOfNull() throws Exception {
-        UnaryPredicate p = new IsInstanceOf(null);
+        UnaryPredicate p = IsInstanceOf.instance(null);
         try {
             p.test("foo");
             fail("Expected NullPointerException");
@@ -79,11 +79,35 @@
     }
     
     public void testEquals() throws Exception {
-        UnaryPredicate p = new IsInstanceOf(Object.class);
+        UnaryPredicate p = IsInstanceOf.instance(Object.class);
         assertEquals(p,p);
-        assertObjectsAreEqual(p,new IsInstanceOf(Object.class));
+        assertObjectsAreEqual(p,IsInstanceOf.instance(Object.class));
         assertObjectsAreNotEqual(p,Constant.truePredicate());
-        assertObjectsAreNotEqual(p,new IsInstanceOf(null));
-        assertObjectsAreNotEqual(p,new IsInstanceOf(String.class));
+        assertObjectsAreNotEqual(p,IsInstanceOf.instance(null));
+        assertObjectsAreNotEqual(p,IsInstanceOf.instance(String.class));
+    }
+    
+    public void testInstanceOf(){
+        //    BinaryPredicate
+        assertTrue(IsInstanceOf.instance().test(Boolean.TRUE, Boolean.class));
+        assertFalse(IsInstanceOf.instance().test(null, Boolean.class));
+        assertFalse(IsInstanceOf.instance().test(new Integer(0), Boolean.class));
+        assertEquals(IsInstanceOf.instance(), IsInstanceOf.instance());
+
+        //    UnaryPredicate
+        assertTrue(IsInstanceOf.instance(Boolean.class).test(Boolean.TRUE));
+        assertFalse(IsInstanceOf.instance(Boolean.class).test(null));
+        assertFalse(IsInstanceOf.instance(Boolean.class).test(new Integer(0)));
+        assertEquals(IsInstanceOf.instance(Boolean.class), 
IsInstanceOf.instance(Boolean.class));
+
+        //    Predicate
+        assertTrue(IsInstanceOf.instance(Boolean.TRUE, Boolean.class).test());
+        assertFalse(IsInstanceOf.instance(null, Boolean.class).test());
+        assertFalse(IsInstanceOf.instance(new Integer(0), Boolean.class).test());
+        assertEquals(IsInstanceOf.instance(Boolean.TRUE, Boolean.class), 
+                     IsInstanceOf.instance(Boolean.TRUE, Boolean.class));
+        assertObjectsAreNotEqual(IsInstanceOf.instance(Boolean.TRUE, Boolean.class), 
+                                 IsInstanceOf.instance(Boolean.FALSE, Boolean.class));
+
     }
 }
Index: src/test/org/apache/commons/functor/example/FlexiMapExample.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/FlexiMapExample.java,v
retrieving revision 1.11
diff -u -r1.11 FlexiMapExample.java
--- src/test/org/apache/commons/functor/example/FlexiMapExample.java    28 Feb 2004 
03:35:29 -0000      1.11
+++ src/test/org/apache/commons/functor/example/FlexiMapExample.java    28 Apr 2004 
19:11:21 -0000
@@ -461,7 +461,7 @@
                     /*
                      * we'll test the type of the right-hand argument,
                      */      
-                                       new IsInstanceOf(clazz),
+                                       IsInstanceOf.instance(clazz),
                     /*
                      * and either pass the given value through,
                      */      
Index: src/test/org/apache/commons/functor/example/map/TestPredicatedMap.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/TestPredicatedMap.java,v
retrieving revision 1.3
diff -u -r1.3 TestPredicatedMap.java
--- src/test/org/apache/commons/functor/example/map/TestPredicatedMap.java      28 Feb 
2004 03:35:30 -0000      1.3
+++ src/test/org/apache/commons/functor/example/map/TestPredicatedMap.java      28 Apr 
2004 19:11:21 -0000
@@ -44,7 +44,7 @@
     public void setUp() throws Exception {
         super.setUp();
         baseMap = new HashMap();
-        predicatedMap = new PredicatedMap(baseMap,new IsInstanceOf(String.class),new 
IsInstanceOf(Integer.class));
+        predicatedMap = new 
PredicatedMap(baseMap,IsInstanceOf.instance(String.class),IsInstanceOf.instance(Integer.class));
     }
     
     public void tearDown() throws Exception {

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

Reply via email to