net.sf.btw.commons
Class ArrayUtils

java.lang.Object
  extended by net.sf.btw.commons.ArrayUtils

public final class ArrayUtils
extends Object

Array utilities.

Author:
Martin Vysny

Nested Class Summary
static class ArrayUtils.ArrayEnumeration
          Converts given object into an enumeration.
 
Method Summary
static void addAll(Vector vector, Object copyFrom)
          Appends all items from given array after last item of the vector.
static void addAll(Vector vector, Object[] copyFrom)
          Appends all items from given array after last item of the vector.
static void addAll(Vector vector, Vector copyFrom)
          Appends all items from given array after last item of the vector.
static Enumeration arrayToEnumeration(Object array)
          Creates an enumeration from given object.
static String arrayToString(Object array)
          Converts given array to a string representation.
static void checkIsArray(Object array, boolean allowEnumeration)
          Checks if given object is an array, as per isArray(Object).
static Object clone(Object array, boolean deep)
          Clones given array.
static boolean equals(Enumeration enum1, Enumeration enum2)
          Compares two enumerations if they contain equal objects in correct order.
static boolean equals(Enumeration enum1, Object[] enum2)
          Compares two enumerations if they contain equal objects in correct order.
static boolean equals(Object[] enum1, Object[] enum2)
          Compares two arrays if they contain equal objects in correct order.
static boolean equals(Object enum1, Object enum2)
          Compares two arrays if they contain equal objects in correct order.
static boolean equalsObjects(Object o1, Object o2)
          The ultimate comparator.
static Object getElementAt(Object array, int index)
          Retrieves item from given array.
static int getLength(Object array)
           Returns length of given array.
static int hashCode(Object object)
          Computes correct hashcode for given object.
static int indexOf(Vector vector, Object obj)
          Finds first occurence of given object in given vector.
static boolean isArray(Object array)
          Returns true if given object is an array or a Queue, a Vector or an ArrayUtils.ArrayEnumeration.
static void setElementAt(Object array, int index, Object object)
          Sets item into given array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

indexOf

public static int indexOf(Vector vector,
                          Object obj)
Finds first occurence of given object in given vector. To compare objects Object.equals(Object) is used.

Parameters:
vector - vector
obj - object to find, may be null.
Returns:
index of given object or -1 if the enumeration does not contain the object.

addAll

public static void addAll(Vector vector,
                          Object[] copyFrom)
Appends all items from given array after last item of the vector.

Parameters:
vector - here all items will be appended.
copyFrom - array to append.

addAll

public static void addAll(Vector vector,
                          Object copyFrom)
Appends all items from given array after last item of the vector.

Parameters:
vector - here all items will be appended.
copyFrom - array to append. See arrayToEnumeration(Object) for details. Does not support ArrayUtils.ArrayEnumeration.

addAll

public static void addAll(Vector vector,
                          Vector copyFrom)
Appends all items from given array after last item of the vector.

Parameters:
vector - here all items will be appended.
copyFrom - vector to append.

equals

public static boolean equals(Enumeration enum1,
                             Enumeration enum2)
Compares two enumerations if they contain equal objects in correct order.

Parameters:
enum1 - first enumeration
enum2 - second enumeration
Returns:
true if two enumerations match, false otherwise.

equalsObjects

public static boolean equalsObjects(Object o1,
                                    Object o2)
The ultimate comparator. Handles nulls and arrays correctly.

Parameters:
o1 - first object.
o2 - second object.
Returns:
true if objects equals, false otherwise.

equals

public static boolean equals(Enumeration enum1,
                             Object[] enum2)
Compares two enumerations if they contain equal objects in correct order.

Parameters:
enum1 - first enumeration
enum2 - second enumeration
Returns:
true if two enumerations match, false otherwise.

equals

public static boolean equals(Object[] enum1,
                             Object[] enum2)
Compares two arrays if they contain equal objects in correct order.

Parameters:
enum1 - first array
enum2 - second array
Returns:
true if two enumerations match, false otherwise.

equals

public static boolean equals(Object enum1,
                             Object enum2)
Compares two arrays if they contain equal objects in correct order. For details please see arrayToEnumeration(Object).

Parameters:
enum1 - first array
enum2 - second array
Returns:
true if two enumerations match, false otherwise.
Throws:
IllegalArgumentException - if the object is not an array.

hashCode

public static int hashCode(Object object)
Computes correct hashcode for given object.

Parameters:
object - the object to compute hash from. It will compute correct hashcode from arrays and enumerations aswell.
Returns:
the hashcode, 0 if null was given.

arrayToEnumeration

public static Enumeration arrayToEnumeration(Object array)
Creates an enumeration from given object. If the object is an array of primitive types then the enumeration will enumerate appropriate object equivalents. If the object is a Queue or a Vector then their enumerations are returned instead.

Parameters:
array - the object to convert to an enumeration.
Returns:
enumeration instance.
Throws:
IllegalArgumentException - if the object is none of the above.

isArray

public static boolean isArray(Object array)
Returns true if given object is an array or a Queue, a Vector or an ArrayUtils.ArrayEnumeration.

Parameters:
array - the array instance.
Returns:
true if given object is an array, false if it is a regular object or null.

checkIsArray

public static void checkIsArray(Object array,
                                boolean allowEnumeration)
Checks if given object is an array, as per isArray(Object).

Parameters:
array - the array to check
allowEnumeration - if true then enumeration is allowed as a valid array type.
Throws:
IllegalArgumentException - if given object is not array.

getLength

public static int getLength(Object array)

Returns length of given array.

Note: there is no way to get number of items for Enumeration but to walk through it. This effectively 'destroys' given enumeration instance as it is no longer usable.

Parameters:
array - the array to check. See arrayToEnumeration(Object) for details.
Returns:
length of an array.

getElementAt

public static Object getElementAt(Object array,
                                  int index)
Retrieves item from given array.

Parameters:
array - the array instance. For details please see arrayToEnumeration(Object).
index - the index of array.
Returns:
object at given index.

setElementAt

public static void setElementAt(Object array,
                                int index,
                                Object object)
Sets item into given array.

Parameters:
array - the array instance. For details please see arrayToEnumeration(Object). Note that Queue does not support setting of arbitrary item.
index - the index of array.
object - object to set.

clone

public static Object clone(Object array,
                           boolean deep)
Clones given array. For details please see arrayToEnumeration(Object).

Parameters:
array - the array to clone. ArrayUtils.ArrayEnumerations are not supported.
deep - if true then sub-arrays are cloned aswell. Note that if you introduce a nesting cycle the function will recurse until stack overflow is reached.
Returns:
array of same class but cloned. Returns null if null array was given.

arrayToString

public static String arrayToString(Object array)
Converts given array to a string representation.

Parameters:
array - the array to convert.
Returns:
string representation of the array.


Copyright © 2007 Moto a Judas. All Rights Reserved.