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/25 19:22:50 UTC
cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor BaseFunctorTest.java
rwaldhoff 2003/11/25 10:22:50
Modified: functor/src/java/org/apache/commons/functor Algorithms.java
functor/src/test/org/apache/commons/functor/generator
TestAll.java
functor/src/java/org/apache/commons/functor/generator
IteratorToGeneratorAdapter.java
functor/src/test/org/apache/commons/functor
BaseFunctorTest.java
Added: functor/src/test/org/apache/commons/functor/generator
TestIteratorToGeneratorAdapter.java
Log:
* add functionality and tests to IteratorToGeneratorAdapater
* add javadocs to Algorithms
Revision Changes Path
1.8 +11 -11 jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/Algorithms.java
Index: Algorithms.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/Algorithms.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Algorithms.java 25 Nov 2003 17:49:35 -0000 1.7
+++ Algorithms.java 25 Nov 2003 18:22:50 -0000 1.8
@@ -86,7 +86,7 @@
public class Algorithms {
/**
- * apply(new IteratorToGeneratorAdapater(iter),func);
+ * Equivalent to <code>{@link #apply(Generator,UnaryFunction) apply}(new {@link org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),func)</code>.
*/
public static final Generator apply(Iterator iter, UnaryFunction func) {
return apply(new IteratorToGeneratorAdapter(iter),func);
@@ -109,7 +109,7 @@
}
/**
- * contains(new IteratorToGeneratorAdapater(iter),pred);
+ * Equivalent to <code>{@link #contains(Generator,UnaryPredicate) contains}(new {@link org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred)</code>.
*/
public static final boolean contains(Iterator iter, UnaryPredicate pred) {
return contains(new IteratorToGeneratorAdapter(iter),pred);
@@ -139,14 +139,14 @@
}
/**
- * detect(new IteratorToGeneratorAdapater(iter), pred);
+ * Equivalent to <code>{@link #detect(Generator,UnaryPredicate) detect}(new {@link org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred)</code>.
*/
public static final Object detect(Iterator iter, UnaryPredicate pred) {
return detect(new IteratorToGeneratorAdapter(iter), pred);
}
/**
- * detect(new IteratorToGeneratorAdapater(iter), pred, ifNone);
+ * Equivalent to <code>{@link #detect(Generator,UnaryPredicate,Object) detect}(new {@link org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred,ifNone)</code>.
*/
public static final Object detect(Iterator iter, UnaryPredicate pred, Object ifNone) {
return detect(new IteratorToGeneratorAdapter(iter), pred, ifNone);
@@ -206,7 +206,7 @@
}
/**
- * foreach(new IteratorToGeneratorAdapater(iter), proc);
+ * Equivalent to <code>{@link #foreach(Generator,UnaryProcedure) foreach}(new {@link org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),proc)</code>.
*/
public static final void foreach(Iterator iter, UnaryProcedure proc) {
foreach(new IteratorToGeneratorAdapter(iter), proc);
@@ -221,7 +221,7 @@
}
/**
- * inject(new IteratorToGeneratorAdapater(iter), seed, func);
+ * Equivalent to <code>{@link #inject(Generator,Object,BinaryFunction) inject}(new {@link org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),seed,func)</code>.
*/
public static final Object inject(Iterator iter, Object seed, BinaryFunction func) {
return inject(new IteratorToGeneratorAdapter(iter), seed, func);
@@ -258,7 +258,7 @@
/**
- * reject(new IteratorToGeneratorAdapater(iter), pred);
+ * Equivalent to <code>{@link #reject(Generator,UnaryPredicate) reject}(new {@link org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred)</code>.
*/
public static Generator reject(Iterator iter, UnaryPredicate pred) {
return reject(new IteratorToGeneratorAdapter(iter), pred);
@@ -283,7 +283,7 @@
}
/**
- * select(new IteratorToGeneratorAdapater(iter), pred);
+ * Equivalent to <code>{@link #select(Generator,UnaryPredicate) select}(new {@link org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred)</code>.
*/
public static final Generator select(Iterator iter, UnaryPredicate pred) {
return select(new IteratorToGeneratorAdapter(iter), pred);
@@ -308,7 +308,7 @@
}
/**
- * until(new IteratorToGeneratorAdapater(iter), pred);
+ * Equivalent to <code>{@link #until(Generator,UnaryPredicate) until}(new {@link org.apache.commons.functor.generator.IteratorToGeneratorAdapter IteratorToGeneratorAdapter}(iter),pred)</code>.
*/
public static final Generator until(Iterator iter, UnaryPredicate pred) {
return until(new IteratorToGeneratorAdapter(iter), pred);
1.3 +3 -2 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/TestAll.java
Index: TestAll.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/TestAll.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestAll.java 17 Jul 2003 17:48:03 -0000 1.2
+++ TestAll.java 25 Nov 2003 18:22:50 -0000 1.3
@@ -72,6 +72,7 @@
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(TestGenerator.suite());
+ suite.addTest(TestIteratorToGeneratorAdapter.suite());
suite.addTest(org.apache.commons.functor.generator.util.TestAll.suite());
return suite;
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/TestIteratorToGeneratorAdapter.java
Index: TestIteratorToGeneratorAdapter.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/TestIteratorToGeneratorAdapter.java,v 1.1 2003/11/25 18:22:50 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.generator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.functor.BaseFunctorTest;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/25 18:22:50 $
* @author Rodney Waldhoff
*/
public class TestIteratorToGeneratorAdapter extends BaseFunctorTest {
// Conventional
// ------------------------------------------------------------------------
public TestIteratorToGeneratorAdapter(String name) {
super(name);
}
public static Test suite() {
return new TestSuite(TestIteratorToGeneratorAdapter.class);
}
public Object makeFunctor() {
List list = new ArrayList();
list.add("1");
return new IteratorToGeneratorAdapter(list.iterator());
}
// Lifecycle
// ------------------------------------------------------------------------
private List list = null;
public void setUp() throws Exception {
super.setUp();
list = new ArrayList();
list.add("1");
list.add("two");
list.add("c");
}
public void tearDown() throws Exception {
super.tearDown();
list = null;
}
// Tests
// ------------------------------------------------------------------------
public void testAdaptNull() {
assertNull(IteratorToGeneratorAdapter.adapt(null));
}
public void testAdaptNonNull() {
assertNotNull(IteratorToGeneratorAdapter.adapt(list.iterator()));
}
public void testGenerate() {
Iterator iter = list.iterator();
Generator gen = new IteratorToGeneratorAdapter(iter);
List list2 = new ArrayList();
list2.addAll(gen.toCollection());
assertEquals(list,list2);
}
public void testConstructNull() {
try {
new IteratorToGeneratorAdapter(null);
fail("Expected NullPointerException");
} catch(NullPointerException e) {
// expected
}
}
public void testEquals() {
Iterator iter = list.iterator();
Generator gen = new IteratorToGeneratorAdapter(iter);
assertObjectsAreEqual(gen,gen);
assertObjectsAreEqual(gen,new IteratorToGeneratorAdapter(iter));
}
}
1.4 +45 -20 jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/generator/IteratorToGeneratorAdapter.java
Index: IteratorToGeneratorAdapter.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/generator/IteratorToGeneratorAdapter.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- IteratorToGeneratorAdapter.java 17 Jul 2003 22:44:45 -0000 1.3
+++ IteratorToGeneratorAdapter.java 25 Nov 2003 18:22:50 -0000 1.4
@@ -62,32 +62,28 @@
import java.util.Iterator;
/**
- * Adapts an iterator into a generator.
- *
+ * Adapts an {@link Iterator} to the {@link Generator} interface.
+ *
* @since 1.0
* @version $Revision$ $Date$
- * @author Jason Horman (jason@jhorman.org)
+ * @author Jason Horman (jason@jhorman.org)
+ * @author Rodney Waldhoff
*/
+public final class IteratorToGeneratorAdapter extends BaseGenerator {
-public class IteratorToGeneratorAdapter extends BaseGenerator {
-
- /***************************************************
- * Instance variables
- ***************************************************/
-
- private Iterator iter = null;
-
- /***************************************************
- * Constructors
- ***************************************************/
+ // constructors
+ //-----------------------------------------------------
public IteratorToGeneratorAdapter(Iterator iter) {
- this.iter = iter;
+ if(null == iter) {
+ throw new NullPointerException();
+ } else {
+ this.iter = iter;
+ }
}
- /***************************************************
- * Instance methods
- ***************************************************/
+ // instance methods
+ //-----------------------------------------------------
public void run(UnaryProcedure proc) {
while(iter.hasNext()) {
@@ -96,7 +92,36 @@
}
}
+ public boolean equals(Object obj) {
+ if(obj instanceof IteratorToGeneratorAdapter) {
+ IteratorToGeneratorAdapter that = (IteratorToGeneratorAdapter)obj;
+ return this.iter.equals(that.iter);
+ } else {
+ return false;
+ }
+ }
+
+ public int hashCode() {
+ int hash = "IteratorToGeneratorAdapater".hashCode();
+ hash <<= 2;
+ hash ^= iter.hashCode();
+ return hash;
+ }
+
public String toString() {
return "IteratorToGeneratorAdapter<" + iter + ">";
}
+
+
+ // class methods
+ //-----------------------------------------------------
+
+ public static IteratorToGeneratorAdapter adapt(Iterator iter) {
+ return null == iter ? null : new IteratorToGeneratorAdapter(iter);
+ }
+
+ // instance variables
+ //-----------------------------------------------------
+
+ private Iterator iter = null;
}
1.4 +5 -5 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/BaseFunctorTest.java
Index: BaseFunctorTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/BaseFunctorTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- BaseFunctorTest.java 28 Jan 2003 12:54:37 -0000 1.3
+++ BaseFunctorTest.java 25 Nov 2003 18:22:50 -0000 1.4
@@ -92,7 +92,7 @@
// ------------------------------------------------------------------------
protected abstract Object makeFunctor() throws Exception;
-
+
// Tests
// ------------------------------------------------------------------------
@@ -137,14 +137,14 @@
// protected utils
// ------------------------------------------------------------------------
- protected void assertObjectsAreEqual(Object a, Object b) {
+ public static void assertObjectsAreEqual(Object a, Object b) {
assertEquals(a,b);
assertEquals(b,a);
assertEquals(a.hashCode(),b.hashCode());
assertEquals(a.toString(),b.toString()); // not strictly required
}
- protected void assertObjectsAreNotEqual(Object a, Object b) {
+ public static void assertObjectsAreNotEqual(Object a, Object b) {
assertTrue(!a.equals(b));
assertTrue(!b.equals(a));
assertTrue(a.hashCode() != b.hashCode()); // not strictly required
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org