org.eclipse.xtext.xbase.lib
Class ListExtensions

java.lang.Object
  extended by org.eclipse.xtext.xbase.lib.ListExtensions

public class ListExtensions
extends java.lang.Object

This is an extension library for lists.

Author:
Sebastian Zarnekow - Initial contribution and API

Constructor Summary
ListExtensions()
           
 
Method Summary
static
<T,R> java.util.List<R>
map(java.util.List<T> original, Functions.Function1<? super T,R> transformation)
          Returns a list that performs the given transformation for each element of original when requested.
static
<T> java.util.List<T>
reverse(java.util.List<T> list)
          Reverses the order of the elements in the specified list.
static
<T> java.lang.Iterable<T>
reverseView(java.util.List<T> list)
          Provides a reverse view on the given list which is especially useful to traverse a list backwards in a for-each loop.
static
<T extends java.lang.Comparable<? super T>>
java.util.List<T>
sortInplace(java.util.List<T> list)
          Sorts the specified list itself into ascending order, according to the natural ordering of its elements.
static
<T> java.util.List<T>
sortInplace(java.util.List<T> list, java.util.Comparator<? super T> comparator)
          Sorts the specified list itself according to the order induced by the specified comparator.
static
<T,C extends java.lang.Comparable<? super C>>
java.util.List<T>
sortInplaceBy(java.util.List<T> list, Functions.Function1<? super T,C> key)
          Sorts the specified list itself according to the order induced by applying a key function to each element which yields a comparable criteria.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ListExtensions

public ListExtensions()
Method Detail

sortInplace

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

Parameters:
list - the list to be sorted. May not be null.
Returns:
the sorted list itself.
See Also:
Collections.sort(List)

sortInplace

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

Parameters:
list - the list to be sorted. May not be null.
comparator - the comparator to be used. May be null to indicate that the natural ordering of the elements should be used.
Returns:
the sorted list itself.
See Also:
Collections.sort(List, Comparator), sortInplace(List), #sortInplaceBy(List, Function1)

sortInplaceBy

public static <T,C extends java.lang.Comparable<? super C>> java.util.List<T> sortInplaceBy(java.util.List<T> list,
                                                                                            Functions.Function1<? super T,C> key)
Sorts the specified list itself according to the order induced by applying a key function to each element which yields a comparable criteria.

Parameters:
list - the list to be sorted. May not be null.
key - the key function to-be-used. May not be null.
Returns:
the sorted list itself.
See Also:
Collections.sort(List)

reverseView

public static <T> java.lang.Iterable<T> reverseView(java.util.List<T> list)
Provides a reverse view on the given list which is especially useful to traverse a list backwards in a for-each loop. The list itself is not modified by calling this method.

Parameters:
list - the list whose elements should be traversed in reverse. May not be null.
Returns:
an iterable with the same elements as the list, in reverse

reverse

public static <T> java.util.List<T> reverse(java.util.List<T> list)
Reverses the order of the elements in the specified list. The list itself will be modified.

Parameters:
list - the list whose elements are to be reversed.
Returns:
the list itself
Throws:
java.lang.UnsupportedOperationException - if the specified list or its list-iterator does not support the set method.

map

public static final <T,R> java.util.List<R> map(java.util.List<T> original,
                                                Functions.Function1<? super T,R> transformation)
Returns a list that performs the given transformation for each element of original when requested. The mapping is done lazily. That is, subsequent iterations of the elements in the iterable will repeatedly apply the transformation. The returned list is a transformed view of original; changes to original will be reflected in the returned list and vice versa.

Parameters:
original - the original iterable. May not be null.
transformation - the transformation. May not be null.
Returns:
a list that effectively contains the results of the transformation. Never null.