Add TransformedMap and TransformedSortedMap

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131095 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-05-17 14:53:11 +00:00
parent d10c1d3207
commit 7bbd45c3ec
5 changed files with 708 additions and 2 deletions

View File

@ -0,0 +1,280 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TransformedMap.java,v 1.1 2003/05/17 14:53:11 scolebourne 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 names 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.collections.decorators;
import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.Transformer;
/**
* <code>TransformedMap</code> decorates another <code>Map</code>
* to transform objects that are added.
* <p>
* The Map put methods and Map.Entry setValue method are affected by this class.
* Thus objects must be removed or searched for using their transformed form.
* For example, if the transformation converts Strings to Integers, you must
* use the Integer form to remove objects.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/05/17 14:53:11 $
*
* @author Stephen Colebourne
*/
public class TransformedMap extends AbstractMapDecorator {
/** The transformer to use for the key */
protected final Transformer keyTransformer;
/** The transformer to use for the value */
protected final Transformer valueTransformer;
/**
* Factory method to create a transforming map.
* <p>
* If there are any elements already in the map being decorated, they
* are NOT transformed.
*
* @param map the map to decorate, must not be null
* @param keyTransformer the transformer to use for key conversion, null means no conversion
* @param valueTransformer the transformer to use for value conversion, null means no conversion
* @throws IllegalArgumentException if map is null
*/
public static Map decorate(Map map, Transformer keyTransformer, Transformer valueTransformer) {
return new TransformedMap(map, keyTransformer, valueTransformer);
}
/**
* Constructor that wraps (not copies).
* <p>
* If there are any elements already in the collection being decorated, they
* are NOT transformed.
*
* @param map the map to decorate, must not be null
* @param keyTransformer the transformer to use for key conversion, null means no conversion
* @param valueTransformer the transformer to use for value conversion, null means no conversion
* @throws IllegalArgumentException if map is null
*/
protected TransformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer) {
super(map);
this.keyTransformer = keyTransformer;
this.valueTransformer = valueTransformer;
}
/**
* Transforms a key.
* <p>
* The transformer itself may throw an exception if necessary.
*
* @param object the object to transform
* @throws the transformed object
*/
protected Object transformKey(Object object) {
if (keyTransformer == null) {
return object;
}
return keyTransformer.transform(object);
}
/**
* Transforms a value.
* <p>
* The transformer itself may throw an exception if necessary.
*
* @param object the object to transform
* @throws the transformed object
*/
protected Object transformValue(Object object) {
if (valueTransformer == null) {
return object;
}
return valueTransformer.transform(object);
}
/**
* Transforms a map.
* <p>
* The transformer itself may throw an exception if necessary.
*
* @param map the map to transform
* @throws the transformed object
*/
protected Map transformMap(Map map) {
Map result = new HashMap(map.size());
for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry) it.next();
result.put(transformKey(entry.getKey()), transformValue(entry.getValue()));
}
return result;
}
//-----------------------------------------------------------------------
public Object put(Object key, Object value) {
key = transformKey(key);
value = transformValue(value);
return getMap().put(key, value);
}
public void putAll(Map mapToCopy) {
mapToCopy = transformMap(mapToCopy);
getMap().putAll(mapToCopy);
}
public Set entrySet() {
if (valueTransformer == null) {
return map.entrySet();
}
return new TransformedMapEntrySet(map.entrySet(), valueTransformer);
}
//-----------------------------------------------------------------------
/**
* Implementation of an entry set that uses a transforming map entry.
*/
protected static class TransformedMapEntrySet extends AbstractCollectionDecorator implements Set {
/** The transformer to use */
private final Transformer valueTransformer;
protected TransformedMapEntrySet(Set set, Transformer valueTransformer) {
super(set);
this.valueTransformer = valueTransformer;
}
public Iterator iterator() {
return new TransformedMapEntrySetIterator(collection.iterator(), valueTransformer);
}
public Object[] toArray() {
Object[] array = collection.toArray();
for (int i = 0; i < array.length; i++) {
array[i] = new TransformedMapEntry((Map.Entry) array[i], valueTransformer);
}
return array;
}
public Object[] toArray(Object array[]) {
Object[] result = array;
if (array.length > 0) {
// we must create a new array to handle multi-threaded situations
// where another thread could access data before we decorate it
result = (Object[]) Array.newInstance(array.getClass().getComponentType(), 0);
}
result = collection.toArray(result);
for (int i = 0; i < result.length; i++) {
result[i] = new TransformedMapEntry((Map.Entry) result[i], valueTransformer);
}
// check to see if result should be returned straight
if (result.length > array.length) {
return result;
}
// copy back into input array to fulfil the method contract
System.arraycopy(result, 0, array, 0, result.length);
if (array.length > result.length) {
array[result.length] = null;
}
return array;
}
}
/**
* Implementation of an entry set iterator.
*/
protected static class TransformedMapEntrySetIterator extends AbstractIteratorDecorator {
/** The transformer to use */
private final Transformer valueTransformer;
protected TransformedMapEntrySetIterator(Iterator iterator, Transformer valueTransformer) {
super(iterator);
this.valueTransformer = valueTransformer;
}
public Object next() {
Map.Entry entry = (Map.Entry) iterator.next();
return new TransformedMapEntry(entry, valueTransformer);
}
}
/**
* Implementation of a map entry that transforms additions.
*/
protected static class TransformedMapEntry extends AbstractMapEntryDecorator {
/** The transformer to use */
private final Transformer valueTransformer;
protected TransformedMapEntry(Map.Entry entry, Transformer valueTransformer) {
super(entry);
this.valueTransformer = valueTransformer;
}
public Object setValue(Object object) {
if (valueTransformer != null) {
object = valueTransformer.transform(object);
}
return entry.setValue(object);
}
}
}

