Author: scolebourne
Date: Fri Aug  5 16:07:00 2005
New Revision: 230513

URL: http://svn.apache.org/viewcvs?rev=230513&view=rev
Log:
CollectionUtils/MapUtils.isEmpty/isNotEmpty - Null-safe checks of collection 
emptyness

bug 35890, from Stephen Smith

CollectionUtils.sizeIsEmpty - Checks if a collection, array, map, iterator or 
enumeration is empty

Modified:
    jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html
    jakarta/commons/proper/collections/trunk/project.xml
    
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/CollectionUtils.java
    
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/MapUtils.java
    
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java
    
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.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=230513&r1=230512&r2=230513&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html (original)
+++ jakarta/commons/proper/collections/trunk/RELEASE-NOTES.html Fri Aug  5 
16:07:00 2005
@@ -64,8 +64,10 @@
 <ul>
 <li>CollectionUtils.addIgnoreNull - Adds to the collection if the value being 
added is not null [30020]</li>
 <li>MapUtils.putAll - Puts an array of key/value pairs into a map [30882]</li>
-<li>ExtendedProperties - No longer uses an exception in normal processing 
[30497]</li>
+<li>CollectionUtils/MapUtils.isEmpty/isNotEmpty - Null-safe checks of 
collection emptyness [35890]</li>
+<li>CollectionUtils.sizeIsEmpty - Checks if a collection, array, map, iterator 
or enumeration is empty</li>
 <li>CollectionUtils/ListUtils - retainAll/removeAll that don't change original 
colllection</li>
+<li>ExtendedProperties - No longer uses an exception in normal processing 
[30497]</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>

Modified: jakarta/commons/proper/collections/trunk/project.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/project.xml?rev=230513&r1=230512&r2=230513&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/project.xml (original)
+++ jakarta/commons/proper/collections/trunk/project.xml Fri Aug  5 16:07:00 
2005
@@ -316,6 +316,9 @@
       <name>Michael Smith</name>
     </contributor>
     <contributor>
+      <name>Stephen Smith</name>
+    </contributor>
+    <contributor>
       <name>Jan Sorensen</name>
     </contributor>
     <contributor>

Modified: 
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/CollectionUtils.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/CollectionUtils.java?rev=230513&r1=230512&r2=230513&view=diff
==============================================================================
--- 
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/CollectionUtils.java
 (original)
+++ 
jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/CollectionUtils.java
 Fri Aug  5 16:07:00 2005
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2004 The Apache Software Foundation
+ *  Copyright 2001-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.
@@ -52,6 +52,7 @@
  * @author Steven Melzer
  * @author Jon Schewe
  * @author Neil O'Toole
+ * @author Stephen Smith
  */
 public class CollectionUtils {
 
@@ -920,6 +921,76 @@
         return total;
     }
     
