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.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -712,4 +713,26 @@ public class CollectionUtils
c.add(element); 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);
}
}
} }

View File

@ -33,11 +33,14 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.kingsrook.qqq.backend.core.BaseTest; import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.utils.collections.ListBuilder;
import com.kingsrook.qqq.backend.core.utils.collections.MapBuilder; import com.kingsrook.qqq.backend.core.utils.collections.MapBuilder;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@ -639,4 +642,35 @@ class CollectionUtilsTest extends BaseTest
assertEquals(Set.of("", "1"), s); assertEquals(Set.of("", "1"), s);
} }
/*******************************************************************************
**
*******************************************************************************/
@Test
void testAddAllIfNotNull()
{
BiFunction<Collection<Integer>, Collection<Integer>, Collection<Integer>> doAddAllIfNotNull = (Collection<Integer> destination, Collection<Integer> source) ->
{
CollectionUtils.addAllIfNotNull(destination, source);
return (destination);
};
assertThatThrownBy(() -> doAddAllIfNotNull.apply(null, null)).hasMessage("destination may not be null");
assertThatThrownBy(() -> doAddAllIfNotNull.apply(null, Collections.emptyList())).hasMessage("destination may not be null");
assertThatThrownBy(() -> doAddAllIfNotNull.apply(null, List.of(1))).hasMessage("destination may not be null");
assertEquals(List.of(), doAddAllIfNotNull.apply(new ArrayList<>(), null));
assertEquals(List.of(), doAddAllIfNotNull.apply(new ArrayList<>(), Collections.emptyList()));
assertEquals(List.of(1), doAddAllIfNotNull.apply(new ArrayList<>(), List.of(1)));
assertEquals(List.of(1), doAddAllIfNotNull.apply(ListBuilder.of(1), null));
assertEquals(List.of(1, 2), doAddAllIfNotNull.apply(ListBuilder.of(1), ListBuilder.of(2)));
assertEquals(List.of(1, 2, 3), doAddAllIfNotNull.apply(ListBuilder.of(1), ListBuilder.of(2, 3)));
assertEquals(Set.of(1), doAddAllIfNotNull.apply(new HashSet<>(List.of(1)), null));
assertEquals(Set.of(1, 2), doAddAllIfNotNull.apply(new HashSet<>(List.of(1)), List.of(2)));
assertEquals(Set.of(1, 2, 3), doAddAllIfNotNull.apply(new HashSet<>(List.of(1)), List.of(2, 3)));
}
} }