org.eclipse.xtext.xbase.lib
Class IterableExtensions

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

public class IterableExtensions
extends Object

This is an extension library for iterables.

Author:
Sven Efftinge - Initial contribution and API, Sebastian Zarnekow

Constructor Summary
IterableExtensions()
           
 
Method Summary
static
<T> Iterable<T>
drop(Iterable<T> iterable, int count)
          Returns a view on this iterable that provides all elements except the first count entries.
static boolean elementsEqual(Iterable<?> iterable, Iterable<?> other)
          Determines whether two iterables contain equal elements in the same order.
static
<T> boolean
exists(Iterable<T> iterable, Functions.Function1<? super T,Boolean> predicate)
          Returns true if one or more elements in iterable satisfy the predicate.
static
<T> Iterable<T>
filter(Iterable<?> unfiltered, Class<T> type)
          Returns all instances of class type in unfiltered.
static
<T> Iterable<T>
filter(Iterable<T> unfiltered, Functions.Function1<? super T,Boolean> predicate)
          Returns the elements of unfiltered that satisfy a predicate.
static
<T> Iterable<T>
filterNull(Iterable<T> unfiltered)
          Returns a new iterable filtering any null references.
static
<T> T
findFirst(Iterable<T> iterable, Functions.Function1<? super T,Boolean> predicate)
          Finds the first element in the given iterable that fulfills the predicate.
static
<T> T
findLast(Iterable<T> iterable, Functions.Function1<? super T,Boolean> predicate)
          Finds the last element in the given iterable that fulfills the predicate.
static
<T> Iterable<T>
flatten(Iterable<? extends Iterable<? extends T>> inputs)
          Combines multiple iterables into a single iterable.
static
<T,R> R
fold(Iterable<T> iterable, R seed, Functions.Function2<? super R,? super T,? extends R> function)
           Applies the combinator function to all elements of the iterable in turn and uses seed as the start value.
static
<T> boolean
forall(Iterable<T> iterable, Functions.Function1<? super T,Boolean> predicate)
          Returns true if every element in iterable satisfies the predicate.
static
<T> void
forEach(Iterable<T> iterable, Procedures.Procedure1<? super T> procedure)
          Applies procedure for each element of the given iterable.
static
<T> void
forEach(Iterable<T> iterable, Procedures.Procedure2<? super T,? super Integer> procedure)
          Applies procedure for each element of the given iterable.
static
<T> T
head(Iterable<T> iterable)
          Returns the first element in the given iterable or null if empty.
static boolean isEmpty(Iterable<?> iterable)
          Determines if the given iterable contains no elements.
static boolean isNullOrEmpty(Iterable<?> iterable)
          Determines if the given iterable is null or contains no elements.
static String join(Iterable<?> iterable)
          Returns the concatenated string representation of the elements in the given iterable.
static String join(Iterable<?> iterable, CharSequence separator)
          Returns the concatenated string representation of the elements in the given iterable.
static
<T> String
join(Iterable<T> iterable, CharSequence before, CharSequence separator, CharSequence after, Functions.Function1<? super T,? extends CharSequence> function)
          Returns the concatenated string representation of the elements in the given iterable.
static
<T> String
join(Iterable<T> iterable, CharSequence separator, Functions.Function1<? super T,? extends CharSequence> function)
          Returns the concatenated string representation of the elements in the given iterable.
static
<T> T
last(Iterable<T> iterable)
          Returns the last element in the given iterable or null if empty.
static
<T,R> Iterable<R>
map(Iterable<T> original, Functions.Function1<? super T,? extends R> transformation)
          Returns an iterable that performs the given transformation for each element of original when requested.
static
<T> Iterable<T>
operator_plus(Iterable<? extends T> a, Iterable<? extends T> b)
           Concatenates two iterables into a single iterable.
static
<T> T
reduce(Iterable<? extends T> iterable, Functions.Function2<? super T,? super T,? extends T> function)
           Applies the combinator function to all elements of the iterable in turn.
static int size(Iterable<?> iterable)
          Returns the number of elements in iterable.
static
<T extends Comparable<? super T>>
List<T>
sort(Iterable<T> iterable)
          Creates a sorted list that contains the items of the given iterable.
