info.javelot.functionalj.util
Class Lists

java.lang.Object
  extended by info.javelot.functionalj.util.Lists

public final class Lists
extends Object

Methods concerning Collections, Lists and Sets.

Author:
Copyright © 2006 Frederic Daoud

Field Summary
static Function1<Integer,Collection> sizeOf
           
 
Method Summary
static boolean[] asBooleanArray(Collection<Boolean> p_coll)
          Returns an array of booleans from a collection of Booleans.
static byte[] asByteArray(Collection<Byte> p_coll)
          Returns an array of bytes from a collection of Bytes.
static char[] asCharArray(Collection<Character> p_coll)
          Returns an array of chars from a collection of Characters.
static double[] asDoubleArray(Collection<Double> p_coll)
          Returns an array of doubles from a collection of Doubles.
static float[] asFloatArray(Collection<Float> p_coll)
          Returns an array of floats from a collection of Floats.
static int[] asIntArray(Collection<Integer> p_coll)
          Returns an array of ints from a collection of Integers.
static List<Boolean> asList(boolean... p_booleans)
          Returns a List of Booleans from an array of booleans.
static List<Byte> asList(byte... p_bytes)
          Returns a List of Bytes from an array of bytes.
static List<Character> asList(char... p_chars)
          Returns a List of Characters from an array of chars.
static List<Double> asList(double... p_doubles)
          Returns a List of Doubles from an array of doubles.
static List<Float> asList(float... p_floats)
          Returns a List of Floats from an array of floats.
static List<Integer> asList(int... p_ints)
          Returns a List of Integers from an array of ints.
static List<Long> asList(long... p_longs)
          Returns a List of Longs from an array of longs.
static List<Short> asList(short... p_shorts)
          Returns a List of Shorts from an array of shorts.
static List<Character> asList(String p_string)
          Returns a List of Characters from a String.
static
<T> List<T>
asList(T... p_objects)
          Returns a List from an array of Objects, returning an empty List if the array is null.
static long[] asLongArray(Collection<Long> p_coll)
          Returns an array of longs from a collection of Longs.
static short[] asShortArray(Collection<Short> p_coll)
          Returns an array of shorts from a collection of Shorts.
static Object[] concat(Object[] p_array1, Object[] p_array2)
          Combines two arrays of parameters into one by concatenating the two, treating null arrays as zero-length arrays.
static
<T> T
first(Collection<T> p_collection)
          Returns the first element of a collection.
static
<T> T
first(T[] p_array)
          Version of first(Collection) which accepts an array.
static
<T> List<T>
init(Collection<T> p_collection)
          Returns the initial of a collection, that is, all elements except the last.
static
<T> List<T>
init(T[] p_array)
          Version of init(Collection) that accepts an array.
static Object[] initArray(Object[] p_array)
          Version of init(Collection) which accepts an array and returns an array, without using an intermediate Collection.
static
<T> Iterable<? extends T>
iterable(Collection<? extends T> p_coll)
          Returns the Iterable for the specified collection, or an empty Iterable if the collection is null, to avoid NullPointerExceptions.
static
<T> Iterable<T>
iterable(T[] p_array)
          Returns the Iterable for the specified array, or an empty Iterable if the array is null, to avoid NullPointerExceptions.
static
<T> Iterator<? extends T>
iterator(Collection<? extends T> p_coll)
          Returns the Iterator for the specified collection, or an empty iterator if the collection is null, to avoid NullPointerExceptions.
static
<T> Iterator<T>
iterator(T[] p_array)
          Returns the Iterator for the specified array, or an empty iterator if the array is null, to avoid NullPointerExceptions.
static
<T> T
last(Collection<T> p_collection)
          Returns the last element of a collection.
static
<T> T
last(T[] p_array)
          Version of last(Collection) that accepts an array.
static int sizeOf(Collection p_collection)
          Returns the size of a collection, returning zero if the list is null.
static
<T> int
sizeOf(T... p_array)
          Returns the size of a array, returning zero if the array is null.
static
<T> Pair<List<T>,List<T>>
splitAt(int p_index, List<T> p_list)
          Splits a list in two at the specified index, returning a pair of lists.
static
<T> Pair<List<T>,List<T>>
splitAt(int p_index, T[] p_array)
          Version of splitAt(int, List) that accepts an array.
static Pair<Object[],Object[]> splitAtArray(int p_index, Object[] p_array)
          Version of splitAt(int, List) that accepts an array and returns a Pair of arrays, without using an intermediate Collection.
