de.fu_berlin.ties.util
Class CollectionUtils

java.lang.Object
  extended by de.fu_berlin.ties.util.CollectionUtils

public final class CollectionUtils
extends Object

A static class that provides utility methods for working with Collections and arrays. No instances of this class can be created, only the static members should be used.

Version:
$Revision: 1.1 $, $Date: 2004/12/06 17:59:41 $, $Author: siefkes $
Author:
Christian Siefkes

Field Summary
static String SEPARATOR
          Default separator used to flatten array if no other separator is specified: " " (a single space).
 
Method Summary
static
<T> Set<T>
arrayAsSet(T[] array)
          Wraps an array into a set.
static boolean[] asBooleanArray(Object[] objArray)
          Converts an object array into a boolean array, calling Util.asBoolean(Object) on each element.
static byte[] asByteArray(Object[] objArray)
          Converts an object array into a byte array, calling Util.asByte(Object) on each element.
static char[] asCharArray(Object[] objArray)
          Converts an object array into a char array, calling Util.asChar(Object) on each element.
static double[] asDoubleArray(Object[] objArray)
          Converts an object array into a double array, calling Util.asDouble(Object) on each element.
static float[] asFloatArray(Object[] objArray)
          Converts an object array into a float array, calling Util.asFloat(Object) on each element.
static int[] asIntArray(Object[] objArray)
          Converts an object array into an integer array, calling Util.asInt(Object) on each element.
static long[] asLongArray(Object[] objArray)
          Converts an object array into a long array, calling Util.asLong(Object) on each element.
static short[] asShortArray(Object[] objArray)
          Converts an object array into a short array, calling Util.asShort(Object) on each element.
static String[] asStringArray(Object[] objArray)
          Converts an object array into a String array, calling Util.asString(Object) on each element.
static void combineArrays(Object[] array1, Object[] array2, Object[] targetArray)
          Combines two array into a target array, inserting all elements of the first array and then all elements of the second array in the target array.
static String flatten(boolean[] array)
          Flattens the elements of the provided array into a single string, separating elements by a space character.
static String flatten(byte[] array)
          Flattens the elements of the provided array into a single string, separating elements by a space character.
static String flatten(char[] array)
          Flattens the elements of the provided array into a single string, separating elements by a space character.
static String flatten(double[] array)
          Flattens the elements of the provided array into a single string, separating elements by a space character.
static String flatten(float[] array)
          Flattens the elements of the provided array into a single string, separating elements by a space character.
static String flatten(int[] array)
          Flattens the elements of the provided array into a single string, separating elements by a space character.
static String flatten(long[] array)
          Flattens the elements of the provided array into a single string, separating elements by a space character.
static String flatten(Object[] array)
          Flattens the elements of the provided array into a single string, separating elements by a space character.
static String flatten(Object[] array, String separator)
          Flattens the elements of the provided array into a single string, separating elements by the provided separator.
static String flatten(short[] array)
          Flattens the elements of the provided array into a single string, separating elements by a space character.
static
<T> ArrayList<T>
lastN(List<? extends T> list, int number)
          Copied the last n elements from a list into a new list (or all elements, if the size of the input list is smaller or equal to n).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SEPARATOR

public static final String SEPARATOR
Default separator used to flatten array if no other separator is specified: " " (a single space).

See Also:
Constant Field Values
Method Detail

arrayAsSet

public static <T> Set<T> arrayAsSet(T[] array)
Wraps an array into a set. Duplicates will be ignored.

Parameters:
array - the array to wrap
Returns:
a set containing the contents of the set (in random order, without duplicates)

asBooleanArray

public static boolean[] asBooleanArray(Object[] objArray)
                                throws IllegalArgumentException
Converts an object array into a boolean array, calling Util.asBoolean(Object) on each element.

Parameters:
objArray - the array of objects to convert
Returns:
the converted boolean array
Throws:
IllegalArgumentException - if an object's toString() output cannot be parsed as a boolean

asByteArray

public static byte[] asByteArray(Object[] objArray)
                          throws NumberFormatException
Converts an object array into a byte array, calling Util.asByte(Object) on each element.

Parameters:
objArray - the array of objects to convert
Returns:
the converted byte array
Throws:
NumberFormatException - if an object's toString() output does not contain a parsable byte

asCharArray

public static char[] asCharArray(Object[] objArray)
                          throws IndexOutOfBoundsException
Converts an object array into a char array, calling Util.asChar(Object) on each element.

Parameters:
objArray - the array of objects to convert
Returns:
the converted char array
Throws:
IndexOutOfBoundsException - if the object's toString() output is the empty string (after trimming outer whitespace)

asDoubleArray

