[LANG-920] Add ArrayUtils#nullToEmpty(Class<?>[])
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1531399 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
06df5cbe7c
commit
a9ae686ec8
|
@ -473,6 +473,26 @@ public class ArrayUtils {
|
|||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Defensive programming technique to change a {@code null}
|
||||
* reference to an empty one.</p>
|
||||
*
|
||||
* <p>This method returns an empty array for a {@code null} input array.</p>
|
||||
*
|
||||
* <p>As a memory optimizing technique an empty array passed in will be overridden with
|
||||
* the empty {@code public static} references in this class.</p>
|
||||
*
|
||||
* @param array the array to check for {@code null} or empty
|
||||
* @return the same array, {@code public static} empty array if {@code null} or empty input
|
||||
* @since 3.2
|
||||
*/
|
||||
public static Class<?>[] nullToEmpty(final Class<?>[] array) {
|
||||
if (array == null || array.length == 0) {
|
||||
return EMPTY_CLASS_ARRAY;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Defensive programming technique to change a {@code null}
|
||||
* reference to an empty one.</p>
|
||||
|
|
|
@ -488,6 +488,20 @@ public class ArrayUtilsTest {
|
|||
assertTrue(empty != result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullToEmptyClass() {
|
||||
// Test null handling
|
||||
assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, ArrayUtils.nullToEmpty((Class<?>[]) null));
|
||||
// Test valid array handling
|
||||
final Class<?>[] original = { Object.class, String.class };
|
||||
assertArrayEquals(original, ArrayUtils.nullToEmpty(original));
|
||||
// Test empty array handling
|
||||
final Class<?>[] empty = {};
|
||||
final Class<?>[] result = ArrayUtils.nullToEmpty(empty);
|
||||
assertArrayEquals(ArrayUtils.EMPTY_CLASS_ARRAY, result);
|
||||
assertTrue(empty != result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullToEmptyString() {
|
||||
// Test null handling
|
||||
|
|
Loading…
Reference in New Issue