Here you can find the source of addAllIgnoreNull(final Collection
Parameter | Description |
---|---|
target | target collection |
source | source collection |
Parameter | Description |
---|---|
NullPointerException | if the target collection is null |
public static <T> boolean addAllIgnoreNull(final Collection<T> target, final Collection<T> source)
//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); } }