org.mulgara.util
Class C

java.lang.Object
  extended by org.mulgara.util.C

public class C
extends Object

Generic Collections utility class. This class defines static methods for operating on Collections and the functors found in Fn1 and Fn2.

Author:
Paul Gearon

Constructor Summary
C()
           
 
Method Summary
static
<T extends Comparable<T>>
List<T>
ascendingInsert(List<T> list, T c)
          Inserts an element into a list in ascending order.
static
<T extends Comparable<T>>
List<T>
descendingInsert(List<T> list, T c)
          Inserts an element into an ordered list in descending order.
static
<T1> T1
first(Collection<T1> arg)
          Returns the first item from an Iterable.
static
<T1> T1
first(LinkedList<T1> arg)
          Returns the first item from a Collection.
static
<T1> T1
head(LinkedList<T1> arg)
          Returns the head of a linked list.
static
<T1> T1
head(List<T1> arg)
          Returns the head of a list.
static
<T1> T1
headN(LinkedList<T1> arg)
          Returns the head of a linked list, with a null for an empty list.
static
<T1> T1
headN(List<T1> arg)
          Returns the head of a list.
static
<T> List<T>
intersect(List<T> list, T[] array)
          Method to create an intersection of a list and an array.
static
<T> Set<T>
intersect(Set<T> set, T[] array)
          Method to create an intersection of a set and an array.
static
<T> String
join(List<T> list, String separator)
          Method to join the elements of a list into a string.
static
<T> String
join(List<T> list, String start, String separator, String end)
          General method to join the elements of a list into a string.
static
<T1> T1
last(LinkedList<T1> arg)
          Returns the final elements of a linked list.
static
<T1> T1
last(List<T1> arg)
          Returns the final element of a list.
static
<T1> T1
lastN(LinkedList<T1> arg)
          Returns the tail of a list.
static
<T1> T1
lastN(List<T1> arg)
          Returns the tail of a list.
static
<T1,T2> List<T2>
map(Collection<T1> args, Fn1<T1,T2> op)
          Creates a new list by applying an operator to each element of an initial list.
static
<T1,T2,E extends Exception>
List<T2>
map(Collection<T1> args, Fn1E<T1,T2,E> op)
          Creates a new list by applying an operator to each element of an initial list.
static
<T1,T2> List<T2>
map(T1[] args, Fn1<T1,T2> op)
          The same method as map(Collection, Fn1) for arrays.
static
<T1,T2,E extends Exception>
List<T2>
map(T1[] args, Fn1E<T1,T2,E> op)
          The same method as map(Collection, Fn1E) for arrays.
static
<T1> List<T1>
tail(List<T1> arg)
          Returns the tail of a list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

C

public C()
Method Detail

map

public static final <T1,T2,E extends Exception> List<T2> map(Collection<T1> args,
                                                             Fn1E<T1,T2,E> op)
                          throws E extends Exception
Creates a new list by applying an operator to each element of an initial list. The final result meets the condition:
result.get(i) == op( args.get(i) )   for 0 <= i < args.size()
The operation in op may throw an exception.

Type Parameters:
T1 - The type of the elements in the arg list, which is also the argument type for the operation.
T2 - The type of the elements in the result list, which is also the return type of the operation.
E - The exception that my be thrown from Fn1E.call(Object).
Parameters:
args - The input list.
op - The operation to apply to the elements of the input list.
Returns:
A list whose elements are the result of applying op to each element of args.
Throws:
E - An exception that may be thrown from the Fn1E.call(Object) method.
E extends Exception

map

public static final <T1,T2,E extends Exception> List<T2> map(T1[] args,
                                                             Fn1E<T1,T2,E> op)
                          throws E extends Exception
The same method as map(Collection, Fn1E) for arrays.

Throws:
E extends Exception

map

public static final <T1,T2> List<T2> map(Collection<T1> args,
                                         Fn1<T1,T2> op)
Creates a new list by applying an operator to each element of an initial list. The final result meets the condition:
result.get(i) == op( args.get(i) )   for 0 <= i < args.size()
The operation in op may not throw an exception.

Type Parameters:
T1 - The type of the elements in the arg list, which is also the argument type for the operation.
T2 - The type of the elements in the result list, which is also the return type of the operation.
Parameters:
args - The input list.
op - The operation to apply to the elements of the input list.
Returns:
A list whose elements are the result of applying op to each element of args.

map

public static final <T1,T2> List<T2> map(T1[] args,
                                         Fn1<T1,T2> op)
The same method as map(Collection, Fn1) for arrays.


head

public static final <T1> T1 head(LinkedList<T1> arg)
                     throws NoSuchElementException
Returns the head of a linked list. This is a simple wrapper for LinkedList.getFirst()

Type Parameters:
T1 - The list element type.
Parameters:
arg - The list.
Returns:
The first element in the list.
Throws:
NoSuchElementException - If the list is empty.

