You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2003/02/01 14:46:26 UTC
cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/composite TestCompositeUnaryFunction.java TestAll.java TestCompositeFunction.java
rwaldhoff 2003/02/01 05:46:26
Modified: functor/src/test/org/apache/commons/functor/core/composite
TestAll.java
Added: functor/src/java/org/apache/commons/functor/core/composite
CompositeUnaryFunction.java
functor/src/test/org/apache/commons/functor/core/composite
TestCompositeUnaryFunction.java
Removed: functor/src/java/org/apache/commons/functor/core/composite
CompositeFunction.java
functor/src/test/org/apache/commons/functor/core/composite
TestCompositeFunction.java
Log:
move CompositeFunction to CompositeUnaryFunction
Revision Changes Path
1.1 jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/composite/CompositeUnaryFunction.java
Index: CompositeUnaryFunction.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/composite/CompositeUnaryFunction.java,v 1.1 2003/02/01 13:46:26 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 apache@apache.org.
*
* 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 java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.functor.UnaryFunction;
/**
* A {@link UnaryFunction UnaryFunction}
* representing the composition of
* {@link UnaryFunction UnaryFunctions},
* "chaining" the output of one to the input
* of another. For example,
* <pre>new CompositeUnaryFunction(f).of(g)</code>
* {@link #evaluate evaluates} to
* <code>f.evaluate(g.evaluate(obj))</code>, and
* <pre>new CompositeUnaryFunction(f).of(g).of(h)</pre>
* {@link #evaluate evaluates} to
* <code>f.evaluate(g.evaluate(h.evaluate(obj)))</code>.
* <p>
* When the collection is empty, this function is
* an identity function.
* </p>
* <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 13:46:26 $
* @author Rodney Waldhoff
*/
public class CompositeUnaryFunction implements UnaryFunction, Serializable {
// constructor
// ------------------------------------------------------------------------
public CompositeUnaryFunction() {
}
public CompositeUnaryFunction(UnaryFunction f) {
of(f);
}
public CompositeUnaryFunction(UnaryFunction f, UnaryFunction g) {
of(f);
of(g);
}
// modifiers
// ------------------------------------------------------------------------
public CompositeUnaryFunction of(UnaryFunction f) {
list.add(f);
return this;
}
// predicate interface
// ------------------------------------------------------------------------
public Object evaluate(Object obj) {
Object result = obj;
for(ListIterator iter = list.listIterator(list.size()); iter.hasPrevious();) {
result = ((UnaryFunction)iter.previous()).evaluate(result);
}
return result;
}
public boolean equals(Object that) {
if(that instanceof CompositeUnaryFunction) {
return equals((CompositeUnaryFunction)that);
} else {
return false;
}
}
public boolean equals(CompositeUnaryFunction that) {
// by construction, list is never null
return null != that && list.equals(that.list);
}
public int hashCode() {
// by construction, list is never null
return "CompositeUnaryFunction".hashCode() ^ list.hashCode();
}
public String toString() {
return "CompositeUnaryFunction<" + list + ">";
}
// attributes
// ------------------------------------------------------------------------
private List list = new ArrayList();
}
1.2 +3 -3 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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestAll.java 27 Jan 2003 19:33:43 -0000 1.1
+++ TestAll.java 1 Feb 2003 13:46:26 -0000 1.2
@@ -88,7 +88,7 @@
suite.addTest(TestUnaryProcedureSequence.suite());
suite.addTest(TestBinaryProcedureSequence.suite());
- suite.addTest(TestCompositeFunction.suite());
+ suite.addTest(TestCompositeUnaryFunction.suite());
suite.addTest(TestTransposedFunction.suite());
suite.addTest(TestTransposedPredicate.suite());
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/composite/TestCompositeUnaryFunction.java
Index: TestCompositeUnaryFunction.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/composite/TestCompositeUnaryFunction.java,v 1.1 2003/02/01 13:46:26 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 apache@apache.org.
*
* 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.UnaryFunction;
import org.apache.commons.functor.core.ConstantFunction;
import org.apache.commons.functor.core.IdentityFunction;
/**
* @version $Revision: 1.1 $ $Date: 2003/02/01 13:46:26 $
* @author Rodney Waldhoff
*/
public class TestCompositeUnaryFunction extends BaseFunctorTest {
// Conventional
// ------------------------------------------------------------------------
public TestCompositeUnaryFunction(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestCompositeUnaryFunction.class);
}
// Functor Testing Framework
// ------------------------------------------------------------------------
protected Object makeFunctor() {
return new CompositeUnaryFunction(new IdentityFunction(),new ConstantFunction(new Integer(3)));
}
// Lifecycle
// ------------------------------------------------------------------------
public void setUp() throws Exception {
super.setUp();
}
public void tearDown() throws Exception {
super.tearDown();
}
// Tests
// ------------------------------------------------------------------------
public void testEvaluate() throws Exception {
// empty composite acts like identity function
assertEquals("xyzzy",(new CompositeUnaryFunction()).evaluate("xyzzy"));
assertNull(null,(new CompositeUnaryFunction()).evaluate(null));
assertEquals(new Integer(4),(new CompositeUnaryFunction(new ConstantFunction(new Integer(4)))).evaluate(null));
assertEquals(new Integer(4),(new CompositeUnaryFunction(new ConstantFunction(new Integer(4)),new ConstantFunction(new Integer(3)))).evaluate("xyzzy"));
assertEquals(new Integer(3),(new CompositeUnaryFunction(new ConstantFunction(new Integer(3)),new ConstantFunction(new Integer(4)))).evaluate("xyzzy"));
}
public void testOf() throws Exception {
CompositeUnaryFunction f = new CompositeUnaryFunction();
assertNull(f.evaluate(null));
for(int i=0;i<10;i++) {
f.of(new UnaryFunction() {
public Object evaluate(Object obj) {
if(obj instanceof Integer) {
return new Integer((((Integer)obj).intValue())+1);
} else {
return new Integer(1);
}
}
});
assertEquals(new Integer(i+1),f.evaluate(null));
}
}
public void testEquals() throws Exception {
CompositeUnaryFunction f = new CompositeUnaryFunction();
assertEquals(f,f);
CompositeUnaryFunction g = new CompositeUnaryFunction();
assertObjectsAreEqual(f,g);
for(int i=0;i<3;i++) {
f.of(new ConstantFunction("x"));
assertObjectsAreNotEqual(f,g);
g.of(new ConstantFunction("x"));
assertObjectsAreEqual(f,g);
f.of(new CompositeUnaryFunction(new ConstantFunction("y"),new ConstantFunction("z")));
assertObjectsAreNotEqual(f,g);
g.of(new CompositeUnaryFunction(new ConstantFunction("y"),new ConstantFunction("z")));
assertObjectsAreEqual(f,g);
}
assertObjectsAreNotEqual(f,new ConstantFunction("y"));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org