de.fu_berlin.ties.util
Class CollUtils

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

public final class CollUtils
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.10 $, $Date: 2006/10/21 16:04:27 $, $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,E extends T>
void
addAll(Collection<T> coll, E[] array)
          Convenience method that adds all members of an array to a collection.
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 boolean[] asBooleanArray(String input)
          Converts a string into a boolean array, calling Util.asBoolean(char) on each character.
static char[] asCharArray(Object[] objArray)
          Converts an object array into a char array, calling Util.asChar(Object) on each element.
static char[] asCharArray(String input)
          Converts a string array into a char array.
static double[] asDoubleArray(Object[] objArray)
          Converts an object array into a double array, calling Util.asDouble(Object) on each element.
static double[] asDoubleArray(String input)
          Converts a string into a double array, by splitting on whitespace and calling asDoubleArray(Object[]) on the result.
static float[] asFloatArray(Object[] objArray)
          Converts an object array into a float array, calling Util.asFloat(Object) on each element.
static float[] asFloatArray(String input)
          Converts a string into a float array, by splitting on whitespace and calling asFloatArray(Object[]) on the result.
static int[] asIntArray(Object[] objArray)
          Converts an object array into an integer array, calling Util.asInt(Object) on each element.
static int[] asIntArray(String input)
          Converts a string into an integer array, by splitting on whitespace and calling asIntArray(Object[]) on the result.
static long[] asLongArray(Object[] objArray)
          Converts an object array into a long array, calling Util.asLong(Object) on each element.
static long[] asLongArray(String input)
          Converts a string into a long array, by splitting on whitespace and calling asLongArray(Object[]) on the result.
static short[] asShortArray(Object[] objArray)
          Converts an object array into a short array, calling Util.asShort(Object) on each element.
static short[] asShortArray(String input)
          Converts a string into a short array, by splitting on whitespace and calling asShortArray(Object[]) on the result.
static String[] asStringArray(Object[] objArray)
          Converts an object array into a String array, calling Util.asString(Object) on each element.
static String[] asStringArray(String input)
          Converts a string into an array of whitespace-separated tokens.
static Set<String> asStringSet(Set rawSet)
          Converts a raw set into a set of strings, calling the Object.toString() method for each non-null object.
static Set<String> asStringSet(String input)
          Converts a string into an set of whitespace-separated tokens.
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 of Util.TRUE_CHAR and Util.FALSE_CHAR characters, without using separator characters.
static String flatten(char[] array)
          Flattens the elements of the provided array into a single string, without using separator characters.
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(Iterator iterator)
          Flattens the objects returned by an iterator into a single string, separating elements by a space character.
static String flatten(Iterator iterator, String separator)
          Flattens the objects returned by an iterator into a single string, separating elements by the provided separator.
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).
static
<T> boolean
removeByIdentity(Collection<T> coll, T obj)
          Removes an object from a collection (if it is present), using identity-based comparisons instead of the equals-based comparisons used by Collection.remove(Object).
 
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

addAll

public static <T,E extends T> void addAll(Collection<T> coll,
                                          E[] array)
Convenience method that adds all members of an array to a collection.

Type Parameters:
T - the type of the collection
E - the type of the array
Parameters:
coll - the collection to add to
array - the array to value to add

arrayAsSet

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

Type Parameters:
T - the type of the array
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

asBooleanArray

public static boolean[] asBooleanArray(String input)
                                throws IllegalArgumentException
Converts a string into a boolean array, calling Util.asBoolean(char) on each character.

Parameters:
input - the input string to convert
Returns:
the converted boolean array
Throws:
IllegalArgumentException - if one of the characters does not contain a parsable boolean

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)

asCharArray

public static char[] asCharArray(String input)
Converts a string array into a char array. This is just a convenience wrapper for String.toCharArray().

Parameters:
input - the input string to convert
Returns:
the converted char array

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

asDoubleArray

public static double[] asDoubleArray(String input)
                              throws NumberFormatException
Converts a string into a double array, by splitting on whitespace and calling asDoubleArray(Object[]) on the result.

Parameters:
input - the input string to convert
Returns:
the converted double array
Throws:
NumberFormatException - if one of the tokens 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

asFloatArray

public static float[] asFloatArray(String input)
                            throws NumberFormatException
Converts a string into a float array, by splitting on whitespace and calling asFloatArray(Object[]) on the result.

Parameters:
input - the input string to convert
Returns:
the converted float array
Throws:
NumberFormatException - if one of the tokens 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

asIntArray

public static int[] asIntArray(String input)
                        throws NumberFormatException
Converts a string into an integer array, by splitting on whitespace and calling asIntArray(Object[]) on the result.

Parameters:
input - the input string to convert
Returns:
the converted integer array
Throws:
NumberFormatException - if one of the tokens does not contain a parsable integer

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

asLongArray

public static long[] asLongArray(String input)
                          throws NumberFormatException
Converts a string into a long array, by splitting on whitespace and calling asLongArray(Object[]) on the result.

Parameters:
input - the input string to convert
Returns:
the converted long array
Throws:
NumberFormatException - if one of the tokens 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

asShortArray

public static short[] asShortArray(String input)
                            throws NumberFormatException
Converts a string into a short array, by splitting on whitespace and calling asShortArray(Object[]) on the result.

Parameters:
input - the input string to convert
Returns:
the converted short array
Throws:
NumberFormatException - if one of the tokens 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

asStringArray

public static String[] asStringArray(String input)
Converts a string into an array of whitespace-separated tokens. This is just a convenience wrapper for TextUtils.splitString(CharSequence).

Parameters:
input - the input string to convert
Returns:
the converted token array

asStringSet

public static Set<String> asStringSet(String input)
Converts a string into an set of whitespace-separated tokens. This implementation converts the array returned by asShortArray(String)} into a LinkedHashSet, so the original iteration order is preserved (but duplicates will be discarded).

Parameters:
input - the input string to convert
Returns:
the converted set of tokens, in original order

asStringSet

public static Set<String> asStringSet(Set rawSet)
Converts a raw set into a set of strings, calling the Object.toString() method for each non-null object.

Parameters:
rawSet - the set of objects to convert
Returns:
the converted set of strings, in original order

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 of Util.TRUE_CHAR and Util.FALSE_CHAR characters, without using separator characters.

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, without using separator characters.

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(Iterator iterator)
Flattens the objects returned by an iterator into a single string, separating elements by a space character.

Parameters:
iterator - the iterator over the elements to join
Returns:
the flattened string

flatten

public static String flatten(Iterator iterator,
                             String separator)
Flattens the objects returned by an iterator into a single string, separating elements by the provided separator.

Parameters:
iterator - the iterator over the elements to join
separator - the separator string to use
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).

Type Parameters:
T - the type of the list
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

removeByIdentity

public static <T> boolean removeByIdentity(Collection<T> coll,
                                           T obj)
Removes an object from a collection (if it is present), using identity-based comparisons instead of the equals-based comparisons used by Collection.remove(Object).

Type Parameters:
T - the type of the collection
Parameters:
coll - the collection to use
obj - the object to remove
Returns:
true if the object has been removed from the collection


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