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/11/26 00:13:38 UTC
cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example TestAll.java
rwaldhoff 2003/11/25 15:13:38
Modified: functor/src/test/org/apache/commons/functor/generator/util
TestIntegerRange.java TestLongRange.java
functor/src/test/org/apache/commons/functor/example
TestAll.java
Added: functor/src/test/org/apache/commons/functor/example/lines
WordCount.java Lines.java Count.java Contains.java
Sum.java StartsWith.java TestLines.java
Log:
add examples
Revision Changes Path
1.4 +23 -2 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestIntegerRange.java
Index: TestIntegerRange.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestIntegerRange.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestIntegerRange.java 25 Nov 2003 00:01:15 -0000 1.3
+++ TestIntegerRange.java 25 Nov 2003 23:13:38 -0000 1.4
@@ -57,6 +57,9 @@
package org.apache.commons.functor.generator.util;
+import java.util.ArrayList;
+import java.util.List;
+
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -85,6 +88,24 @@
// Tests
// ------------------------------------------------------------------------
+
+ public void testGenerateListExample() {
+ // generates a collection of Integers from 0 (inclusive) to 10 (exclusive)
+ {
+ List list = (List)(new IntegerRange(0,10).to(new ArrayList()));
+ for(int i=0;i<10;i++) {
+ assertEquals(new Integer(i),list.get(i));
+ }
+ }
+
+ // generates a collection of Integers from 10 (inclusive) to 0 (exclusive)
+ {
+ List list = (List)(new IntegerRange(10,0).to(new ArrayList()));
+ for(int i=10;i>0;i--) {
+ assertEquals(new Integer(i),list.get(10-i));
+ }
+ }
+ }
public void testStepChecking() {
{
1.3 +23 -2 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestLongRange.java
Index: TestLongRange.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestLongRange.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestLongRange.java 25 Nov 2003 00:01:15 -0000 1.2
+++ TestLongRange.java 25 Nov 2003 23:13:38 -0000 1.3
@@ -57,6 +57,9 @@
package org.apache.commons.functor.generator.util;
+import java.util.ArrayList;
+import java.util.List;
+
import junit.framework.Test;
import junit.framework.TestSuite;
@@ -85,6 +88,24 @@
// Tests
// ------------------------------------------------------------------------
+
+ public void testGenerateListExample() {
+ // generates a collection of Integers from 0 (inclusive) to 10 (exclusive)
+ {
+ List list = (List)(new LongRange(0,10).to(new ArrayList()));
+ for(int i=0;i<10;i++) {
+ assertEquals(new Long(i),list.get(i));
+ }
+ }
+
+ // generates a collection of Integers from 10 (inclusive) to 0 (exclusive)
+ {
+ List list = (List)(new LongRange(10,0).to(new ArrayList()));
+ for(int i=10;i>0;i--) {
+ assertEquals(new Long(i),list.get(10-i));
+ }
+ }
+ }
public void testStepChecking() {
{
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/WordCount.java
Index: WordCount.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/WordCount.java,v 1.1 2003/11/25 23:13:38 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.example.lines;
import java.util.StringTokenizer;
import org.apache.commons.functor.UnaryFunction;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/25 23:13:38 $
* @author Rodney Waldhoff
*/
public class WordCount implements UnaryFunction {
public Object evaluate(Object obj) {
StringTokenizer toker = new StringTokenizer((String)obj);
return new Integer(toker.countTokens());
}
public static WordCount instance() {
return INSTANCE;
}
private static final WordCount INSTANCE = new WordCount();
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/Lines.java
Index: Lines.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/Lines.java,v 1.1 2003/11/25 23:13:38 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.example.lines;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import org.apache.commons.functor.UnaryProcedure;
import org.apache.commons.functor.generator.BaseGenerator;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/25 23:13:38 $
* @author Rodney Waldhoff
*/
public class Lines extends BaseGenerator {
public static Lines from(Reader reader) {
return new Lines(reader);
}
public static Lines from(File file) throws FileNotFoundException {
return new Lines(new FileReader(file));
}
public Lines(Reader reader) {
if(reader instanceof BufferedReader) {
in = (BufferedReader)reader;
} else {
in = new BufferedReader(reader);
}
}
public void run(UnaryProcedure proc) {
try {
for(String line = in.readLine(); line != null; line = in.readLine()) {
proc.run(line);
}
} catch(RuntimeException e) {
throw e;
} catch(Exception e) {
throw new TunneledException(e);
} finally {
stop();
}
}
public void stop() {
super.stop();
try {
in.close();
} catch(RuntimeException e) {
throw e;
} catch(Exception e) {
throw new TunneledException(e);
}
}
private BufferedReader in = null;
private class TunneledException extends RuntimeException {
TunneledException(Exception e) {
super(e.toString());
exception = e;
}
private Exception exception = null;
}
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/Count.java
Index: Count.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/Count.java,v 1.1 2003/11/25 23:13:38 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.example.lines;
import org.apache.commons.functor.Procedure;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/25 23:13:38 $
* @author Rodney Waldhoff
*/
public class Count implements Procedure {
public void run() {
count++;
}
public int getCount() {
return count;
}
private int count = 0;
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/Contains.java
Index: Contains.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/Contains.java,v 1.1 2003/11/25 23:13:38 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.example.lines;
import org.apache.commons.functor.UnaryPredicate;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/25 23:13:38 $
* @author Rodney Waldhoff
*/
public class Contains implements UnaryPredicate {
public Contains(String str) {
this.str = str;
}
public boolean test(Object obj) {
return null != obj && obj.toString().indexOf(str) != -1;
}
private String str = null;
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/Sum.java
Index: Sum.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/Sum.java,v 1.1 2003/11/25 23:13:38 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.example.lines;
import org.apache.commons.functor.BinaryFunction;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/25 23:13:38 $
* @author Rodney Waldhoff
*/
public class Sum implements BinaryFunction {
public Object evaluate(Object left, Object right) {
return new Integer( ((Number)left).intValue() + ((Number)right).intValue() );
}
public static final Sum instance() {
return INSTANCE;
}
private static final Sum INSTANCE = new Sum();
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/StartsWith.java
Index: StartsWith.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/StartsWith.java,v 1.1 2003/11/25 23:13:38 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.example.lines;
import org.apache.commons.functor.UnaryPredicate;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/25 23:13:38 $
* @author Rodney Waldhoff
*/
public class StartsWith implements UnaryPredicate {
public StartsWith(String prefix) {
this.prefix = prefix;
}
public boolean test(Object obj) {
return null != obj && obj.toString().startsWith(prefix);
}
private String prefix = null;
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/TestLines.java
Index: TestLines.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/lines/TestLines.java,v 1.1 2003/11/25 23:13:38 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.example.lines;
import java.io.Reader;
import java.io.StringReader;
import java.util.Collection;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.functor.adapter.ProcedureUnaryProcedure;
import org.apache.commons.functor.core.Offset;
import org.apache.commons.functor.core.collection.Size;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/25 23:13:38 $
* @author Rodney Waldhoff
*/
public class TestLines extends TestCase {
public TestLines(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestLines.class);
}
private Reader reader = null;
public void setUp() throws Exception {
super.setUp();
reader = new StringReader(DOCUMENT);
}
public void tearDown() throws Exception {
super.tearDown();
reader = null;
}
public void testCountCharacters() throws Exception {
Object result = Lines
.from(reader)
.apply(Size.instance())
.inject(new Integer(0),Sum.instance());
assertEquals("Expected 990 characters",new Integer(990),result);
}
public void testCountWords() throws Exception {
Object result = Lines
.from(reader)
.apply(WordCount.instance())
.inject(new Integer(0),Sum.instance());
assertEquals("Expected 157 words",new Integer(157),result);
}
public void testCountLines() throws Exception {
Count count = new Count();
Lines
.from(reader)
.foreach(ProcedureUnaryProcedure.adapt(count));
assertEquals("Expected 16 lines",16,count.getCount());
}
public void testCountWordsExcludingComments() throws Exception {
Object result = Lines
.from(reader)
.reject(new StartsWith("#"))
.apply(WordCount.instance())
.inject(new Integer(0),Sum.instance());
assertEquals("Expected 90 words",new Integer(90),result);
}
public void testCountCommentLines() throws Exception {
Count count = new Count();
Lines
.from(reader)
.select(new StartsWith("#"))
.foreach(ProcedureUnaryProcedure.adapt(count));
assertEquals("Expected 6 lines",6,count.getCount());
}
public void testFindMatchingLines() throws Exception {
Collection matches =
Lines
.from(reader)
.select(new Contains("lo"))
.toCollection();
assertEquals("Expected 5 lines",5,matches.size());
}
public void testFindMatchingFromTail() throws Exception {
Collection matches =
Lines
.from(reader)
.select(new Offset(8))
.select(new Contains("lo"))
.toCollection();
assertEquals("Expected 2 lines",2,matches.size());
}
private static final String DOCUMENT =
"# Lorem ipsum dolor sit amet, consectetuer adipiscing elit. \n" +
"# Aliquam erat volutpat. Donec nec eros. Etiam eget tortor eu \n" +
"tortor rutrum cursus. Pellentesque ornare pretium risus. Nulla \n" +
"libero pede, blandit nec, rutrum ut, sodales eu, enim. Duis leo. \n" +
"Nunc non est. Nunc consequat lobortis nisl. Vivamus et tortor in \n" +
"# nunc euismod elementum. Ut ut dui. Morbi semper, pede eu cursus \n" +
"# tristique, diam nunc luctus nibh, id tempor justo metus eget lorem.\n" +
"\n" +
"Quisque pharetra hendrerit odio. Etiam consequat ante et dui. Etiam \n" +
"accumsan elit ac augue. Mauris porta pulvinar tellus. Nulla ac enim ac \n"+
"augue ornare pharetra. Nunc dignissim eros et nibh. Sed justo dolor, \n" +
"# ullamcorper non, posuere eget, tempus non, ipsum. Praesent at velit. \n" +
"# Mauris tempor nisl sed tortor. Vestibulum ante ipsum primis in faucibus \n" +
"orci luctus et ultrices posuere cubilia Curae; Integer mollis malesuada \n" +
"dolor. Vestibulum cursus, mi in dictum blandit, eros enim convallis wisi, \n" +
"id ullamcorper metus tortor interdum dui.\n";
}
1.4 +3 -2 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/TestAll.java
Index: TestAll.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/TestAll.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestAll.java 12 Mar 2003 00:12:44 -0000 1.3
+++ TestAll.java 25 Nov 2003 23:13:38 -0000 1.4
@@ -74,6 +74,7 @@
suite.addTest(FlexiMapExample.suite());
suite.addTest(QuicksortExample.suite());
+ suite.addTest(org.apache.commons.functor.example.lines.TestLines.suite());
return suite;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org