You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2016/05/20 22:30:06 UTC
svn commit: r1744808 - in /commons/proper/collections/trunk/src:
changes/changes.xml main/java/org/apache/commons/collections4/MapUtils.java
test/java/org/apache/commons/collections4/MapUtilsTest.java
Author: ggregory
Date: Fri May 20 22:30:06 2016
New Revision: 1744808
URL: http://svn.apache.org/viewvc?rev=1744808&view=rev
Log:
[COLLECTIONS-589] Add null-safe MapUtils.size(Map<?, ?>) method.
Modified:
commons/proper/collections/trunk/src/changes/changes.xml
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1744808&r1=1744807&r2=1744808&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Fri May 20 22:30:06 2016
@@ -20,7 +20,10 @@
<title>Commons Collections Changes</title>
</properties>
<body>
- <release version="4.2" date="YYYY-MM-DD" description="">
+ <release version="4.2" date="YYYY-MM-DD" description="New features">
+ <action issue="COLLECTIONS-589" dev="ggregory" type="add" due-to="Gary Gregory">
+ Add null-safe MapUtils.size(Map<?, ?>) method.
+ </action>
</release>
<release version="4.1" date="2015-11-28" description="This is a security and minor release.">
<action issue="COLLECTIONS-508" dev="tn" type="add">
Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java?rev=1744808&r1=1744807&r2=1744808&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/MapUtils.java Fri May 20 22:30:06 2016
@@ -1794,4 +1794,13 @@ public class MapUtils {
new AbstractSortedMapDecorator<K, V>(sortedMap) {};
}
+ /**
+ * Gets the given map size or 0 if the map is null
+ * @param map a Map or null
+ * @return the given map size or 0 if the map is null
+ */
+ public static int size(Map<?, ?> map) {
+ return map == null ? 0 : map.size();
+ }
+
}
Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/MapUtilsTest.java?rev=1744808&r1=1744807&r2=1744808&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/MapUtilsTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/MapUtilsTest.java Fri May 20 22:30:06 2016
@@ -16,7 +16,11 @@
*/
package org.apache.commons.collections4;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
@@ -890,4 +894,21 @@ public class MapUtilsTest {
assertSame(iMap, MapUtils.iterableMap(iMap));
}
+ @Test
+ public void testSize0() {
+ assertEquals(0, MapUtils.size(new HashMap<Object, Object>()));
+ }
+
+ @Test
+ public void testSizeNull() {
+ assertEquals(0, MapUtils.size(null));
+ }
+
+ @Test
+ public void testSize() {
+ final HashMap<Object, Object> map = new HashMap<Object, Object>();
+ map.put("A", "1");
+ map.put("B", "2");
+ assertEquals(2, MapUtils.size(map));
+ }
}