@GwtCompatible
public class IteratorExtensions
extends java.lang.Object
iterators
.Constructor and Description |
---|
IteratorExtensions() |
Modifier and Type | Method and Description |
---|---|
static <T> java.util.Iterator<T> |
drop(java.util.Iterator<T> iterator,
int count)
Returns a view on this iterator that provides all elements except the first
count entries. |
static <T> java.util.Iterator<T> |
dropWhile(java.util.Iterator<? extends T> iterator,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns an Iterator containing all elements starting from the first element for which the drop-predicate returned
false.
|
static boolean |
elementsEqual(java.util.Iterator<?> iterator,
java.lang.Iterable<?> iterable)
Determines whether two iterators contain equal elements in the same order.
|
static boolean |
elementsEqual(java.util.Iterator<?> iterator,
java.util.Iterator<?> other)
Determines whether two iterators contain equal elements in the same order.
|
static <T> boolean |
exists(java.util.Iterator<T> iterator,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns
true if one or more elements in iterator satisfy the predicate. |
static <T> java.util.Iterator<T> |
filter(java.util.Iterator<?> unfiltered,
java.lang.Class<T> type)
Returns all instances of class
type in unfiltered . |
static <T> java.util.Iterator<T> |
filter(java.util.Iterator<T> unfiltered,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns the elements of
unfiltered that satisfy a predicate. |
static <T> java.util.Iterator<T> |
filterNull(java.util.Iterator<T> unfiltered)
Returns a new iterator filtering any null references.
|
static <T> T |
findFirst(java.util.Iterator<T> iterator,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Finds the first element in the given iterator that fulfills the predicate.
|
static <T> T |
findLast(java.util.Iterator<T> iterator,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Finds the last element in the given iterator that fulfills the predicate.
|
static <T,R> R |
fold(java.util.Iterator<T> iterator,
R seed,
Functions.Function2<? super R,? super T,? extends R> function)
Applies the combinator
function to all elements of the iterator in turn and uses seed as the
start value. |
static <T> boolean |
forall(java.util.Iterator<T> iterator,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns
true if every element in iterator satisfies the predicate. |
static <T> void |
forEach(java.util.Iterator<T> iterator,
Procedures.Procedure1<? super T> procedure)
Applies
procedure for each element of the given iterator. |
static <T> void |
forEach(java.util.Iterator<T> iterator,
Procedures.Procedure2<? super T,? super java.lang.Integer> procedure)
Applies
procedure for each element of the given iterator. |
static <K,V> java.util.Map<K,java.util.List<V>> |
groupBy(java.util.Iterator<? extends V> values,
Functions.Function1<? super V,? extends K> computeKeys)
Returns a map for which the
Map.values() is a collection of lists, where the elements in the list will
appear in the order as they appeared in the iterator. |
static <T> T |
head(java.util.Iterator<T> iterator)
Returns the first element in the given iterator or
null if empty. |
static <A> java.util.Iterator<Pair<java.lang.Integer,A>> |
indexed(java.util.Iterator<? extends A> iterator)
Returns an Iterator of Pairs where the nth pair is created by taking the nth element of the source as the value
and its 0-based index as the key.
|
static boolean |
isEmpty(java.util.Iterator<?> iterator)
Determines if the given iterator contains no elements.
|
static boolean |
isNullOrEmpty(java.util.Iterator<?> iterator)
Determines if the given iterator is
null or contains no elements. |
static java.lang.String |
join(java.util.Iterator<?> iterator)
Returns the concatenated string representation of the elements in the given iterator.
|
static java.lang.String |
join(java.util.Iterator<?> iterator,
java.lang.CharSequence separator)
Returns the concatenated string representation of the elements in the given iterator.
|
static <T> java.lang.String |
join(java.util.Iterator<T> iterator,
java.lang.CharSequence before,
java.lang.CharSequence separator,
java.lang.CharSequence after,
Functions.Function1<? super T,? extends java.lang.CharSequence> function)
Returns the concatenated string representation of the elements in the given iterator.
|
static <T> java.lang.String |
join(java.util.Iterator<T> iterator,
java.lang.CharSequence separator,
Functions.Function1<? super T,? extends java.lang.CharSequence> function)
Returns the concatenated string representation of the elements in the given iterator.
|
static <T> T |
last(java.util.Iterator<T> iterator)
Returns the last element in the given iterator or
null if empty. |
static <T,R> java.util.Iterator<R> |
map(java.util.Iterator<T> original,
Functions.Function1<? super T,? extends R> transformation)
Returns an iterator that performs the given
transformation for each element of original when
requested. |
static <T extends java.lang.Comparable<? super T>> |
max(java.util.Iterator<T> iterator)
Finds the maximum of the elements according to their natural ordering.
|
static <T> T |
max(java.util.Iterator<T> iterator,
java.util.Comparator<? super T> comparator)
Finds the maximum element according to
comparator . |
static <T,C extends java.lang.Comparable<? super C>> |
maxBy(java.util.Iterator<T> iterator,
Functions.Function1<? super T,C> compareBy)
Finds the element that yields the maximum value when passed to
compareBy If there are several
maxima, the first one will be returned. |
static <T extends java.lang.Comparable<? super T>> |
min(java.util.Iterator<T> iterator)
Finds the minimum of the given elements according to their natural ordering.
|
static <T> T |
min(java.util.Iterator<T> iterator,
java.util.Comparator<? super T> comparator)
Finds the mininmum element according to
comparator . |
static <T,C extends java.lang.Comparable<? super C>> |
minBy(java.util.Iterator<T> iterator,
Functions.Function1<? super T,C> compareBy)
Finds the element that yields the minimum value when passed to
compareBy . |
static <T> java.util.Iterator<T> |
operator_plus(java.util.Iterator<? extends T> a,
java.util.Iterator<? extends T> b)
Concatenates two iterators into a single iterator.
|
static <T> T |
reduce(java.util.Iterator<? extends T> iterator,
Functions.Function2<? super T,? super T,? extends T> function)
Applies the combinator
function to all elements of the iterator in turn. |
static int |
size(java.util.Iterator<?> iterator)
Returns the number of elements in
iterator . |
static <T> java.util.Iterator<T> |
tail(java.util.Iterator<T> iterator)
Returns a view on this iterator that contains all the elements except the first.
|
static <T> java.util.Iterator<T> |
take(java.util.Iterator<T> iterator,
int count)
Returns a view on this iterator that provides at most the first
count entries. |
static <T> java.util.Iterator<T> |
takeWhile(java.util.Iterator<? extends T> iterator,
Functions.Function1<? super T,java.lang.Boolean> predicate)
Returns an Iterator containing all elements starting from the head of the source up to and excluding the first
element that violates the predicate.
|
static <K,V> java.util.Map<K,V> |
toInvertedMap(java.util.Iterator<? 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> java.lang.Iterable<T> |
toIterable(java.util.Iterator<T> iterator)
Wraps an
Iterator in an Iterable . |
static <T> java.util.List<T> |
toList(java.util.Iterator<? extends T> iterator)
Returns a list that contains all the entries of the given iterator in the same order.
|
static <K,V> java.util.Map<K,V> |
toMap(java.util.Iterator<? 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> java.util.Set<T> |
toSet(java.util.Iterator<? extends T> iterator)
Returns a set that contains all the unique entries of the given iterator in the order of their appearance.
|
@Pure public static <T> java.lang.Iterable<T> toIterable(java.util.Iterator<T> iterator)
Iterator
in an Iterable
.iterator
- the Iterator
to wrap in an Iterable
. May not be null
.Iterable
providing the given Iterator
. Never null
.@Pure public static <T> java.util.Iterator<T> operator_plus(java.util.Iterator<? extends T> a, java.util.Iterator<? extends T> b)
Concatenates two iterators into a single iterator. The returned iterator traverses the
elements in a
, followed by the elements in b
. The resulting iterator is effectivly a view on the
source iterators. That is, the source iterators are not polled until necessary and the result will reflect
changes in the sources.
The returned iterator supports remove()
when the corresponding input iterator supports it.
a
- the first iterator. May not be null
.b
- the second iterator. May not be null
.null
.public static <T> T findFirst(java.util.Iterator<T> iterator, Functions.Function1<? super T,java.lang.Boolean> predicate)
null
is returned.iterator
- the iterator. May not be null
.predicate
- the predicate. May not be null
.true
, returns
null
if no element matches the predicate or the iterator is empty.public static <T> T findLast(java.util.Iterator<T> iterator, Functions.Function1<? super T,java.lang.Boolean> predicate)
null
is returned.iterator
- the iterator. May not be null
.predicate
- the predicate. May not be null
.true
, returns
null
if no element matches the predicate or the iterator is empty.public static <T> T head(java.util.Iterator<T> iterator)
null
if empty.iterator
- the iterator. May not be null
.null
.public static <T> java.util.Iterator<T> tail(java.util.Iterator<T> iterator)
iterator
- the iterator. May not be null
.null
.drop(Iterator, int)
public static <T> T last(java.util.Iterator<T> iterator)
null
if empty.iterator
- the iterator. May not be null
.null
.@Pure public static <T> java.util.Iterator<T> take(java.util.Iterator<T> iterator, int count)
count
entries.iterator
- the iterator. 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.util.Iterator<T> drop(java.util.Iterator<T> iterator, int count)
count
entries.iterator
- the iterator. 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.util.Iterator<T> iterator, Functions.Function1<? super T,java.lang.Boolean> predicate)
true
if one or more elements in iterator
satisfy the predicate.iterator
- the iterator. May not be null
.predicate
- the predicate. May not be null
.true
if one or more elements in iterator
satisfy the predicate.public static <T> boolean forall(java.util.Iterator<T> iterator, Functions.Function1<? super T,java.lang.Boolean> predicate)
true
if every element in iterator
satisfies the predicate. If iterator
is empty,
true
is returned. In other words, false
is returned if at least one element fails to fulfill
the predicate.iterator
- the iterator. May not be null
.predicate
- the predicate. May not be null
.true
if one or more elements in iterator
satisfy the predicate.@Pure public static <T> java.util.Iterator<T> filter(java.util.Iterator<T> unfiltered, Functions.Function1<? super T,java.lang.Boolean> predicate)
unfiltered
that satisfy a predicate. The resulting iterator does not
support remove()
. The returned iterator is a view on the original elements. Changes in the unfiltered
original are reflected in the view.unfiltered
- the unfiltered iterator. May not be null
.predicate
- the predicate. May not be null
.null
.@Pure @GwtIncompatible(value="Class.isInstance") public static <T> java.util.Iterator<T> filter(java.util.Iterator<?> unfiltered, java.lang.Class<T> type)
type
in unfiltered
. The returned iterator has elements whose class
is type
or a subclass of type
. The returned iterator does not support remove()
.
The returned iterator is a view on the original elements. Changes in the unfiltered original are reflected in
the view.unfiltered
- the unfiltered iterator. May not be null
.type
- the type of elements desirednull
.@Pure public static <T> java.util.Iterator<T> filterNull(java.util.Iterator<T> unfiltered)
unfiltered
- the unfiltered iterator. May not be null
.null
references. Never null
.@Pure public static <T,R> java.util.Iterator<R> map(java.util.Iterator<T> original, Functions.Function1<? super T,? extends R> transformation)
transformation
for each element of original
when
requested. The mapping is done lazily.
The returned iterator's iterator supports remove()
if the provided iterator does. After a successful
remove()
call, original
no longer contains the corresponding element.original
- the original iterator. May not be null
.transformation
- the transformation. May not be null
.null
.public static <T> void forEach(java.util.Iterator<T> iterator, Procedures.Procedure1<? super T> procedure)
procedure
for each element of the given iterator.iterator
- the iterator. May not be null
.procedure
- the procedure. May not be null
.public static <T> void forEach(java.util.Iterator<T> iterator, Procedures.Procedure2<? super T,? super java.lang.Integer> procedure)
procedure
for each element of the given iterator.
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.iterator
- the iterator. May not be null
.procedure
- the procedure. May not be null
.public static java.lang.String join(java.util.Iterator<?> iterator)
iterator
- the iterator. May not be null
.null
.#join(Iterator, CharSequence, Function1)
public static java.lang.String join(java.util.Iterator<?> iterator, 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. The given iterator is left exhausted.iterator
- the iterator. May not be null
.separator
- the separator. May not be null
.null
.#join(Iterator, CharSequence, Function1)
public static <T> java.lang.String join(java.util.Iterator<T> iterator, 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. The given iterator is left exhausted.iterator
- the iterator. 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
.null
.public static <T> java.lang.String join(java.util.Iterator<T> iterator, java.lang.CharSequence before, java.lang.CharSequence separator, java.lang.CharSequence after, 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. The given iterator is left exhausted.iterator
- the iterator. May not be null
.before
- prepends the resulting string if the iterator 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 iterator 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
.null
.public static boolean elementsEqual(java.util.Iterator<?> iterator, java.util.Iterator<?> other)
true
if iterator
and other
contain the same number of elements and every element of
iterator
is equal to the corresponding element of other
.iterator
- an iterator. May not be null
.other
- an iterator. May not be null
.true
if the two iterators contain equal elements in the same order.public static boolean elementsEqual(java.util.Iterator<?> iterator, java.lang.Iterable<?> iterable)
true
if iterator
and iterable
contain the same number of elements and every element of
iterator
is equal to the corresponding element of iterable
.iterator
- an iterator. May not be null
.iterable
- an iterable. May not be null
.true
if the two iterators contain equal elements in the same order.public static boolean isNullOrEmpty(java.util.Iterator<?> iterator)
null
or contains no elements.iterator
- the to-be-queried iterator. May be null
.true
if the iterator is null
or contains no elementspublic static boolean isEmpty(java.util.Iterator<?> iterator)
iterator
- the to-be-queried iterator. May not be null
.true
if the iterator contains no elementsisNullOrEmpty(Iterator)
public static int size(java.util.Iterator<?> iterator)
iterator
.
The given iterator is left exhausted.iterator
- the iterator. May not be null
.iterator
.public static <T> T reduce(java.util.Iterator<? extends T> iterator, Functions.Function2<? super T,? super T,? extends T> function)
Applies the combinator function
to all elements of the iterator in turn.
One of the function parameters is an element of the iterator, and the other is the result of previous application of the function. The seed of the operation is the first element in the iterator. The second value is computed by applying the function to the seed together with the second element of the iterator. 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 iterator is empty, null
is returned.
More formally, given an iterator [a, b, c, d]
and a function f
, the result of reduce
is
f(f(f(a, b), c), d)
iterator
- the to-be-reduced iterator. May not be null
.function
- the combinator function. May not be null
.null
for the empty input.public static <T,R> R fold(java.util.Iterator<T> iterator, R seed, Functions.Function2<? super R,? super T,? extends R> function)
Applies the combinator function
to all elements of the iterator in turn and uses seed
as the
start value.
One of the function parameters is an element of the iterator, 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 iterator. This intermediate result together with the second element of the iterator 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 iterator is empty, seed
is returned.
More formally, given an iterator [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)
iterator
- the to-be-folded iterator. 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.util.Iterator<? extends T> iterator)
iterator
- the iterator. May not be null
.null
.public static <T> java.util.Set<T> toSet(java.util.Iterator<? extends T> iterator)
iterator
- the iterator. May not be null
.null
.public static <K,V> java.util.Map<K,V> toInvertedMap(java.util.Iterator<? extends 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 iterator 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.
The key iterator is left exhausted.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.util.Iterator<? extends 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. The value iterator is left exhausted.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
iterator to that valuepublic static <K,V> java.util.Map<K,java.util.List<V>> groupBy(java.util.Iterator<? extends V> values, Functions.Function1<? super V,? extends K> computeKeys)
Map.values()
is a collection of lists, where the elements in the list will
appear in the order as they appeared in the iterator. Each key is the product of invoking the supplied
function computeKeys
on its corresponding value. So a key of that map groups a list of values for
which the function produced exactly that key. The value iterator is left exhausted.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
iterator to that value. As there can be more than one value mapped by a key, the mapping result is is a
list of values.public static <T> java.util.Iterator<T> takeWhile(java.util.Iterator<? extends T> iterator, Functions.Function1<? super T,java.lang.Boolean> predicate)
Iterator.remove()
iterator
- the elements from which to take. May not be null
.predicate
- the predicate which decides whether to keep taking elements. May not be null
.public static <T> java.util.Iterator<T> dropWhile(java.util.Iterator<? extends T> iterator, Functions.Function1<? super T,java.lang.Boolean> predicate)
Iterator.remove()
iterator
- the elements from which to drop. May not be null
.predicate
- the predicate which decides whether to keep dropping elements. May not be null
.public static <A> java.util.Iterator<Pair<java.lang.Integer,A>> indexed(java.util.Iterator<? extends A> iterator)
zipWitIndex(#["a", "b", "c"]) == #[(0, "a"), (1, "b"), (2, "c")]
If the index would overflow, Integer.MAX_VALUE
is returned for all subsequent elements.
The resulting Iterator is a lazily computed view, so any modifications to the underlying Iterator will be
reflected on iteration. The result does not support Iterator.remove()
iterator
- the elements. May not be null
.public static <T extends java.lang.Comparable<? super T>> T min(java.util.Iterator<T> iterator)
iterator
- the mutually comparable elements. May not be null
.java.util.NoSuchElementException
- if the iterator is emptypublic static <T,C extends java.lang.Comparable<? super C>> T minBy(java.util.Iterator<T> iterator, Functions.Function1<? super T,C> compareBy)
compareBy
. If there are several
maxima, the first one will be returned.iterator
- the elements to find the minimum of. May not be null
.compareBy
- a function that returns a comparable characteristic to compare the elements by. May not be null
.java.util.NoSuchElementException
- if the iterator is emptypublic static <T> T min(java.util.Iterator<T> iterator, java.util.Comparator<? super T> comparator)
comparator
. If there are several minima, the first one will
be returned.iterator
- the elements to find the minimum of. May not be null
.comparator
- the comparison function. May not be null
.java.util.NoSuchElementException
- if the iterator is emptypublic static <T extends java.lang.Comparable<? super T>> T max(java.util.Iterator<T> iterator)
iterator
- the mutually comparable elements. May not be null
.java.util.NoSuchElementException
- if the iterator is emptypublic static <T,C extends java.lang.Comparable<? super C>> T maxBy(java.util.Iterator<T> iterator, Functions.Function1<? super T,C> compareBy)
compareBy
If there are several
maxima, the first one will be returned.iterator
- the elements to find the maximum of. May not be null
.compareBy
- a function that returns a comparable characteristic to compare the elements by. May not be null
.java.util.NoSuchElementException
- if the iterator is emptypublic static <T> T max(java.util.Iterator<T> iterator, java.util.Comparator<? super T> comparator)
comparator
. If there are several maxima, the first one will
be returned.iterator
- the elements to find the maximum of. May not be null
.comparator
- the comparison function. May not be null
.java.util.NoSuchElementException
- if the iterator is empty