Adding nullToEmpty methods to ArrayUtils per LANG-534 and Levon Karayan's patch.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@905925 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2010-02-03 07:53:47 +00:00
parent 6c53b134b6
commit 6b4cb16608
2 changed files with 619 additions and 0 deletions

View File

@ -43,6 +43,7 @@
* @author Gary Gregory
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
* @author Maarten Coene
* @author <a href="mailto:levon@lk.otherinbox.com">Levon Karayan</a>
* @since 2.0
* @version $Id$
*/
@ -441,6 +442,387 @@ public static boolean[] clone(boolean[] array) {
return array.clone();
}
// nullToEmpty
//-----------------------------------------------------------------------
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static Object[] nullToEmpty(Object[] array) {
if (array == null) {
return EMPTY_OBJECT_ARRAY;
} else if (array.length == 0) {
return EMPTY_OBJECT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static String[] nullToEmpty(String[] array) {
if (array == null) {
return EMPTY_STRING_ARRAY;
} else if (array.length == 0) {
return EMPTY_STRING_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static long[] nullToEmpty(long[] array) {
if (array == null) {
return EMPTY_LONG_ARRAY;
}
if (array.length == 0) {
return EMPTY_LONG_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static int[] nullToEmpty(int[] array) {
if (array == null) {
return EMPTY_INT_ARRAY;
} else if (array.length == 0) {
return EMPTY_INT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static short[] nullToEmpty(short[] array) {
if (array == null) {
return EMPTY_SHORT_ARRAY;
} else if (array.length == 0) {
return EMPTY_SHORT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static char[] nullToEmpty(char[] array) {
if (array == null) {
return EMPTY_CHAR_ARRAY;
} else if (array.length == 0) {
return EMPTY_CHAR_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static byte[] nullToEmpty(byte[] array) {
if (array == null) {
return EMPTY_BYTE_ARRAY;
} else if (array.length == 0) {
return EMPTY_BYTE_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static double[] nullToEmpty(double[] array) {
if (array == null) {
return EMPTY_DOUBLE_ARRAY;
} else if (array.length == 0) {
return EMPTY_DOUBLE_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static float[] nullToEmpty(float[] array) {
if (array == null) {
return EMPTY_FLOAT_ARRAY;
} else if (array.length == 0) {
return EMPTY_FLOAT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static boolean[] nullToEmpty(boolean[] array) {
if (array == null) {
return EMPTY_BOOLEAN_ARRAY;
} else if (array.length == 0) {
return EMPTY_BOOLEAN_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static Long[] nullToEmpty(Long[] array) {
if (array == null) {
return EMPTY_LONG_OBJECT_ARRAY;
} else if (array.length == 0) {
return EMPTY_LONG_OBJECT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static Integer[] nullToEmpty(Integer[] array) {
if (array == null) {
return EMPTY_INTEGER_OBJECT_ARRAY;
} else if (array.length == 0) {
return EMPTY_INTEGER_OBJECT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static Short[] nullToEmpty(Short[] array) {
if (array == null) {
return EMPTY_SHORT_OBJECT_ARRAY;
}else if (array.length == 0) {
return EMPTY_SHORT_OBJECT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static Character[] nullToEmpty(Character[] array) {
if (array == null) {
return EMPTY_CHARACTER_OBJECT_ARRAY;
} else if (array.length == 0) {
return EMPTY_CHARACTER_OBJECT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static Byte[] nullToEmpty(Byte[] array) {
if (array == null) {
return EMPTY_BYTE_OBJECT_ARRAY;
} else if (array.length == 0) {
return EMPTY_BYTE_OBJECT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static Double[] nullToEmpty(Double[] array) {
if (array == null) {
return EMPTY_DOUBLE_OBJECT_ARRAY;
} else if (array.length == 0) {
return EMPTY_DOUBLE_OBJECT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static Float[] nullToEmpty(Float[] array) {
if (array == null) {
return EMPTY_FLOAT_OBJECT_ARRAY;
} else if (array.length == 0) {
return EMPTY_FLOAT_OBJECT_ARRAY;
}
return array;
}
/**
* <p>Defensive programming technique to change a <code>null</code>
* reference to an empty one..</p>
*
* <p>This method returns an empty array for a <code>null</code> input array.</p>
*
* <p>As a memory optimizing technique an empty array passed in will be overridden with
* the empty <code>public static</code> references in this class.</p>
*
* @param array the array to check for <code>null</code> or empty
* @return the same array, <code>public static</code> empty array if <code>null</code> or empty input
*/
public static Boolean[] nullToEmpty(Boolean[] array) {
if (array == null) {
return EMPTY_BOOLEAN_OBJECT_ARRAY;
} else if (array.length == 0) {
return EMPTY_BOOLEAN_OBJECT_ARRAY;
}
return array;
}
// Subarrays
//-----------------------------------------------------------------------
/**

View File

@ -36,6 +36,7 @@
* @author Fredrik Westermarck
* @author Gary Gregory
* @author Maarten Coene
* @author <a href="mailto:levon@lk.otherinbox.com">Levon Karayan</a>
* @version $Id$
*/
public class ArrayUtilsTest extends TestCase {
@ -325,6 +326,242 @@ public void testCloneFloat() {
//-----------------------------------------------------------------------
public void testNullToEmptyBoolean() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, ArrayUtils.nullToEmpty((boolean[]) null));
// Test valid array handling
boolean[] original = new boolean[] {true, false};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
boolean[] empty = new boolean[]{};
boolean[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_BOOLEAN_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyLong() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_LONG_ARRAY, ArrayUtils.nullToEmpty((long[]) null));
// Test valid array handling
long[] original = new long[] {1L, 2L};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
long[] empty = new long[]{};
long[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_LONG_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyInt() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_INT_ARRAY, ArrayUtils.nullToEmpty((int[]) null));
// Test valid array handling
int[] original = new int[] {1, 2};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
int[] empty = new int[]{};
int[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_INT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyShort() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_SHORT_ARRAY, ArrayUtils.nullToEmpty((short[]) null));
// Test valid array handling
short[] original = new short[] {1, 2};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
short[] empty = new short[]{};
short[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_SHORT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyChar() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, ArrayUtils.nullToEmpty((char[]) null));
// Test valid array handling
char[] original = new char[] {'a', 'b'};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
char[] empty = new char[]{};
char[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyByte() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.nullToEmpty((byte[]) null));
// Test valid array handling
byte[] original = new byte[] {0x0F, 0x0E};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
byte[] empty = new byte[]{};
byte[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_BYTE_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyDouble() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, ArrayUtils.nullToEmpty((double[]) null));
// Test valid array handling
double[] original = new double[] {1L, 2L};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
double[] empty = new double[]{};
double[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_DOUBLE_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyFloat() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, ArrayUtils.nullToEmpty((float[]) null));
// Test valid array handling
float[] original = new float[] {2.6f, 3.8f};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
float[] empty = new float[]{};
float[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_FLOAT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyObject() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_OBJECT_ARRAY, ArrayUtils.nullToEmpty((Object[]) null));
// Test valid array handling
Object[] original = new Object[] {true, false};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
Object[] empty = new Object[]{};
Object[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_OBJECT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyString() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.nullToEmpty((String[]) null));
// Test valid array handling
String[] original = new String[] {"abc", "def"};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
String[] empty = new String[]{};
String[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_STRING_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyBooleanObject() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, ArrayUtils.nullToEmpty((Boolean[]) null));
// Test valid array handling
Boolean[] original = new Boolean[] {Boolean.TRUE, Boolean.FALSE};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
Boolean[] empty = new Boolean[]{};
Boolean[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyLongObject() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_LONG_OBJECT_ARRAY, ArrayUtils.nullToEmpty((Long[]) null));
// Test valid array handling
Long[] original = new Long[] {1L, 2L};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
Long[] empty = new Long[]{};
Long[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_LONG_OBJECT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyIntObject() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_INTEGER_OBJECT_ARRAY, ArrayUtils.nullToEmpty((Integer[]) null));
// Test valid array handling
Integer[] original = new Integer[] {1, 2};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
Integer[] empty = new Integer[]{};
Integer[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_INTEGER_OBJECT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyShortObject() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_SHORT_OBJECT_ARRAY, ArrayUtils.nullToEmpty((Short[]) null));
// Test valid array handling
Short[] original = new Short[] {1, 2};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
Short[] empty = new Short[]{};
Short[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_SHORT_OBJECT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyCharObject() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_CHARACTER_OBJECT_ARRAY, ArrayUtils.nullToEmpty((Character[]) null));
// Test valid array handling
Character[] original = new Character[] {'a', 'b'};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
Character[] empty = new Character[]{};
Character[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_CHARACTER_OBJECT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyByteObject() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_BYTE_OBJECT_ARRAY, ArrayUtils.nullToEmpty((Byte[]) null));
// Test valid array handling
Byte[] original = new Byte[] {0x0F, 0x0E};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
Byte[] empty = new Byte[]{};
Byte[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_BYTE_OBJECT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyDoubleObject() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_DOUBLE_OBJECT_ARRAY, ArrayUtils.nullToEmpty((Double[]) null));
// Test valid array handling
Double[] original = new Double[] {1D, 2D};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
Double[] empty = new Double[]{};
Double[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_DOUBLE_OBJECT_ARRAY, result);
assertTrue(empty != result);
}
public void testNullToEmptyFloatObject() {
// Test null handling
assertEquals(ArrayUtils.EMPTY_FLOAT_OBJECT_ARRAY, ArrayUtils.nullToEmpty((Float[]) null));
// Test valid array handling
Float[] original = new Float[] {2.6f, 3.8f};
assertEquals(original, ArrayUtils.nullToEmpty(original));
// Test empty array handling
Float[] empty = new Float[]{};
Float[] result = ArrayUtils.nullToEmpty(empty);
assertEquals(ArrayUtils.EMPTY_FLOAT_OBJECT_ARRAY, result);
assertTrue(empty != result);
}
//-----------------------------------------------------------------------
public void testSubarrayObject() {
Object[] nullArray = null;
Object[] objectArray = { "a", "b", "c", "d", "e", "f"};