TUM CCSM Commons

edu.tum.cs.commons.collections
Class CollectionUtils

java.lang.Object
  extended by edu.tum.cs.commons.collections.CollectionUtils

public class CollectionUtils
extends java.lang.Object

This class offers utility methods for collections. In can be seen as an extension to Collections.

Version:
$Revision: 27324 $
Author:
Florian Deissenboeck, $Author: hummelb $
Rating:
GREEN Hash: 18A8495D2E7BB7DE351987B60C0E3338

Constructor Summary
CollectionUtils()
           
 
Method Summary
static
<T> java.util.HashSet<T>
asHashSet(T... elements)
          Create an hashed set from an array.
static
<T extends java.lang.Comparable<T>>
UnmodifiableList<T>
asSortedUnmodifiableList(java.util.Collection<T> collection)
          Returns a sorted unmodifiable list for a collection.
static
<T extends java.lang.Comparable<T>>
UnmodifiableList<T>
asSortedUnmodifiableList(java.util.Collection<T> collection, java.util.Comparator<? super T> comparator)
          Returns a sorted unmodifiable list for a collection.
static
<T> UnmodifiableCollection<T>
asUnmodifiable(java.util.Collection<T> c)
          Returns a new unmodifiable collection wrapping the given collection.
static
<S,T> UnmodifiableHashedListMap<S,T>
asUnmodifiable(HashedListMap<S,T> m)
          Returns a new unmodifiable hashed list map wrapping the given map.
static
<T> UnmodifiableList<T>
asUnmodifiable(java.util.List<T> l)
          Returns a new unmodifiable list wrapping the given list.
static
<S,T> UnmodifiableMap<S,T>
asUnmodifiable(java.util.Map<S,T> m)
          Returns a new unmodifiable map wrapping the given map.
static
<T> UnmodifiableSet<T>
asUnmodifiable(java.util.Set<T> s)
          Returns a new unmodifiable set wrapping the given set.
static
<S,T> UnmodifiableSortedMap<S,T>
asUnmodifiable(java.util.SortedMap<S,T> m)
          Returns a new unmodifiable sorted map wrapping the given sorted map.
static
<T> UnmodifiableSortedSet<T>
asUnmodifiable(java.util.SortedSet<T> s)
          Returns a new unmodifiable sorted set wrapping the given sorted set.
static
<T> java.util.List<ImmutablePair<T,T>>
computeUnorderedPairs(java.util.Collection<T> collection)
          Compute list of unordered pairs for all elements contained in a collection.
static
<T> T[]
copyArray(T[] original)
          Copy an array.
static
<T> UnmodifiableList<T>
emptyList()
          The way this method is defined allows to assign it to all parameterized types, i.e. bot of the following statements are accepted by the compiler without warnings: UnmodifiableList<String> emptyList = CollectionUtils.emptyList(); UnmodifiableList<Date> emptyList = CollectionUtils.emptyList();
static
<S,T> UnmodifiableMap<S,T>
emptyMap()
          Returns an empty map.
static
<T> UnmodifiableSet<T>
emptySet()
          Returns an empty set.
static
<T> T
getAny(java.lang.Iterable<T> iterable)
          Returns one object from an Iterable or null if the iterable is empty.
static
<T> T
getLast(java.util.List<T> list)
          Returns the last element in list or null, if list is empty.
static
<T> java.util.List<T>
reverse(java.util.Collection<T> list)
          Returns a list that contains the elements of the specified list in reversed order.
static
<T extends java.lang.Comparable<? super T>>
java.util.List<T>
sort(java.util.Collection<T> collection)
          Sorts the specified list into ascending order, according to the natural ordering of its elements.
static
<T> java.util.List<T>
sort(java.util.Collection<T> collection, java.util.Comparator<? super T> comparator)
          Sorts the specified list according to the order induced by the specified comparator.
static
<T> T[]
toArray(java.util.Collection<? extends T> collection, java.lang.Class<T> type)
          Convert collection to array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionUtils

public CollectionUtils()
Method Detail

asHashSet

public static <T> java.util.HashSet<T> asHashSet(T... elements)
Create an hashed set from an array.

Type Parameters:
T - type
Parameters:
elements - elements in the set.
Returns:
the set.
See Also:
Arrays.asList(Object[])

asUnmodifiable

