From 643302af8c0e10ed7ee1d9ca3c60fa0f61eaaf08 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Wed, 3 Feb 2010 07:20:40 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang3/ArrayUtils.java | 100 ++++++++++++++++++ .../apache/commons/lang3/ArrayUtilsTest.java | 70 ++++++++++++ 2 files changed, 170 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 3a543ce29..98e136ec0 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -2974,6 +2974,106 @@ public class ArrayUtils { return false; } + // ---------------------------------------------------------------------- + /** + *

Checks if an array of Objects is not empty or not null.

+ * + * @param array the array to test + * @return true if the array is not empty or not null + * + */ + public static boolean isNotEmpty(T[] array) { + return (array != null && array.length != 0); + } + + /** + *

Checks if an array of primitive longs is not empty or not null.

+ * + * @param array the array to test + * @return true if the array is not empty or not null + * + */ + public static boolean isNotEmpty(long[] array) { + return (array != null && array.length != 0); + } + + /** + *

Checks if an array of primitive ints is not empty or not null.

+ * + * @param array the array to test + * @return true if the array is not empty or not null + * + */ + public static boolean isNotEmpty(int[] array) { + return (array != null && array.length != 0); + } + + /** + *

Checks if an array of primitive shorts is not empty or not null.

+ * + * @param array the array to test + * @return true if the array is not empty or not null + * + */ + public static boolean isNotEmpty(short[] array) { + return (array != null && array.length != 0); + } + + /** + *

Checks if an array of primitive chars is not empty or not null.

+ * + * @param array the array to test + * @return true if the array is not empty or not null + * + */ + public static boolean isNotEmpty(char[] array) { + return (array != null && array.length != 0); + } + + /** + *

Checks if an array of primitive bytes is not empty or not null.

+ * + * @param array the array to test + * @return true if the array is not empty or not null + * + */ + public static boolean isNotEmpty(byte[] array) { + return (array != null && array.length != 0); + } + + /** + *

Checks if an array of primitive doubles is not empty or not null.

+ * + * @param array the array to test + * @return true if the array is not empty or not null + * + */ + public static boolean isNotEmpty(double[] array) { + return (array != null && array.length != 0); + } + + /** + *

Checks if an array of primitive floats is not empty or not null.

+ * + * @param array the array to test + * @return true if the array is not empty or not null + * + */ + public static boolean isNotEmpty(float[] array) { + return (array != null && array.length != 0); + } + + /** + *

Checks if an array of primitive booleans is not empty or not null.

+ * + * @param array the array to test + * @return true if the array is not empty or not null + * + */ + public static boolean isNotEmpty(boolean[] array) { + return (array != null && array.length != 0); + } + /** *

Adds all the elements of the given arrays into a new array.

*

The new array contains all of the element of array1 followed diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 9806e399c..abe452b13 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -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));