+    /**
+     * Checks if the specified collection/array/iterator is empty.
+     * <p>
+     * This method can handles objects as follows
+     * <ul>
+     * <li>Collection - via collection isEmpty
+     * <li>Map - via map isEmpty
+     * <li>Array - using array size
+     * <li>Iterator - via hasNext
+     * <li>Enumeration - via hasMoreElements
+     * </ul>
+     * <p>
+     * Note: This method is named to avoid clashing with
+     * [EMAIL PROTECTED] #isEmpty(Collection)}.
+     * 
+     * @param object  the object to get the size of, not null
+     * @return true if empty
+     * @throws IllegalArgumentException thrown if object is not recognised or 
null
+     * @since Commons Collections 3.2
+     */
+    public static boolean sizeIsEmpty(Object object) {
+        if (object instanceof Collection) {
+            return ((Collection) object).isEmpty();
+        } else if (object instanceof Map) {
+            return ((Map) object).isEmpty();
+        } else if (object instanceof Object[]) {
+            return ((Object[]) object).length == 0;
+        } else if (object instanceof Iterator) {
+            return ((Iterator) object).hasNext() == false;
+        } else if (object instanceof Enumeration) {
+            return ((Enumeration) object).hasMoreElements() == false;
+        } else if (object == null) {
+            throw new IllegalArgumentException("Unsupported object type: 
null");
+        } else {
+            try {
+                return Array.getLength(object) == 0;
+            } catch (IllegalArgumentException ex) {
+                throw new IllegalArgumentException("Unsupported object type: " 
+ object.getClass().getName());
+            }
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Null-safe check if the specified collection is empty.
+     * <p>
+     * Null returns true.
+     * 
+     * @param coll  the collection to check, may be null
+     * @return true if empty or null
+     * @since Commons Collections 3.2
+     */
+    public static boolean isEmpty(Collection coll) {
+        return (coll == null || coll.isEmpty());
+    }
+
+    /**
+     * Null-safe check if the specified collection is not empty.
+     * <p>
+     * Null returns false.
+     * 
+     * @param coll  the collection to check, may be null
+     * @return true if non-null and non-empty
+     * @since Commons Collections 3.2
+     */
+    public static boolean isNotEmpty(Collection coll) {
+        return !CollectionUtils.isEmpty(coll);
+    }
+
+    //-----------------------------------------------------------------------
     /**
      * Reverses the order of the given array.
      * 

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=230513&r1=230512&r2=230513&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
 Fri Aug  5 16:07:00 2005
@@ -18,6 +18,7 @@
 import java.io.PrintStream;
 import java.text.NumberFormat;
 import java.text.ParseException;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -1183,6 +1184,33 @@
             }
         }
         return map;
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Null-safe check if the specified map is empty.
+     * <p>
+     * Null returns true.
+     * 
+     * @param map  the map to check, may be null
+     * @return true if empty or null
+     * @since Commons Collections 3.2
+     */
+    public static boolean isEmpty(Map map) {
+        return (map == null || map.isEmpty());
+    }
+
+    /**
+     * Null-safe check if the specified map is not empty.
+     * <p>
+     * Null returns false.
+     * 
+     * @param map  the map to check, may be null
+     * @return true if non-null and non-empty
+     * @since Commons Collections 3.2
+     */
+    public static boolean isNotEmpty(Map map) {
+        return !MapUtils.isEmpty(map);
     }
 
     // Map decorators

Modified: 
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java?rev=230513&r1=230512&r2=230513&view=diff
==============================================================================
--- 
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java
 (original)
+++ 
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java
 Fri Aug  5 16:07:00 2005
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2004 The Apache Software Foundation
+ *  Copyright 2001-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.
@@ -50,6 +50,7 @@
  * @author Phil Steitz
  * @author Steven Melzer
  * @author Neil O'Toole
+ * @author Stephen Smith
  * 
  * @version $Revision$ $Date$
  */
@@ -735,6 +736,7 @@
         }
     }
 
+    //-----------------------------------------------------------------------
     public void testSize_List() {
         List list = new ArrayList();
         assertEquals(0, CollectionUtils.size(list));
@@ -799,7 +801,104 @@
             fail("Expecting IllegalArgumentException");
         } catch (IllegalArgumentException e) {}
     }
