mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
add nonNullArray and mergeLists
This commit is contained in:
@ -415,6 +415,22 @@ public class CollectionUtils
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Returns the input array, unless it was null - in which case a new (empty) array is returned.
|
||||||
|
**
|
||||||
|
** Meant to help avoid null checks on foreach loops.
|
||||||
|
*******************************************************************************/
|
||||||
|
public static <T> T[] nonNullArray(T[] array)
|
||||||
|
{
|
||||||
|
if(array == null)
|
||||||
|
{
|
||||||
|
return (T[]) new ArrayList<T>().toArray();
|
||||||
|
}
|
||||||
|
return (array);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
** Returns the input list, unless it was null - in which case a new array list is returned.
|
** Returns the input list, unless it was null - in which case a new array list is returned.
|
||||||
**
|
**
|
||||||
@ -513,4 +529,25 @@ public class CollectionUtils
|
|||||||
return (mapper.convertValue(o, Map.class));
|
return (mapper.convertValue(o, Map.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
public static <T> List<T> mergeLists(List<T>... lists)
|
||||||
|
{
|
||||||
|
List<T> rs = new ArrayList<>();
|
||||||
|
if(lists != null)
|
||||||
|
{
|
||||||
|
for(List<T> list : lists)
|
||||||
|
{
|
||||||
|
if(list != null)
|
||||||
|
{
|
||||||
|
rs.addAll(list);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (rs);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ import java.util.Map;
|
|||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import com.kingsrook.qqq.backend.core.BaseTest;
|
import com.kingsrook.qqq.backend.core.BaseTest;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
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;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
@ -499,4 +500,48 @@ class CollectionUtilsTest extends BaseTest
|
|||||||
assertEquals(list, accumulator);
|
assertEquals(list, accumulator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testNonNullArray()
|
||||||
|
{
|
||||||
|
assertArrayEquals(new Integer[] { }, CollectionUtils.nonNullArray(null));
|
||||||
|
assertArrayEquals(new Integer[] { }, CollectionUtils.nonNullArray(new Integer[] { }));
|
||||||
|
assertArrayEquals(new Integer[] { 1, 2, 3 }, CollectionUtils.nonNullArray(new Integer[] { 1, 2, 3 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testNonNullList()
|
||||||
|
{
|
||||||
|
assertEquals(List.of(), CollectionUtils.nonNullList(null));
|
||||||
|
assertEquals(List.of(), CollectionUtils.nonNullList(List.of()));
|
||||||
|
assertEquals(List.of(1, 2, 3), CollectionUtils.nonNullList(List.of(1, 2, 3)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
void testMergeLists()
|
||||||
|
{
|
||||||
|
assertEquals(List.of(), CollectionUtils.mergeLists());
|
||||||
|
assertEquals(List.of(), CollectionUtils.mergeLists((List<Object>) null));
|
||||||
|
assertEquals(List.of(), CollectionUtils.mergeLists((List<Object>[]) null));
|
||||||
|
assertEquals(List.of(), CollectionUtils.mergeLists(List.of()));
|
||||||
|
assertEquals(List.of(1, 2, 3), CollectionUtils.mergeLists(List.of(1, 2, 3)));
|
||||||
|
assertEquals(List.of(1, 2, 3), CollectionUtils.mergeLists(List.of(1, 2), List.of(3)));
|
||||||
|
assertEquals(List.of(1, 2, 3), CollectionUtils.mergeLists(List.of(1, 2), null, List.of(3)));
|
||||||
|
assertEquals(List.of(1, 2, 3), CollectionUtils.mergeLists(null, List.of(1, 2, 3), null));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user