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/03/05 01:16:40 UTC
cvs commit: jakarta-commons-sandbox/functor project.xml
rwaldhoff 2003/03/04 16:16:40
Modified: functor/src/test/org/apache/commons/functor/example
FlexiMapExample.java
functor project.xml
Log:
add multimap example
Revision Changes Path
1.3 +119 -25 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/FlexiMapExample.java
Index: FlexiMapExample.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/FlexiMapExample.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FlexiMapExample.java 4 Mar 2003 23:11:14 -0000 1.2
+++ FlexiMapExample.java 5 Mar 2003 00:16:38 -0000 1.3
@@ -56,8 +56,11 @@
*/
package org.apache.commons.functor.example;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -73,6 +76,7 @@
import org.apache.commons.functor.adapter.UnaryProcedureUnaryFunction;
import org.apache.commons.functor.core.ConstantFunction;
import org.apache.commons.functor.core.IdentityFunction;
+import org.apache.commons.functor.core.IsInstanceOf;
import org.apache.commons.functor.core.IsNull;
import org.apache.commons.functor.core.RightIdentityFunction;
import org.apache.commons.functor.core.composite.ConditionalUnaryFunction;
@@ -154,6 +158,53 @@
assertEquals( new Integer(0), map.get("key") );
}
+ public void testIntegerValuesOnly() {
+ Map map = makeIntegerValuedMap();
+ map.put("key", new Integer(2));
+ assertEquals( new Integer(2), map.get("key") );
+ try {
+ map.put("key2","value");
+ fail("Expected ClassCastException");
+ } catch(ClassCastException e) {
+ // expected
+ }
+ }
+
+ public void testMultiMap() {
+ Map map = makeMultiMap();
+
+ map.put("key", "value 1");
+
+ {
+ Collection result = (Collection)(map.get("key"));
+ assertEquals(1,result.size());
+ assertEquals("value 1", result.iterator().next());
+ }
+
+ map.put("key", "value 2");
+
+ {
+ Collection result = (Collection)(map.get("key"));
+ assertEquals(2,result.size());
+ Iterator iter = result.iterator();
+ assertEquals("value 1", iter.next());
+ assertEquals("value 2", iter.next());
+ }
+
+ map.put("key", "value 3");
+
+ {
+ Collection result = (Collection)(map.get("key"));
+ assertEquals(3,result.size());
+ Iterator iter = result.iterator();
+ assertEquals("value 1", iter.next());
+ assertEquals("value 2", iter.next());
+ assertEquals("value 3", iter.next());
+ }
+
+ }
+
+
static class FlexiMap implements Map {
public FlexiMap(BinaryFunction putfn, BinaryFunction getfn) {
@@ -244,31 +295,74 @@
);
}
- private Map makeNullAsZeroMap() {
- return new FlexiMap(
- IgnoreLeftFunction.adapt(
- new ConditionalUnaryFunction(
- IsNull.getIsNullPredicate(),
- new ConstantFunction(new Integer(0)),
- IdentityFunction.getIdentityFunction()
- )
- ),
- null
- );
- }
+ private Map makeNullAsZeroMap() {
+ return new FlexiMap(
+ IgnoreLeftFunction.adapt(
+ new ConditionalUnaryFunction(
+ IsNull.getIsNullPredicate(),
+ new ConstantFunction(new Integer(0)),
+ IdentityFunction.getIdentityFunction()
+ )
+ ),
+ null
+ );
+ }
+
+ private Map makeIntegerValuedMap() {
+ return new FlexiMap(
+ IgnoreLeftFunction.adapt(
+ new ConditionalUnaryFunction(
+ new IsInstanceOf(Integer.class),
+ IdentityFunction.getIdentityFunction(),
+ UnaryProcedureUnaryFunction.adapt(throwCCE)
+ )
+ ),
+ null
+ );
+ }
+
+ private Map makeMultiMap() {
+ return new FlexiMap(
+ new BinaryFunction() {
+ public Object evaluate(Object oldval, Object newval) {
+ List list = null;
+ if(null == oldval) {
+ list = new ArrayList();
+ } else {
+ list = (List)oldval;
+ }
+ list.add(newval);
+ return list;
+ }
+ },
+ null
+ );
+ }
private interface UniversalProcedure extends Procedure, UnaryProcedure, BinaryProcedure { }
- private UniversalProcedure throwNPE = new UniversalProcedure() {
- public void run() {
- throw new NullPointerException();
- }
- public void run(Object obj) {
- run();
- }
- public void run(Object left, Object right) {
- run();
- }
- };
+ private UniversalProcedure throwNPE = new UniversalProcedure() {
+ public void run() {
+ throw new NullPointerException();
+ }
+ public void run(Object obj) {
+ run();
+ }
+ public void run(Object left, Object right) {
+ run();
+ }
+ };
+
+ private UniversalProcedure throwCCE = new UniversalProcedure() {
+ public void run() {
+ throw new ClassCastException();
+ }
+ public void run(Object obj) {
+ run();
+ }
+ public void run(Object left, Object right) {
+ run();
+ }
+ };
}
1.2 +1 -1 jakarta-commons-sandbox/functor/project.xml
Index: project.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/functor/project.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- project.xml 27 Jan 2003 19:33:37 -0000 1.1
+++ project.xml 5 Mar 2003 00:16:39 -0000 1.2
@@ -64,7 +64,7 @@
<dependencies>
<dependency>
<id>junit</id>
- <version>3.7</version>
+ <version>3.8.1</version>
</dependency>
<!-- these two aren't really required by functor, but by maven -->
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org