org.norther.tammi.acorn.util.converter
Class Arrays

java.lang.Object
  extended by org.norther.tammi.acorn.util.converter.Arrays

public abstract class Arrays
extends Object

Converts between array types.

Author:
Ilkka Priha

Constructor Summary
Arrays()
           
 
Method Summary
static boolean equals(Object a1, Object a2)
          Checks whether two objects, whether arrays or single ones, are equal.
static Object newArray(Class class1, Class class2, int length)
          Returns a new array of the specified length and of assignment compatible type with the specified classes.
static Object newArray(Object object1, Object object2)
          Returns a new array long enough to hold the specified objects and of assigment compatible type with the objects.
static String[] toArray(Locale locale)
          Tokenizes a locale and returns the tokens in an array.
static String[] toArray(Locale locale, String variant)
          Tokenizes a locale and variant and returns the tokens in an array.
static Object toArray(Object array, int index, int length)
          Returns a range from the specified array.
static Object toArray(Object object1, int index, Object object2)
          Returns the specified objects in a new array of assignment compatible type with the objects.
static Object toArray(Object object1, int index, Object object2, Object array)
          Returns the specified objects in the given array.
static Object toArray(Object object1, Object object2)
          Returns the specified objects in a new array of assignment compatible type with the objects.
static Object toArray(Object object1, Object object2, Object array)
          Returns the specified objects in the given array.
static String[] toArray(String lines)
          Converts the CR, LF and CRLF separated lines of a string to an array.
static Object toCloned(Object array)
          Clones an array and its elements.
static Object toCopied(Object array)
          Copies an array.
static int[] toDimensions(Object array)
          Converts the specified array to its dimensions.
static String toLines(Object[] array)
          Converts an array to CRLF separated lines.
static List toList(Object array)
          Converts the specified array to a list.
static Object[] toObject(Object array)
          Converts primitives to the corresponding objects.
static Object toPrimitive(Object array)
          Converts objects to the corresponding primitives.
static Object toPrimitive(Object array, Class type)
          Converts objects to the corresponding primitives.
static Object toString(Object array)
          Copies an array to strings.
static void trim(String[] array)
          Trims elements of a string array and converts empty string to nulls.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Arrays

public Arrays()
Method Detail

newArray

public static Object newArray(Class class1,
                              Class class2,
                              int length)
Returns a new array of the specified length and of assignment compatible type with the specified classes. For array classes the corresponding component types are applied. If the classes are not assignable, an array of Objects is returned.

Parameters:
class1 - the first class.
class2 - the second object.
length - the array length.
Returns:
an array of objects.

newArray

public static Object newArray(Object object1,
                              Object object2)
Returns a new array long enough to hold the specified objects and of assigment compatible type with the objects. For array objects the corresponding component types are applied.

Parameters:
object1 - the first object.
object2 - the second object.
Returns:
an array of objects.

toList

public static List toList(Object array)
Converts the specified array to a list.

Parameters:
array - the array to convert.
Returns:
the corresponding list.
Throws:
IllegalArgumentException - if array is not an array.

toDimensions

public static int[] toDimensions(Object array)
Converts the specified array to its dimensions.

Parameters:
array - the array to convert.
Returns:
the corresponding dimensions.

toPrimitive

public static Object toPrimitive(Object array)
Converts objects to the corresponding primitives.

Note that nulls are converted to zeros or falses.

Parameters:
array - the objects to convert.
Returns:
the corresponding primitives.
Throws:
ClassCastException - if conversion is not possible.
UnsupportedOperationException - if conversion not supported.

toPrimitive

public static Object toPrimitive(Object array,
                                 Class type)
Converts objects to the corresponding primitives.

Note that nulls are converted to zeros or falses.

Parameters:
array - the objects to convert.
type - the primitive type.
Returns:
the corresponding primitives.
Throws:
ClassCastException - if conversion is not possible.
IllegalArgumentException - if array is not an array.
UnsupportedOperationException - if conversion not supported.

toObject

public static Object[] toObject(Object array)
Converts primitives to the corresponding objects.

