org.jtheque.utils.collections
Class CollectionUtils

java.lang.Object
  extended by org.jtheque.utils.collections.CollectionUtils

public final class CollectionUtils
extends Object

Provide some utilities operations on collections.

Author:
Baptiste Wicht

Method Summary
static
<T> List<T>
copyOf(Collection<T> list)
          Return a copy of the list.
static
<T> List<T>
emptyList()
          Return an empty list.
static
<S,T> Collection<T>
expand(Collection<S> collection, Expander<S,T> expander)
          Return a collection expanded.
static
<T> void
filter(Collection<T> collection, Filter<T> filter)
          Filter a collection with a filter.
static
<T> T
first(Iterable<T> collection)
          Return the first element of the collection.
static
<T> void
forAllDo(Iterable<T> collection, Closure<T> closure)
          Perform a closure on all the objects of the collection.
static
<T> void
goToFirst(ListIterator<T> iterator)
          Move the iterator to the first element.
static
<T> void
goToLast(Iterator<T> iterator)
          Move the iterator to the last element.
static void reverse(List<?> list)
          Reverse a list.
static
<T,K> void
reverse(Map<T,K> map)
          Reverse the order of a map.
static
<T> void
sort(List<T> list, Comparator<T> comparator)
          Sort a list.
static
<T> Collection<T>
toCollection(Enumeration<T> enumeration)
          Convert the enumeration to a collection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

forAllDo

public static <T> void forAllDo(Iterable<T> collection,
                                Closure<T> closure)
Perform a closure on all the objects of the collection.

Parameters:
collection - The collection of the objects.
closure - The closure.

copyOf

public static <T> List<T> copyOf(Collection<T> list)
Return a copy of the list.

Type Parameters:
T - The type contained in the list.
Parameters:
list - The List to copy.
Returns:
A copy of the list.

expand

public static <S,T> Collection<T> expand(Collection<S> collection,
                                         Expander<S,T> expander)
Return a collection expanded.

Type Parameters:
T - The type of object in the collection.
Parameters:
collection - The collection to expand.
expander - The expander object.
Returns:
The expanded collection.

filter

public static <T> void filter(Collection<T> collection,
                              Filter<T> filter)
Filter a collection with a filter.

Type Parameters:
T - The type of object in the collection.
Parameters:
collection - The collection to filter.
filter - The filter.

reverse

public static <T,K> void reverse(Map<T,K> map)
Reverse the order of a map. This method provide correct result only for map who retain the insertion order.

Type Parameters:
T - The Key type.
K - The value type.
Parameters:
map - The map to reverse.

reverse

public static void reverse(List<?> list)
Reverse a list.

Parameters:
list - The list to reverse.

sort

public static <T> void sort(List<T> list,
                            Comparator<T> comparator)
Sort a list.

Type Parameters:
T - The type of object in the collection.
Parameters:
list - The list to sort.
comparator - The comparator to use to sort the list.

emptyList

public static <T> List<T> emptyList()
Return an empty list. This list is unmodifiable.

Type Parameters:
T - The Type of object to store in the list.
Returns:
The empty list.

goToFirst

public static <T> void goToFirst(ListIterator<T> iterator)
Move the iterator to the first element.

Type Parameters:
T - The type of object stored in the iterator.
Parameters:
iterator - The list iterator.

goToLast

public static <T> void goToLast(Iterator<T> iterator)
Move the iterator to the last element.

Type Parameters:
T - The type of object stored in the iterator.
Parameters:
iterator - The iterator.

toCollection

public static <T> Collection<T> toCollection(Enumeration<T> enumeration)
Convert the enumeration to a collection.

Type Parameters:
T - The type of object stored in the enumeration.
Parameters:
enumeration - The enumeration to convert to Collection.
Returns:
A Collection containing all the elements of the enumeration.

first

public static <T> T first(Iterable<T> collection)
Return the first element of the collection.

Type Parameters:
T - The objects stored in the collection.
Parameters:
collection - The collection.
Returns:
The first element of the collection or null if the collection is empty.


Copyright © 2010 JTheque. All Rights Reserved.