static
<T> List<T>
sort(Iterable<T> iterable, Comparator<? super T> comparator)
          Creates a sorted list that contains the items of the given iterable.
static
<T,C extends Comparable<? super C>>
List<T>
sortBy(Iterable<T> iterable, Functions.Function1<? super T,C> key)
          Creates a sorted list that contains the items of the given iterable.
static
<T> Iterable<T>
tail(Iterable<T> iterable)
          Returns a view on this iterable that contains all the elements except the first.
static
<T> Iterable<T>
take(Iterable<T> iterable, int count)
          Returns a view on this iterable that provides at most the first count entries.
static
<K,V> Map<K,V>
toInvertedMap(Iterable<? extends K> keys, Functions.Function1<? super K,V> computeValues)
          Returns a map for which the Map.values() are computed by the given function, and each key is an element in the given keys.
static
<T> List<T>
toList(Iterable<? extends T> iterable)
          Returns a list that contains all the entries of the given iterable in the same order.
static
<K,V> Map<K,V>
toMap(Iterable<? extends V> values, Functions.Function1<? super V,K> computeKeys)
          Returns a map for which the Map.values() are the given elements in the given order, and each key is the product of invoking a supplied function computeKeys on its corresponding value.
static
<T> Set<T>
toSet(Iterable<? extends T> iterable)
          Returns a set that contains all the unique entries of the given iterable in the order of their appearance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IterableExtensions

public IterableExtensions()
Method Detail

operator_plus

public static <T> Iterable<T> operator_plus(Iterable<? extends T> a,
                                            Iterable<? extends T> b)

Concatenates two iterables into a single iterable. The returned iterable has an iterator that traverses the elements in a, followed by the elements in b. The resulting iterable is effectivly a view on the source iterables. That is, the source iterators are not polled until necessary and the result will reflect changes in the sources.

The returned iterable's iterator supports remove() when the corresponding input iterator supports it.

Parameters:
a - the first iterable. May not be null.
b - the second iterable. May not be null.
Returns:
a combined iterable. Never null.

findFirst

public static <T> T findFirst(Iterable<T> iterable,
                              Functions.Function1<? super T,Boolean> predicate)
Finds the first element in the given iterable that fulfills the predicate. If none is found or the iterable is empty, null is returned.

Parameters:
iterable - the iterable. May not be null.
predicate - the predicate. May not be null.
Returns:
the first element in the iterable for which the given predicate returns true, returns null if no element matches the predicate or the iterable is empty.

findLast

public static <T> T findLast(Iterable<T> iterable,
                             Functions.Function1<? super T,Boolean> predicate)
Finds the last element in the given iterable that fulfills the predicate. If none is found or the iterable is empty, null is returned.

Parameters:
iterable - the iterable. May not be null.
predicate - the predicate. May not be null.
Returns:
the last element in the iterable for which the given predicate returns true, returns null if no element matches the predicate or the iterable is empty.

head

public static <T> T head(Iterable<T> iterable)
Returns the first element in the given iterable or null if empty.

Parameters:
iterable - the iterable. May not be null.
Returns:
the first element in the iterable or null.

tail

public static <T> Iterable<T> tail(Iterable<T> iterable)
Returns a view on this iterable that contains all the elements except the first.

Parameters:
iterable - the iterable. May not be null.
Returns:
an iterable with all elements except the first. Never null.
See Also:
drop(Iterable, int)

last

public static <T> T last(Iterable<T> iterable)
Returns the last element in the given iterable or null if empty.

Parameters:
iterable - the iterable. May not be null.
Returns:
the last element in the iterable or null.

take

public static <T> Iterable<T> take(Iterable<T> iterable,
                                   int count)
Returns a view on this iterable that provides at most the first count entries.

Parameters:
iterable - the iterable. May not be null.
count - the number of elements that should be returned at most.
Returns:
an iterable with count elements. Never null.
Throws:
IllegalArgumentException - if count is negative.

drop

public static <T> Iterable<T> drop(Iterable<T> iterable,
                                   int count)
Returns a view on this iterable that provides all elements except the first count entries.

Parameters:
iterable - the iterable. May not be null.
count - the number of elements that should be dropped.
Returns:
an iterable without the first count elements. Never null.
Throws:
IllegalArgumentException - if count is negative.