head

public static final <T1> T1 head(List<T1> arg)
                     throws NoSuchElementException
Returns the head of a list.

Type Parameters:
T1 - The list element type.
Parameters:
arg - The list.
Returns:
The first element in the list.
Throws:
NoSuchElementException - If the list is empty.

headN

public static final <T1> T1 headN(LinkedList<T1> arg)
Returns the head of a linked list, with a null for an empty list.

Type Parameters:
T1 - The list element type.
Parameters:
arg - The list.
Returns:
The first element in the list, or null if the list is empty.

headN

public static final <T1> T1 headN(List<T1> arg)
Returns the head of a list.

Type Parameters:
T1 - The list element type.
Parameters:
arg - The list.
Returns:
The first element in the list, or null if the list is empty.

last

public static final <T1> T1 last(LinkedList<T1> arg)
                     throws NoSuchElementException
Returns the final elements of a linked list. This is a simple wrapper for LinkedList.getLast().

Type Parameters:
T1 - The list element type.
Parameters:
arg - The list.
Returns:
The last element in the list.
Throws:
NoSuchElementException - If the list is empty.

last

public static final <T1> T1 last(List<T1> arg)
                     throws NoSuchElementException
Returns the final element of a list.

Type Parameters:
T1 - The list element type.
Parameters:
arg - The list.
Returns:
The last element in the list.
Throws:
IndexOutOfBoundsException - If the list is empty.
NoSuchElementException

tail

public static final <T1> List<T1> tail(List<T1> arg)
Returns the tail of a list.

Type Parameters:
T1 - The list element type.
Parameters:
arg - The list to get the tail of.
Returns:
A list containing all but the head of arg.

lastN

public static final <T1> T1 lastN(LinkedList<T1> arg)
Returns the tail of a list.

Type Parameters:
T1 - The list element type.
Parameters:
arg - The list.
Returns:
The last element in the list, or null if the list is empty.

lastN

public static final <T1> T1 lastN(List<T1> arg)
Returns the tail of a list.

Type Parameters:
T1 - The list element type.
Parameters:
arg - The list.
Returns:
The last element in the list, or null if the list is empty.

first

public static final <T1> T1 first(LinkedList<T1> arg)
                      throws NoSuchElementException
Returns the first item from a Collection. This is a simple wrapper for LinkedList.getFirst()

Type Parameters:
T1 - The element type.
Parameters:
arg - The list.
Returns:
The first element in the list.
Throws:
NoSuchElementException - If the list is empty.

first

public static final <T1> T1 first(Collection<T1> arg)
                      throws NoSuchElementException
Returns the first item from an Iterable.

Type Parameters:
T1 - The element type.
Parameters:
arg - The iterable collection.
Returns:
The first element in the collection.
Throws:
NoSuchElementException - If the collection is empty.

ascendingInsert

public static final <T extends Comparable<T>> List<T> ascendingInsert(List<T> list,
                                                                      T c)
Inserts an element into a list in ascending order.

Type Parameters:
T - The type of the element to be inserted. Must be comparable on itself.
Parameters:
list - The list to insert into. This must already be ordered.
c - The element to insert.
Returns:
The newly modified list with all elements in ascending.

descendingInsert

public static final <T extends Comparable<T>> List<T> descendingInsert(List<T> list,
                                                                       T c)
Inserts an element into an ordered list in descending order.

Type Parameters:
T - The type of the element to be inserted. Must be comparable on itself.
Parameters:
list - The list to insert into. This must already be ordered.
c - The element to insert.
Returns:
The newly modified list, with all elements in descending order.

join

public static final <T> String join(List<T> list,
                                    String separator)
Method to join the elements of a list into a string.

Type Parameters:
T - The type of element in the list.
Parameters:
list - The list to be converted to a string.
separator - The separator to use between elements of the list. May be null.
Returns:
The final string.

join

public static final <T> String join(List<T> list,
                                    String start,
                                    String separator,
                                    String end)
General method to join the elements of a list into a string.

Type Parameters:
T - The type of element in the list.
Parameters:
list - The list to be converted to a string.
start - The start of the string. May be null.
separator - The separator to use between elements of the list. May be null.
end - The end of the string. May be null.
Returns:
The final string.

intersect

public static final <T> List<T> intersect(List<T> list,
                                          T[] array)
Method to create an intersection of a list and an array. The order of the result will be the same as the order of the original list.

Parameters:
list - The list to intersect. This will be an ArrayList, no matter the source type.
array - The array to intersect against the list.
Returns:
A list containing elements that are in both list and array.

intersect

public static final <T> Set<T> intersect(Set<T> set,
                                         T[] array)
Method to create an intersection of a set and an array.

Parameters:
set - The set to intersect. This will be a HashSet, no matter the source type.
array - The array to intersect against the set.
Returns:
A set containing elements that are in both set and array.


Copyright © 2011. All Rights Reserved.