commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r665767 - in /commons/sandbox/functor/trunk/src: main/java/org/apache/commons/functor/adapter/BinaryFunctionUnaryFunction.java test/java/org/apache/commons/functor/example/kata/one/BinaryFunctionUnaryFunction.java
Date Mon, 09 Jun 2008 16:55:04 GMT
Author: mbenson
Date: Mon Jun  9 09:55:04 2008
New Revision: 665767

URL: http://svn.apache.org/viewvc?rev=665767&view=rev
Log:
mv BinaryFunctionUnaryFunction to src/main

Added:
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryFunctionUnaryFunction.java
      - copied, changed from r661495, commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/one/BinaryFunctionUnaryFunction.java
Removed:
    commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/one/BinaryFunctionUnaryFunction.java

Copied: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryFunctionUnaryFunction.java
(from r661495, commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/one/BinaryFunctionUnaryFunction.java)
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryFunctionUnaryFunction.java?p2=commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryFunctionUnaryFunction.java&p1=commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/one/BinaryFunctionUnaryFunction.java&r1=661495&r2=665767&rev=665767&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/example/kata/one/BinaryFunctionUnaryFunction.java
(original)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryFunctionUnaryFunction.java
Mon Jun  9 09:55:04 2008
@@ -14,32 +14,82 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.commons.functor.example.kata.one;
+package org.apache.commons.functor.adapter;
 
 import org.apache.commons.functor.BinaryFunction;
 import org.apache.commons.functor.UnaryFunction;
 
 /**
+ * Adapts a BinaryFunction as a UnaryFunction by sending the same argument to both sides
of the BinaryFunction.
+ * It sounds nonsensical, but using Composite functions, can be made to do something useful.
  * @version $Revision$ $Date$
  * @author Rodney Waldhoff
  */
-public final class BinaryFunctionUnaryFunction implements UnaryFunction {
-    public BinaryFunctionUnaryFunction(BinaryFunction f) {
-        if (null == f) {
-            throw new NullPointerException();
-        } else {
-            this.function = f;
+public final class BinaryFunctionUnaryFunction<A, T> implements UnaryFunction<A,
T> {
+    private BinaryFunction<? super A, ? super A, ? extends T> function;
+
+    /**
+     * Create a new BinaryFunctionUnaryFunction.
+     * @param function to adapt
+     */
+    public BinaryFunctionUnaryFunction(BinaryFunction<? super A, ? super A, ? extends
T> function) {
+        if (null == function) {
+            throw new IllegalArgumentException("BinaryFunction argument was null");
         }
+        this.function = function;
     }
 
-    public Object evaluate(Object obj) {
+    /**
+     * 
+     * {@inheritDoc}
+     */
+    public T evaluate(A obj) {
         return function.evaluate(obj,obj);
     }
 
-    public static UnaryFunction adapt(BinaryFunction f) {
-        return null == f ? null : new BinaryFunctionUnaryFunction(f);
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object obj) {
+        return obj == this || obj instanceof BinaryFunctionUnaryFunction
+                && equals((BinaryFunctionUnaryFunction<?, ?>) obj);
+    }
+
+    /**
+     * Learn whether another BinaryFunctionUnaryFunction is equal to <code>this</code>.
+     * @param that BinaryFunctionUnaryFunction to check
+     * @return whether equal
+     */
+    public boolean equals(BinaryFunctionUnaryFunction<?, ?> that) {
+        return that != null && that.function.equals(this.function);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode() {
+        return ("BinaryFunctionUnaryFunction".hashCode() << 2) | function.hashCode();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return "BinaryFunctionUnaryFunction<" + function + ">";
     }
 
-    private BinaryFunction function;
+    /**
+     * Adapt a BinaryFunction as a UnaryFunction.
+     * @param <A>
+     * @param <T>
+     * @param function to adapt
+     * @return UnaryFunction<A, T>
+     */
+    public static <A, T> UnaryFunction<A, T> adapt(BinaryFunction<? super
A, ? super A, ? extends T> function) {
+        return null == function ? null : new BinaryFunctionUnaryFunction<A, T>(function);
+    }
 
 }



Mime
View raw message