diff --git a/RELEASE-NOTES.html b/RELEASE-NOTES.html
index a060e1307..a9f879f93 100644
--- a/RELEASE-NOTES.html
+++ b/RELEASE-NOTES.html
@@ -34,12 +34,12 @@ The only new deprecations are ................
NEW CLASSES
-- LoopingListIterator - When the end of the list is reached the iteration continues from the start
+- LoopingListIterator - When the end of the list is reached the iteration continues from the start [30166]
ENHANCEMENTS
-- ........
+- CollectionUtils.addIgnoreNull - Adds to the collection if the value being added is not null [30020]
BUG FIXES
@@ -49,5 +49,5 @@ The only new deprecations are ................
JAVADOC
-- ........
+- MapUtils.safeAddToMap - Better comment
diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java
index 94bee14b6..da9d57586 100644
--- a/src/java/org/apache/commons/collections/CollectionUtils.java
+++ b/src/java/org/apache/commons/collections/CollectionUtils.java
@@ -38,7 +38,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection;
* Provides utility methods and decorators for {@link Collection} instances.
*
* @since Commons Collections 1.0
- * @version $Revision: 1.61 $ $Date: 2004/04/27 20:00:18 $
+ * @version $Revision: 1.62 $ $Date: 2004/07/17 21:38:33 $
*
* @author Rodney Waldhoff
* @author Paul Jack
@@ -633,11 +633,24 @@ public class CollectionUtils {
return outputCollection;
}
+ //-----------------------------------------------------------------------
+ /**
+ * Adds an element to the collection unless the element is null.
+ *
+ * @param collection the collection to add to, must not be null
+ * @param object the object to add, if null it will not be added
+ * @return true if the collection changed
+ * @throws NullPointerException if the collection is null
+ */
+ public static boolean addIgnoreNull(Collection collection, Object object) {
+ return (object == null ? false : collection.add(object));
+ }
+
/**
* Adds all elements in the iteration to the given collection.
*
- * @param collection the collection to add to
- * @param iterator the iterator of elements to add, may not be null
+ * @param collection the collection to add to, must not be null
+ * @param iterator the iterator of elements to add, must not be null
* @throws NullPointerException if the collection or iterator is null
*/
public static void addAll(Collection collection, Iterator iterator) {
@@ -649,8 +662,8 @@ public class CollectionUtils {
/**
* Adds all elements in the enumeration to the given collection.
*
- * @param collection the collection to add to
- * @param enumeration the enumeration of elements to add, may not be null
+ * @param collection the collection to add to, must not be null
+ * @param enumeration the enumeration of elements to add, must not be null
* @throws NullPointerException if the collection or enumeration is null
*/
public static void addAll(Collection collection, Enumeration enumeration) {
@@ -662,8 +675,8 @@ public class CollectionUtils {
/**
* Adds all elements in the array to the given collection.
*
- * @param collection the collection to add to, may not be null
- * @param elements the array of elements to add, may not be null
+ * @param collection the collection to add to, must not be null
+ * @param elements the array of elements to add, must not be null
* @throws NullPointerException if the collection or array is null
*/
public static void addAll(Collection collection, Object[] elements) {
diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java
index fbcf1921d..21a87d72a 100644
--- a/src/test/org/apache/commons/collections/TestCollectionUtils.java
+++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java
@@ -50,7 +50,7 @@ import org.apache.commons.collections.collection.UnmodifiableCollection;
* @author Phil Steitz
* @author Steven Melzer
*
- * @version $Revision: 1.39 $ $Date: 2004/06/26 10:00:42 $
+ * @version $Revision: 1.40 $ $Date: 2004/07/17 21:38:33 $
*/
public class TestCollectionUtils extends TestCase {
@@ -965,6 +965,22 @@ public class TestCollectionUtils extends TestCase {
assertEquals(new Integer(4), set.iterator().next());
}
+ //-----------------------------------------------------------------------
+ public void testAddIgnoreNull() {
+ Set set = new HashSet();
+ set.add("1");
+ set.add("2");
+ set.add("3");
+ assertEquals(false, CollectionUtils.addIgnoreNull(set, null));
+ assertEquals(3, set.size());
+ assertEquals(false, CollectionUtils.addIgnoreNull(set, "1"));
+ assertEquals(3, set.size());
+ assertEquals(true, CollectionUtils.addIgnoreNull(set, "4"));
+ assertEquals(4, set.size());
+ assertEquals(true, set.contains("4"));
+ }
+
+ //-----------------------------------------------------------------------
public void testPredicatedCollection() {
Predicate predicate = new Predicate() {
public boolean evaluate(Object o) {