exists

public static <T> boolean exists(Iterable<T> iterable,
                                 Functions.Function1<? super T,Boolean> predicate)
Returns true if one or more elements in iterable satisfy the predicate.

Parameters:
iterable - the iterable. May not be null.
predicate - the predicate. May not be null.
Returns:
true if one or more elements in iterable satisfy the predicate.

forall

public static <T> boolean forall(Iterable<T> iterable,
                                 Functions.Function1<? super T,Boolean> predicate)
Returns true if every element in iterable satisfies the predicate. If iterable is empty, true is returned. In other words, false is returned if at least one element fails to fulfill the predicate.

Parameters:
iterable - the iterable. May not be null.
predicate - the predicate. May not be null.
Returns:
true if one or more elements in iterable satisfy the predicate.

filter

public static <T> Iterable<T> filter(Iterable<T> unfiltered,
                                     Functions.Function1<? super T,Boolean> predicate)
Returns the elements of unfiltered that satisfy a predicate. The resulting iterable's iterator does not support remove(). The returned iterable is a view on the original elements. Changes in the unfiltered original are reflected in the view.

Parameters:
unfiltered - the unfiltered iterable. May not be null.
predicate - the predicate. May not be null.
Returns:
an iterable that contains only the elements that fulfill the predicate. Never null.

filter

public static <T> Iterable<T> filter(Iterable<?> unfiltered,
                                     Class<T> type)
Returns all instances of class type in unfiltered. The returned iterable has elements whose class is type or a subclass of type. The returned iterable's iterator does not support remove() . The returned iterable is a view on the original elements. Changes in the unfiltered original are reflected in the view.

Parameters:
unfiltered - the unfiltered iterable. May not be null.
type - the type of elements desired
Returns:
an unmodifiable iterable containing all elements of the original iterable that were of the requested type. Never null.

filterNull

public static <T> Iterable<T> filterNull(Iterable<T> unfiltered)
Returns a new iterable filtering any null references.

Parameters:
unfiltered - the unfiltered iterable. May not be null.
Returns:
an unmodifiable iterable containing all elements of the original iterable without any null references. Never null.

map

public static <T,R> Iterable<R> map(Iterable<T> original,
                                    Functions.Function1<? super T,? extends R> transformation)
Returns an iterable 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 iterable's iterator supports remove() if the provided iterator does. After a successful remove() call, original no longer contains the corresponding element.

Parameters:
original - the original iterable. May not be null.
transformation - the transformation. May not be null.
Returns:
an iterable that provides the result of the transformation. Never null.

flatten

public static <T> Iterable<T> flatten(Iterable<? extends Iterable<? extends T>> inputs)
Combines multiple iterables into a single iterable. The returned iterable has an iterator that traverses the elements of each iterable in inputs. The input iterators are not polled until necessary.

The returned iterable's iterator supports remove() when the corresponding input iterator supports it. The methods of the returned iterable may throw NullPointerException if any of the input iterators are null.

Parameters:
inputs - the to be flattened iterables. May not be null.
Returns:
an iterable that provides the concatenated values of the input elements. Never null.

forEach

public static <T> void forEach(Iterable<T> iterable,
                               Procedures.Procedure1<? super T> procedure)
Applies procedure for each element of the given iterable.

Parameters:
iterable - the iterable. May not be null.
procedure - the procedure. May not be null.

forEach

public static <T> void forEach(Iterable<T> iterable,
                               Procedures.Procedure2<? super T,? super Integer> procedure)
Applies procedure for each element of the given iterable. The procedure takes the element and a loop counter. If the counter would overflow, Integer.MAX_VALUE is returned for all subsequent elements. The first element is at index zero.

Parameters:
iterable - the iterable. May not be null.
procedure - the procedure. May not be null.
Since:
2.3

join

public static String join(Iterable<?> iterable)
Returns the concatenated string representation of the elements in the given iterable. No delimiter is used.

Parameters:
iterable - the iterable. May not be null.
Returns:
the string representation of the iterable's elements. Never null.
See Also:
#join(Iterable, CharSequence, Function1)

join

public static String join(Iterable<?> iterable,
                          CharSequence separator)
