neilotoole 2004/12/10 22:26:14 Modified: collections/src/java/org/apache/commons/collections MapUtils.java collections/src/test/org/apache/commons/collections TestMapUtils.java Log: Added new convenience method to MapUtils: #unmodifiableMapCopy Revision Changes Path 1.50 +17 -2 jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java Index: MapUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java,v retrieving revision 1.49 retrieving revision 1.50 diff -u -r1.49 -r1.50 --- MapUtils.java 22 Sep 2004 23:09:54 -0000 1.49 +++ MapUtils.java 11 Dec 2004 06:26:13 -0000 1.50 @@ -79,6 +79,7 @@ * @author Janek Bogucki * @author Max Rydahl Andersen * @author <a href="mailto:[EMAIL PROTECTED]">Ashwin S</a> + * @author Neil O'Toole */ public class MapUtils { @@ -1144,7 +1145,7 @@ * @throws NullPointerException if map is null * @throws IllegalArgumentException if sub-array or entry matching used and an * entry is invalid - * @throws ClassCaseException if the array contents is mixed + * @throws ClassCastException if the array contents is mixed * @since Commons Collections 3.2 */ public static Map putAll(Map map, Object[] array) { @@ -1220,6 +1221,20 @@ public static Map unmodifiableMap(Map map) { return UnmodifiableMap.decorate(map); } + + /** + * Returns an unmodifiable copy of the map. + * @param map the map to make an unmodifiable copy of, must not be null + * @return an unmodifiable map backed by the given map + * @throws IllegalArgumentException if the map is null + */ + public static Map unmodifiableMapCopy(final Map map) { + if (map == null) throw new IllegalArgumentException("null not permitted."); + + final Map copy = new HashMap(map.size(), 1.0f); + copy.putAll(map); + return MapUtils.unmodifiableMap(copy); + } /** * Returns a predicated (validating) map backed by the given map. 1.25 +34 -1 jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java Index: TestMapUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java,v retrieving revision 1.24 retrieving revision 1.25 diff -u -r1.24 -r1.25 --- TestMapUtils.java 22 Sep 2004 23:03:50 -0000 1.24 +++ TestMapUtils.java 11 Dec 2004 06:26:13 -0000 1.25 @@ -43,6 +43,7 @@ * @author Arun Mammen Thomas * @author Max Rydahl Andersen * @author Janek Bogucki + * @author Neil O'Toole */ public class TestMapUtils extends BulkTest { @@ -769,5 +770,37 @@ assertEquals(EXPECTED_OUT, out.toString()); } + + public void testUnmodifiableMapCopy() { + Map map = new HashMap(); + map.put("key", "value"); + + Map copy = MapUtils.unmodifiableMapCopy(map); + assertTrue(copy instanceof Unmodifiable); + assertEquals(map, copy); + map.clear(); + assertFalse(map.equals(copy)); + + try + { + copy.clear(); + fail("should be unmodifiable."); + } + catch (UnsupportedOperationException uoe) + { + // this is what we want + } + + try + { + map = MapUtils.unmodifiableMapCopy(null); + fail("expecting IllegalArgumentException"); + } + catch (IllegalArgumentException iae) + { + // this is what we want + } + + } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]