org.eclipse.xtext.xbase.lib
Class ListExtensions

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

public class ListExtensions
extends Object

This is an extension library for lists.

Author:
Sebastian Zarnekow - Initial contribution and API

Constructor Summary
ListExtensions()
           
 
Method Summary
static
<T,R> List<R>
map(List<T> original, Functions.Function1<? super T,? extends R> transformation)
          Returns a list that performs the given transformation for each element of original when requested.
static
<T> List<T>
reverse(List<T> list)
          Reverses the order of the elements in the specified list.
static
<T> List<T>
reverseView(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 Comparable<? super T>>
List<T>
sortInplace(List<T> list)
          Sorts the specified list itself into ascending order, according to the natural ordering of its elements.
static
<T> List<T>
sortInplace(List<T> list, Comparator<? super T> comparator)
          Sorts the specified list itself according to the order induced by the specified comparator.
static
<T,C extends Comparable<? super C>>
List<T>
sortInplaceBy(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 Comparable<? super T>> List<T> sortInplace(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> List<T> sortInplace(List<T> list,
                                      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 Comparable<? super C>> List<T> sortInplaceBy(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> List<T> reverseView(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:
a list with the same elements as the given list, in reverse

reverse

public static <T> List<T> reverse(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:
UnsupportedOperationException - if the specified list or its list-iterator does not support the set method.

map

public static <T,R> List<R> map(List<T> original,
                                Functions.Function1<? super T,? extends 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 list 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 (e.g. invocations of List.remove(int)).

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


Copyright © 2012. All Rights Reserved.