Returns the concatenated string representation of the elements in the given iterable. The separator is used to between each pair of entries in the input. The string null is used for null entries in the input.

Parameters:
iterable - the iterable. May not be null.
separator - the separator. May not be null.
Returns:
the string representation of the iterable's elements. Never null.
See Also:
#join(Iterable, CharSequence, Function1)

join

public static <T> String join(Iterable<T> iterable,
                              CharSequence separator,
                              Functions.Function1<? super T,? extends CharSequence> function)
Returns the concatenated string representation of the elements in the given iterable. The function is used to compute the string for each element. The separator is used to between each pair of entries in the input. The string null is used if the function yields null as the string representation for an entry.

Parameters:
iterable - the iterable. May not be null.
separator - the separator. May not be null.
function - the function that is used to compute the string representation of a single element. May not be null.
Returns:
the string representation of the iterable's elements. Never null.

join

public static <T> String join(Iterable<T> iterable,
                              CharSequence before,
                              CharSequence separator,
                              CharSequence after,
                              Functions.Function1<? super T,? extends CharSequence> function)
Returns the concatenated string representation of the elements in the given iterable. The function is used to compute the string for each element. The separator is used to between each pair of entries in the input. The string null is used if the function yields null as the string representation for an entry.

Parameters:
iterable - the iterable. May not be null.
before - prepends the resulting string if the iterable contains at least one element. May be null which is equivalent to passing an empty string.
separator - the separator. May be null which is equivalent to passing an empty string.
after - appended to the resulting string if the iterable contain at least one element. May be null which is equivalent to passing an empty string.
function - the function that is used to compute the string representation of a single element. May not be null.
Returns:
the string representation of the iterable's elements. Never null.

elementsEqual

public static boolean elementsEqual(Iterable<?> iterable,
                                    Iterable<?> other)
Determines whether two iterables contain equal elements in the same order. More specifically, this method returns true if iterable and other contain the same number of elements and every element of iterable is equal to the corresponding element of other.

Parameters:
iterable - an iterable. May not be null.
other - an iterable. May not be null.
Returns:
true if the two iterables contain equal elements in the same order.

isNullOrEmpty

public static boolean isNullOrEmpty(Iterable<?> iterable)
Determines if the given iterable is null or contains no elements.

Parameters:
iterable - the to-be-queried iterable. May be null.
Returns:
true if the iterable is null or contains no elements

isEmpty

public static boolean isEmpty(Iterable<?> iterable)
Determines if the given iterable contains no elements.

Parameters:
iterable - the to-be-queried iterable. May not be null.
Returns:
true if the iterable contains no elements
See Also:
isNullOrEmpty(Iterable)

size

public static int size(Iterable<?> iterable)
Returns the number of elements in iterable.

Parameters:
iterable - the iterable. May not be null.
Returns:
the number of elements in iterable.

reduce

public static <T> T reduce(Iterable<? extends T> iterable,
                           Functions.Function2<? super T,? super T,? extends T> function)

Applies the combinator function to all elements of the iterable in turn.

One of the function parameters is an element of the iterable, and the other is the result of previous application of the function. The seed of the operation is the first element in the iterable. The second value is computed by applying the function to the seed together with the second element of the iterable. The third value is computed from the previous result together with the third element and so on. In other words, the previous result of each step is taken and passed together with the next element to the combinator function.

If the iterable is empty, null is returned.

More formally, given an iterable [a, b, c, d] and a function f, the result of reduce is f(f(f(a, b), c), d)

Parameters:
iterable - the to-be-reduced iterable. May not be null.
function - the combinator function. May not be null.
Returns:
the last result of the applied combinator function or null for the empty input.

fold

public static <T,R> R fold(Iterable<T> iterable,
                           R seed,
                           Functions.Function2<? super R,? super T,? extends R> function)

Applies the combinator function to all elements of the iterable in turn and uses seed as the start value.

One of the function parameters is an element of the iterable, and the other is the result of previous application of the function. The seed of the operation is explicitly passed to fold. The first computed value is the result of the applied function for seed and the first element of the iterable. This intermediate result together with the second element of the iterable produced the next result and so on.