View File

@ -0,0 +1,148 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/TransformedSortedMap.java,v 1.1 2003/05/17 14:53:11 scolebourne 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 names 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.collections.decorators;
import java.util.Comparator;
import java.util.SortedMap;
import org.apache.commons.collections.Transformer;
/**
* <code>TransformedSortedMap</code> decorates another <code>SortedMap </code>
* to transform objects that are added.
* <p>
* The Map put methods and Map.Entry setValue method are affected by this class.
* Thus objects must be removed or searched for using their transformed form.
* For example, if the transformation converts Strings to Integers, you must
* use the Integer form to remove objects.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/05/17 14:53:11 $
*
* @author Stephen Colebourne
*/
public class TransformedSortedMap extends TransformedMap implements SortedMap {
/**
* Factory method to create a transforming sorted map.
* <p>
* If there are any elements already in the map being decorated, they
* are NOT transformed.
*
* @param map the map to decorate, must not be null
* @param keyTransformer, the predicate to validate the keys, null means no transformation
* @param valueTransformer, the predicate to validate to values, null means no transformation
* @throws IllegalArgumentException if the map is null
*/
public static SortedMap decorate(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) {
return new TransformedSortedMap(map, keyTransformer, valueTransformer);
}
/**
* Constructor that wraps (not copies).
* <p>
* If there are any elements already in the collection being decorated, they
* are NOT transformed.
*
* @param map the map to decorate, must not be null
* @param keyTransformer, the predicate to validate the keys, null means no transformation
* @param valueTransformer, the predicate to validate to values, null means no transformation
* @throws IllegalArgumentException if the map is null
*/
protected TransformedSortedMap(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) {
super(map, keyTransformer, valueTransformer);
}
/**
* Gets the map being decorated.
*
* @return the decorated map
*/
protected SortedMap getSortedMap() {
return (SortedMap) map;
}
//-----------------------------------------------------------------------
public Object firstKey() {
return getSortedMap().firstKey();
}
public Object lastKey() {
return getSortedMap().lastKey();
}
public Comparator comparator() {
return getSortedMap().comparator();
}
public SortedMap subMap(Object fromKey, Object toKey) {
SortedMap map = getSortedMap().subMap(fromKey, toKey);
return new TransformedSortedMap(map, keyTransformer, valueTransformer);
}
public SortedMap headMap(Object toKey) {
SortedMap map = getSortedMap().headMap(toKey);
return new TransformedSortedMap(map, keyTransformer, valueTransformer);
}
public SortedMap tailMap(Object fromKey) {
SortedMap map = getSortedMap().tailMap(fromKey);
return new TransformedSortedMap(map, keyTransformer, valueTransformer);
}
}

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestAll.java,v 1.3 2003/05/11 13:18:27 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestAll.java,v 1.4 2003/05/17 14:53:11 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -65,7 +65,7 @@ import junit.framework.TestSuite;
* Entry point for all collections decorators tests. * Entry point for all collections decorators tests.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.3 $ $Date: 2003/05/11 13:18:27 $ * @version $Revision: 1.4 $ $Date: 2003/05/17 14:53:11 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -90,8 +90,10 @@ public class TestAll extends TestCase {
suite.addTest(TestTransformedBuffer.suite()); suite.addTest(TestTransformedBuffer.suite());
suite.addTest(TestTransformedCollection.suite()); suite.addTest(TestTransformedCollection.suite());
suite.addTest(TestTransformedList.suite()); suite.addTest(TestTransformedList.suite());
suite.addTest(TestTransformedMap.suite());
suite.addTest(TestTransformedSet.suite()); suite.addTest(TestTransformedSet.suite());
suite.addTest(TestTransformedSortedBag.suite()); suite.addTest(TestTransformedSortedBag.suite());
suite.addTest(TestTransformedSortedMap.suite());
suite.addTest(TestTransformedSortedSet.suite()); suite.addTest(TestTransformedSortedSet.suite());
return suite; return suite;
} }

