com.mockrunner.util.common
Class ArrayUtil

java.lang.Object
  extended by com.mockrunner.util.common.ArrayUtil

public class ArrayUtil
extends java.lang.Object

Util class for arrays


Constructor Summary
ArrayUtil()
           
 
Method Summary
static void addBytesToList(byte[] data, int offset, int len, java.util.List list, int index)
          Copies the bytes from the specified array to the specified List as Byte objects starting at the specified index.
static void addBytesToList(byte[] data, java.util.List list, int index)
          Copies the bytes from the specified array to the specified List as Byte objects starting at the specified index.
static boolean areArraysEqual(java.lang.Object array1, java.lang.Object array2)
          Compares the two specified arrays.
static int computeHashCode(java.lang.Object array)
          Returns a suitable hash code for the specified array.
static java.lang.Object convertToArray(java.lang.Object object)
          Creates an array with a single object as component.
static java.lang.Object[] convertToObjectArray(java.lang.Object sourceArray)
          Returns an object array by wrapping primitive types.
static java.lang.Object convertToPrimitiveArray(java.lang.Object[] sourceArray)
          Returns a primitive array by unwrapping the corresponding types.
static java.lang.Object copyArray(java.lang.Object array)
          Returns a copy of the specified array.
static void ensureUnique(java.lang.String[] values)
          Ensures that each entry in the specified string array is unique by adding a number to duplicate entries.
static byte[] getByteArrayFromList(java.util.List data)
          Returns a byte array containing the bytes from the List.
static byte[] getByteArrayFromList(java.util.List data, int index)
          Returns a byte array containing the bytes from the List.
static byte[] getByteArrayFromList(java.util.List data, int index, int len)
          Returns a byte array containing the bytes from the List.
static java.util.List getListFromByteArray(byte[] data)
          Returns a List containing the bytes from the specified array as Byte objects.
static int indexOf(byte[] source, byte[] bytes)
          Returns the index of the first occurence of the array bytes in the array source.
static int indexOf(byte[] source, byte[] bytes, int index)
          Returns the index of the first occurence of the array bytes in the array source.
static java.lang.Object truncateArray(java.lang.Object sourceArray, int len)
          Returns a truncated copy of sourceArray.
static java.lang.Object truncateArray(java.lang.Object sourceArray, int index, int len)
          Returns a truncated copy of sourceArray.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayUtil

public ArrayUtil()
Method Detail

getListFromByteArray

public static java.util.List getListFromByteArray(byte[] data)
Returns a List containing the bytes from the specified array as Byte objects.

Parameters:
data - the byte data
Returns:
the List with the Byte objects

getByteArrayFromList

public static byte[] getByteArrayFromList(java.util.List data)
Returns a byte array containing the bytes from the List. The List must contain Byte objects. null entries in the List are allowed, the resulting byte will be 0.

Parameters:
data - the List
Returns:
the resulting byte array

getByteArrayFromList

public static byte[] getByteArrayFromList(java.util.List data,
                                          int index)
Returns a byte array containing the bytes from the List. The List must contain Byte objects. null entries in the List are allowed, the resulting byte will be 0.

Parameters:
data - the List
index - the index at which to start
Returns:
the resulting byte array

getByteArrayFromList

public static byte[] getByteArrayFromList(java.util.List data,
                                          int index,
                                          int len)
Returns a byte array containing the bytes from the List. The List must contain Byte objects. null entries in the List are allowed, the resulting byte will be 0.

Parameters:
data - the List
index - the index at which to start
len - the number of bytes
Returns:
the resulting byte array

addBytesToList

public static void addBytesToList(byte[] data,
                                  java.util.List list,
                                  int index)
Copies the bytes from the specified array to the specified List as Byte objects starting at the specified index. Grows the list if necessary. index must be a valid index in the list.

Parameters:
data - the byte data
list - the List
index - the index at which to start copying

addBytesToList

public static void addBytesToList(byte[] data,
                                  int offset,
                                  int len,
                                  java.util.List list,
                                  int index)
Copies the bytes from the specified array to the specified List as Byte objects starting at the specified index. Grows the list if necessary. index must be a valid index in the list.