fold is similar to reduce but allows a seed value and the combinator function may be asymmetric. It takes T and R and returns R.

If the iterable is empty, seed is returned.

More formally, given an iterable [a, b, c, d], a seed initial and a function f, the result of fold is f(f(f(f(initial, a), b), c), d)

Parameters:
iterable - the to-be-folded iterable. May not be null.
seed - the initial value. May be null.
function - the combinator function. May not be null.
Returns:
the last result of the applied combinator function or seed for the empty input.

toList

@Beta
public static <T> List<T> toList(Iterable<? extends T> iterable)
Returns a list that contains all the entries of the given iterable in the same order. If the iterable is of type List, itself is returned. Therefore an unchecked cast is performed. In all other cases, the result list is a copy of the iterable.

Parameters:
iterable - the iterable. May not be null.
Returns:
a list with the same entries as the given iterable. May be the same as the given iterable iff it implements List, otherwise a copy is returned. Never null.

toSet

@Beta
public static <T> Set<T> toSet(Iterable<? extends T> iterable)
Returns a set that contains all the unique entries of the given iterable in the order of their appearance. If the iterable is of type Set, itself is returned. Therefore an unchecked cast is performed. In all other cases, the result set is a copy of the iterable with stable order.

Parameters:
iterable - the iterable. May not be null.
Returns:
a set with the unique entries of the given iterable. May be the same as the given iterable iff it implements Set, otherwise a copy is returned. Never null.

toInvertedMap

public static <K,V> Map<K,V> toInvertedMap(Iterable<? extends K> keys,
                                           Functions.Function1<? super K,V> computeValues)
Returns a map for which the Map.values() are computed by the given function, and each key is an element in the given keys. If the iterable contains equal keys more than once, the last one will be contained in the map. The map is computed eagerly. That is, subsequent changes in the keys are not reflected by the map.

Parameters:
keys - the keys to use when constructing the Map. May not be null.
computeValues - the function used to produce the values for each key. May not be null.
Returns:
a map mapping each entry in the given iterable to the corresponding result when evaluating the function computeValues.

toMap

public static <K,V> Map<K,V> toMap(Iterable<? extends V> values,
                                   Functions.Function1<? super V,K> computeKeys)
Returns a map for which the Map.values() are the given elements in the given order, and each key is the product of invoking a supplied function computeKeys on its corresponding value. If the function produces the same key for different values, the last one will be contained in the map.

Parameters:
values - the values to use when constructing the Map. May not be null.
computeKeys - the function used to produce the key for each value. May not be null.
Returns:
a map mapping the result of evaluating the function keyFunction on each value in the input collection to that value

sort

public static <T extends Comparable<? super T>> List<T> sort(Iterable<T> iterable)
Creates a sorted list that contains the items of the given iterable. The resulting list is in ascending order, according to the natural ordering of the elements in the iterable.

Parameters:
iterable - the items to be sorted. May not be null.
Returns:
a sorted list as a shallow copy of the given iterable.
See Also:
Collections.sort(List), sort(Iterable, Comparator), #sortBy(Iterable, Function1), ListExtensions.sortInplace(List)

sort

public static <T> List<T> sort(Iterable<T> iterable,
                               Comparator<? super T> comparator)
Creates a sorted list that contains the items of the given iterable. The resulting list is sorted according to the order induced by the specified comparator.

Parameters:
iterable - the items 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:
a sorted list as a shallow copy of the given iterable.
See Also:
Collections.sort(List, Comparator), sort(Iterable), #sortBy(Iterable, Function1), ListExtensions.sortInplace(List, Comparator)

sortBy

public static <T,C extends Comparable<? super C>> List<T> sortBy(Iterable<T> iterable,
                                                                 Functions.Function1<? super T,C> key)
Creates a sorted list that contains the items of the given iterable. The resulting list is sorted according to the order induced by applying a key function to each element which yields a comparable criteria.

Parameters:
iterable - the elements to be sorted. May not be null.
key - the key function to-be-used. May not be null.
Returns:
a sorted list as a shallow copy of the given iterable.
See Also:
sort(Iterable), sort(Iterable, Comparator), ListExtensions#sortInplaceBy(List, Function1)


Copyright © 2012. All Rights Reserved.