View File

@ -0,0 +1,138 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestTransformedMap.java,v 1.1 2003/05/17 14:53:11 scolebourne 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 names 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.collections.decorators;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.TestMap;
/**
* Extension of {@link TestMap} for exercising the {@link TransformedMap}
* implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/05/17 14:53:11 $
*
* @author Stephen Colebourne
*/
public class TestTransformedMap extends TestMap {
public TestTransformedMap(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestTransformedMap.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestTransformedMap.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
public Map makeEmptyMap() {
return TransformedMap.decorate(new HashMap(), TestTransformedCollection.NOOP_TRANSFORMER, TestTransformedCollection.NOOP_TRANSFORMER);
}
public void testTransformedMap() {
Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "6"};
Map map = TransformedMap.decorate(new HashMap(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER, null);
assertEquals(0, map.size());
for (int i = 0; i < els.length; i++) {
map.put(els[i], els[i]);
assertEquals(i + 1, map.size());
assertEquals(true, map.containsKey(new Integer((String) els[i])));
assertEquals(false, map.containsKey(els[i]));
assertEquals(true, map.containsValue(els[i]));
assertEquals(els[i], map.get(new Integer((String) els[i])));
}
assertEquals(null, map.remove(els[0]));
assertEquals(els[0], map.remove(new Integer((String) els[0])));
map = TransformedMap.decorate(new HashMap(), null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
assertEquals(0, map.size());
for (int i = 0; i < els.length; i++) {
map.put(els[i], els[i]);
assertEquals(i + 1, map.size());
assertEquals(true, map.containsValue(new Integer((String) els[i])));
assertEquals(false, map.containsValue(els[i]));
assertEquals(true, map.containsKey(els[i]));
assertEquals(new Integer((String) els[i]), map.get(els[i]));
}
assertEquals(new Integer((String) els[0]), map.remove(els[0]));
Set entrySet = map.entrySet();
Map.Entry[] array = (Map.Entry[]) entrySet.toArray(new Map.Entry[0]);
array[0].setValue("66");
assertEquals(new Integer(66), array[0].getValue());
assertEquals(new Integer(66), map.get(array[0].getKey()));
Map.Entry entry = (Map.Entry) entrySet.iterator().next();
entry.setValue("88");
assertEquals(new Integer(88), entry.getValue());
assertEquals(new Integer(88), map.get(entry.getKey()));
}
}

View File

@ -0,0 +1,138 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestTransformedSortedMap.java,v 1.1 2003/05/17 14:53:11 scolebourne 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 names 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.collections.decorators;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.TestSortedMap;
/**
* Extension of {@link TestSortedMap} for exercising the {@link TransformedSortedMap}
* implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/05/17 14:53:11 $
*
* @author Stephen Colebourne
*/
public class TestTransformedSortedMap extends TestSortedMap {
public TestTransformedSortedMap(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestTransformedSortedMap.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestTransformedSortedMap.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
public Map makeEmptyMap() {
return TransformedMap.decorate(new HashMap(), TestTransformedCollection.NOOP_TRANSFORMER, TestTransformedCollection.NOOP_TRANSFORMER);
}
public void testTransformedMap() {
Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "6"};
Map map = TransformedMap.decorate(new HashMap(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER, null);
assertEquals(0, map.size());
for (int i = 0; i < els.length; i++) {
map.put(els[i], els[i]);
assertEquals(i + 1, map.size());
assertEquals(true, map.containsKey(new Integer((String) els[i])));
assertEquals(false, map.containsKey(els[i]));
assertEquals(true, map.containsValue(els[i]));
assertEquals(els[i], map.get(new Integer((String) els[i])));
}
assertEquals(null, map.remove(els[0]));
assertEquals(els[0], map.remove(new Integer((String) els[0])));
map = TransformedMap.decorate(new HashMap(), null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
assertEquals(0, map.size());
for (int i = 0; i < els.length; i++) {
map.put(els[i], els[i]);
assertEquals(i + 1, map.size());
assertEquals(true, map.containsValue(new Integer((String) els[i])));
assertEquals(false, map.containsValue(els[i]));
assertEquals(true, map.containsKey(els[i]));
assertEquals(new Integer((String) els[i]), map.get(els[i]));
}
assertEquals(new Integer((String) els[0]), map.remove(els[0]));
Set entrySet = map.entrySet();
Map.Entry[] array = (Map.Entry[]) entrySet.toArray(new Map.Entry[0]);
array[0].setValue("66");
assertEquals(new Integer(66), array[0].getValue());
assertEquals(new Integer(66), map.get(array[0].getKey()));
Map.Entry entry = (Map.Entry) entrySet.iterator().next();
entry.setValue("88");
assertEquals(new Integer(88), entry.getValue());
assertEquals(new Integer(88), map.get(entry.getKey()));
}
}