Java Collection Add addAllIgnoreNull(final Collection target, final Collection source)

Here you can find the source of addAllIgnoreNull(final Collection target, final Collection source)

Description

Add elements from a source collection to a target collection unless they or the source collection itself are null.

License

Apache License

Parameter

Parameter Description
target target collection
source source collection

Exception

Parameter Description
NullPointerException if the target collection is null

Return

true if the target collection changed

Declaration

public static <T> boolean addAllIgnoreNull(final Collection<T> target, final Collection<T> source) 

Method Source Code

//package com.java2s;
/**//  www. j  av a 2 s.  c o m
 * Sapelli data collection platform: http://sapelli.org
 * 
 * Copyright 2012-2016 University College London - ExCiteS group
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

import java.util.Collection;

public class Main {
    /**
     * Add elements from a source collection to a target collection unless they or the source collection itself are null.
     * 
     * @param target  target collection
     * @param source  source collection
     * @return true if the target collection changed
     * @throws NullPointerException if the target collection is null
     */
    public static <T> boolean addAllIgnoreNull(final Collection<T> target, final Collection<T> source) {
        if (target == null)
            throw new NullPointerException("The target collection must not be null");
        if (source == null)
            return false;
        boolean changed = false;
        for (T element : source)
            changed |= addIgnoreNull(target, element);
        return changed;
    }

    /**
     * Adds an element to the collection unless the element is null.
     * Taken from Apache Commons Collections library (licensed under Apache License v2.0)
     * 
     * @param <T>  the type of object the {@link Collection} contains
     * @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
     * @see <a href="http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/CollectionUtils.java?view=co">Apache Commons Collections (CollectionUtils.java)</a>
     */
    public static <T> boolean addIgnoreNull(final Collection<T> collection, final T object) {
        if (collection == null)
            throw new NullPointerException("The collection must not be null");
        return object != null && collection.add(object);
    }
}

Related

  1. addAllFirst(Collection col, Deque deque)
  2. addAllIfNotNull(final Collection collection, final Collection c)
  3. addAllIfNotNull(final Collection collection, final Collection values)
  4. addAllIfSet(Collection target, Collection c)
  5. addAllIgnoreNull(Collection c, T... elements)
  6. addAllNotNull(final Collection c, final E... elements)
  7. addAllOrSame(Collection collection, Collection newElements)
  8. addAllSafe(Collection collection1, Collection collection2)
  9. addAllUncontained(Collection col1, Collection col2)