diff --git a/src/java/org/apache/commons/collections/decorators/TransformedMap.java b/src/java/org/apache/commons/collections/decorators/TransformedMap.java
new file mode 100644
index 000000000..c0bdb0534
--- /dev/null
+++ b/src/java/org/apache/commons/collections/decorators/TransformedMap.java
@@ -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
+ * TransformedMap
decorates another Map
+ * to transform objects that are added.
+ *
+ * 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. + *
+ * 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). + *
+ * 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. + *
+ * 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. + *
+ * 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. + *
+ * 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);
+ }
+ }
+
+}
diff --git a/src/java/org/apache/commons/collections/decorators/TransformedSortedMap.java b/src/java/org/apache/commons/collections/decorators/TransformedSortedMap.java
new file mode 100644
index 000000000..fa200cb09
--- /dev/null
+++ b/src/java/org/apache/commons/collections/decorators/TransformedSortedMap.java
@@ -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
+ * TransformedSortedMap
decorates another SortedMap
+ * to transform objects that are added.
+ *
+ * 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. + *
+ * 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). + *
+ * 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);
+ }
+
+}
diff --git a/src/test/org/apache/commons/collections/decorators/TestAll.java b/src/test/org/apache/commons/collections/decorators/TestAll.java
index 2631835e3..79eeedc37 100644
--- a/src/test/org/apache/commons/collections/decorators/TestAll.java
+++ b/src/test/org/apache/commons/collections/decorators/TestAll.java
@@ -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
@@ -65,7 +65,7 @@ import junit.framework.TestSuite;
* Entry point for all collections decorators tests.
*
* @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
*/
@@ -90,8 +90,10 @@ public class TestAll extends TestCase {
suite.addTest(TestTransformedBuffer.suite());
suite.addTest(TestTransformedCollection.suite());
suite.addTest(TestTransformedList.suite());
+ suite.addTest(TestTransformedMap.suite());
suite.addTest(TestTransformedSet.suite());
suite.addTest(TestTransformedSortedBag.suite());
+ suite.addTest(TestTransformedSortedMap.suite());
suite.addTest(TestTransformedSortedSet.suite());
return suite;
}
diff --git a/src/test/org/apache/commons/collections/decorators/TestTransformedMap.java b/src/test/org/apache/commons/collections/decorators/TestTransformedMap.java
new file mode 100644
index 000000000..4e1a1519a
--- /dev/null
+++ b/src/test/org/apache/commons/collections/decorators/TestTransformedMap.java
@@ -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
+ *