Add method addAllIfNotNull

This commit is contained in:
2025-07-03 07:57:37 -05:00
parent b5134cd0c6
commit fa80daa778
2 changed files with 57 additions and 0 deletions

View File

@ -30,6 +30,7 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -712,4 +713,26 @@ public class CollectionUtils
c.add(element);
}
}
/***************************************************************************
* add all objects in a source collection to a destination collection, in a
* null-safe manner with regard to the source.
*
* @param destination collection to put objects into. May NOT be null.
* if it's immutable, and source is not null, that will
* fail (as you'd expect) too.
* @param source collection to get objects from. if null, is ignored.
* @throws NullPointerException if destination is null.
***************************************************************************/
public static <T> void addAllIfNotNull(Collection<T> destination, Collection<T> source)
{
Objects.requireNonNull(destination, "destination may not be null");
if(source != null)
{
destination.addAll(source);
}
}
}