commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r690030 - in /commons/sandbox/functor/trunk/src: main/java/org/apache/commons/functor/adapter/ test/java/org/apache/commons/functor/adapter/
Date Thu, 28 Aug 2008 22:33:44 GMT
Author: mbenson
Date: Thu Aug 28 15:33:43 2008
New Revision: 690030

URL: http://svn.apache.org/viewvc?rev=690030&view=rev
Log:
add missing binaryfunctor to corresponding unaryfunctor adapters

Added:
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryPredicateUnaryPredicate.java
  (with props)
    commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryProcedureUnaryProcedure.java
  (with props)
    commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryPredicateUnaryPredicate.java
  (with props)
    commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryProcedureUnaryProcedure.java
  (with props)
Modified:
    commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestAll.java

Added: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryPredicateUnaryPredicate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryPredicateUnaryPredicate.java?rev=690030&view=auto
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryPredicateUnaryPredicate.java
(added)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryPredicateUnaryPredicate.java
Thu Aug 28 15:33:43 2008
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.adapter;
+
+import org.apache.commons.functor.BinaryPredicate;
+import org.apache.commons.functor.UnaryPredicate;
+
+/**
+ * Adapts a BinaryPredicate as a UnaryPredicate by sending the same argument to both sides
of the BinaryPredicate.
+ * @version $Revision$ $Date$
+ * @author Matt Benson
+ */
+public final class BinaryPredicateUnaryPredicate<A> implements UnaryPredicate<A>
{
+    private BinaryPredicate<? super A, ? super A> predicate;
+
+    /**
+     * Create a new BinaryPredicateUnaryPredicate.
+     * @param predicate to adapt
+     */
+    public BinaryPredicateUnaryPredicate(BinaryPredicate<? super A, ? super A> predicate)
{
+        if (null == predicate) {
+            throw new IllegalArgumentException("BinaryPredicate argument was null");
+        }
+        this.predicate = predicate;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean test(A obj) {
+        return predicate.test(obj, obj);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object obj) {
+        return obj == this || obj instanceof BinaryPredicateUnaryPredicate
+                && equals((BinaryPredicateUnaryPredicate<?>) obj);
+    }
+
+    /**
+     * Learn whether another BinaryPredicateUnaryPredicate is equal to
+     * <code>this</code>.
+     * 
+     * @param that BinaryPredicateUnaryPredicate to check
+     * 
+     * @return whether equal
+     */
+    public boolean equals(BinaryPredicateUnaryPredicate<?> that) {
+        return that != null && that.predicate.equals(this.predicate);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode() {
+        return ("BinaryPredicateUnaryPredicate".hashCode() << 2) | predicate.hashCode();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return "BinaryPredicateUnaryPredicate<" + predicate + ">";
+    }
+
+    /**
+     * Adapt a BinaryFunction as a UnaryFunction.
+     * @param <A>
+     * @param predicate BinaryPredicate to adapt
+     * @return UnaryPredicate<A>
+     */
+    public static <A> UnaryPredicate<A> adapt(BinaryPredicate<? super A, ?
super A> predicate) {
+        return null == predicate ? null : new BinaryPredicateUnaryPredicate<A>(predicate);
+    }
+
+}

Propchange: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryPredicateUnaryPredicate.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryPredicateUnaryPredicate.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Thu Aug 28 15:33:43 2008
@@ -0,0 +1,5 @@
+Date
+Author
+Id
+Revision
+HeadURL

Added: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryProcedureUnaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryProcedureUnaryProcedure.java?rev=690030&view=auto
==============================================================================
--- commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryProcedureUnaryProcedure.java
(added)
+++ commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryProcedureUnaryProcedure.java
Thu Aug 28 15:33:43 2008
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.adapter;
+
+import org.apache.commons.functor.BinaryProcedure;
+import org.apache.commons.functor.UnaryProcedure;
+
+/**
+ * Adapts a BinaryProcedure as a UnaryProcedure by sending the same argument to both sides
of the BinaryProcedure.
+ * @version $Revision$ $Date$
+ * @author Matt Benson
+ */
+public final class BinaryProcedureUnaryProcedure<A> implements UnaryProcedure<A>
{
+    private BinaryProcedure<? super A, ? super A> procedure;
+
+    /**
+     * Create a new BinaryProcedureUnaryProcedure.
+     * @param procedure to adapt
+     */
+    public BinaryProcedureUnaryProcedure(BinaryProcedure<? super A, ? super A> procedure)
{
+        if (null == procedure) {
+            throw new IllegalArgumentException("BinaryProcedure argument was null");
+        }
+        this.procedure = procedure;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void run(A obj) {
+        procedure.run(obj, obj);
+    };
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object obj) {
+        return obj == this || obj instanceof BinaryProcedureUnaryProcedure
+                && equals((BinaryProcedureUnaryProcedure<?>) obj);
+    }
+
+    /**
+     * Learn whether another BinaryProcedureUnaryProcedure is equal to
+     * <code>this</code>.
+     * 
+     * @param that BinaryProcedureUnaryProcedure to check
+     * 
+     * @return whether equal
+     */
+    public boolean equals(BinaryProcedureUnaryProcedure<?> that) {
+        return that != null && that.procedure.equals(this.procedure);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode() {
+        return ("BinaryProcedureUnaryProcedure".hashCode() << 2) | procedure.hashCode();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString() {
+        return "BinaryProcedureUnaryProcedure<" + procedure + ">";
+    }
+
+    /**
+     * Adapt a BinaryProcedure as a UnaryProcedure.
+     * @param <A>
+     * @param predicate BinaryProcedure to adapt
+     * @return UnaryProcedure<A>
+     */
+    public static <A> UnaryProcedure<A> adapt(BinaryProcedure<? super A, ?
super A> procedure) {
+        return null == procedure ? null : new BinaryProcedureUnaryProcedure<A>(procedure);
+    }
+
+}

Propchange: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryProcedureUnaryProcedure.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/functor/trunk/src/main/java/org/apache/commons/functor/adapter/BinaryProcedureUnaryProcedure.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Thu Aug 28 15:33:43 2008
@@ -0,0 +1,5 @@
+Date
+Author
+Id
+Revision
+HeadURL

Modified: commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestAll.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestAll.java?rev=690030&r1=690029&r2=690030&view=diff
==============================================================================
--- commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestAll.java
(original)
+++ commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestAll.java
Thu Aug 28 15:33:43 2008
@@ -39,7 +39,6 @@
         suite.addTest(TestProcedureFunction.suite());
         suite.addTest(TestUnaryProcedureUnaryFunction.suite());
         suite.addTest(TestBinaryProcedureBinaryFunction.suite());
-        suite.addTest(TestBinaryFunctionUnaryFunction.suite());
 
         suite.addTest(TestFunctionPredicate.suite());
         suite.addTest(TestUnaryFunctionUnaryPredicate.suite());
@@ -76,6 +75,10 @@
         suite.addTest(TestRightBoundProcedure.suite());
         suite.addTest(TestFullyBoundProcedure.suite());
 
+        suite.addTest(TestBinaryFunctionUnaryFunction.suite());
+        suite.addTest(TestBinaryPredicateUnaryPredicate.suite());
+        suite.addTest(TestBinaryProcedureUnaryProcedure.suite());
+        
         return suite;
     }
 }

Added: commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryPredicateUnaryPredicate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryPredicateUnaryPredicate.java?rev=690030&view=auto
==============================================================================
--- commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryPredicateUnaryPredicate.java
(added)
+++ commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryPredicateUnaryPredicate.java
Thu Aug 28 15:33:43 2008
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.adapter;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.UnaryPredicate;
+import org.apache.commons.functor.core.Constant;
+import org.apache.commons.functor.core.IsNotSame;
+import org.apache.commons.functor.core.IsSame;
+
+/**
+ * @version $Revision$ $Date$
+ * @author Matt Benson
+ */
+public class TestBinaryPredicateUnaryPredicate extends BaseFunctorTest {
+
+    // Conventional
+    // ------------------------------------------------------------------------
+
+    public TestBinaryPredicateUnaryPredicate(String testName) {
+        super(testName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestBinaryPredicateUnaryPredicate.class);
+    }
+
+    // Functor Testing Framework
+    // ------------------------------------------------------------------------
+
+    protected Object makeFunctor() {
+        return new BinaryPredicateUnaryPredicate<Object>(IsSame.INSTANCE);
+    }
+
+    // Lifecycle
+    // ------------------------------------------------------------------------
+
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+
+    public void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    // Tests
+    // ------------------------------------------------------------------------
+
+    public void testTestWhenTrue() throws Exception {
+        UnaryPredicate<Object> p = new BinaryPredicateUnaryPredicate<Object>(IsSame.INSTANCE);
+        assertTrue(p.test(null));
+    }
+
+    public void testTestWhenFalse() throws Exception {
+        UnaryPredicate<Object> p = new BinaryPredicateUnaryPredicate<Object>(IsNotSame.INSTANCE);
+        assertFalse(p.test(null));
+    }
+
+    public void testEquals() throws Exception {
+        UnaryPredicate<Object> p = new BinaryPredicateUnaryPredicate<Object>(IsSame.INSTANCE);
+        assertEquals(p, p);
+        assertObjectsAreEqual(p, new BinaryPredicateUnaryPredicate<Object>(IsSame.INSTANCE));
+        assertObjectsAreNotEqual(p, Constant.truePredicate());
+        assertObjectsAreNotEqual(p, new BinaryPredicateUnaryPredicate<Object>(IsNotSame.INSTANCE));
+    }
+
+    public void testAdaptNull() throws Exception {
+        assertNull(BinaryPredicateUnaryPredicate.adapt(null));
+    }
+
+    public void testAdapt() throws Exception {
+        assertNotNull(BinaryPredicateUnaryPredicate.adapt(Constant.TRUE));
+    }
+}

Propchange: commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryPredicateUnaryPredicate.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryPredicateUnaryPredicate.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Thu Aug 28 15:33:43 2008
@@ -0,0 +1,5 @@
+Date
+Author
+Id
+Revision
+HeadURL

Added: commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryProcedureUnaryProcedure.java
URL: http://svn.apache.org/viewvc/commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryProcedureUnaryProcedure.java?rev=690030&view=auto
==============================================================================
--- commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryProcedureUnaryProcedure.java
(added)
+++ commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryProcedureUnaryProcedure.java
Thu Aug 28 15:33:43 2008
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.functor.adapter;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.functor.BaseFunctorTest;
+import org.apache.commons.functor.UnaryProcedure;
+import org.apache.commons.functor.core.NoOp;
+
+/**
+ * @version $Revision$ $Date$
+ * @author Matt Benson
+ */
+public class TestBinaryProcedureUnaryProcedure extends BaseFunctorTest {
+
+    // Conventional
+    // ------------------------------------------------------------------------
+
+    public TestBinaryProcedureUnaryProcedure(String testName) {
+        super(testName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestBinaryProcedureUnaryProcedure.class);
+    }
+
+    // Functor Testing Framework
+    // ------------------------------------------------------------------------
+
+    protected Object makeFunctor() {
+        return new BinaryProcedureUnaryProcedure<Object>(NoOp.INSTANCE);
+    }
+
+    // Lifecycle
+    // ------------------------------------------------------------------------
+
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+
+    public void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    // Tests
+    // ------------------------------------------------------------------------
+
+    public void testRun() throws Exception {
+        UnaryProcedure<Object> p = new BinaryProcedureUnaryProcedure<Object>(NoOp.INSTANCE);
+        p.run(null);
+    }
+
+    public void testEquals() throws Exception {
+        UnaryProcedure<Object> p = new BinaryProcedureUnaryProcedure<Object>(NoOp.INSTANCE);
+        assertEquals(p, p);
+        assertObjectsAreEqual(p, new BinaryProcedureUnaryProcedure<Object>(NoOp.INSTANCE));
+        assertObjectsAreNotEqual(p, NoOp.INSTANCE);
+        assertObjectsAreNotEqual(p, new BinaryProcedureUnaryProcedure<Object>(IgnoreLeftProcedure.adapt(NoOp.INSTANCE)));
+    }
+
+    public void testAdaptNull() throws Exception {
+        assertNull(BinaryProcedureUnaryProcedure.adapt(null));
+    }
+
+    public void testAdapt() throws Exception {
+        assertNotNull(BinaryProcedureUnaryProcedure.adapt(NoOp.INSTANCE));
+    }
+}

Propchange: commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryProcedureUnaryProcedure.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/functor/trunk/src/test/java/org/apache/commons/functor/adapter/TestBinaryProcedureUnaryProcedure.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Thu Aug 28 15:33:43 2008
@@ -0,0 +1,5 @@
+Date
+Author
+Id
+Revision
+HeadURL



Mime
View raw message