Added isEmpty for Object and primitives arrays. RFE in bugzilla (#26243).

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137756 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fredrik Westermarck 2004-01-19 21:50:06 +00:00
parent 2e82cc4a15
commit 79a860193f
2 changed files with 195 additions and 5 deletions

View File

@ -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,8 +80,9 @@
* @author Pete Gieser
* @author Gary Gregory
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
* @author Fredrik Westermarck
* @since 2.0
* @version $Id: ArrayUtils.java,v 1.31 2004/01/08 17:50:40 fredrik Exp $
* @version $Id: ArrayUtils.java,v 1.32 2004/01/19 21:50:06 fredrik Exp $
*/
public class ArrayUtils {
@ -2625,4 +2626,130 @@ public static Boolean[] toObject(final boolean[] array) {
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;
}
}

View File

@ -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,7 +73,8 @@
* @author Matthew Hawthorne
* @author Tim O'Brien
* @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
* @version $Id: ArrayUtilsTest.java,v 1.19 2004/01/08 17:54:28 fredrik Exp $
* @author Fredrik Westermarck
* @version $Id: ArrayUtilsTest.java,v 1.20 2004/01/19 21:50:06 fredrik Exp $
*/
public class ArrayUtilsTest extends TestCase {
@ -136,7 +137,7 @@ public void testHashCode() {
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 @@ public void testToObject_double() {
new double[] { Double.MIN_VALUE, Double.MAX_VALUE, 9999999 })));
}
//-----------------------------------------------------------------------
/**
* Test for {@link 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 {@link ArrayUtils#isEmpty(long[])},
* {@link ArrayUtils#isEmpty(int[])},
* {@link ArrayUtils#isEmpty(short[])},
* {@link ArrayUtils#isEmpty(char[])},
* {@link ArrayUtils#isEmpty(byte[])},
* {@link ArrayUtils#isEmpty(double[])},
* {@link ArrayUtils#isEmpty(float[])} and
* {@link 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));
}
}