commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r666186 - in /commons/sandbox/functor/trunk/src: main/java/org/apache/commons/functor/adapter/ test/java/org/apache/commons/functor/adapter/
Date Tue, 10 Jun 2008 16:34:42 GMT
Author: mbenson
Date: Tue Jun 10 09:34:42 2008
New Revision: 666186

URL: http://svn.apache.org/viewvc?rev=666186&view=rev
Log:
unchecked warnings; move construction-only generic parameters to constructors; throw IllegalArgumentException
when null constructor arguments are specified

Modified:
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundFunction.java
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundPredicate.java
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundProcedure.java
    commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundFunction.java
    commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundPredicate.java
    commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundProcedure.java

Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundFunction.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundFunction.java?rev=666186&r1=666185&r2=666186&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundFunction.java
(original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundFunction.java
Tue Jun 10 09:34:42 2008
@@ -38,34 +38,88 @@
  * @version $Revision$ $Date$
  * @author Rodney Waldhoff
  */
-public final class BoundFunction<A, T> implements Function<T>, Serializable {
-    /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
-    private UnaryFunction<? super A, ? extends T> function;
-    /** The parameter to pass to that function. */
-    private A param = null;
+public final class BoundFunction<T> implements Function<T>, Serializable {
+    private class Helper<A> implements Function<T>, Serializable {
+        /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
+        private UnaryFunction<? super A, ? extends T> function;
+
+        /** The parameter to pass to that function. */
+        private A arg = null;
+
+        private Helper(UnaryFunction<? super A, ? extends T> function, A arg) {
+            this.function = function;
+            this.arg = arg;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public T evaluate() {
+            return function.evaluate(arg);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public boolean equals(Object obj) {
+            return obj == this || obj instanceof Helper && equals((Helper<?>)
obj);
+        }
+
+        private boolean equals(Helper<?> that) {
+            if (that != null && that.function.equals(this.function)) {
+                return that.arg == this.arg || that.arg != null && that.arg.equals(this.arg);
+            }
+            return false;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public int hashCode() {
+            int result = "BoundFunction$Helper".hashCode();
+            result <<= 2;
+            result |= function.hashCode();
+            result <<= 2;
+            return arg == null ? result : result | arg.hashCode();
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        @Override
+        public String toString() {
+            return function.toString() + "(" + arg + ")";
+        }
+    }
+
+    private Helper<?> helper;
 
     /**
      * Create a new BoundFunction.
      * @param function the function to adapt
      * @param arg the constant argument to use
      */
-    public BoundFunction(UnaryFunction<? super A, ? extends T> function, A arg) {
-        this.function = function;
-        this.param = arg;
+    public <A> BoundFunction(UnaryFunction<? super A, ? extends T> function,
A arg) {
+        if (function == null) {
+            throw new IllegalArgumentException("UnaryFunction argument was null");
+        }
+        this.helper = new Helper<A>(function, arg);
     }
 
     /**
      * {@inheritDoc}
      */
     public T evaluate() {
-        return function.evaluate(param);
+        return helper.evaluate();
     }
 
     /**
      * {@inheritDoc}
      */
     public boolean equals(Object that) {
-        return that == this || (that instanceof BoundFunction && equals((BoundFunction<?,
?>) that));
+        return that == this || (that instanceof BoundFunction && equals((BoundFunction<?>)
that));
     }
 
     /**
@@ -73,34 +127,22 @@
      * @param that BoundFunction to test
      * @return boolean
      */
-    public boolean equals(BoundFunction<?, ?> that) {
-        return null != that
-                && (null == function ? null == that.function : function.equals(that.function))
-                && (null == param ? null == that.param : param.equals(that.param));
-
+    public boolean equals(BoundFunction<?> that) {
+        return null != that && that.helper.equals(this.helper);
     }
 
     /**
      * {@inheritDoc}
      */
     public int hashCode() {
-        int hash = "BoundFunction".hashCode();
-        if (null != function) {
-            hash <<= 2;
-            hash ^= function.hashCode();
-        }
-        if (null != param) {
-            hash <<= 2;
-            hash ^= param.hashCode();
-        }
-        return hash;
+        return "BoundFunction".hashCode() << 8 | helper.hashCode();
     }
 
     /**
      * {@inheritDoc}
      */
     public String toString() {
-        return "BoundFunction<" + function + "(" + param + ")>";
+        return "BoundFunction<" + helper + ">";
     }
 
     /**
@@ -111,7 +153,8 @@
      * argument.
      * When the given <code>UnaryFunction</code> is <code>null</code>,
      * returns <code>null</code>.
-     *
+     * @param <A>
+     * @param <T>
      * @param function the possibly-<code>null</code>
      *        {@link UnaryFunction UnaryFunction} to adapt
      * @param arg the object to bind as a constant argument
@@ -119,8 +162,8 @@
      *         {@link UnaryFunction UnaryFunction}, or <code>null</code>
      *         if the given <code>UnaryFunction</code> is <code>null</code>
      */
-    public static <A, T> BoundFunction<A, T> bind(UnaryFunction<? super A,
? extends T> function, A arg) {
-        return null == function ? null : new BoundFunction<A, T>(function, arg);
+    public static <A, T> BoundFunction<T> bind(UnaryFunction<? super A, ?
extends T> function, A arg) {
+        return null == function ? null : new BoundFunction<T>(function, arg);
     }
 
 }

Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundPredicate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundPredicate.java?rev=666186&r1=666185&r2=666186&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundPredicate.java
(original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundPredicate.java
Tue Jun 10 09:34:42 2008
@@ -38,19 +38,23 @@
  * @version $Revision$ $Date$
  * @author Rodney Waldhoff
  */
-public final class BoundPredicate<A> implements Predicate, Serializable {
+public final class BoundPredicate implements Predicate, Serializable {
     /** The {@link UnaryPredicate UnaryPredicate} I'm wrapping. */
-    private UnaryPredicate<? super A> predicate;
+    private UnaryPredicate<Object> predicate;
     /** The parameter to pass to that predicate. */
-    private A param;
+    private Object param;
 
     /**
      * Create a new BoundPredicate.
      * @param predicate the predicate to adapt
      * @param arg the constant argument to use
      */
-    public BoundPredicate(UnaryPredicate<? super A> predicate, A arg) {
-        this.predicate = predicate;
+    @SuppressWarnings("unchecked")
+    public <A> BoundPredicate(UnaryPredicate<? super A> predicate, A arg) {
+        if (predicate == null) {
+            throw new IllegalArgumentException("UnaryPredicate argument was null");
+        }
+        this.predicate = (UnaryPredicate<Object>) predicate;
         this.param = arg;
     }
 
@@ -65,7 +69,7 @@
      * {@inheritDoc}
      */
     public boolean equals(Object that) {
-        return that == this || (that instanceof BoundPredicate && equals((BoundPredicate<?>)
that));
+        return that == this || (that instanceof BoundPredicate && equals((BoundPredicate)
that));
     }
 
     /**
@@ -73,7 +77,7 @@
      * @param that BoundPredicate to test
      * @return boolean
      */
-    public boolean equals(BoundPredicate<?> that) {
+    public boolean equals(BoundPredicate that) {
         return null != that
                 && (null == predicate ? null == that.predicate : predicate.equals(that.predicate))
                 && (null == param ? null == that.param : param.equals(that.param));
@@ -119,8 +123,8 @@
      *         {@link UnaryPredicate UnaryPredicate}, or <code>null</code>
      *         if the given <code>UnaryPredicate</code> is <code>null</code>
      */
-    public static <A> BoundPredicate<A> bind(UnaryPredicate<? super A>
predicate, A arg) {
-        return null == predicate ? null : new BoundPredicate<A>(predicate, arg);
+    public static <A> BoundPredicate bind(UnaryPredicate<? super A> predicate,
A arg) {
+        return null == predicate ? null : new BoundPredicate(predicate, arg);
     }
 
 }

Modified: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundProcedure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundProcedure.java?rev=666186&r1=666185&r2=666186&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundProcedure.java
(original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BoundProcedure.java
Tue Jun 10 09:34:42 2008
@@ -38,19 +38,23 @@
  * @version $Revision$ $Date$
  * @author Rodney Waldhoff
  */
-public final class BoundProcedure<A> implements Procedure, Serializable {
+public final class BoundProcedure implements Procedure, Serializable {
     /** The {@link UnaryProcedure UnaryProcedure} I'm wrapping. */
-    private UnaryProcedure<? super A> procedure;
+    private UnaryProcedure<Object> procedure;
     /** The parameter to pass to that procedure. */
-    private A param;
+    private Object param;
 
     /**
      * Create a new BoundProcedure.
      * @param procedure the procedure to adapt
      * @param arg the constant argument to use
      */
-    public BoundProcedure(UnaryProcedure<? super A> procedure, A arg) {
-        this.procedure = procedure;
+    @SuppressWarnings("unchecked")
+    public <A> BoundProcedure(UnaryProcedure<? super A> procedure, A arg) {
+        if (procedure == null) {
+            throw new IllegalArgumentException("UnaryProcedure argument was null");
+        }
+        this.procedure = (UnaryProcedure<Object>) procedure;
         this.param = arg;
     }
 
@@ -65,7 +69,7 @@
      * {@inheritDoc}
      */
     public boolean equals(Object that) {
-        return that == this || (that instanceof BoundProcedure && equals((BoundProcedure<?>)
that));
+        return that == this || (that instanceof BoundProcedure && equals((BoundProcedure)
that));
     }
 
     /**
@@ -73,7 +77,7 @@
      * @param that the BoundProcedure to test
      * @return boolean
      */
-    public boolean equals(BoundProcedure<?> that) {
+    public boolean equals(BoundProcedure that) {
         return null != that
                 && (null == procedure ? null == that.procedure : procedure.equals(that.procedure))
                 && (null == param ? null == that.param : param.equals(that.param));
@@ -118,8 +122,8 @@
      *         {@link UnaryProcedure UnaryProcedure}, or <code>null</code>
      *         if the given <code>UnaryProcedure</code> is <code>null</code>
      */
-    public static <A> BoundProcedure<A> bind(UnaryProcedure<? super A>
procedure, A arg) {
-        return null == procedure ? null : new BoundProcedure<A>(procedure, arg);
+    public static <A> BoundProcedure bind(UnaryProcedure<? super A> procedure,
A arg) {
+        return null == procedure ? null : new BoundProcedure(procedure, arg);
     }
 
 }

Modified: commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundFunction.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundFunction.java?rev=666186&r1=666185&r2=666186&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundFunction.java
(original)
+++ commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundFunction.java
Tue Jun 10 09:34:42 2008
@@ -45,7 +45,7 @@
     // ------------------------------------------------------------------------
 
     protected Object makeFunctor() {
-        return new BoundFunction(new Identity(),"xyzzy");
+        return new BoundFunction<Object>(Identity.INSTANCE,"xyzzy");
     }
 
     // Lifecycle
@@ -63,20 +63,18 @@
     // ------------------------------------------------------------------------
 
     public void testEvaluate() throws Exception {
-        Function f = new BoundFunction(new Identity(),"xyzzy");
+        Function<Object> f = new BoundFunction<Object>(Identity.INSTANCE,"xyzzy");
         assertEquals("xyzzy",f.evaluate());
     }
 
     public void testEquals() throws Exception {
-        Function f = new BoundFunction(new Identity(),"xyzzy");
+        Function<Object> f = new BoundFunction<Object>(Identity.INSTANCE,"xyzzy");
         assertEquals(f,f);
-        assertObjectsAreEqual(f,new BoundFunction(new Identity(),"xyzzy"));
-        assertObjectsAreNotEqual(f,new Constant("xyzzy"));
-        assertObjectsAreNotEqual(f,new BoundFunction(new Identity(),"foo"));
-        assertObjectsAreNotEqual(f,new BoundFunction(new Constant("xyzzy"),"foo"));
-        assertObjectsAreNotEqual(f,new BoundFunction(null,"xyzzy"));
-        assertObjectsAreNotEqual(f,new BoundFunction(new Identity(),null));
-        assertObjectsAreEqual(new BoundFunction(null,null),new BoundFunction(null,null));
+        assertObjectsAreEqual(f,new BoundFunction<Object>(Identity.INSTANCE,"xyzzy"));
+        assertObjectsAreNotEqual(f,Constant.of("xyzzy"));
+        assertObjectsAreNotEqual(f,new BoundFunction<Object>(Identity.INSTANCE,"foo"));
+        assertObjectsAreNotEqual(f,new BoundFunction<Object>(Constant.of("xyzzy"),"foo"));
+        assertObjectsAreNotEqual(f,new BoundFunction<Object>(Identity.INSTANCE,null));
     }
 
     public void testAdaptNull() throws Exception {
@@ -84,7 +82,7 @@
     }
 
     public void testAdapt() throws Exception {
-        assertNotNull(BoundFunction.bind(new Identity(),"xyzzy"));
-        assertNotNull(BoundFunction.bind(new Identity(),null));
+        assertNotNull(BoundFunction.bind(Identity.INSTANCE,"xyzzy"));
+        assertNotNull(BoundFunction.bind(Identity.INSTANCE,null));
     }
 }

Modified: commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundPredicate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundPredicate.java?rev=666186&r1=666185&r2=666186&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundPredicate.java
(original)
+++ commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundPredicate.java
Tue Jun 10 09:34:42 2008
@@ -45,7 +45,7 @@
     // ------------------------------------------------------------------------
 
     protected Object makeFunctor() {
-        return new BoundPredicate(new Constant(true),"xyzzy");
+        return new BoundPredicate(Constant.TRUE,"xyzzy");
     }
 
     // Lifecycle
@@ -64,25 +64,23 @@
 
     public void testTest() throws Exception {
         {
-            Predicate p = new BoundPredicate(new UnaryFunctionUnaryPredicate(new Identity()),Boolean.TRUE);
+            Predicate p = new BoundPredicate(new UnaryFunctionUnaryPredicate<Boolean>(Identity.<Boolean>instance()),Boolean.TRUE);
             assertEquals(true,p.test());
         }
         {
-            Predicate p = new BoundPredicate(new UnaryFunctionUnaryPredicate(new Identity()),Boolean.FALSE);
+            Predicate p = new BoundPredicate(new UnaryFunctionUnaryPredicate<Boolean>(Identity.<Boolean>instance()),Boolean.FALSE);
             assertEquals(false,p.test());
         }
     }
 
     public void testEquals() throws Exception {
-        Predicate f = new BoundPredicate(new Constant(true),"xyzzy");
+        Predicate f = new BoundPredicate(Constant.TRUE,"xyzzy");
         assertEquals(f,f);
-        assertObjectsAreEqual(f,new BoundPredicate(new Constant(true),"xyzzy"));
-        assertObjectsAreNotEqual(f,new Constant(true));
-        assertObjectsAreNotEqual(f,new BoundPredicate(new Constant(true),"foo"));
-        assertObjectsAreNotEqual(f,new BoundPredicate(new Constant(false),"xyzzy"));
-        assertObjectsAreNotEqual(f,new BoundPredicate(null,"xyzzy"));
-        assertObjectsAreNotEqual(f,new BoundPredicate(new Constant(true),null));
-        assertObjectsAreEqual(new BoundPredicate(null,null),new BoundPredicate(null,null));
+        assertObjectsAreEqual(f,new BoundPredicate(Constant.TRUE,"xyzzy"));
+        assertObjectsAreNotEqual(f,Constant.TRUE);
+        assertObjectsAreNotEqual(f,new BoundPredicate(Constant.TRUE,"foo"));
+        assertObjectsAreNotEqual(f,new BoundPredicate(Constant.FALSE,"xyzzy"));
+        assertObjectsAreNotEqual(f,new BoundPredicate(Constant.TRUE,null));
     }
 
     public void testAdaptNull() throws Exception {
@@ -90,7 +88,7 @@
     }
 
     public void testAdapt() throws Exception {
-        assertNotNull(BoundPredicate.bind(new Constant(true),"xyzzy"));
-        assertNotNull(BoundPredicate.bind(new Constant(true),null));
+        assertNotNull(BoundPredicate.bind(Constant.TRUE,"xyzzy"));
+        assertNotNull(BoundPredicate.bind(Constant.TRUE,null));
     }
 }

Modified: commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundProcedure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundProcedure.java?rev=666186&r1=666185&r2=666186&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundProcedure.java
(original)
+++ commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBoundProcedure.java
Tue Jun 10 09:34:42 2008
@@ -45,7 +45,7 @@
     // ------------------------------------------------------------------------
 
     protected Object makeFunctor() {
-        return new BoundProcedure(new NoOp(),"xyzzy");
+        return new BoundProcedure(NoOp.INSTANCE,"xyzzy");
     }
 
     // Lifecycle
@@ -63,20 +63,18 @@
     // ------------------------------------------------------------------------
 
     public void testRun() throws Exception {
-        Procedure p = new BoundProcedure(new UnaryFunctionUnaryProcedure(new Identity()),Boolean.TRUE);
+        Procedure p = new BoundProcedure(new UnaryFunctionUnaryProcedure<Object>(Identity.INSTANCE),Boolean.TRUE);
         p.run();
     }
 
     public void testEquals() throws Exception {
-        Procedure f = new BoundProcedure(new NoOp(),"xyzzy");
+        Procedure f = new BoundProcedure(NoOp.INSTANCE,"xyzzy");
         assertEquals(f,f);
-        assertObjectsAreEqual(f,new BoundProcedure(new NoOp(),"xyzzy"));
-        assertObjectsAreNotEqual(f,new NoOp());
-        assertObjectsAreNotEqual(f,new BoundProcedure(new NoOp(),"foo"));
-        assertObjectsAreNotEqual(f,new BoundProcedure(new UnaryFunctionUnaryProcedure(new
Identity()),"xyzzy"));
-        assertObjectsAreNotEqual(f,new BoundProcedure(null,"xyzzy"));
-        assertObjectsAreNotEqual(f,new BoundProcedure(new NoOp(),null));
-        assertObjectsAreEqual(new BoundProcedure(null,null),new BoundProcedure(null,null));
+        assertObjectsAreEqual(f,new BoundProcedure(NoOp.INSTANCE,"xyzzy"));
+        assertObjectsAreNotEqual(f,NoOp.INSTANCE);
+        assertObjectsAreNotEqual(f,new BoundProcedure(NoOp.INSTANCE,"foo"));
+        assertObjectsAreNotEqual(f,new BoundProcedure(new UnaryFunctionUnaryProcedure<Object>(Identity.INSTANCE),"xyzzy"));
+        assertObjectsAreNotEqual(f,new BoundProcedure(NoOp.INSTANCE,null));
     }
 
     public void testAdaptNull() throws Exception {



Mime
View raw message