com.taco.data
Class ArrayUtilities

java.lang.Object
  extended by com.taco.data.ArrayUtilities

public class ArrayUtilities
extends java.lang.Object

Utility functions for manipulating arrays.


Constructor Summary
protected ArrayUtilities()
          Since this class is a static utility class, its sole constructor is protected.
 
Method Summary
static java.util.Collection addElements(java.util.Collection c, java.lang.Object[] elements)
          Add the elements in the argument array to the argument collection.
static java.lang.Object[] arrayCopyWithoutNull(java.lang.Object[] src, int srcPos, java.lang.Object[] dest, int destPos, int length)
          Copy up to length elements from the source array (starting at index srcPos) to the destination array (starting at index destPos).
static java.util.List asList(int[] array)
          Return a list of instances of Integer backed by the argument array.
static java.util.Collection fromMatrix(java.lang.Object[][] matrix, ICollectionFactory factory, boolean mustCopy)
          Return a collection of collections representing the matrix.
static java.util.LinkedList prependElements(java.lang.Object[] elements, java.util.LinkedList list)
          Add the elements in the array to the front of the argument list, in the order that they appear in the array (The first element of the array becomes the first element of the list).
static java.lang.Object[] remove(java.lang.Object[] src, java.lang.Object obj)
          Remove all elements that match obj (via ==, not equals()) and return a possibly differently sized array that just has the non-matching elements.
static java.util.Collection removeElements(java.util.Collection c, java.lang.Object[] elements)
          Remove the elements in the argument array from the argument collection.
static int[] resize(int[] array, int size)
          Resize the int array to have the argument size, truncating elements if necessary.
static int[] resize(int[] array, int size, IIntMapper mapper)
          Resize the int array to have the argument size, truncating elements if necessary.
static java.lang.Object[][] toMatrix(java.util.Collection rows, java.lang.Object[][] a)
          Given a collection of sub-collections that represent row data, convert the collection into a 2D array with the same type as the argument matrix.
static java.lang.String toString(int[] array)
          Return the comma-separated string representing the elements of array, surrounded by brackets.
static java.lang.Object[] unpack(java.util.Collection c, java.lang.Object[] dest, int startIndex)
          Copy the elements in the collection to the argument array, starting from startIndex.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayUtilities

protected ArrayUtilities()
Since this class is a static utility class, its sole constructor is protected.

Method Detail

prependElements

public static final java.util.LinkedList prependElements(java.lang.Object[] elements,
                                                         java.util.LinkedList list)
Add the elements in the array to the front of the argument list, in the order that they appear in the array (The first element of the array becomes the first element of the list).


addElements

public static final java.util.Collection addElements(java.util.Collection c,
                                                     java.lang.Object[] elements)
Add the elements in the argument array to the argument collection. Return the argument collection, which is modified.

Parameters:
c - The collection to add to and return.
elements - An array of elements to add.
Returns:
The modified collection.
Throws:
java.lang.NullPointerException - if either c or elements is null

removeElements

public static final java.util.Collection removeElements(java.util.Collection c,
                                                        java.lang.Object[] elements)
Remove the elements in the argument array from the argument collection. Return the argument collection, which is modified.

Parameters:
c - The collection to remove from and return.
elements - An array of elements to remove.
Returns:
The modified collection.
Throws:
java.lang.NullPointerException - if either c or elements is null

remove

public static final java.lang.Object[] remove(java.lang.Object[] src,
                                              java.lang.Object obj)
Remove all elements that match obj (via ==, not equals()) and return a possibly differently sized array that just has the non-matching elements. This method may modify the argument array, but if there are no matching elements, the array will be returned unchanged.


arrayCopyWithoutNull

public static final java.lang.Object[] arrayCopyWithoutNull(java.lang.Object[] src,
                                                            int srcPos,
                                                            java.lang.Object[] dest,
                                                            int destPos,
                                                            int length)
Copy up to length elements from the source array (starting at index srcPos) to the destination array (starting at index destPos). If an element in the source array is null, do not copy it; do not copy an additional source element to compensate. Return dest.


toMatrix

public static final java.lang.Object[][] toMatrix(java.util.Collection rows,
                                                  java.lang.Object[][] a)
Given a collection of sub-collections that represent row data, convert the collection into a 2D array with the same type as the argument matrix. If the argument matrix is null or too small, allocate a new matrix with the same type that is big enough to hold the data. If the argument matrix is null, return a matrix whose element type is Object. If the argument matrix is bigger than needed, set all unused locations to null.


unpack

public static final java.lang.Object[] unpack(java.util.Collection c,
                                              java.lang.Object[] dest,
                                              int startIndex)
Copy the elements in the collection to the argument array, starting from startIndex. The argument array must be big enough or else an ArrayIndexOutOfBoundsException will be thrown. Return the argument array.


fromMatrix

public static final java.util.Collection fromMatrix(java.lang.Object[][] matrix,
                                                    ICollectionFactory factory,
                                                    boolean mustCopy)
Return a collection of collections representing the matrix. Each member of the returned collection will be a collection representing a single row of the matrix. If the rows of matrix have a different number of elements, so will the return collection.

Parameters:
matrix - The matrix to convert.
factory - The factory used to create the overall collection and also the collections representing each row.
mustCopy - If false, each row vector will be a wrapper (see Arrays.asList()) over each row in the argument matrix. This means if the returned collection is modified, it will be reflected in the argument matrix. Also, this means that each row vector will not be created by the factory. If true, each element will be copied into a collection created by the factory.
Returns:
A collection of row vectors, which are themselves collections.
Throws:
java.lang.NullPointerException - if matrix or factory is null.

resize

public static final int[] resize(int[] array,
                                 int size)
Resize the int array to have the argument size, truncating elements if necessary. If size is greater than the size of array, leave the additional elements set to zero. If array has the same size as size, return array unchanged.


resize

public static final int[] resize(int[] array,
                                 int size,
                                 IIntMapper mapper)
Resize the int array to have the argument size, truncating elements if necessary. If size is greater than the size of array, set each element at the end of the array to be the result of mapper.map(i), where i is the index of element in the returned array. If mapper is null, leave the elements set to zero. If array has the same size as size, return array unchanged.


asList

public static final java.util.List asList(int[] array)
Return a list of instances of Integer backed by the argument array. Changes to the list are reflected in the argument array. The returned list does not support the add() or remove() methods. It implements Cloneable, RandomAccess, and Serializable. This method does not allocate instances instances if Integer right away -- they are allocated on demand.


toString

public static final java.lang.String toString(int[] array)
Return the comma-separated string representing the elements of array, surrounded by brackets. This is more efficient than using StringListUtilities.createCollectionString(), but not as flexible.

Parameters:
array - An array of integers
Returns:
A bracketed string of elements, separated by commas.
Throws:
java.lang.NullPointerException - if array is null