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/07/20 00:12:26 UTC
cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util TestEachElement.java
rwaldhoff 2003/07/19 15:12:25
Modified: functor/src/java/org/apache/commons/functor/generator/util
EachElement.java
functor/src/test/org/apache/commons/functor/generator
TestGenerator.java
functor/src/test/org/apache/commons/functor/generator/util
TestEachElement.java
Log:
simplify EachElement implementation, add tests
Revision Changes Path
1.3 +33 -89 jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/generator/util/EachElement.java
Index: EachElement.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/java/org/apache/commons/functor/generator/util/EachElement.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- EachElement.java 17 Jul 2003 22:44:46 -0000 1.2
+++ EachElement.java 19 Jul 2003 22:12:25 -0000 1.3
@@ -62,8 +62,6 @@
import java.util.Iterator;
import java.util.Map;
-import org.apache.commons.functor.UnaryProcedure;
-import org.apache.commons.functor.generator.BaseGenerator;
import org.apache.commons.functor.generator.Generator;
import org.apache.commons.functor.generator.IteratorToGeneratorAdapter;
@@ -75,90 +73,36 @@
* @author Jason Horman (jason@jhorman.org)
*/
-public class EachElement extends BaseGenerator {
-
- /***************************************************
- * Instance variables
- ***************************************************/
-
- private Generator generator = null;
-
- /***************************************************
- * Constructors
- ***************************************************/
-
- /**
- * Generator for collections.
- */
- public EachElement(Collection collection) {
- generator = new IteratorToGeneratorAdapter(collection.iterator());
- }
-
- /**
- * Generator for maps. Generates {@link java.util.Map.Entry} objects.
- */
- public EachElement(Map map) {
- generator = new IteratorToGeneratorAdapter(map.entrySet().iterator());
- }
-
- /**
- * Generator for arrays.
- */
- public EachElement(Object[] array) {
- generator = new IteratorToGeneratorAdapter(Arrays.asList(array).iterator());
- }
-
- /**
- * EachElement over a generator.
- */
- public EachElement(Generator generator) {
- this.generator = generator;
- }
-
- /**
- * EachElement over a iterator.
- */
- public EachElement(Iterator iter) {
- this.generator = new IteratorToGeneratorAdapter(iter);
- }
-
- /***************************************************
- * Instance methods
- ***************************************************/
-
- public void run(UnaryProcedure proc) {
- generator.run(proc);
- }
-
- public void stop() {
- generator.stop();
- }
-
- public String toString() {
- return "EachElement<" + generator + ">";
- }
-
- /***************************************************
- * Class methods
- ***************************************************/
-
- public static final EachElement from(Collection col) {
- return new EachElement(col);
- }
-
- public static final EachElement from(Map map) {
- return new EachElement(map);
- }
-
- public static final EachElement from(Object[] array) {
- return new EachElement(array);
- }
-
- public static final EachElement from(Iterator iter) {
- return new EachElement(iter);
- }
-
- public static final EachElement from(Generator gen) {
- return new EachElement(gen);
+public final class EachElement {
+ public static final Generator from(Collection collection) {
+ if(null == collection) {
+ return null;
+ } else {
+ return EachElement.from(collection.iterator());
+ }
+ }
+
+ public static final Generator from(Map map) {
+ if(null == map) {
+ return null;
+ } else {
+ return EachElement.from(map.entrySet().iterator());
+ }
+ }
+
+ public static final Generator from(Object[] array) {
+ if(null == array) {
+ return null;
+ } else {
+ return EachElement.from(Arrays.asList(array).iterator());
+ }
+ }
+
+ public static final Generator from(Iterator iter) {
+ if(null == iter) {
+ return null;
+ } else {
+ return new IteratorToGeneratorAdapter(iter);
+ }
}
}
1.4 +16 -16 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/TestGenerator.java
Index: TestGenerator.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/TestGenerator.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestGenerator.java 17 Jul 2003 22:44:46 -0000 1.3
+++ TestGenerator.java 19 Jul 2003 22:12:25 -0000 1.4
@@ -203,7 +203,7 @@
// ------------------------------------------------------------------------
public void testApply() {
- Collection result = new EachElement(list).apply(IdentityFunction.getIdentityFunction()).toCollection();
+ Collection result = EachElement.from(list).apply(IdentityFunction.getIdentityFunction()).toCollection();
assertNotNull(result);
assertEquals(list.size(),result.size());
assertEquals(list,result);
@@ -211,7 +211,7 @@
public void testApply2() {
Set set = new HashSet();
- assertSame(set,new EachElement(list).apply(IdentityFunction.getIdentityFunction()).to(set));
+ assertSame(set,EachElement.from(list).apply(IdentityFunction.getIdentityFunction()).to(set));
assertEquals(list.size(),set.size());
for(Iterator iter = list.iterator(); iter.hasNext(); ) {
assertTrue(set.contains(iter.next()));
@@ -220,7 +220,7 @@
public void testApply3() {
Set set = new HashSet();
- assertSame(set,new EachElement(listWithDuplicates).apply(IdentityFunction.getIdentityFunction()).to(set));
+ assertSame(set,EachElement.from(listWithDuplicates).apply(IdentityFunction.getIdentityFunction()).to(set));
assertTrue(listWithDuplicates.size() > set.size());
for(Iterator iter = listWithDuplicates.iterator(); iter.hasNext(); ) {
assertTrue(set.contains(iter.next()));
@@ -228,14 +228,14 @@
}
public void testContains() {
- assertTrue(new EachElement(list).contains(equalsThree));
- assertTrue(!new EachElement(list).contains(equalsTwentyThree));
+ assertTrue(EachElement.from(list).contains(equalsThree));
+ assertTrue(!EachElement.from(list).contains(equalsTwentyThree));
}
public void testDetect() {
- assertEquals(new Integer(3),new EachElement(list).detect(equalsThree));
+ assertEquals(new Integer(3),EachElement.from(list).detect(equalsThree));
try {
- new EachElement(list).detect(equalsTwentyThree);
+ EachElement.from(list).detect(equalsTwentyThree);
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
// expected
@@ -243,42 +243,42 @@
}
public void testDetectIfNone() {
- assertEquals(new Integer(3),new EachElement(list).detect(equalsThree,"Xyzzy"));
- assertEquals("Xyzzy",new EachElement(list).detect(equalsTwentyThree,"Xyzzy"));
+ assertEquals(new Integer(3),EachElement.from(list).detect(equalsThree,"Xyzzy"));
+ assertEquals("Xyzzy",EachElement.from(list).detect(equalsTwentyThree,"Xyzzy"));
}
public void testForEach() {
Summer summer = new Summer();
- new EachElement(list).foreach(summer);
+ EachElement.from(list).foreach(summer);
assertEquals(sum,summer.sum);
}
public void testSelect1() {
- Collection result = new EachElement(list).select(isEven).toCollection();
+ Collection result = EachElement.from(list).select(isEven).toCollection();
assertNotNull(result);
assertEquals(evens,result);
}
public void testSelect2() {
ArrayList result = new ArrayList();
- assertSame(result,new EachElement(list).select(isEven).to(result));
+ assertSame(result,EachElement.from(list).select(isEven).to(result));
assertEquals(evens,result);
}
public void testReject1() {
- Collection result = new EachElement(list).reject(isOdd).toCollection();
+ Collection result = EachElement.from(list).reject(isOdd).toCollection();
assertNotNull(result);
assertEquals(evens,result);
}
public void testReject2() {
ArrayList result = new ArrayList();
- assertSame(result,new EachElement(list).reject(isOdd).to(result));
+ assertSame(result,EachElement.from(list).reject(isOdd).to(result));
assertEquals(evens,result);
}
public void testInject() {
- Object result = new EachElement(list).inject(
+ Object result = EachElement.from(list).inject(
new Integer(0),
new BinaryFunction() {
public Object evaluate(Object a, Object b) {
1.2 +11 -6 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestEachElement.java
Index: TestEachElement.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/generator/util/TestEachElement.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestEachElement.java 30 Jun 2003 11:00:13 -0000 1.1
+++ TestEachElement.java 19 Jul 2003 22:12:25 -0000 1.2
@@ -60,6 +60,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -90,7 +91,7 @@
}
protected Object makeFunctor() throws Exception {
- return new EachElement(new ArrayList());
+ return EachElement.from(new ArrayList());
}
// Lifecycle
@@ -128,6 +129,14 @@
// Tests
// ------------------------------------------------------------------------
+ public void testFromNull() {
+ assertNull(EachElement.from((Collection)null));
+ assertNull(EachElement.from((Map)null));
+ assertNull(EachElement.from((Iterator)null));
+ assertNull(EachElement.from((Object[])null));
+ }
+
+
public void testWithList() {
Collection col = EachElement.from(list).toCollection();
assertEquals("[0, 1, 2, 3, 4]", col.toString());
@@ -170,8 +179,4 @@
assertEquals("[0, 1, 2, 3, 4]", col.toString());
}
- public void testWithGenerator() {
- Collection col = EachElement.from(EachElement.from(list)).toCollection();
- assertEquals("[0, 1, 2, 3, 4]", col.toString());
- }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org