Applying Chandrashekar M's patch to LANG-583, adding isNotEmpty(array) methods to ArrayUtils.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@905917 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
23f3b90843
commit
643302af8c
|
@ -2974,6 +2974,106 @@ public class ArrayUtils {
|
|||
return false;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Checks if an array of Objects is not empty or <code>not null</code>.</p>
|
||||
*
|
||||
* @param array the array to test
|
||||
* @return <code>true</code> if the array is not empty or <code>not null</code>
|
||||
*
|
||||
*/
|
||||
public static <T> boolean isNotEmpty(T[] array) {
|
||||
return (array != null && array.length != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if an array of primitive longs is not empty or <code>not null</code>.</p>
|
||||
*
|
||||
* @param array the array to test
|
||||
* @return <code>true</code> if the array is not empty or <code>not null</code>
|
||||
*
|
||||
*/
|
||||
public static boolean isNotEmpty(long[] array) {
|
||||
return (array != null && array.length != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if an array of primitive ints is not empty or <code>not null</code>.</p>
|
||||
*
|
||||
* @param array the array to test
|
||||
* @return <code>true</code> if the array is not empty or <code>not null</code>
|
||||
*
|
||||
*/
|
||||
public static boolean isNotEmpty(int[] array) {
|
||||
return (array != null && array.length != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if an array of primitive shorts is not empty or <code>not null</code>.</p>
|
||||
*
|
||||
* @param array the array to test
|
||||
* @return <code>true</code> if the array is not empty or <code>not null</code>
|
||||
*
|
||||
*/
|
||||
public static boolean isNotEmpty(short[] array) {
|
||||
return (array != null && array.length != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if an array of primitive chars is not empty or <code>not null</code>.</p>
|
||||
*
|
||||
* @param array the array to test
|
||||
* @return <code>true</code> if the array is not empty or <code>not null</code>
|
||||
*
|
||||
*/
|
||||
public static boolean isNotEmpty(char[] array) {
|
||||
return (array != null && array.length != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if an array of primitive bytes is not empty or <code>not null</code>.</p>
|
||||
*
|
||||
* @param array the array to test
|
||||
* @return <code>true</code> if the array is not empty or <code>not null</code>
|
||||
*
|
||||
*/
|
||||
public static boolean isNotEmpty(byte[] array) {
|
||||
return (array != null && array.length != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if an array of primitive doubles is not empty or <code>not null</code>.</p>
|
||||
*
|
||||
* @param array the array to test
|
||||
* @return <code>true</code> if the array is not empty or <code>not null</code>
|
||||
*
|
||||
*/
|
||||
public static boolean isNotEmpty(double[] array) {
|
||||
return (array != null && array.length != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if an array of primitive floats is not empty or <code>not null</code>.</p>
|
||||
*
|
||||
* @param array the array to test
|
||||
* @return <code>true</code> if the array is not empty or <code>not null</code>
|
||||
*
|
||||
*/
|
||||
public static boolean isNotEmpty(float[] array) {
|
||||
return (array != null && array.length != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if an array of primitive booleans is not empty or <code>not null</code>.</p>
|
||||
*
|
||||
* @param array the array to test
|
||||
* @return <code>true</code> if the array is not empty or <code>not null</code>
|
||||
*
|
||||
*/
|
||||
public static boolean isNotEmpty(boolean[] array) {
|
||||
return (array != null && array.length != 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Adds all the elements of the given arrays into a new array.</p>
|
||||
* <p>The new array contains all of the element of <code>array1</code> followed
|
||||
|
|
|
@ -2542,6 +2542,76 @@ public class ArrayUtilsTest extends TestCase {
|
|||
assertEquals(false, ArrayUtils.isEmpty(notEmptyBooleanArray));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link ArrayUtils#isNotEmpty(java.lang.Object[])}.
|
||||
*/
|
||||
public void testIsNotEmptyObject() {
|
||||
Object[] emptyArray = new Object[] {};
|
||||
Object[] notEmptyArray = new Object[] { new String("Value") };
|
||||
assertFalse(ArrayUtils.isNotEmpty((Object[])null));
|
||||
assertFalse(ArrayUtils.isNotEmpty(emptyArray));
|
||||
assertTrue(ArrayUtils.isNotEmpty(notEmptyArray));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for {@link ArrayUtils#isNotEmpty(long[])},
|
||||
* {@link ArrayUtils#isNotEmpty(int[])},
|
||||
* {@link ArrayUtils#isNotEmpty(short[])},
|
||||
* {@link ArrayUtils#isNotEmpty(char[])},
|
||||
* {@link ArrayUtils#isNotEmpty(byte[])},
|
||||
* {@link ArrayUtils#isNotEmpty(double[])},
|
||||
* {@link ArrayUtils#isNotEmpty(float[])} and
|
||||
* {@link ArrayUtils#isNotEmpty(boolean[])}.
|
||||
*/
|
||||
public void testIsNotEmptyPrimitives() {
|
||||
long[] emptyLongArray = new long[] {};
|
||||
long[] notEmptyLongArray = new long[] { 1L };
|
||||
assertFalse(ArrayUtils.isNotEmpty((long[])null));
|
||||
assertFalse(ArrayUtils.isNotEmpty(emptyLongArray));
|
||||
assertTrue(ArrayUtils.isNotEmpty(notEmptyLongArray));
|
||||
|
||||
int[] emptyIntArray = new int[] {};
|
||||
int[] notEmptyIntArray = new int[] { 1 };
|
||||
assertFalse(ArrayUtils.isNotEmpty((int[])null));
|
||||
assertFalse(ArrayUtils.isNotEmpty(emptyIntArray));
|
||||
assertTrue(ArrayUtils.isNotEmpty(notEmptyIntArray));
|
||||
|
||||
short[] emptyShortArray = new short[] {};
|
||||
short[] notEmptyShortArray = new short[] { 1 };
|
||||
assertFalse(ArrayUtils.isNotEmpty((short[])null));
|
||||
assertFalse(ArrayUtils.isNotEmpty(emptyShortArray));
|
||||
assertTrue(ArrayUtils.isNotEmpty(notEmptyShortArray));
|
||||
|
||||
char[] emptyCharArray = new char[] {};
|
||||
char[] notEmptyCharArray = new char[] { 1 };
|
||||
assertFalse(ArrayUtils.isNotEmpty((char[])null));
|
||||
assertFalse(ArrayUtils.isNotEmpty(emptyCharArray));
|
||||
assertTrue(ArrayUtils.isNotEmpty(notEmptyCharArray));
|
||||
|
||||
byte[] emptyByteArray = new byte[] {};
|
||||
byte[] notEmptyByteArray = new byte[] { 1 };
|
||||
assertFalse(ArrayUtils.isNotEmpty((byte[])null));
|
||||
assertFalse(ArrayUtils.isNotEmpty(emptyByteArray));
|
||||
assertTrue(ArrayUtils.isNotEmpty(notEmptyByteArray));
|
||||
|
||||
double[] emptyDoubleArray = new double[] {};
|
||||
double[] notEmptyDoubleArray = new double[] { 1.0 };
|
||||
assertFalse(ArrayUtils.isNotEmpty((double[])null));
|
||||
assertFalse(ArrayUtils.isNotEmpty(emptyDoubleArray));
|
||||
assertTrue(ArrayUtils.isNotEmpty(notEmptyDoubleArray));
|
||||
|
||||
float[] emptyFloatArray = new float[] {};
|
||||
float[] notEmptyFloatArray = new float[] { 1.0F };
|
||||
assertFalse(ArrayUtils.isNotEmpty((float[])null));
|
||||
assertFalse(ArrayUtils.isNotEmpty(emptyFloatArray));
|
||||
assertTrue(ArrayUtils.isNotEmpty(notEmptyFloatArray));
|
||||
|
||||
boolean[] emptyBooleanArray = new boolean[] {};
|
||||
boolean[] notEmptyBooleanArray = new boolean[] { true };
|
||||
assertFalse(ArrayUtils.isNotEmpty((boolean[])null));
|
||||
assertFalse(ArrayUtils.isNotEmpty(emptyBooleanArray));
|
||||
assertTrue(ArrayUtils.isNotEmpty(notEmptyBooleanArray));
|
||||
}
|
||||
// ------------------------------------------------------------------------
|
||||
public void testGetLength() {
|
||||
assertEquals(0, ArrayUtils.getLength(null));
|
||||
|
|
Loading…
Reference in New Issue