public static double[] asDoubleArray(Object[] objArray)
                              throws NumberFormatException
Converts an object array into a double array, calling Util.asDouble(Object) on each element.

Parameters:
objArray - the array of objects to convert
Returns:
the converted double array
Throws:
NumberFormatException - if an object's toString() output does not contain a parsable double

asFloatArray

public static float[] asFloatArray(Object[] objArray)
                            throws NumberFormatException
Converts an object array into a float array, calling Util.asFloat(Object) on each element.

Parameters:
objArray - the array of objects to convert
Returns:
the converted float array
Throws:
NumberFormatException - if an object's toString() output does not contain a parsable float

asIntArray

public static int[] asIntArray(Object[] objArray)
                        throws NumberFormatException
Converts an object array into an integer array, calling Util.asInt(Object) on each element.

Parameters:
objArray - the array of objects to convert
Returns:
the converted int array
Throws:
NumberFormatException - if an object's toString() output does not contain a parsable int

asLongArray

public static long[] asLongArray(Object[] objArray)
                          throws NumberFormatException
Converts an object array into a long array, calling Util.asLong(Object) on each element.

Parameters:
objArray - the array of objects to convert
Returns:
the converted long array
Throws:
NumberFormatException - if an object's toString() output does not contain a parsable long

asShortArray

public static short[] asShortArray(Object[] objArray)
                            throws NumberFormatException
Converts an object array into a short array, calling Util.asShort(Object) on each element.

Parameters:
objArray - the array of objects to convert
Returns:
the converted short array
Throws:
NumberFormatException - if an object's toString() output does not contain a parsable short

asStringArray

public static String[] asStringArray(Object[] objArray)
Converts an object array into a String array, calling Util.asString(Object) on each element.

Parameters:
objArray - the array of objects to convert
Returns:
the converted String array

combineArrays

public static void combineArrays(Object[] array1,
                                 Object[] array2,
                                 Object[] targetArray)
Combines two array into a target array, inserting all elements of the first array and then all elements of the second array in the target array.

Parameters:
array1 - the first array to copy
array2 - the second array to copy
targetArray - the array to copy the two other array into; the type of this array must be suitable to accept elements from both array; the length of this array must be equal or greater than array1.length + array2.length

flatten

public static String flatten(boolean[] array)
Flattens the elements of the provided array into a single string, separating elements by a space character.

Parameters:
array - the array of values to join
Returns:
the flattened string

flatten

public static String flatten(byte[] array)
Flattens the elements of the provided array into a single string, separating elements by a space character.

Parameters:
array - the array of values to join
Returns:
the flattened string

flatten

public static String flatten(char[] array)
Flattens the elements of the provided array into a single string, separating elements by a space character.

Parameters:
array - the array of values to join
Returns:
the flattened string

flatten

public static String flatten(double[] array)
Flattens the elements of the provided array into a single string, separating elements by a space character.

Parameters:
array - the array of values to join
Returns:
the flattened string

flatten

public static String flatten(float[] array)
Flattens the elements of the provided array into a single string, separating elements by a space character.

Parameters:
array - the array of values to join
Returns:
the flattened string

flatten

public static String flatten(int[] array)
Flattens the elements of the provided array into a single string, separating elements by a space character.

Parameters:
array - the array of values to join
Returns:
the flattened string

flatten

public static String flatten(long[] array)
Flattens the elements of the provided array into a single string, separating elements by a space character.

Parameters:
array - the array of values to join
Returns:
the flattened string

flatten

public static String flatten(Object[] array)
Flattens the elements of the provided array into a single string, separating elements by a space character.

Parameters:
array - the array of values to join
Returns:
the flattened string

flatten

public static String flatten(Object[] array,
                             String separator)
Flattens the elements of the provided array into a single string, separating elements by the provided separator.

Parameters:
array - the array of values to join
separator - the separator string to use
Returns:
the flattened string

flatten

public static String flatten(short[] array)
Flattens the elements of the provided array into a single string, separating elements by a space character.

Parameters:
array - the array of values to join
Returns:
the flattened string

lastN

public static <T> ArrayList<T> lastN(List<? extends T> list,
                                     int number)
Copied the last n elements from a list into a new list (or all elements, if the size of the input list is smaller or equal to n). Modifications of the returned list will not affect the original list and vice versa.

If number is 0 or negative or if the original list is null or empty, an empty list is returned.

Note that this is somewhat inefficient if the input list is a LinkedList because repeated calls to List.get(int) are necessary (unless the whole list is copied).

Parameters:
list - the input list
number - the number of elements to copy
Returns:
an ArrayList containing the last elements from the original list; or an empty ArrayList iff the list is null


Copyright © 2003-2004 Christian Siefkes. All Rights Reserved.