add methods to wrap Maps and SortedMaps not based on Commons Collections 
classes to their Iterable*Map counterparts

git-svn-id: 
https://svn.apache.org/repos/asf/commons/proper/collections/branches/collections_jdk5_branch@751887
 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/commons-collections/commit/3338e259
Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/3338e259
Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/3338e259

Branch: refs/heads/collections_jdk5_branch
Commit: 3338e25927741c0a8b02225723539c5d1007e84b
Parents: b29b93a
Author: Matthew Jason Benson <mben...@apache.org>
Authored: Mon Mar 9 22:40:01 2009 +0000
Committer: Matthew Jason Benson <mben...@apache.org>
Committed: Mon Mar 9 22:40:01 2009 +0000

----------------------------------------------------------------------
 .../apache/commons/collections/MapUtils.java    | 40 +++++++++++++++++++-
 .../commons/collections/TestMapUtils.java       | 34 +++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-collections/blob/3338e259/src/java/org/apache/commons/collections/MapUtils.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/commons/collections/MapUtils.java 
b/src/java/org/apache/commons/collections/MapUtils.java
index d8ed529..88a422e 100644
--- a/src/java/org/apache/commons/collections/MapUtils.java
+++ b/src/java/org/apache/commons/collections/MapUtils.java
@@ -30,6 +30,8 @@ import java.util.ResourceBundle;
 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 @@ public class MapUtils {
      * @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 @@ public class MapUtils {
         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) {
+                };
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/commons-collections/blob/3338e259/src/test/org/apache/commons/collections/TestMapUtils.java
----------------------------------------------------------------------
diff --git a/src/test/org/apache/commons/collections/TestMapUtils.java 
b/src/test/org/apache/commons/collections/TestMapUtils.java
index 2f7006c..6db8920 100644
--- a/src/test/org/apache/commons/collections/TestMapUtils.java
+++ b/src/test/org/apache/commons/collections/TestMapUtils.java
@@ -31,6 +31,7 @@ import junit.framework.Test;
 
 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 @@ public class TestMapUtils extends BulkTest {
         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));
+    }
+
 }

Reply via email to