org.knowceans.util
Class ArrayUtils

java.lang.Object
  extended by org.knowceans.util.ArrayUtils

public class ArrayUtils
extends java.lang.Object

ArrayUtils provides functionality for conversion between primitive and object arrays and lists as well as a copying method for arrays of arbitrary type and dimensions. For array arguments, reflection and Object arguments are chosen as an alternative to overloading. Hopefully, this class will be obsolete in Java 1.6...

Author:
gregor heinrich

Field Summary
static java.lang.Class[][] types
           
 
Constructor Summary
ArrayUtils()
           
 
Method Summary
static java.util.List<?> asList(java.lang.Object array)
          Convert an array of primitive-type elements into a list of its wrapper-type elements.
static java.lang.Object asPrimitiveArray(java.util.List<? extends java.lang.Object> objects)
          Convert an list of objects into an array of primitive types.
static java.lang.Object asPrimitiveArray(java.util.List<? extends java.lang.Object> objects, java.lang.Class<?> type)
          Convert an list of objects into an array of primitive types.
static java.lang.Object[] convert(java.lang.Object array)
          Get an array of the wrapper type corresponding to the primitive element type of the argument.
static java.lang.Object convert(java.lang.Object[] objects)
          Get an array of the number type corresponding to the element type of the argument.
static java.lang.Object copy(java.lang.Object array)
          Create a copy of the argument array.
static void copy(java.lang.Object target, java.lang.Object array)
          Create a copy of the argument array in the pointer provided, which is assumed to be of the same dimensions than array.
static boolean isArray(java.lang.Object s)
          determines whether an object is an array and has nonzero length
static void main(java.lang.String[] args)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

types

public static final java.lang.Class[][] types
Constructor Detail

ArrayUtils

public ArrayUtils()
Method Detail

main

public static void main(java.lang.String[] args)

convert

public static java.lang.Object convert(java.lang.Object[] objects)
Get an array of the number type corresponding to the element type of the argument. The element type is determined from the runtime type of the first element, which, even if the array was initialised as Object[], can be a subclass of Object wrapping a primitive.

The contract is that the element runtime type is equal for all elements in the array and no null element is included, otherwise runtime exceptions will be thrown. If the array has size 0, the class type is determined by the component type, resulting in null return value if the array was not initialised as an Wrapper[] where wrapper is one of the primitive wrapper object types excluding Void.

Parameters:
objects - object array with elements of primitive wrapper classes excluding Void.
Returns:
an array of the relevant primitive type or null if size = 0 and or invalid type.

convert

public static java.lang.Object[] convert(java.lang.Object array)
Get an array of the wrapper type corresponding to the primitive element type of the argument.

Parameters:
array - array of a primitive type
Returns:
array of the object type corresponding to the primitive type or null if invalid element type or no array in argument.

asList

public static java.util.List<?> asList(java.lang.Object array)
Convert an array of primitive-type elements into a list of its wrapper-type elements.

Parameters:
array -
Returns:
the array or null if invalid

asPrimitiveArray

public static java.lang.Object asPrimitiveArray(java.util.List<? extends java.lang.Object> objects)
Convert an list of objects into an array of primitive types. This extends the functionality of the List.toArray() method to primitive types. If the list does not consist of wrapper-type elements or if it has zero length, null is returned (in the second case because the element type cannot be determined).

Parameters:
objects -
Returns:
array of primitive types or null if invalid.

asPrimitiveArray

public static java.lang.Object asPrimitiveArray(java.util.List<? extends java.lang.Object> objects,
                                                java.lang.Class<?> type)
Convert an list of objects into an array of primitive types. This extends the functionality of the List.toArray() method to primitive types. If the list does not consist of wrapper-type elements or if it has zero length, null is returned (in the second case because the element type cannot be determined).

Parameters:
objects -
sample - object (instantiate primitive array[0] if objects.size = 0)
Returns:
array of primitive types or null if invalid.

copy

public static java.lang.Object copy(java.lang.Object array)
Create a copy of the argument array. There are almost no restrictions on the type of array to be copied: The array can be of object or primitive element type and can have any number of dimensions (a[], a[][], a[][][] etc.). For primitive element types, this is a deep copy.

Parameters:
array -
Returns:
the copied array or null if the argument was not an array type.

copy

public static void copy(java.lang.Object target,
                        java.lang.Object array)
Create a copy of the argument array in the pointer provided, which is assumed to be of the same dimensions than array. There are almost no restrictions on the type of array to be copied: The arrays can be of object or primitive element type and can have any number of dimensions (a[], a[][], a[][][] etc.). For primitive element types, this is a deep copy.

Note: This method is rather slow. Use System.arraycopy for scalability.

Parameters:
array - whose content is copied
target - array whose content is overwritten (same size as array).

isArray

public static boolean isArray(java.lang.Object s)
determines whether an object is an array and has nonzero length

Parameters:
s -
Returns: