rwaldhoff 2003/11/24 16:18:58 Modified: functor/src/test/org/apache/commons/functor/core TestAll.java Added: functor/src/test/org/apache/commons/functor/core TestLimit.java functor/src/java/org/apache/commons/functor/core Limit.java Log: add Limit and tests Revision Changes Path 1.7 +3 -2 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/TestAll.java Index: TestAll.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/TestAll.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- TestAll.java 24 Feb 2003 11:48:09 -0000 1.6 +++ TestAll.java 25 Nov 2003 00:18:58 -0000 1.7 @@ -86,6 +86,7 @@ suite.addTest(TestIdentityPredicate.suite()); suite.addTest(TestLeftIdentityPredicate.suite()); suite.addTest(TestRightIdentityPredicate.suite()); + suite.addTest(TestLimit.suite()); suite.addTest(org.apache.commons.functor.core.composite.TestAll.suite()); suite.addTest(org.apache.commons.functor.core.collection.TestAll.suite()); 1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/TestLimit.java Index: TestLimit.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/core/TestLimit.java,v 1.1 2003/11/25 00:18:58 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; import junit.framework.Test; import junit.framework.TestSuite; import org.apache.commons.functor.BaseFunctorTest; import org.apache.commons.functor.BinaryPredicate; import org.apache.commons.functor.Predicate; import org.apache.commons.functor.UnaryPredicate; /** * @version $Revision: 1.1 $ $Date: 2003/11/25 00:18:58 $ * @author Rodney Waldhoff */ public class TestLimit extends BaseFunctorTest { // Conventional // ------------------------------------------------------------------------ public TestLimit(String testName) { super(testName); } public static Test suite() { return new TestSuite(TestLimit.class); } // Functor Testing Framework // ------------------------------------------------------------------------ protected Object makeFunctor() { return new Limit(3); } // Lifecycle // ------------------------------------------------------------------------ // Tests // ------------------------------------------------------------------------ public void testZero() throws Exception { Predicate p = new Limit(0); assertTrue(! p.test()); assertTrue(! p.test()); assertTrue(! p.test()); } public void testBadArgs() throws Exception { try { new Limit(-1); fail("Expected IllegalArgumentException"); } catch(IllegalArgumentException e) { // expected } } public void testTestNilary() throws Exception { Predicate p = new Limit(3); assertTrue(p.test()); assertTrue(p.test()); assertTrue(p.test()); assertTrue(! p.test()); } public void testTestUnary() throws Exception { UnaryPredicate p = new Limit(3); assertTrue(p.test(null)); assertTrue(p.test(null)); assertTrue(p.test(null)); assertTrue(! p.test(null)); } public void testTestBinary() throws Exception { BinaryPredicate p = new Limit(3); assertTrue(p.test(null,null)); assertTrue(p.test(null,null)); assertTrue(p.test(null,null)); assertTrue(! p.test(null,null)); } } 1.1 jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/Limit.java Index: Limit.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/core/Limit.java,v 1.1 2003/11/25 00:18:58 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; import org.apache.commons.functor.BinaryPredicate; import org.apache.commons.functor.Predicate; import org.apache.commons.functor.UnaryPredicate; /** * A predicate that returns <code>true</code> * the first <i>n</i> times it is invoked. * * @since 1.0 * @version $Revision: 1.1 $ $Date: 2003/11/25 00:18:58 $ * @author Jason Horman ([EMAIL PROTECTED]) * @author Rodney Waldhoff */ public final class Limit implements Predicate, UnaryPredicate, BinaryPredicate { public Limit(int count) { if(count < 0) { throw new IllegalArgumentException("Argument must be a non-negative integer."); } this.max = count; } public boolean test() { // stop incremeting when we've hit max, so we don't loop around if(current < max) { current++; return true; } else { return false; } } public boolean test(Object obj) { return test(); } public boolean test(Object a, Object b) { return test(); } public String toString() { return "Limit<" + max + ">"; } // instance variables //--------------------------------------------------------------- private int max; private int current = 0; }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]