Parameters:
data - the byte data
offset - the offset into the byte array at which to start
len - the number of bytes to copy
list - the List
index - the index at which to start copying

truncateArray

public static java.lang.Object truncateArray(java.lang.Object sourceArray,
                                             int len)
Returns a truncated copy of sourceArray. len entries are copied.

Parameters:
sourceArray - the source array
len - the truncate length
Returns:
the truncated array
Throws:
java.lang.IllegalArgumentException - if the specified object is not an array (either of reference or primitive component type)

truncateArray

public static java.lang.Object truncateArray(java.lang.Object sourceArray,
                                             int index,
                                             int len)
Returns a truncated copy of sourceArray. len entries are copied starting at the specified index.

Parameters:
sourceArray - the source array
index - the start index
len - the truncate length
Returns:
the truncated array
Throws:
java.lang.IllegalArgumentException - if the specified object is not an array (either of reference or primitive component type)

copyArray

public static java.lang.Object copyArray(java.lang.Object array)
Returns a copy of the specified array. If array is not an array, the object itself will be returned. Otherwise a copy of the array will be returned. The components themselves are not cloned.

Parameters:
array - the array
Returns:
the copy of the array

convertToObjectArray

public static java.lang.Object[] convertToObjectArray(java.lang.Object sourceArray)
Returns an object array by wrapping primitive types. If the specified array is of primitive component type, an Object[] with the corresponding wrapper component type is returned. If the specified array is already an object array, the instance is returned unchanged.

Parameters:
sourceArray - the array
Returns:
the corresponding object array
Throws:
java.lang.IllegalArgumentException - if the specified object is not an array (either of reference or primitive component type)

convertToPrimitiveArray

public static java.lang.Object convertToPrimitiveArray(java.lang.Object[] sourceArray)
Returns a primitive array by unwrapping the corresponding types. If the specified array is not an array of primitive wrapper types (e.g. Integer[]), an IllegalArgumentException will be thrown. If an array element is null, an IllegalArgumentException will be thrown.

Parameters:
sourceArray - the array
Returns:
the corresponding primitive array
Throws:
java.lang.IllegalArgumentException - if the specified array is not an array of primitive wrapper types or if an array element is null

convertToArray

public static java.lang.Object convertToArray(java.lang.Object object)
Creates an array with a single object as component. If the specified object is an array, it will be returned unchanged. Otherwise an array with the specified object as the single element will be returned.

Parameters:
object - the object
Returns:
the corresponding array

areArraysEqual

public static boolean areArraysEqual(java.lang.Object array1,
                                     java.lang.Object array2)
Compares the two specified arrays. If both passed objects are null, true is returned. If both passed objects are not arrays, they are compared using equals. Otherwise all array elements are compared using equals. This method does not handle multidimensional arrays, i.e. if an array contains another array, comparison is based on identity.

Parameters:
array1 - the first array
array2 - the second array
Returns:
true if the arrays are equal, false otherwise

computeHashCode

public static int computeHashCode(java.lang.Object array)
Returns a suitable hash code for the specified array. If the passed object is null, 0 is returned. It is allowed to pass an object that is not an array, in this case, the hash code of the object will be returned. Otherwise the hash code will be based on the array elements. null elements are allowed. This method does not handle multidimensional arrays, i.e. if an array contains another array, the hash code is based on identity.

Parameters:
array - the array
Returns:
a suitable hash code

indexOf

public static int indexOf(byte[] source,
                          byte[] bytes)
Returns the index of the first occurence of the array bytes in the array source.

Parameters:
source - the array in which to search
bytes - the array to search
Returns:
the index of the first occurence or -1, if source does not contain bytes

indexOf

public static int indexOf(byte[] source,
                          byte[] bytes,
                          int index)
Returns the index of the first occurence of the array bytes in the array source.

Parameters:
source - the array in which to search
bytes - the array to search
index - the index where to begin the search
Returns:
the index of the first occurence or -1, if source does not contain bytes

ensureUnique

public static void ensureUnique(java.lang.String[] values)
Ensures that each entry in the specified string array is unique by adding a number to duplicate entries. I.e. if the string "entry" occurs three times, the three entries will be renamed to "entry1", "entry2" and "entry3".

Parameters:
values - the array of strings