You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2008/06/09 18:58:00 UTC

svn commit: r665769 - in /commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core: IsInstance.java IsInstanceOf.java

Author: mbenson
Date: Mon Jun  9 09:58:00 2008
New Revision: 665769

URL: http://svn.apache.org/viewvc?rev=665769&view=rev
Log:
generify; rename IsInstanceOf to IsInstance and change to a BinaryPredicate instead of a UnaryPredicate

Added:
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsInstance.java
      - copied, changed from r661495, commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsInstanceOf.java
Removed:
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsInstanceOf.java

Copied: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsInstance.java (from r661495, commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsInstanceOf.java)
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsInstance.java?p2=commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsInstance.java&p1=commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsInstanceOf.java&r1=661495&r2=665769&rev=665769&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsInstanceOf.java (original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/core/IsInstance.java Mon Jun  9 09:58:00 2008
@@ -18,7 +18,9 @@
 
 import java.io.Serializable;
 
+import org.apache.commons.functor.BinaryPredicate;
 import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.adapter.RightBoundPredicate;
 
 /**
  * {@link #test Tests}
@@ -29,63 +31,59 @@
  * @version $Revision$ $Date$
  * @author Rodney Waldhoff
  */
-public final class IsInstanceOf implements UnaryPredicate, Serializable {
-
-    // attributes
-    // ------------------------------------------------------------------------
-    private Class klass;
-
-    // constructor
-    // ------------------------------------------------------------------------
+public final class IsInstance<T> implements BinaryPredicate<T, Class<?>>, Serializable {
     /**
-     * Create a new IsInstanceOf.
-     * @param klass Class of which a tested object must be an instance.
+     * Basic IsInstanceOf instance.
      */
-    public IsInstanceOf(Class klass) {
-        this.klass = klass;
-    }
+    public static final IsInstance<Object> INSTANCE = IsInstance.<Object>instance();
 
     // predicate interface
     // ------------------------------------------------------------------------
+
     /**
      * {@inheritDoc}
      */
-    public boolean test(Object obj) {
-        return klass.isInstance(obj);
+    public boolean test(T left, Class<?> right) {
+        return right.isInstance(left);
     }
 
     /**
      * {@inheritDoc}
      */
     public boolean equals(Object that) {
-        return that == this || (that instanceof IsInstanceOf && equals((IsInstanceOf) that));
+        return that instanceof IsInstance;
     }
 
     /**
-     * Learn whether another IsInstanceOf is equal to this.
-     * @param that IsInstanceOf to test
-     * @return boolean
+     * {@inheritDoc}
      */
-    public boolean equals(IsInstanceOf that) {
-        return (null != that && (null == this.klass ? null == that.klass : this.klass.equals(that.klass)));
+    public int hashCode() {
+        return ("IsInstance".hashCode() << 4) | 37;
     }
 
     /**
      * {@inheritDoc}
      */
-    public int hashCode() {
-        int hash = "IsInstanceOf".hashCode();
-        if (null != klass) {
-            hash ^= klass.hashCode();
-        }
-        return hash;
+    public String toString() {
+        return "IsInstance";
     }
 
     /**
-     * {@inheritDoc}
+     * Get an IsInstance instance.
+     * @param <T>
+     * @return IsInstance<T>
      */
-    public String toString() {
-        return "IsInstanceOf<" + klass + ">";
+    public static <T> IsInstance<T> instance() {
+        return new IsInstance<T>();
     }
 
+    /**
+     * Get an IsInstanceOf UnaryPredicate. 
+     * @param <T>
+     * @param clazz bound right-side argument
+     * @return UnaryPredicate<T>
+     */
+    public static <T> UnaryPredicate<T> of(Class<?> clazz) {
+        return RightBoundPredicate.bind(new IsInstance<T>(), clazz);
+    }
 }