org.fest.util
Class Arrays

java.lang.Object
  extended by org.fest.util.Arrays

public class Arrays
extends Object

Utility methods related to arrays.

Author:
Alex Ruiz, Joel Costigliola

Method Summary
static
<T> T[]
array(T... values)
          Returns an array containing the given arguments.
static
<T> T[]
copyOf(T[] original, int newLength)
          Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.
static String format(Object array)
          Returns the String representation of the given array, or null if the given object is either null or not an array.
static
<T> boolean
hasOnlyNullElements(T[] array)
          Returns true if the given array has only null elements, false otherwise.
static boolean isArray(Object o)
          Returns true if the given object is not null and is an array.
static
<T> boolean
isEmpty(T[] array)
          Returns true if the given array is null or empty.
static
<T> T[]
nonNullElements(T[] array)
          Returns a new array containing the non-null elements of the given array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isArray

public static boolean isArray(Object o)
Returns true if the given object is not null and is an array.

Parameters:
o - the given object.
Returns:
true if the given object is not null and is an array, otherwise false.

isEmpty

public static <T> boolean isEmpty(T[] array)
Returns true if the given array is null or empty.

Type Parameters:
T - the type of elements of the array.
Parameters:
array - the array to check.
Returns:
true if the given array is null or empty, otherwise false.

array

public static <T> T[] array(T... values)
Returns an array containing the given arguments.

Type Parameters:
T - the type of the array to return.
Parameters:
values - the values to store in the array.
Returns:
an array containing the given arguments.

format

public static String format(Object array)
Returns the String representation of the given array, or null if the given object is either null or not an array. This method supports arrays having other arrays as elements.

Parameters:
array - the object that is expected to be an array.
Returns:
the String representation of the given array.

nonNullElements

public static <T> T[] nonNullElements(T[] array)
Returns a new array containing the non-null elements of the given array. This method returns an empty array if the given array has only null elements or if it is empty. This method returns null if the given array is null.

Type Parameters:
T - the type of elements of the array.
Parameters:
array - the array we want to extract the non-null elements from.
Returns:
a new array containing the non-null elements of the given array, or null if the given array is null.
Since:
1.1.3

hasOnlyNullElements

public static <T> boolean hasOnlyNullElements(T[] array)
Returns true if the given array has only null elements, false otherwise. If given array is empty, this method returns true.

Type Parameters:
T - the type of elements of the array.
Parameters:
array - the given array. It must not be null.
Returns:
true if the given array has only null elements or is empty, false otherwise.
Throws:
NullPointerException - if the given array is null.
Since:
1.1.3

copyOf

public static <T> T[] copyOf(T[] original,
                             int newLength)
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array.

Type Parameters:
T - the component type of the array.
Parameters:
original - the array to be copied.
newLength - the length of the copy to be returned.
Returns:
a copy of the original array, truncated or padded with nulls to obtain the specified length.
Throws:
NegativeArraySizeException - if newLength is negative.
NullPointerException - if original is null.
Since:
1.1.5


Copyright © 2007-2012 FEST (Fixtures for Easy Software Testing). All Rights Reserved.