static
<T> List<T>
tail(Collection<T> p_collection)
          Returns the tail of a collection, that is, all elements except the first.
static
<T> List<T>
tail(T[] p_array)
          Version of tail(Collection) which accepts an array.
static Object[] tailArray(Object[] p_array)
          Version of tail(Collection) which accepts an array and returns an array, without using an intermediate Collection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sizeOf

public static final Function1<Integer,Collection> sizeOf
Method Detail

first

public static <T> T first(Collection<T> p_collection)
               throws FunctionException
Returns the first element of a collection.

Parameters:
p_collection - the collection for which to return the first element.
Returns:
the first element of the collection.
Throws:
FunctionException - thrown if the collection is null or empty.

first

public static <T> T first(T[] p_array)
               throws FunctionException
Version of first(Collection) which accepts an array.

Throws:
FunctionException

last

public static <T> T last(Collection<T> p_collection)
Returns the last element of a collection.

Parameters:
p_collection - the list for which to return the last element.
Returns:
the last element of the collection.
Throws:
FunctionException - thrown if the collection is null or empty.

last

public static <T> T last(T[] p_array)
Version of last(Collection) that accepts an array.


init

public static <T> List<T> init(Collection<T> p_collection)
Returns the initial of a collection, that is, all elements except the last.

Parameters:
p_collection - the collection for which to return the initial part.
Returns:
all elements of the collection except the last, or an empty list if the specified collection is empty.

init

public static <T> List<T> init(T[] p_array)
Version of init(Collection) that accepts an array.


initArray

public static Object[] initArray(Object[] p_array)
Version of init(Collection) which accepts an array and returns an array, without using an intermediate Collection.


tail

public static <T> List<T> tail(Collection<T> p_collection)
Returns the tail of a collection, that is, all elements except the first.

Parameters:
p_collection - the collection for which to return the tail.
Returns:
all elements of the collection except the first, or an empty list if the specified collection is empty.

tail

public static <T> List<T> tail(T[] p_array)
Version of tail(Collection) which accepts an array.


tailArray

public static Object[] tailArray(Object[] p_array)
Version of tail(Collection) which accepts an array and returns an array, without using an intermediate Collection.


splitAt

public static <T> Pair<List<T>,List<T>> splitAt(int p_index,
                                                List<T> p_list)
Splits a list in two at the specified index, returning a pair of lists. The first list contains as many elements as the specified index; the second list contains the rest of the elements.

Particular cases:


splitAt

public static <T> Pair<List<T>,List<T>> splitAt(int p_index,
                                                T[] p_array)
Version of splitAt(int, List) that accepts an array.


splitAtArray

public static Pair<Object[],Object[]> splitAtArray(int p_index,
                                                   Object[] p_array)
Version of splitAt(int, List) that accepts an array and returns a Pair of arrays, without using an intermediate Collection.


sizeOf

public static int sizeOf(Collection p_collection)
Returns the size of a collection, returning zero if the list is null.

Parameters:
p_collection - the collection for which to return the size.
Returns:
the size of the collection, or 0 if the collection is null.

sizeOf

public static <T> int sizeOf(T... p_array)
Returns the size of a array, returning zero if the array is null.

Parameters:
p_array - the array for which to return the size.
Returns:
the size of the array, or 0 if the array is null.

asList

public static <T> List<T> asList(T... p_objects)
Returns a List from an array of Objects, returning an empty List if the array is null.

Parameters:
p_objects - the array of Objects to be converted to a List.
Returns:
a List of Objects.

asList

public static List<Boolean> asList(boolean... p_booleans)
Returns a List of Booleans from an array of booleans. Returns an empty list if the array is null.

Parameters:
p_booleans - the array of boolean values to be converted to a List.
Returns:
a List of Boolean objects.

asList

public static List<Byte> asList(byte... p_bytes)
Returns a List of Bytes from an array of bytes. Returns an empty list if the array is null.

Parameters:
p_bytes - the array of byte values to be converted to a List.
Returns:
a List of Byte objects.

asList

public static List<Character> asList(char... p_chars)
Returns a List of Characters from an array of chars. Returns an empty list if the array is null.

Parameters:
p_chars - the array of char values to be converted to a List.
Returns:
a List of Character objects.

asList

public static List<Double> asList(double... p_doubles)
Returns a List of Doubles from an array of doubles. Returns an empty list if the array is null.

