|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.eclipse.xtext.xbase.lib.IterableExtensions
public class IterableExtensions
This is an extension library for iterables
.
Nested Class Summary | |
---|---|
protected static class |
IterableExtensions.BooleanFunctionDelegate<T>
|
protected static class |
IterableExtensions.FunctionDelegate<P,R>
|
Constructor Summary | |
---|---|
IterableExtensions()
|
Method Summary | ||
---|---|---|
static
|
drop(java.lang.Iterable<T> iterable,
int count)
Returns a view on this iterable that provides all elements except the first count entries. |
|
static boolean |
elementsEqual(java.lang.Iterable<?> iterable,
java.lang.Iterable<?> other)
Determines whether two iterables contain equal elements in the same order. |
|
static
|
exists(java.lang.Iterable<T> iterable,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns true if one or more elements in iterable satisfy the predicate. |
|
static
|
filter(java.lang.Iterable<?> unfiltered,
java.lang.Class<T> type)
Returns all instances of class type in unfiltered . |
|
static
|
filter(java.lang.Iterable<T> unfiltered,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns the elements of unfiltered that satisfy a predicate. |
|
static
|
findFirst(java.lang.Iterable<T> iterable,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Finds the first element in the given iterable that fulfills the predicate. |
|
static
|
findLast(java.lang.Iterable<T> iterable,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Finds the last element in the given iterable that fulfills the predicate. |
|
static
|
flatten(java.lang.Iterable<? extends java.lang.Iterable<? extends T>> inputs)
Combines multiple iterables into a single iterable. |
|
static
|
fold(java.lang.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
|
forall(java.lang.Iterable<T> iterable,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns true if every element in iterable satisfies the predicate. |
|
static
|
forEach(java.lang.Iterable<T> iterable,
Functions.Function1<? super T,?> function)
Applies function with each element of the given iterable. |
|
static
|
head(java.lang.Iterable<T> iterable)
Returns the first element in the given iterable or null if empty. |
|
static boolean |
isEmpty(java.lang.Iterable<?> iterable)
Determines if the given iterable contains no elements. |
|
static boolean |
isNullOrEmpty(java.lang.Iterable<?> iterable)
Determines if the given iterable is null or contains no elements. |
|
static java.lang.String |
join(java.lang.Iterable<?> iterable)
Returns the concatenated string representation of the elements in the given iterable. |
|
static java.lang.String |
join(java.lang.Iterable<?> iterable,
java.lang.CharSequence separator)
Returns the concatenated string representation of the elements in the given iterable. |
|
static
|
join(java.lang.Iterable<T> iterable,
java.lang.CharSequence separator,
Functions.Function1<? super T,? extends java.lang.CharSequence> function)
Returns the concatenated string representation of the elements in the given iterable. |
|
static
|
last(java.lang.Iterable<T> iterable)
Returns the last element in the given iterable or null if empty. |
|
static
|
map(java.lang.Iterable<T> original,
Functions.Function1<? super T,R> transformation)
Returns an iterable that performs the given transformation for each element of original when
requested. |
|
static
|
operator_plus(java.lang.Iterable<? extends T> a,
java.lang.Iterable<? extends T> b)
Concatenates two iterables into a single iterable. |
|
static
|
reduce(java.lang.Iterable<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(java.lang.Iterable<?> iterable)
Returns the number of elements in iterable . |
|
static
|
sort(java.lang.Iterable<T> iterable)
Creates a sorted list that contains the items of the given iterable. |
|
static
|
sort(java.lang.Iterable<T> iterable,
java.util.Comparator<? super T> comparator)
Creates a sorted list that contains the items of the given iterable. |
|
static
|
sortBy(java.lang.Iterable<T> iterable,
Functions.Function1<? super T,C> key)
Creates a sorted list that contains the items of the given iterable. |
|
static
|
tail(java.lang.Iterable<T> iterable)
Returns a view on this iterable that contains all the elements except the first. |
|
static
|
take(java.lang.Iterable<T> iterable,
int count)
Returns a view on this iterable that provides at most the first count entries. |
|
static
|
toInvertedMap(java.lang.Iterable<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
|
toList(java.lang.Iterable<T> iterable)
Returns a list that contains all the entries of the given iterable in the same order. |
|
static
|
toMap(java.lang.Iterable<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
|
toSet(java.lang.Iterable<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 |
---|
public IterableExtensions()
Method Detail |
---|
public static <T> java.lang.Iterable<T> operator_plus(java.lang.Iterable<? extends T> a, java.lang.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.
a
- the first iterable. May not be null
.b
- the second iterable. May not be null
.
null
.public static <T> T findFirst(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
null
is returned.
iterable
- the iterable. May not be null
.predicate
- the predicate. May not be null
.
true
, returns
null
if no element matches the predicate or the iterable is empty.public static <T> T findLast(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
null
is returned.
iterable
- the iterable. May not be null
.predicate
- the predicate. May not be null
.
true
, returns
null
if no element matches the predicate or the iterable is empty.public static <T> T head(java.lang.Iterable<T> iterable)
null
if empty.
iterable
- the iterable. May not be null
.
null
.public static <T> java.lang.Iterable<T> tail(java.lang.Iterable<T> iterable)
iterable
- the iterable. May not be null
.
null
.drop(Iterable, int)
public static <T> T last(java.lang.Iterable<T> iterable)
null
if empty.
iterable
- the iterable. May not be null
.
null
.public static <T> java.lang.Iterable<T> take(java.lang.Iterable<T> iterable, int count)
count
entries.
iterable
- the iterable. May not be null
.count
- the number of elements that should be returned at most.
count
elements. Never null
.
java.lang.IllegalArgumentException
- if count
is negative.public static <T> java.lang.Iterable<T> drop(java.lang.Iterable<T> iterable, int count)
count
entries.
iterable
- the iterable. May not be null
.count
- the number of elements that should be dropped.
count
elements. Never null
.
java.lang.IllegalArgumentException
- if count
is negative.public static <T> boolean exists(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
true
if one or more elements in iterable
satisfy the predicate.
iterable
- the iterable. May not be null
.predicate
- the predicate. May not be null
.
true
if one or more elements in iterable
satisfy the predicate.public static <T> boolean forall(java.lang.Iterable<T> iterable, Functions.Function1<? super T,java.lang.Boolean> predicate)
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.
iterable
- the iterable. May not be null
.predicate
- the predicate. May not be null
.
true
if one or more elements in iterable
satisfy the predicate.public static final <T> java.lang.Iterable<T> filter(java.lang.Iterable<T> unfiltered, Functions.Function1<? super T,java.lang.Boolean> predicate)
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.
unfiltered
- the unfiltered iterable. May not be null
.predicate
- the predicate. May not be null
.
null
.public static final <T> java.lang.Iterable<T> filter(java.lang.Iterable<?> unfiltered, java.lang.Class<T> type)
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.
unfiltered
- the unfiltered iterable. May not be null
.type
- the type of elements desired
null
.public static final <T,R> java.lang.Iterable<R> map(java.lang.Iterable<T> original, Functions.Function1<? super T,R> transformation)
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.
original
- the original iterable. May not be null
.transformation
- the transformation. May not be null
.
null
.public static final <T> java.lang.Iterable<T> flatten(java.lang.Iterable<? extends java.lang.Iterable<? extends T>> inputs)
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.
inputs
- the to be flattened iterables. May not be null
.
null
.public static final <T> void forEach(java.lang.Iterable<T> iterable, Functions.Function1<? super T,?> function)
function
with each element of the given iterable. The return value is ignored.
iterable
- the iterable. May not be null
.function
- the function. May not be null
.public static final java.lang.String join(java.lang.Iterable<?> iterable)
iterable
- the iterable. May not be null
.the
- string representation of the iterable's elements. Never null
.#join(Iterable, CharSequence, Function1)
public static final java.lang.String join(java.lang.Iterable<?> iterable, java.lang.CharSequence separator)
separator
is used to between each pair of entries in the input.
The string null
is used for null
entries in the input.
iterable
- the iterable. May not be null
.separator
- the separator. May not be null
.the
- string representation of the iterable's elements. Never null
.#join(Iterable, CharSequence, Function1)
public static final <T> java.lang.String join(java.lang.Iterable<T> iterable, java.lang.CharSequence separator, Functions.Function1<? super T,? extends java.lang.CharSequence> function)
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.
iterable
- the iterable. May not be null
.separator
- the separator. May not be null
.separator
- the separator. May not be null
.the
- string representation of the iterable's elements. Never null
.public static final boolean elementsEqual(java.lang.Iterable<?> iterable, java.lang.Iterable<?> other)
true
if iterable1
and iterable2
contain the same number of elements and every element of
iterable1
is equal to the corresponding element of iterable2
.
iterable
- an iterable. May not be null
.other
- an iterable. May not be null
.
true
if the two iterables contain equal elements in the same order.public static final boolean isNullOrEmpty(java.lang.Iterable<?> iterable)
null
or contains no elements.
iterable
- the to-be-queried iterable. May be null
.
true
if the iterable is null
or contains no elementspublic static final boolean isEmpty(java.lang.Iterable<?> iterable)
iterable
- the to-be-queried iterable. May not be null
.
true
if the iterable contains no elementsisNullOrEmpty(Iterable)
public static final int size(java.lang.Iterable<?> iterable)
iterable
.
iterable
- the iterable. May not be null
.
iterable
.public static <T> T reduce(java.lang.Iterable<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)
iterable
- the to-be-reduced iterable. May not be null
.function
- the combinator function. May not be null
.
null
for the empty input.public static <T,R> R fold(java.lang.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)
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
.
seed
for the empty input.public static <T> java.util.List<T> toList(java.lang.Iterable<T> iterable)
List
, itself is returned. In all other cases, the result list is a copy of the iterable.
iterable
- the iterable. May not be null
.
List
, otherwise a copy is returned. Never null
.public static <T> java.util.Set<T> toSet(java.lang.Iterable<T> iterable)
Set
, itself is returned. In all other cases, the result set is a copy of the iterable with stable order.
iterable
- the iterable. May not be null
.
Set
, otherwise a copy is returned. Never null
.public static <K,V> java.util.Map<K,V> toInvertedMap(java.lang.Iterable<K> keys, Functions.Function1<? super K,V> computeValues)
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.
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
.
computeValues
.public static <K,V> java.util.Map<K,V> toMap(java.lang.Iterable<V> values, Functions.Function1<? super V,K> computeKeys)
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.
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
.
keyFunction
on each value in the input
collection to that valuepublic static <T extends java.lang.Comparable<? super T>> java.util.List<T> sort(java.lang.Iterable<T> iterable)
iterable
- the items to be sorted. May not be null
.
Collections.sort(List)
,
sort(Iterable, Comparator)
,
#sortBy(Iterable, Function1)
,
ListExtensions.sortInplace(List)
public static <T> java.util.List<T> sort(java.lang.Iterable<T> iterable, java.util.Comparator<? super T> comparator)
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.
Collections.sort(List, Comparator)
,
sort(Iterable)
,
#sortBy(Iterable, Function1)
,
ListExtensions.sortInplace(List, Comparator)
public static <T,C extends java.lang.Comparable<? super C>> java.util.List<T> sortBy(java.lang.Iterable<T> iterable, Functions.Function1<? super T,C> key)
iterable
- the elements to be sorted. May not be null
.key
- the key function to-be-used. May not be null
.
sort(Iterable)
,
sort(Iterable, Comparator)
,
ListExtensions#sortInplaceBy(List, Function1)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |