You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2009/03/09 23:40:01 UTC

svn commit: r751887 - in /commons/proper/collections/branches/collections_jdk5_branch/src: java/org/apache/commons/collections/MapUtils.java test/org/apache/commons/collections/TestMapUtils.java

Author: mbenson
Date: Mon Mar  9 22:40:01 2009
New Revision: 751887

URL: http://svn.apache.org/viewvc?rev=751887&view=rev
Log:
add methods to wrap Maps and SortedMaps not based on Commons Collections classes to their Iterable*Map counterparts

Modified:
    commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/MapUtils.java
    commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestMapUtils.java

Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/MapUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/MapUtils.java?rev=751887&r1=751886&r2=751887&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/MapUtils.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/MapUtils.java Mon Mar  9 22:40:01 2009
@@ -30,6 +30,8 @@
 import java.util.SortedMap;
 import java.util.TreeMap;
 
+import org.apache.commons.collections.map.AbstractMapDecorator;
+import org.apache.commons.collections.map.AbstractSortedMapDecorator;
 import org.apache.commons.collections.map.FixedSizeMap;
 import org.apache.commons.collections.map.FixedSizeSortedMap;
 import org.apache.commons.collections.map.LazyMap;
@@ -1397,7 +1399,7 @@
      * @return an ordered map backed by the given map
      * @throws IllegalArgumentException  if the Map is null
      */
-    public static <K, V> IterableMap<K, V> orderedMap(Map<K, V> map) {
+    public static <K, V> OrderedMap<K, V> orderedMap(Map<K, V> map) {
         return ListOrderedMap.decorate(map);
     }
 
@@ -1621,4 +1623,40 @@
         return LazySortedMap.getLazySortedMap(map, transformerFactory);
     }
 
+    /**
+     * Get the specified {@link Map} as an {@link IterableMap}.
+     * @param <K>
+     * @param <V>
+     * @param map to wrap if necessary.
+     * @return IterableMap<K, V>
+     * @since Commons Collections 5
+     * @TODO fix version
+     */
+    public static <K, V> IterableMap<K, V> iterableMap(Map<K, V> map) {
+        if (map == null) {
+            throw new IllegalArgumentException("Map must not be null");
+        }
+        return map instanceof IterableMap ? (IterableMap<K, V>) map
+                : new AbstractMapDecorator<K, V>(map) {
+                };
+    }
+
+    /**
+     * Get the specified {@link SortedMap} as an {@link IterableSortedMap}.
+     * @param <K>
+     * @param <V>
+     * @param sortedMap to wrap if necessary
+     * @return {@link IterableSortedMap}<K, V>
+     * @since Commons Collections 5
+     * @TODO fix version
+     */
+    public static <K, V> IterableSortedMap<K, V> iterableSortedMap(SortedMap<K, V> sortedMap) {
+        if (sortedMap == null) {
+            throw new IllegalArgumentException("Map must not be null");
+        }
+        return sortedMap instanceof IterableSortedMap ? (IterableSortedMap<K, V>) sortedMap
+                : new AbstractSortedMapDecorator<K, V>(sortedMap) {
+                };
+    }
+
 }

Modified: commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestMapUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestMapUtils.java?rev=751887&r1=751886&r2=751887&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestMapUtils.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestMapUtils.java Mon Mar  9 22:40:01 2009
@@ -31,6 +31,7 @@
 
 import org.apache.commons.collections.keyvalue.DefaultKeyValue;
 import org.apache.commons.collections.keyvalue.DefaultMapEntry;
+import org.apache.commons.collections.map.HashedMap;
 import org.apache.commons.collections.map.LazyMap;
 import org.apache.commons.collections.map.PredicatedMap;
 
@@ -737,4 +738,37 @@
         assertEquals(false, MapUtils.isNotEmpty(map));
     }
 
+    public void testIterableMap() {
+        try {
+            MapUtils.iterableMap(null);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+        }
+        HashMap<String, String> map = new HashMap<String, String>();
+        map.put("foo", "foov");
+        map.put("bar", "barv");
+        map.put("baz", "bazv");
+        IterableMap<String, String> iMap = MapUtils.iterableMap(map);
+        assertEquals(map, iMap);
+        assertNotSame(map, iMap);
+        HashedMap<String, String> hMap = new HashedMap<String, String>(map);
+        assertSame(hMap, MapUtils.iterableMap(hMap));
+    }
+
+    public void testIterableSortedMap() {
+        try {
+            MapUtils.iterableSortedMap(null);
+            fail("Should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+        }
+        TreeMap<String, String> map = new TreeMap<String, String>();
+        map.put("foo", "foov");
+        map.put("bar", "barv");
+        map.put("baz", "bazv");
+        IterableSortedMap<String, String> iMap = MapUtils.iterableSortedMap(map);
+        assertEquals(map, iMap);
+        assertNotSame(map, iMap);
+        assertSame(iMap, MapUtils.iterableMap(iMap));
+    }
+
 }