CollectionUtils.addIgnoreNull new method

bug 30020, from Rafael U. C. Afonso


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131811 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-07-17 21:38:33 +00:00
parent df3509d391
commit 3ae470ee28
3 changed files with 40 additions and 11 deletions

View File

@ -34,12 +34,12 @@ The only new deprecations are ................
<center><h3>NEW CLASSES</h3></center>
<ul>
<li>LoopingListIterator - When the end of the list is reached the iteration continues from the start</li>
<li>LoopingListIterator - When the end of the list is reached the iteration continues from the start [30166]</li>
</ul>
<center><h3>ENHANCEMENTS</h3></center>
<ul>
<li>........</li>
<li>CollectionUtils.addIgnoreNull - Adds to the collection if the value being added is not null [30020]</li>
</ul>
<center><h3>BUG FIXES</h3></center>
@ -49,5 +49,5 @@ The only new deprecations are ................
<center><h3>JAVADOC</h3></center>
<ul>
<li>........</li>
<li>MapUtils.safeAddToMap - Better comment</li>
</ul>

View File

@ -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) {

View File

@ -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) {