public static <T> UnmodifiableCollection<T> asUnmodifiable(java.util.Collection<T> c)
Returns a new unmodifiable collection wrapping the given collection. This method is here to avoid retyping the generic argument for the collection class.


asUnmodifiable

public static <S,T> UnmodifiableHashedListMap<S,T> asUnmodifiable(HashedListMap<S,T> m)
Returns a new unmodifiable hashed list map wrapping the given map. This method is here to avoid retyping the generic arguments for the sorted map class.


asUnmodifiable

public static <T> UnmodifiableList<T> asUnmodifiable(java.util.List<T> l)
Returns a new unmodifiable list wrapping the given list. This method is here to avoid retyping the generic argument for the list class.


asUnmodifiable

public static <S,T> UnmodifiableMap<S,T> asUnmodifiable(java.util.Map<S,T> m)
Returns a new unmodifiable map wrapping the given map. This method is here to avoid retyping the generic arguments for the map class.


asUnmodifiable

public static <T> UnmodifiableSet<T> asUnmodifiable(java.util.Set<T> s)
Returns a new unmodifiable set wrapping the given set. This method is here to avoid retyping the generic argument for the set class.


asUnmodifiable

public static <S,T> UnmodifiableSortedMap<S,T> asUnmodifiable(java.util.SortedMap<S,T> m)
Returns a new unmodifiable sorted map wrapping the given sorted map. This method is here to avoid retyping the generic arguments for the sorted map class.


asUnmodifiable

public static <T> UnmodifiableSortedSet<T> asUnmodifiable(java.util.SortedSet<T> s)
Returns a new unmodifiable sorted set wrapping the given sorted set. This method is here to avoid retyping the generic argument for the sorted set class.


emptyList

public static final <T> UnmodifiableList<T> emptyList()
The way this method is defined allows to assign it to all parameterized types, i.e. bot of the following statements are accepted by the compiler without warnings:
 UnmodifiableList<String> emptyList = CollectionUtils.emptyList();
 
 UnmodifiableList<Date> emptyList = CollectionUtils.emptyList();
 

Type Parameters:
T -
Returns:
the emtpy list

emptyMap

public static final <S,T> UnmodifiableMap<S,T> emptyMap()
Returns an empty map. See emptyList() for further details.


emptySet

public static final <T> UnmodifiableSet<T> emptySet()
Returns an empty set. See emptyList() for further details.


sort

public static <T extends java.lang.Comparable<? super T>> java.util.List<T> sort(java.util.Collection<T> collection)
Sorts the specified list into ascending order, according to the natural ordering of its elements.

All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

This method does not modify the original collection.


reverse

public static <T> java.util.List<T> reverse(java.util.Collection<T> list)
Returns a list that contains the elements of the specified list in reversed order.

This method does not modify the original collection.


sort

public static <T> java.util.List<T> sort(java.util.Collection<T> collection,
                                         java.util.Comparator<? super T> comparator)
Sorts the specified list according to the order induced by the specified comparator.

All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

This method does not modify the original collection.


asSortedUnmodifiableList

public static <T extends java.lang.Comparable<T>> UnmodifiableList<T> asSortedUnmodifiableList(java.util.Collection<T> collection)
Returns a sorted unmodifiable list for a collection.


asSortedUnmodifiableList

public static <T extends java.lang.Comparable<T>> UnmodifiableList<T> asSortedUnmodifiableList(java.util.Collection<T> collection,
                                                                                               java.util.Comparator<? super T> comparator)
Returns a sorted unmodifiable list for a collection.

Parameters:
comparator - Comparator used for sorting

getAny

public static <T> T getAny(java.lang.Iterable<T> iterable)
Returns one object from an Iterable or null if the iterable is empty.


toArray

public static <T> T[] toArray(java.util.Collection<? extends T> collection,
                              java.lang.Class<T> type)
Convert collection to array. This is a bit cleaner version of Collection.toArray(Object[]).


copyArray

public static <T> T[] copyArray(T[] original)
Copy an array. This is a shortcut for Arrays.copyOf(Object[], int) that does not require to specify the length.


computeUnorderedPairs

public static <T> java.util.List<ImmutablePair<T,T>> computeUnorderedPairs(java.util.Collection<T> collection)
Compute list of unordered pairs for all elements contained in a collection.


getLast

public static <T> T getLast(java.util.List<T> list)
Returns the last element in list or null, if list is empty.


TUM CCSM Commons

TUM CCSM Commons - 2.7