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 02:18:28 UTC
cvs commit: jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map TestLazyMap.java FunctoredMap.java FixedSizeMap.java TestAll.java TestPredicatedMap.java PredicatedMap.java LazyMap.java TestFixedSizeMap.java
rwaldhoff 2003/11/25 17:18:28
Modified: functor/src/test/org/apache/commons/functor/example
TestAll.java
Added: functor/src/test/org/apache/commons/functor/example/map
TestLazyMap.java FunctoredMap.java
FixedSizeMap.java TestAll.java
TestPredicatedMap.java PredicatedMap.java
LazyMap.java TestFixedSizeMap.java
Log:
more examples
Revision Changes Path
1.6 +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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TestAll.java 25 Nov 2003 23:33:15 -0000 1.5
+++ TestAll.java 26 Nov 2003 01:18:28 -0000 1.6
@@ -75,6 +75,7 @@
suite.addTest(FlexiMapExample.suite());
suite.addTest(QuicksortExample.suite());
suite.addTest(org.apache.commons.functor.example.lines.TestAll.suite());
+ suite.addTest(org.apache.commons.functor.example.map.TestAll.suite());
return suite;
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/TestLazyMap.java
Index: TestLazyMap.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/TestLazyMap.java,v 1.1 2003/11/26 01:18:28 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.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.functor.core.collection.Size;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/26 01:18:28 $
* @author Rodney Waldhoff
*/
public class TestLazyMap extends TestCase {
public TestLazyMap(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestLazyMap.class);
}
private Map baseMap = null;
private Map lazyMap = null;
private Map expectedMap = null;
public void setUp() throws Exception {
super.setUp();
expectedMap = new HashMap();
expectedMap.put("one",new Integer(3));
expectedMap.put("two",new Integer(3));
expectedMap.put("three", new Integer(5));
expectedMap.put("four", new Integer(4));
expectedMap.put("five", new Integer(4));
baseMap = new HashMap();
lazyMap = new LazyMap(baseMap,Size.instance());
}
public void tearDown() throws Exception {
super.tearDown();
baseMap = null;
lazyMap = null;
expectedMap = null;
}
// tests
public void test() {
for(Iterator iter = expectedMap.keySet().iterator(); iter.hasNext();) {
Object key = iter.next();
assertFalse(baseMap.containsKey(key));
assertFalse(lazyMap.containsKey(key));
assertEquals(expectedMap.get(key),lazyMap.get(key));
assertEquals(expectedMap.get(key),baseMap.get(key));
assertTrue(lazyMap.containsKey(key));
assertTrue(baseMap.containsKey(key));
}
assertEquals(expectedMap,lazyMap);
assertEquals(expectedMap,baseMap);
baseMap.clear();
for(Iterator iter = expectedMap.keySet().iterator(); iter.hasNext();) {
Object key = iter.next();
assertFalse(baseMap.containsKey(key));
assertFalse(lazyMap.containsKey(key));
assertEquals(expectedMap.get(key),lazyMap.get(key));
assertEquals(expectedMap.get(key),baseMap.get(key));
assertTrue(lazyMap.containsKey(key));
assertTrue(baseMap.containsKey(key));
}
assertEquals(expectedMap,lazyMap);
assertEquals(expectedMap,baseMap);
}
public void testBaseMapOverrides() {
assertEquals(new Integer(5),lazyMap.get("xyzzy"));
baseMap.put("xyzzy","xyzzy");
assertEquals("xyzzy",lazyMap.get("xyzzy"));
}
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/FunctoredMap.java
Index: FunctoredMap.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/FunctoredMap.java,v 1.1 2003/11/26 01:18:28 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.map;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.BinaryProcedure;
import org.apache.commons.functor.Procedure;
import org.apache.commons.functor.UnaryPredicate;
import org.apache.commons.functor.UnaryProcedure;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/26 01:18:28 $
* @author Rodney Waldhoff
*/
public class FunctoredMap implements Map {
public FunctoredMap(Map map) {
this.map = map;
}
public int hashCode() {
return map.hashCode();
}
public String toString() {
return map.toString();
}
public Collection values() {
return map.values();
}
public Set keySet() {
return map.keySet();
}
public Object get(Object key) {
return onget.evaluate(map,key);
}
public void clear() {
onclear.run(map);
}
public int size() {
return map.size();
}
public Object put(Object key, Object value) {
return onput.evaluate(map, new Object[] { key, value });
}
public void putAll(Map src) {
onputall.run(map, src);
}
public Set entrySet() {
return map.entrySet();
}
public boolean containsKey(Object key) {
return map.containsKey(key);
}
public boolean isEmpty() {
return map.isEmpty();
}
public Object remove(Object key) {
return onremove.evaluate(map,key);
}
public boolean equals(Object obj) {
return map.equals(obj);
}
public boolean containsValue(Object value) {
return map.containsValue(value);
}
// protected
protected void setOnClear(UnaryProcedure procedure) {
onclear = procedure;
}
protected void setOnPut(BinaryFunction function) {
onput = function;
}
protected void setOnGet(BinaryFunction function) {
onget = function;
}
protected void setOnPutAll(BinaryProcedure procedure) {
onputall = procedure;
}
protected void setOnRemove(BinaryFunction function) {
onremove = function;
}
// attributes
protected static final BinaryFunction DEFAULT_ON_PUT = new BinaryFunction() {
public Object evaluate(Object a, Object b) {
Map map = (Map)a;
Object key = Array.get(b,0);
Object value = Array.get(b,1);
return map.put(key,value);
}
};
private BinaryFunction onput = DEFAULT_ON_PUT;
protected static final BinaryFunction DEFAULT_ON_GET = new BinaryFunction() {
public Object evaluate(Object map, Object key) {
return ((Map)map).get(key);
}
};
private BinaryFunction onget = DEFAULT_ON_GET;
protected static final BinaryProcedure DEFAULT_ON_PUT_ALL = new BinaryProcedure() {
public void run(Object a, Object b) {
Map dest = (Map)a;
Map src = (Map)b;
dest.putAll(src);
}
};
private BinaryProcedure onputall = DEFAULT_ON_PUT_ALL;
protected static final BinaryFunction DEFAULT_ON_REMOVE = new BinaryFunction() {
public Object evaluate(Object a, Object key) {
Map map = (Map)a;
return map.remove(key);
}
};
private BinaryFunction onremove = DEFAULT_ON_REMOVE;
protected static final UnaryProcedure DEFAULT_ON_CLEAR = new UnaryProcedure() {
public void run(Object map) {
((Map)map).clear();
}
};
private UnaryProcedure onclear = DEFAULT_ON_CLEAR;
private Map map = null;
// inner classes
protected static class ContainsKey implements UnaryPredicate {
ContainsKey(Map map) {
this.map = map;
}
public boolean test(Object obj) {
return map.containsKey(obj);
}
private Map map = null;
}
protected static class Throw implements Procedure, UnaryProcedure, BinaryProcedure {
Throw(RuntimeException e) {
this.klass = e.getClass();
}
public void run() {
try {
throw (RuntimeException)(klass.newInstance());
} catch(IllegalAccessException e) {
throw new RuntimeException();
} catch (InstantiationException e) {
throw new RuntimeException();
}
}
public void run(Object obj) {
run();
}
public void run(Object a, Object b) {
run();
}
private Class klass = null;
}
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/FixedSizeMap.java
Index: FixedSizeMap.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/FixedSizeMap.java,v 1.1 2003/11/26 01:18:28 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.map;
import java.lang.reflect.Array;
import java.util.Map;
import org.apache.commons.functor.Algorithms;
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.BinaryProcedure;
import org.apache.commons.functor.adapter.BinaryProcedureBinaryFunction;
import org.apache.commons.functor.core.composite.UnaryNot;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/26 01:18:28 $
* @author Rodney Waldhoff
*/
public class FixedSizeMap extends FunctoredMap {
public FixedSizeMap(Map map) {
super(map);
setOnPut(new BinaryFunction() {
public Object evaluate(Object a, Object b) {
Map map = (Map)a;
Object key = Array.get(b,0);
Object value = Array.get(b,1);
if(map.containsKey(key)) {
return map.put(key,value);
} else {
throw new IllegalArgumentException();
}
}
});
setOnPutAll(new BinaryProcedure() {
public void run(Object a, Object b) {
Map dest = (Map)a;
Map src = (Map)b;
if(Algorithms.contains(src.keySet().iterator(),UnaryNot.not(new ContainsKey(dest)))) {
throw new IllegalArgumentException();
} else {
dest.putAll(src);
}
}
});
setOnRemove(new BinaryProcedureBinaryFunction(new Throw(new UnsupportedOperationException())));
setOnClear(new Throw(new UnsupportedOperationException()));
}
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/TestAll.java
Index: TestAll.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/TestAll.java,v 1.1 2003/11/26 01:18:28 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.map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/26 01:18:28 $
* @author Rodney Waldhoff
*/
public class TestAll extends TestCase {
public TestAll(String testName) {
super(testName);
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(TestPredicatedMap.suite());
suite.addTest(TestFixedSizeMap.suite());
suite.addTest(TestLazyMap.suite());
return suite;
}
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/TestPredicatedMap.java
Index: TestPredicatedMap.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/TestPredicatedMap.java,v 1.1 2003/11/26 01:18:28 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.map;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.functor.core.IsInstanceOf;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/26 01:18:28 $
* @author Rodney Waldhoff
*/
public class TestPredicatedMap extends TestCase {
public TestPredicatedMap(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestPredicatedMap.class);
}
private Map baseMap = null;
private Map predicatedMap = null;
public void setUp() throws Exception {
super.setUp();
baseMap = new HashMap();
predicatedMap = new PredicatedMap(baseMap,new IsInstanceOf(String.class),new IsInstanceOf(Integer.class));
}
public void tearDown() throws Exception {
super.tearDown();
baseMap = null;
predicatedMap = null;
}
// tests
public void testCanPutMatchingPair() {
predicatedMap.put("xyzzy", new Integer(17));
}
public void testCantPutInvalidValue() {
try {
predicatedMap.put("xyzzy", "xyzzy");
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
}
public void testCantPutInvalidKey() {
try {
predicatedMap.put(new Long(1), new Integer(3));
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
}
public void testOnlyValidPairsAreAddedInPutAll() {
HashMap map = new HashMap();
map.put("one", new Integer(17));
map.put("two", "rejected");
map.put(new Integer(17), "xyzzy");
map.put(new Integer(7), new Integer(3));
predicatedMap.putAll(map);
assertEquals(new Integer(17), predicatedMap.get("one"));
assertFalse(predicatedMap.containsKey("two"));
/*
assertFalse(predicatedMap.containsKey(new Integer(17)));
assertFalse(predicatedMap.containsKey(new Integer(7)));
*/
}
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/PredicatedMap.java
Index: PredicatedMap.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/PredicatedMap.java,v 1.1 2003/11/26 01:18:28 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.map;
import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.functor.BinaryPredicate;
import org.apache.commons.functor.BinaryProcedure;
import org.apache.commons.functor.UnaryPredicate;
import org.apache.commons.functor.adapter.BinaryProcedureBinaryFunction;
import org.apache.commons.functor.core.composite.ConditionalBinaryFunction;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/26 01:18:28 $
* @author Rodney Waldhoff
*/
public class PredicatedMap extends FunctoredMap {
public PredicatedMap(Map map, final UnaryPredicate keyPredicate, final UnaryPredicate valuePredicate) {
super(map);
setOnPut(new ConditionalBinaryFunction(
new BinaryPredicate() {
public boolean test(Object a, Object b) {
return keyPredicate.test(Array.get(b,0)) &&
valuePredicate.test(Array.get(b,1));
}
},
DEFAULT_ON_PUT,
BinaryProcedureBinaryFunction.adapt(new Throw(new IllegalArgumentException()))));
setOnPutAll(new BinaryProcedure() {
public void run(Object d, Object s) {
Map dest = (Map)d;
Map src = (Map)s;
for(Iterator iter = src.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry pair = (Map.Entry)iter.next();
if(keyPredicate.test(pair.getKey()) &&
valuePredicate.test(pair.getValue())) {
dest.put(pair.getKey(),pair.getValue());
}
}
}
});
}
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/LazyMap.java
Index: LazyMap.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/LazyMap.java,v 1.1 2003/11/26 01:18:28 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.map;
import java.util.Map;
import org.apache.commons.functor.BinaryFunction;
import org.apache.commons.functor.UnaryFunction;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/26 01:18:28 $
* @author Rodney Waldhoff
*/
public class LazyMap extends FunctoredMap {
public LazyMap(Map map, final UnaryFunction factory) {
super(map);
setOnGet(new BinaryFunction() {
public Object evaluate(Object m, Object key) {
Map map = (Map)m;
if(map.containsKey(key)) {
return map.get(key);
} else {
Object value = factory.evaluate(key);
map.put(key,value);
return value;
}
}
});
}
}
1.1 jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/TestFixedSizeMap.java
Index: TestFixedSizeMap.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/functor/src/test/org/apache/commons/functor/example/map/TestFixedSizeMap.java,v 1.1 2003/11/26 01:18:28 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.map;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* @version $Revision: 1.1 $ $Date: 2003/11/26 01:18:28 $
* @author Rodney Waldhoff
*/
public class TestFixedSizeMap extends TestCase {
public TestFixedSizeMap(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestFixedSizeMap.class);
}
private Map baseMap = null;
private Map fixedMap = null;
public void setUp() throws Exception {
super.setUp();
baseMap = new HashMap();
baseMap.put(new Integer(1),"one");
baseMap.put(new Integer(2),"two");
baseMap.put(new Integer(3),"three");
baseMap.put(new Integer(4),"four");
baseMap.put(new Integer(5),"five");
fixedMap = new FixedSizeMap(baseMap);
}
public void tearDown() throws Exception {
super.tearDown();
baseMap = null;
fixedMap = null;
}
// tests
public void testCantPutNewPair() {
try {
fixedMap.put("xyzzy","xyzzy");
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
}
public void testCantPutNewPairViaPutAll() {
Map map = new HashMap();
map.put(new Integer(1),"uno");
map.put("xyzzy","xyzzy");
map.put(new Integer(2),"dos");
try {
fixedMap.putAll(map);
fail("Expected IllegalArgumentException");
} catch(IllegalArgumentException e) {
// expected
}
assertEquals("one",fixedMap.get(new Integer(1)));
assertEquals("two",fixedMap.get(new Integer(2)));
}
public void testCantClear() {
try {
fixedMap.clear();
fail("Expected UnsupportedOperationException");
} catch(UnsupportedOperationException e) {
// expected
}
}
public void testCantRemove() {
try {
fixedMap.remove(new Integer(1));
fail("Expected UnsupportedOperationException");
} catch(UnsupportedOperationException e) {
// expected
}
}
public void testCanAssociateNewValueWithOldKey() {
fixedMap.put(new Integer(1),"uno");
assertEquals("uno",fixedMap.get(new Integer(1)));
assertEquals("two",fixedMap.get(new Integer(2)));
assertEquals("three",fixedMap.get(new Integer(3)));
}
public void testCanAssociateNewValueWithOldKeyViaPutAll() {
Map map = new HashMap();
map.put(new Integer(1),"uno");
map.put(new Integer(2),"dos");
fixedMap.putAll(map);
assertEquals("uno",fixedMap.get(new Integer(1)));
assertEquals("dos",fixedMap.get(new Integer(2)));
assertEquals("three",fixedMap.get(new Integer(3)));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org