Parameters:
p_doubles - the array of double values to be converted to a List.
Returns:
a List of Double objects.

asList

public static List<Float> asList(float... p_floats)
Returns a List of Floats from an array of floats. Returns an empty list if the array is null.

Parameters:
p_floats - the array of float values to be converted to a List.
Returns:
a List of Float objects.

asList

public static List<Integer> asList(int... p_ints)
Returns a List of Integers from an array of ints. Returns an empty list if the array is null.

Parameters:
p_ints - the array of int values to be converted to a List.
Returns:
a List of Integer objects.

asList

public static List<Long> asList(long... p_longs)
Returns a List of Longs from an array of longs. Returns an empty list if the array is null.

Parameters:
p_longs - the array of long values to be converted to a List.
Returns:
a List of Long objects.

asList

public static List<Short> asList(short... p_shorts)
Returns a List of Shorts from an array of shorts. Returns an empty list if the array is null.

Parameters:
p_shorts - the array of short values to be converted to a List.
Returns:
a List of Short objects.

asList

public static List<Character> asList(String p_string)
Returns a List of Characters from a String. Returns an empty list if the string is null.

Parameters:
p_string - the String to be converted to a List.
Returns:
a List of Character objects.

concat

public static Object[] concat(Object[] p_array1,
                              Object[] p_array2)
Combines two arrays of parameters into one by concatenating the two, treating null arrays as zero-length arrays.

Parameters:
p_array1 - the first array.
p_array2 - the second array.
Returns:
a non-null array that combines the two arrays by concatenation.

asBooleanArray

public static boolean[] asBooleanArray(Collection<Boolean> p_coll)
Returns an array of booleans from a collection of Booleans. Returns an empty array if the collection is null.

Parameters:
p_coll - the collection of Booleans to be converted to an array of booleans.
Returns:
an array of booleans.

asByteArray

public static byte[] asByteArray(Collection<Byte> p_coll)
Returns an array of bytes from a collection of Bytes. Returns an empty array if the collection is null.

Parameters:
p_coll - the collection of Bytes to be converted to an array of bytes.
Returns:
an array of bytes.

asCharArray

public static char[] asCharArray(Collection<Character> p_coll)
Returns an array of chars from a collection of Characters. Returns an empty array if the collection is null.

Parameters:
p_coll - the collection of Characters to be converted to an array of chars.
Returns:
an array of chars.

asDoubleArray

public static double[] asDoubleArray(Collection<Double> p_coll)
Returns an array of doubles from a collection of Doubles. Returns an empty array if the collection is null.

Parameters:
p_coll - the collection of Doubles to be converted to an array of doubles.
Returns:
an array of doubles.

asFloatArray

public static float[] asFloatArray(Collection<Float> p_coll)
Returns an array of floats from a collection of Floats. Returns an empty array if the collection is null.

Parameters:
p_coll - the collection of Floats to be converted to an array of floats.
Returns:
an array of doubles.

asIntArray

public static int[] asIntArray(Collection<Integer> p_coll)
Returns an array of ints from a collection of Integers. Returns an empty array if the collection is null.

Parameters:
p_coll - the collection of Integers to be converted to an array of ints.
Returns:
an array of ints.

asLongArray

public static long[] asLongArray(Collection<Long> p_coll)
Returns an array of longs from a collection of Longs. Returns an empty array if the collection is null.

Parameters:
p_coll - the collection of Longs to be converted to an array of longs.
Returns:
an array of longs.

asShortArray

public static short[] asShortArray(Collection<Short> p_coll)
Returns an array of shorts from a collection of Shorts. Returns an empty array if the collection is null.

Parameters:
p_coll - the collection of Shorts to be converted to an array of shorts.
Returns:
an array of shorts.

iterator

public static <T> Iterator<? extends T> iterator(Collection<? extends T> p_coll)
Returns the Iterator for the specified collection, or an empty iterator if the collection is null, to avoid NullPointerExceptions.


iterator

public static <T> Iterator<T> iterator(T[] p_array)
Returns the Iterator for the specified array, or an empty iterator if the array is null, to avoid NullPointerExceptions.


iterable

public static <T> Iterable<? extends T> iterable(Collection<? extends T> p_coll)
Returns the Iterable for the specified collection, or an empty Iterable if the collection is null, to avoid NullPointerExceptions.


iterable

public static <T> Iterable<T> iterable(T[] p_array)
Returns the Iterable for the specified array, or an empty Iterable if the array is null, to avoid NullPointerExceptions.