fredrik 2004/01/19 13:50:06
Modified: lang/src/java/org/apache/commons/lang ArrayUtils.java
lang/src/test/org/apache/commons/lang ArrayUtilsTest.java
Log:
Added isEmpty for Object and primitives arrays. RFE in bugzilla (#26243).
Revision Changes Path
1.32 +129 -2
jakarta-commons/lang/src/java/org/apache/commons/lang/ArrayUtils.java
Index: ArrayUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/ArrayUtils.java,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -r1.31 -r1.32
--- ArrayUtils.java 8 Jan 2004 17:50:40 -0000 1.31
+++ ArrayUtils.java 19 Jan 2004 21:50:06 -0000 1.32
@@ -1,7 +1,7 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
+ * Copyright (c) 2002-2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -80,6 +80,7 @@
* @author Pete Gieser
* @author Gary Gregory
* @author <a href="mailto:[EMAIL PROTECTED]">Ashwin S</a>
+ * @author Fredrik Westermarck
* @since 2.0
* @version $Id$
*/
@@ -2625,4 +2626,130 @@
return result;
}
+ // ----------------------------------------------------------------------
+ /**
+ * <p>Checks if an array of Objects is empty or <code>null</code>.</p>
+ *
+ * @param array the array to test
+ * @return <code>true</code> if the array is empty or <code>null</code>
+ * @since 2.1
+ */
+ public static boolean isEmpty(final Object[] array) {
+ if (array == null || array.length == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * <p>Checks if an array of primitive longs is empty or <code>null</code>.</p>
+ *
+ * @param array the array to test
+ * @return <code>true</code> if the array is empty or <code>null</code>
+ * @since 2.1
+ */
+ public static boolean isEmpty(final long[] array) {
+ if (array == null || array.length == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * <p>Checks if an array of primitive ints is empty or <code>null</code>.</p>
+ *
+ * @param array the array to test
+ * @return <code>true</code> if the array is empty or <code>null</code>
+ * @since 2.1
+ */
+ public static boolean isEmpty(final int[] array) {
+ if (array == null || array.length == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * <p>Checks if an array of primitive shorts is empty or <code>null</code>.</p>
+ *
+ * @param array the array to test
+ * @return <code>true</code> if the array is empty or <code>null</code>
+ * @since 2.1
+ */
+ public static boolean isEmpty(final short[] array) {
+ if (array == null || array.length == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * <p>Checks if an array of primitive chars is empty or <code>null</code>.</p>
+ *
+ * @param array the array to test
+ * @return <code>true</code> if the array is empty or <code>null</code>
+ * @since 2.1
+ */
+ public static boolean isEmpty(final char[] array) {
+ if (array == null || array.length == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * <p>Checks if an array of primitive bytes is empty or <code>null</code>.</p>
+ *
+ * @param array the array to test
+ * @return <code>true</code> if the array is empty or <code>null</code>
+ * @since 2.1
+ */
+ public static boolean isEmpty(final byte[] array) {
+ if (array == null || array.length == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * <p>Checks if an array of primitive doubles is empty or <code>null</code>.</p>
+ *
+ * @param array the array to test
+ * @return <code>true</code> if the array is empty or <code>null</code>
+ * @since 2.1
+ */
+ public static boolean isEmpty(final double[] array) {
+ if (array == null || array.length == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * <p>Checks if an array of primitive floats is empty or <code>null</code>.</p>
+ *
+ * @param array the array to test
+ * @return <code>true</code> if the array is empty or <code>null</code>
+ * @since 2.1
+ */
+ public static boolean isEmpty(final float[] array) {
+ if (array == null || array.length == 0) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * <p>Checks if an array of primitive booleans is empty or
<code>null</code>.</p>
+ *
+ * @param array the array to test
+ * @return <code>true</code> if the array is empty or <code>null</code>
+ * @since 2.1
+ */
+ public static boolean isEmpty(final boolean[] array) {
+ if (array == null || array.length == 0) {
+ return true;
+ }
+ return false;
+ }
}
1.20 +66 -3
jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsTest.java
Index: ArrayUtilsTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ArrayUtilsTest.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- ArrayUtilsTest.java 8 Jan 2004 17:54:28 -0000 1.19
+++ ArrayUtilsTest.java 19 Jan 2004 21:50:06 -0000 1.20
@@ -1,7 +1,7 @@
/* ====================================================================
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
+ * Copyright (c) 2002-2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -73,6 +73,7 @@
* @author Matthew Hawthorne
* @author Tim O'Brien
* @author <a href="mailto:[EMAIL PROTECTED]">Ashwin S</a>
+ * @author Fredrik Westermarck
* @version $Id$
*/
public class ArrayUtilsTest extends TestCase {
@@ -136,7 +137,7 @@
assertEquals(true, ArrayUtils.hashCode(array3) ==
ArrayUtils.hashCode(array3));
assertEquals(true, ArrayUtils.hashCode(array3) ==
ArrayUtils.hashCode(array4));
}
-
+
//-----------------------------------------------------------------------
public void testIsEquals() {
long[][] array1 = new long[][] {{2,5}, {4,5}};
@@ -2289,4 +2290,66 @@
new double[] { Double.MIN_VALUE, Double.MAX_VALUE, 9999999 })));
}
+ //-----------------------------------------------------------------------
+ /**
+ * Test for [EMAIL PROTECTED] ArrayUtils#isEmpty(java.lang.Object[])}.
+ */
+ public void testIsEmptyObject() {
+ Object[] emptyArray = new Object[] {};
+ Object[] notEmptyArray = new Object[] { new String("Value") };
+ assertEquals(true, ArrayUtils.isEmpty(emptyArray));
+ assertEquals(false, ArrayUtils.isEmpty(notEmptyArray));
+ }
+
+ /**
+ * Tests for [EMAIL PROTECTED] ArrayUtils#isEmpty(long[])},
+ * [EMAIL PROTECTED] ArrayUtils#isEmpty(int[])},
+ * [EMAIL PROTECTED] ArrayUtils#isEmpty(short[])},
+ * [EMAIL PROTECTED] ArrayUtils#isEmpty(char[])},
+ * [EMAIL PROTECTED] ArrayUtils#isEmpty(byte[])},
+ * [EMAIL PROTECTED] ArrayUtils#isEmpty(double[])},
+ * [EMAIL PROTECTED] ArrayUtils#isEmpty(float[])} and
+ * [EMAIL PROTECTED] ArrayUtils#isEmpty(boolean[])}.
+ */
+ public void testIsEmptyPrimitives() {
+ long[] emptyLongArray = new long[] {};
+ long[] notEmptyLongArray = new long[] { 1L };
+ assertEquals(true, ArrayUtils.isEmpty(emptyLongArray));
+ assertEquals(false, ArrayUtils.isEmpty(notEmptyLongArray));
+
+ int[] emptyIntArray = new int[] {};
+ int[] notEmptyIntArray = new int[] { 1 };
+ assertEquals(true, ArrayUtils.isEmpty(emptyIntArray));
+ assertEquals(false, ArrayUtils.isEmpty(notEmptyIntArray));
+
+ short[] emptyShortArray = new short[] {};
+ short[] notEmptyShortArray = new short[] { 1 };
+ assertEquals(true, ArrayUtils.isEmpty(emptyShortArray));
+ assertEquals(false, ArrayUtils.isEmpty(notEmptyShortArray));
+
+ char[] emptyCharArray = new char[] {};
+ char[] notEmptyCharArray = new char[] { 1 };
+ assertEquals(true, ArrayUtils.isEmpty(emptyCharArray));
+ assertEquals(false, ArrayUtils.isEmpty(notEmptyCharArray));
+
+ byte[] emptyByteArray = new byte[] {};
+ byte[] notEmptyByteArray = new byte[] { 1 };
+ assertEquals(true, ArrayUtils.isEmpty(emptyByteArray));
+ assertEquals(false, ArrayUtils.isEmpty(notEmptyByteArray));
+
+ double[] emptyDoubleArray = new double[] {};
+ double[] notEmptyDoubleArray = new double[] { 1.0 };
+ assertEquals(true, ArrayUtils.isEmpty(emptyDoubleArray));
+ assertEquals(false, ArrayUtils.isEmpty(notEmptyDoubleArray));
+
+ float[] emptyFloatArray = new float[] {};
+ float[] notEmptyFloatArray = new float[] { 1.0F };
+ assertEquals(true, ArrayUtils.isEmpty(emptyFloatArray));
+ assertEquals(false, ArrayUtils.isEmpty(notEmptyFloatArray));
+
+ boolean[] emptyBooleanArray = new boolean[] {};
+ boolean[] notEmptyBooleanArray = new boolean[] { true };
+ assertEquals(true, ArrayUtils.isEmpty(emptyBooleanArray));
+ assertEquals(false, ArrayUtils.isEmpty(notEmptyBooleanArray));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]