rwaldhoff 2003/02/01 06:40:45
Modified: functor/src/java/org/apache/commons/functor/core/composite
UnaryCompositeBinaryFunction.java
functor/src/test/org/apache/commons/functor/core/composite
TestAll.java
Added: functor/src/java/org/apache/commons/functor/core/composite
BinaryCompositeBinaryFunction.java
functor/src/test/org/apache/commons/functor/core/composite
TestBinaryCompositeBinaryFunction.java
Log:
add BinaryCompositeBinaryFunction ( f(x,y) = f(g(x,y),h(x,y)) ) and tests
Revision Changes Path
1.2 +3 -3
jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/composite/UnaryCompositeBinaryFunction.java
Index: UnaryCompositeBinaryFunction.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/composite/UnaryCompositeBinaryFunction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- UnaryCompositeBinaryFunction.java 1 Feb 2003 14:23:05 -0000 1.1
+++ UnaryCompositeBinaryFunction.java 1 Feb 2003 14:40:45 -0000 1.2
@@ -123,7 +123,7 @@
}
public String toString() {
- return "CompositeUnaryFunction<" + binary + ";" + leftUnary + ";" +
rightUnary + ">";
+ return "UnaryCompositeBinaryFunction<" + binary + ";" + leftUnary + ";" +
rightUnary + ">";
}
// attributes
1.1
jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/composite/BinaryCompositeBinaryFunction.java
Index: BinaryCompositeBinaryFunction.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/composite/BinaryCompositeBinaryFunction.java,v
1.1 2003/02/01 14:40:45 rwaldhoff Exp $
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.functor.core.composite;
import java.io.Serializable;
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.BinaryFunction;
/**
* f(x,y) := f(g(x,y),h(x,y))
* <p>
* Note that although this class implements
* {@link Serializable}, a given instance will
* only be truly <code>Serializable</code> if all the
* underlying functors are. Attempts to serialize
* an instance whose delegates are not all
* <code>Serializable</code> will result in an exception.
* </p>
* @version $Revision: 1.1 $ $Date: 2003/02/01 14:40:45 $
* @author Rodney Waldhoff
*/
public class BinaryCompositeBinaryFunction implements BinaryFunction, Serializable {
// constructor
// ------------------------------------------------------------------------
public BinaryCompositeBinaryFunction(BinaryFunction f, BinaryFunction g,
BinaryFunction h) {
binary = f;
leftBinary = g;
rightBinary = h;
}
// function interface
// ------------------------------------------------------------------------
public Object evaluate(Object left, Object right) {
return binary.evaluate(leftBinary.evaluate(left,right),
rightBinary.evaluate(left,right));
}
public boolean equals(Object that) {
if(that instanceof BinaryCompositeBinaryFunction) {
return equals((BinaryCompositeBinaryFunction)that);
} else {
return false;
}
}
public boolean equals(BinaryCompositeBinaryFunction that) {
return (null != that) &&
(null == binary ? null == that.binary : binary.equals(that.binary)) &&
(null == leftBinary ? null == that.leftBinary :
leftBinary.equals(that.leftBinary)) &&
(null == rightBinary ? null == that.rightBinary :
rightBinary.equals(that.rightBinary));
}
public int hashCode() {
int hash = "BinaryCompositeBinaryFunction".hashCode();
if(null != binary) {
hash <<= 4;
hash ^= binary.hashCode();
}
if(null != leftBinary) {
hash <<= 4;
hash ^= leftBinary.hashCode();
}
if(null != rightBinary) {
hash <<= 4;
hash ^= rightBinary.hashCode();
}
return hash;
}
public String toString() {
return "BinaryCompositeBinaryFunction<" + binary + ";" + leftBinary + ";" +
rightBinary + ">";
}
// attributes
// ------------------------------------------------------------------------
private BinaryFunction binary = null;
private BinaryFunction leftBinary = null;
private BinaryFunction rightBinary = null;
}
1.4 +3 -2
jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/composite/TestAll.java
Index: TestAll.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/composite/TestAll.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestAll.java 1 Feb 2003 14:23:05 -0000 1.3
+++ TestAll.java 1 Feb 2003 14:40:45 -0000 1.4
@@ -90,6 +90,7 @@
suite.addTest(TestCompositeUnaryFunction.suite());
suite.addTest(TestUnaryCompositeBinaryFunction.suite());
+ suite.addTest(TestBinaryCompositeBinaryFunction.suite());
suite.addTest(TestTransposedFunction.suite());
suite.addTest(TestTransposedPredicate.suite());
1.1
jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/composite/TestBinaryCompositeBinaryFunction.java
Index: TestBinaryCompositeBinaryFunction.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/composite/TestBinaryCompositeBinaryFunction.java,v
1.1 2003/02/01 14:40:45 rwaldhoff Exp $
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.functor.core.composite;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.functor.BaseFunctorTest;
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.core.ConstantFunction;
import org.apache.commons.functor.core.IdentityFunction;
import org.apache.commons.functor.core.LeftIdentityFunction;
import org.apache.commons.functor.core.RightIdentityFunction;
/**
* @version $Revision: 1.1 $ $Date: 2003/02/01 14:40:45 $
* @author Rodney Waldhoff
*/
public class TestBinaryCompositeBinaryFunction extends BaseFunctorTest {
// Conventional
// ------------------------------------------------------------------------
public TestBinaryCompositeBinaryFunction(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestBinaryCompositeBinaryFunction.class);
}
// Functor Testing Framework
// ------------------------------------------------------------------------
protected Object makeFunctor() {
return new BinaryCompositeBinaryFunction(
new RightIdentityFunction(),
new ConstantFunction("left"),
new RightIdentityFunction());
}
// Lifecycle
// ------------------------------------------------------------------------
public void setUp() throws Exception {
super.setUp();
}
public void tearDown() throws Exception {
super.tearDown();
}
// Tests
// ------------------------------------------------------------------------
public void testEvaluate() throws Exception {
BinaryFunction f = new BinaryCompositeBinaryFunction(
new RightIdentityFunction(),
new ConstantFunction("K"),
new RightIdentityFunction());
assertEquals("right",f.evaluate("left","right"));
assertNull("right",f.evaluate("left",null));
assertEquals("right",f.evaluate(null,"right"));
}
public void testEquals() throws Exception {
BinaryFunction f = new BinaryCompositeBinaryFunction(
new LeftIdentityFunction(),
new ConstantFunction("left"),
new ConstantFunction("right"));
assertEquals(f,f);
assertObjectsAreEqual(f,new BinaryCompositeBinaryFunction(
new LeftIdentityFunction(),
new ConstantFunction("left"),
new ConstantFunction("right")));
assertObjectsAreNotEqual(f,new BinaryCompositeBinaryFunction(
new RightIdentityFunction(),
new ConstantFunction("left"),
new ConstantFunction("right")));
assertObjectsAreNotEqual(f,new BinaryCompositeBinaryFunction(
new LeftIdentityFunction(),
new RightIdentityFunction(),
new ConstantFunction("right")));
assertObjectsAreNotEqual(f,new BinaryCompositeBinaryFunction(
new LeftIdentityFunction(),
new ConstantFunction("left"),
new RightIdentityFunction()));
assertObjectsAreNotEqual(f,new
BinaryCompositeBinaryFunction(null,null,null));
assertObjectsAreEqual(
new BinaryCompositeBinaryFunction(null,null,null),
new BinaryCompositeBinaryFunction(null,null,null));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]