You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2005/07/16 19:54:01 UTC

svn commit: r219347 - in /jakarta/commons/proper/collections/trunk: ./ src/java/org/apache/commons/collections/ src/java/org/apache/commons/collections/map/ src/test/org/apache/commons/collections/map/

Author: scolebourne
Date: Sat Jul 16 10:54:00 2005
New Revision: 219347

URL: http://svn.apache.org/viewcvs?rev=219347&view=rev
Log:
New Transformed*Map factory decorateTransform() that transforms any existing entries in the map
rfe 30959

Modified:
    jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html
    jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java
    jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/TransformedMap.java
    jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/TransformedSortedMap.java
    jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestTransformedMap.java
    jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestTransformedSortedMap.java

Modified: jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html?rev=219347&r1=219346&r2=219347&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html (original)
+++ jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html Sat Jul 16 10:54:00 2005
@@ -68,6 +68,7 @@
 <li>CollectionUtils/ListUtils - retainAll/removeAll that don't change original colllection</li>
 <li>BlockingBuffer - now includes stack trace if InterupttedException occurs [33700]</li>
 <li>BlockingBuffer - new methods that allow get and remove with a timeout [27691]</li>
+<li>Transformed*Map - new factory decorateTransform() that transforms any existing entries in the map [30959]</li>
 </ul>
 
 <center><h3>BUG FIXES</h3></center>
@@ -98,6 +99,7 @@
 <center><h3>JAVADOC</h3></center>
 <ul>
 <li>MapUtils.safeAddToMap - Better comment</li>
+<li>MapUtils.transformed*Map - Better comment</li>
 <li>ListOrderedSet.decorate(List) - Better comment [32073]</li>
 </ul>
 </body>

Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java?rev=219347&r1=219346&r2=219347&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java (original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java Sat Jul 16 10:54:00 2005
@@ -1264,11 +1264,19 @@
     /**
      * Returns a transformed map backed by the given map.
      * <p>
+     * This method returns a new map (decorating the specified map) that
+     * will transform any new entries added to it.
+     * Existing entries in the specified map will not be transformed.
+     * If you want that behaviour, see {@link TransformedMap#decorateTransform}.
+     * <p>
      * Each object is passed through the transformers as it is added to the
      * Map. It is important not to use the original map after invoking this 
      * method, as it is a backdoor for adding untransformed objects.
+     * <p>
+     * If there are any elements already in the map being decorated, they
+     * are NOT transformed.
      *
-     * @param map  the map to transform, must not be null
+     * @param map  the map to transform, must not be null, typically empty
      * @param keyTransformer  the transformer for the map keys, null means no transformation
      * @param valueTransformer  the transformer for the map values, null means no transformation
      * @return a transformed map backed by the given map
@@ -1499,11 +1507,19 @@
     /**
      * Returns a transformed sorted map backed by the given map.
      * <p>
+     * This method returns a new sorted map (decorating the specified map) that
+     * will transform any new entries added to it.
+     * Existing entries in the specified map will not be transformed.
+     * If you want that behaviour, see {@link TransformedSortedMap#decorateTransform}.
+     * <p>
      * Each object is passed through the transformers as it is added to the
      * Map. It is important not to use the original map after invoking this 
      * method, as it is a backdoor for adding untransformed objects.
+     * <p>
+     * If there are any elements already in the map being decorated, they
+     * are NOT transformed.
      *
-     * @param map  the map to transform, must not be null
+     * @param map  the map to transform, must not be null, typically empty
      * @param keyTransformer  the transformer for the map keys, null means no transformation
      * @param valueTransformer  the transformer for the map values, null means no transformation
      * @return a transformed map backed by the given map

Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/TransformedMap.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/TransformedMap.java?rev=219347&r1=219346&r2=219347&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/TransformedMap.java (original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/TransformedMap.java Sat Jul 16 10:54:00 2005
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2003-2004 The Apache Software Foundation
+ *  Copyright 2003-2005 The Apache Software Foundation
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -56,14 +56,38 @@
      * <p>
      * If there are any elements already in the map being decorated, they
      * are NOT transformed.
+     * Constrast this with {@link #decorateTransform}.
      * 
      * @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
+     * @param keyTransformer  the transformer to use for key conversion, null means no transformation
+     * @param valueTransformer  the transformer to use for value conversion, null means no transformation
      * @throws IllegalArgumentException if map is null
      */
     public static Map decorate(Map map, Transformer keyTransformer, Transformer valueTransformer) {
         return new TransformedMap(map, keyTransformer, valueTransformer);
+    }
+
+    /**
+     * Factory method to create a transforming map that will transform
+     * existing contents of the specified map.
+     * <p>
+     * If there are any elements already in the map being decorated, they
+     * will be transformed by this method.
+     * Constrast this with {@link #decorate}.
+     * 
+     * @param map  the map to decorate, must not be null
+     * @param keyTransformer  the transformer to use for key conversion, null means no transformation
+     * @param valueTransformer  the transformer to use for value conversion, null means no transformation
+     * @throws IllegalArgumentException if map is null
+     */
+    public static Map decorateTransform(Map map, Transformer keyTransformer, Transformer valueTransformer) {
+        TransformedMap decorated = new TransformedMap(map, keyTransformer, valueTransformer);
+        if (map.size() > 0) {
+            Map transformed = decorated.transformMap(map);
+            decorated.clear();
+            decorated.getMap().putAll(transformed);  // avoids double transformation
+        }
+        return decorated;
     }
 
     //-----------------------------------------------------------------------

Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/TransformedSortedMap.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/TransformedSortedMap.java?rev=219347&r1=219346&r2=219347&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/TransformedSortedMap.java (original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/TransformedSortedMap.java Sat Jul 16 10:54:00 2005
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2003-2004 The Apache Software Foundation
+ *  Copyright 2003-2005 The Apache Software Foundation
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
 package org.apache.commons.collections.map;
 
 import java.util.Comparator;
+import java.util.Map;
 import java.util.SortedMap;
 
 import org.apache.commons.collections.Transformer;
@@ -47,6 +48,7 @@
      * <p>
      * If there are any elements already in the map being decorated, they
      * are NOT transformed.
+     * Constrast this with {@link #decorateTransform}.
      * 
      * @param map  the map to decorate, must not be null
      * @param keyTransformer  the predicate to validate the keys, null means no transformation
@@ -55,6 +57,29 @@
      */
     public static SortedMap decorate(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) {
         return new TransformedSortedMap(map, keyTransformer, valueTransformer);
+    }
+
+    /**
+     * Factory method to create a transforming sorted map that will transform
+     * existing contents of the specified map.
+     * <p>
+     * If there are any elements already in the map being decorated, they
+     * will be transformed by this method.
+     * Constrast this with {@link #decorate}.
+     * 
+     * @param map  the map to decorate, must not be null
+     * @param keyTransformer  the transformer to use for key conversion, null means no transformation
+     * @param valueTransformer  the transformer to use for value conversion, null means no transformation
+     * @throws IllegalArgumentException if map is null
+     */
+    public static SortedMap decorateTransform(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) {
+        TransformedSortedMap decorated = new TransformedSortedMap(map, keyTransformer, valueTransformer);
+        if (map.size() > 0) {
+            Map transformed = decorated.transformMap(map);
+            decorated.clear();
+            decorated.getMap().putAll(transformed);  // avoids double transformation
+        }
+        return decorated;
     }
 
     //-----------------------------------------------------------------------

Modified: jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestTransformedMap.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestTransformedMap.java?rev=219347&r1=219346&r2=219347&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestTransformedMap.java (original)
+++ jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestTransformedMap.java Sat Jul 16 10:54:00 2005
@@ -97,6 +97,38 @@
         assertEquals(new Integer(88), map.get(entry.getKey()));
     }
 
+    //-----------------------------------------------------------------------
+    public void testFactory_Decorate() {
+        Map base = new HashMap();
+        base.put("A", "1");
+        base.put("B", "2");
+        base.put("C", "3");
+        
+        Map trans = TransformedMap.decorate(base, null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
+        assertEquals(3, trans.size());
+        assertEquals("1", trans.get("A"));
+        assertEquals("2", trans.get("B"));
+        assertEquals("3", trans.get("C"));
+        trans.put("D", "4");
+        assertEquals(new Integer(4), trans.get("D"));
+    }
+
+    public void testFactory_decorateTransform() {
+        Map base = new HashMap();
+        base.put("A", "1");
+        base.put("B", "2");
+        base.put("C", "3");
+        
+        Map trans = TransformedMap.decorateTransform(base, null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
+        assertEquals(3, trans.size());
+        assertEquals(new Integer(1), trans.get("A"));
+        assertEquals(new Integer(2), trans.get("B"));
+        assertEquals(new Integer(3), trans.get("C"));
+        trans.put("D", "4");
+        assertEquals(new Integer(4), trans.get("D"));
+    }
+
+    //-----------------------------------------------------------------------
     public String getCompatibilityVersion() {
         return "3.1";
     }

Modified: jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestTransformedSortedMap.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestTransformedSortedMap.java?rev=219347&r1=219346&r2=219347&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestTransformedSortedMap.java (original)
+++ jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/map/TestTransformedSortedMap.java Sat Jul 16 10:54:00 2005
@@ -17,6 +17,7 @@
 
 import java.util.Map;
 import java.util.Set;
+import java.util.SortedMap;
 import java.util.TreeMap;
 
 import junit.framework.Test;
@@ -108,6 +109,38 @@
         assertEquals(new Integer(88), map.get(entry.getKey()));
     }
 
+    //-----------------------------------------------------------------------
+    public void testFactory_Decorate() {
+        SortedMap base = new TreeMap();
+        base.put("A", "1");
+        base.put("B", "2");
+        base.put("C", "3");
+        
+        SortedMap trans = TransformedSortedMap.decorate(base, null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
+        assertEquals(3, trans.size());
+        assertEquals("1", trans.get("A"));
+        assertEquals("2", trans.get("B"));
+        assertEquals("3", trans.get("C"));
+        trans.put("D", "4");
+        assertEquals(new Integer(4), trans.get("D"));
+    }
+
+    public void testFactory_decorateTransform() {
+        SortedMap base = new TreeMap();
+        base.put("A", "1");
+        base.put("B", "2");
+        base.put("C", "3");
+        
+        SortedMap trans = TransformedSortedMap.decorateTransform(base, null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
+        assertEquals(3, trans.size());
+        assertEquals(new Integer(1), trans.get("A"));
+        assertEquals(new Integer(2), trans.get("B"));
+        assertEquals(new Integer(3), trans.get("C"));
+        trans.put("D", "4");
+        assertEquals(new Integer(4), trans.get("D"));
+    }
+
+    //-----------------------------------------------------------------------
     public String getCompatibilityVersion() {
         return "3.1";
     }



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