org.ehcache.impl.internal.classes.commonslang
Class ArrayUtils

java.lang.Object
  extended by org.ehcache.impl.internal.classes.commonslang.ArrayUtils

public class ArrayUtils
extends java.lang.Object

Operations on arrays, primitive arrays (like int[]) and primitive wrapper arrays (like Integer[]).

This class tries to handle null input gracefully. An exception will not be thrown for a null array input. However, an Object array that contains a null element may throw an exception. Each method documents its behaviour.

#ThreadSafe#

Since:
2.0

Field Summary
static java.lang.Class<?>[] EMPTY_CLASS_ARRAY
          An empty immutable Class array.
static java.lang.Object[] EMPTY_OBJECT_ARRAY
          An empty immutable Object array.
 
Constructor Summary
ArrayUtils()
          ArrayUtils instances should NOT be constructed in standard programming.
 
Method Summary
static int getLength(java.lang.Object array)
          Returns the length of the specified array.
static boolean isEmpty(java.lang.Object[] array)
          Checks if an array of Objects is empty or null.
static boolean isSameLength(java.lang.Object[] array1, java.lang.Object[] array2)
          Checks whether two arrays are the same length, treating null arrays as length 0.
static java.lang.Class<?>[] nullToEmpty(java.lang.Class<?>[] array)
          Defensive programming technique to change a null reference to an empty one.
static java.lang.Object[] nullToEmpty(java.lang.Object[] array)
          Defensive programming technique to change a null reference to an empty one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_OBJECT_ARRAY

public static final java.lang.Object[] EMPTY_OBJECT_ARRAY
An empty immutable Object array.


EMPTY_CLASS_ARRAY

public static final java.lang.Class<?>[] EMPTY_CLASS_ARRAY
An empty immutable Class array.

Constructor Detail

ArrayUtils

public ArrayUtils()

ArrayUtils instances should NOT be constructed in standard programming. Instead, the class should be used as ArrayUtils.clone(new int[] {2}).

This constructor is public to permit tools that require a JavaBean instance to operate.

Method Detail

nullToEmpty

public static java.lang.Object[] nullToEmpty(java.lang.Object[] array)

Defensive programming technique to change a null reference to an empty one.

This method returns an empty array for a null input array.

As a memory optimizing technique an empty array passed in will be overridden with the empty public static references in this class.

Parameters:
array - the array to check for null or empty
Returns:
the same array, public static empty array if null or empty input
Since:
2.5

nullToEmpty

public static java.lang.Class<?>[] nullToEmpty(java.lang.Class<?>[] array)

Defensive programming technique to change a null reference to an empty one.

This method returns an empty array for a null input array.

As a memory optimizing technique an empty array passed in will be overridden with the empty public static references in this class.

Parameters:
array - the array to check for null or empty
Returns:
the same array, public static empty array if null or empty input
Since:
3.2

isSameLength

public static boolean isSameLength(java.lang.Object[] array1,
                                   java.lang.Object[] array2)

Checks whether two arrays are the same length, treating null arrays as length 0.

Any multi-dimensional aspects of the arrays are ignored.

Parameters:
array1 - the first array, may be null
array2 - the second array, may be null
Returns:
true if length of arrays matches, treating null as an empty array

getLength

public static int getLength(java.lang.Object array)

Returns the length of the specified array. This method can deal with Object arrays and with primitive arrays.

If the input array is null, 0 is returned.

 ArrayUtils.getLength(null)            = 0
 ArrayUtils.getLength([])              = 0
 ArrayUtils.getLength([null])          = 1
 ArrayUtils.getLength([true, false])   = 2
 ArrayUtils.getLength([1, 2, 3])       = 3
 ArrayUtils.getLength(["a", "b", "c"]) = 3
 

Parameters:
array - the array to retrieve the length from, may be null
Returns:
The length of the array, or 0 if the array is null
Throws:
java.lang.IllegalArgumentException - if the object argument is not an array.
Since:
2.1

isEmpty

public static boolean isEmpty(java.lang.Object[] array)

Checks if an array of Objects is empty or null.

Parameters:
array - the array to test
Returns:
true if the array is empty or null
Since:
2.1