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 02:12:47 UTC

cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example FlexiMapExample.java

rwaldhoff    2003/03/04 17:12:47

  Modified:    functor/src/test/org/apache/commons/functor/example
                        FlexiMapExample.java
  Log:
  add StringConcatMap example
  
  Revision  Changes    Path
  1.4       +40 -4     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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FlexiMapExample.java	5 Mar 2003 00:16:38 -0000	1.3
  +++ FlexiMapExample.java	5 Mar 2003 01:12:47 -0000	1.4
  @@ -204,6 +204,15 @@
   
   	}
   
  +	public void testStringConcatMap() {
  +		Map map = makeStringConcatMap();
  +		map.put("key", "value 1");
  +		assertEquals("value 1",map.get("key"));
  +		map.put("key", "value 2");
  +		assertEquals("value 1, value 2",map.get("key"));
  +		map.put("key", "value 3");
  +		assertEquals("value 1, value 2, value 3",map.get("key"));
  +	}
   
       static class FlexiMap implements Map {
   
  @@ -224,9 +233,9 @@
           }        
           
           public Object put(Object key, Object value) {
  -            Object oldvalue = get(key);
  +            Object oldvalue = proxiedMap.get(key);
               proxiedMap.put(key, onPut.evaluate(oldvalue, value));
  -            return oldvalue;
  +            return onGet.evaluate(key,oldvalue);
           }
   
           public Object get(Object key) {
  @@ -336,6 +345,33 @@
   				}
   			},
   			null
  +		);
  +	}
  +
  +	private Map makeStringConcatMap() {
  +		return new FlexiMap(
  +			new BinaryFunction() {
  +				public Object evaluate(Object oldval, Object newval) {
  +					StringBuffer buf = null;
  +					if(null == oldval) {
  +						buf = new StringBuffer();
  +					} else {
  +						buf = (StringBuffer)oldval;
  +						buf.append(", ");
  +					}
  +					buf.append(newval);
  +					return buf;
  +				}
  +			},
  +			new BinaryFunction() {
  +				public Object evaluate(Object key, Object val) {
  +					if(null == val) {
  +						return null;
  +					} else {
  +						return ((StringBuffer)val).toString();
  +					}
  +				}
  +			}
   		);
   	}
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org