diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index cbd32fdd816..a1b3466495b 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -458,6 +458,27 @@ public static boolean[] clone(final boolean[] array) { // nullToEmpty //----------------------------------------------------------------------- + /** + *

Defensive programming technique to change a {@code null} + * reference to an empty one.

+ * + *

This method returns an empty array for a {@code null} input array.

+ * + * @param array the array to check for {@code null} or empty + * @param type the class representation of the desired array + * @return the same array, {@code public static} empty array if {@code null} + * @throws IllegalArgumentException if the type argument is null + */ + public static T[] nullToEmpty(final T[] array, final Class type) { + if(type == null) { + throw new IllegalArgumentException("The Type must not be null"); + } + else if(array == null) { + return type.cast(Array.newInstance(type.getComponentType(), 0)); + } + return array; + } + /** *

Defensive programming technique to change a {@code null} * reference to an empty one.

diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 27150aea3f8..b48863c270c 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -16,7 +16,7 @@ */ package org.apache.commons.lang3; -import static org.junit.Assert.*; +import org.junit.Test; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; @@ -25,7 +25,15 @@ import java.util.Date; import java.util.Map; -import org.junit.Test; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * Unit tests {@link org.apache.commons.lang3.ArrayUtils}. @@ -356,6 +364,37 @@ public void testCloneFloat() { //----------------------------------------------------------------------- + private class TestClass{} + + @Test + public void testNullToEmptyGenericNull() { + TestClass[] output = ArrayUtils.nullToEmpty(null, TestClass[].class); + + assertTrue(output != null); + assertTrue(output.length == 0); + } + + @Test + public void testNullToEmptyGenericEmpty() { + TestClass[] input = new TestClass[]{}; + TestClass[] output = ArrayUtils.nullToEmpty(input, TestClass[].class); + + assertSame(input, output); + } + + @Test + public void testNullToEmptyGeneric() { + TestClass[] input = new TestClass[]{new TestClass(), new TestClass()}; + TestClass[] output = ArrayUtils.nullToEmpty(input, TestClass[].class); + + assertSame(input, output); + } + + @Test(expected=IllegalArgumentException.class) + public void testNullToEmptyGenericNullType() { + TestClass[] input = new TestClass[]{}; + ArrayUtils.nullToEmpty(input, null); + } @Test public void testNullToEmptyBooleanNull() throws Exception {