-    
+
+    //-----------------------------------------------------------------------
+    public void testSizeIsEmpty_List() {
+        List list = new ArrayList();
+        assertEquals(true, CollectionUtils.sizeIsEmpty(list));
+        list.add("a");
+        assertEquals(false, CollectionUtils.sizeIsEmpty(list));
+    }
+    public void testSizeIsEmpty_Map() {
+        Map map = new HashMap();
+        assertEquals(true, CollectionUtils.sizeIsEmpty(map));
+        map.put("1", "a");
+        assertEquals(false, CollectionUtils.sizeIsEmpty(map));
+    }
+    public void testSizeIsEmpty_Array() {
+        Object[] objectArray = new Object[0];
+        assertEquals(true, CollectionUtils.sizeIsEmpty(objectArray));
+        
+        String[] stringArray = new String[3];
+        assertEquals(false, CollectionUtils.sizeIsEmpty(stringArray));
+        stringArray[0] = "a";
+        stringArray[1] = "b";
+        stringArray[2] = "c";
+        assertEquals(false, CollectionUtils.sizeIsEmpty(stringArray));
+    }
+    public void testSizeIsEmpty_PrimitiveArray() {
+        int[] intArray = new int[0];
+        assertEquals(true, CollectionUtils.sizeIsEmpty(intArray));
+        
+        double[] doubleArray = new double[3];
+        assertEquals(false, CollectionUtils.sizeIsEmpty(doubleArray));
+        doubleArray[0] = 0.0d;
+        doubleArray[1] = 1.0d;
+        doubleArray[2] = 2.5d;
+        assertEquals(false, CollectionUtils.sizeIsEmpty(doubleArray));
+    }
+    public void testSizeIsEmpty_Enumeration() {
+        Vector list = new Vector();
+        assertEquals(true, CollectionUtils.sizeIsEmpty(list.elements()));
+        list.add("a");
+        assertEquals(false, CollectionUtils.sizeIsEmpty(list.elements()));
+        Enumeration en = list.elements();
+        en.nextElement();
+        assertEquals(true, CollectionUtils.sizeIsEmpty(en));
+    }
+    public void testSizeIsEmpty_Iterator() {
+        List list = new ArrayList();
+        assertEquals(true, CollectionUtils.sizeIsEmpty(list.iterator()));
+        list.add("a");
+        assertEquals(false, CollectionUtils.sizeIsEmpty(list.iterator()));
+        Iterator it = list.iterator();
+        it.next();
+        assertEquals(true, CollectionUtils.sizeIsEmpty(it));
+    }
+    public void testSizeIsEmpty_Other() {
+        try {
+            CollectionUtils.sizeIsEmpty(null);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {}
+        try {
+            CollectionUtils.sizeIsEmpty("not a list");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {}
+    }
+
+    //-----------------------------------------------------------------------
+    public void testIsEmptyWithEmptyCollection() {
+        Collection coll = new ArrayList();
+        assertEquals(true, CollectionUtils.isEmpty(coll));
+    }
+
+    public void testIsEmptyWithNonEmptyCollection() {
+        Collection coll = new ArrayList();
+        coll.add("item");
+        assertEquals(false, CollectionUtils.isEmpty(coll));
+    }
+
+    public void testIsEmptyWithNull() {
+        Collection coll = null;
+        assertEquals(true, CollectionUtils.isEmpty(coll));
+    }
+
+    public void testIsNotEmptyWithEmptyCollection() {
+        Collection coll = new ArrayList();
+        assertEquals(false, CollectionUtils.isNotEmpty(coll));
+    }
+
+    public void testIsNotEmptyWithNonEmptyCollection() {
+        Collection coll = new ArrayList();
+        coll.add("item");
+        assertEquals(true, CollectionUtils.isNotEmpty(coll));
+    }
+
+    public void testIsNotEmptyWithNull() {
+        Collection coll = null;
+        assertEquals(false, CollectionUtils.isNotEmpty(coll));
+    }
+
     //-----------------------------------------------------------------------
     private static Predicate EQUALS_TWO = new Predicate() {
         public boolean evaluate(Object input) {

Modified: 
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java?rev=230513&r1=230512&r2=230513&view=diff
==============================================================================
--- 
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java
 (original)
+++ 
jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java
 Fri Aug  5 16:07:00 2005
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2004 The Apache Software Foundation
+ *  Copyright 2001-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.
@@ -771,4 +771,37 @@
         assertEquals(EXPECTED_OUT, out.toString());
     }
     
+    //-----------------------------------------------------------------------
+    public void testIsEmptyWithEmptyMap() {
+        Map map = new HashMap();
+        assertEquals(true, MapUtils.isEmpty(map));
+    }
+
+    public void testIsEmptyWithNonEmptyMap() {
+        Map map = new HashMap();
+        map.put("item", "value");
+        assertEquals(false, MapUtils.isEmpty(map));
+    }
+
+    public void testIsEmptyWithNull() {
+        Map map = null;
+        assertEquals(true, MapUtils.isEmpty(map));
+    }
+
+    public void testIsNotEmptyWithEmptyMap() {
+        Map map = new HashMap();
+        assertEquals(false, MapUtils.isNotEmpty(map));
+    }
+
+    public void testIsNotEmptyWithNonEmptyMap() {
+        Map map = new HashMap();
+        map.put("item", "value");
+        assertEquals(true, MapUtils.isNotEmpty(map));
+    }
+
+    public void testIsNotEmptyWithNull() {
+        Map map = null;
+        assertEquals(false, MapUtils.isNotEmpty(map));
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to