diff --git a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/CollectionUtils.java b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/CollectionUtils.java index 90e2756d..17ac02fd 100755 --- a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/CollectionUtils.java +++ b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/CollectionUtils.java @@ -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 void addAllIfNotNull(Collection destination, Collection source) + { + Objects.requireNonNull(destination, "destination may not be null"); + + if(source != null) + { + destination.addAll(source); + } + } } diff --git a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/CollectionUtilsTest.java b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/CollectionUtilsTest.java index a243c775..7ccc9780 100644 --- a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/CollectionUtilsTest.java +++ b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/CollectionUtilsTest.java @@ -33,11 +33,14 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; +import java.util.function.BiFunction; import java.util.function.Function; import com.google.gson.reflect.TypeToken; 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 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.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -639,4 +642,35 @@ class CollectionUtilsTest extends BaseTest assertEquals(Set.of("", "1"), s); } + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testAddAllIfNotNull() + { + BiFunction, Collection, Collection> doAddAllIfNotNull = (Collection destination, Collection 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))); + } + }