Parameters:
array - the primitives to convert.
Returns:
the corresponding objects.
Throws:
IllegalArgumentException - if array is not an array.
UnsupportedOperationException - if conversion is not supported.

toCopied

public static Object toCopied(Object array)
Copies an array. Non-array arguments are returned as such.

Primitive and multi-dimensional arrays are also supported.

Parameters:
array - the array to copy.
Returns:
the copied array, if any.

toCloned

public static Object toCloned(Object array)
Clones an array and its elements. Non-array arguments are returned as cloned.

Primitive and multi-dimensional arrays are also supported.

Parameters:
array - the array to clone.
Returns:
the cloned array, if any.

toString

public static Object toString(Object array)
Copies an array to strings. Non-array arguments are returned as strings.

Primitive and multi-dimensional arrays are also supported.

Parameters:
array - the array to copy.
Returns:
the string array, if any.

toArray

public static Object toArray(Object object1,
                             Object object2)
Returns the specified objects in a new array of assignment compatible type with the objects. If either or both of the objects are arrays themselves, the result array merges the original ones.

Parameters:
object1 - the first object.
object2 - the second object.
Returns:
an array of objects.

toArray

public static Object toArray(Object object1,
                             int index,
                             Object object2)
Returns the specified objects in a new array of assignment compatible type with the objects. If either or both of the objects are arrays themselves, the result array merges the original ones. The index specifies the location of the second object within the first one, -1 appends it at the end.

Parameters:
object1 - the first object.
index - the index.
object2 - the second object.
Returns:
an array of objects.

toArray

public static Object toArray(Object object1,
                             Object object2,
                             Object array)
Returns the specified objects in the given array. If either or both of the objects are arrays themselves, the result array merges the original ones.

The runtime type of the returned array is that of the specified array. If the objects fit in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of the combined size of the objects.

If the objects fit into the specified array with room to spare, the element in the array immediately following the end of the objects is set to null.

Parameters:
object1 - the first object.
object2 - the second object.
array - the result array.
Returns:
an array of objects.
Throws:
IllegalArgumentException - if the array is invalid.

toArray

public static Object toArray(Object object1,
                             int index,
                             Object object2,
                             Object array)
Returns the specified objects in the given array. If either or both of the objects are arrays themselves, the result array merges the original ones. The index specifies the location of the second object within the the first one, -1 appends it at the end.

The runtime type of the returned array is that of the specified array. If the objects fit in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of the combined size of the objects.

If the objects fit into the specified array with room to spare, the element in the array immediately following the end of the objects is set to null.

Parameters:
object1 - the first object.
index - the index.
object2 - the second object.
array - the result array.
Returns:
an array of objects.
Throws:
IllegalArgumentException - if the array is invalid.
ArrayIndexOutOfBoundsException - if the index is invalid.

toArray

public static Object toArray(Object array,
                             int index,
                             int length)
Returns a range from the specified array. A negative length removes the range.

Parameters:
array - the array.
index - the index.
length - the length.
Returns:
an array range.

toArray

public static String[] toArray(String lines)
Converts the CR, LF and CRLF separated lines of a string to an array.

Parameters:
lines - the lines to convert.
Returns:
the string array.

toLines

public static String toLines(Object[] array)
Converts an array to CRLF separated lines.

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

toArray

public static String[] toArray(Locale locale)
Tokenizes a locale and returns the tokens in an array.

Parameters:
locale - the locale to tokenize.
Returns:
an array of tokens or an empty array.

toArray

public static String[] toArray(Locale locale,
                               String variant)
Tokenizes a locale and variant and returns the tokens in an array. If the variant is not null, it replaces any variants in the locale.

Parameters:
locale - the locale to tokenize.
variant - a optional variant.
Returns:
an array of tokens or an empty array.

trim

public static void trim(String[] array)
Trims elements of a string array and converts empty string to nulls.

Parameters:
array - the array to trim.

equals

public static boolean equals(Object a1,
                             Object a2)
Checks whether two objects, whether arrays or single ones, are equal. Nulls are accepted as parameters. Numbers are compared as doubles.

Parameters:
a1 - the first object.
a2 - the second object.
Returns:
true if the two objects are equal.


Copyright © 2004 The Norther Organization. All rights reserved.