| java.lang.Object net.sf.jga.util.Iterators
Iterators | public class Iterators (Code) | | Facade for the Algorithms adapted from STL, defined to work primarily with
iterators. These algorithms are adapted from STL, with modifications to be
consistent with typical java practice. For example, typical STL algorithms
are defined with pairs of iterators defining a half-open range over some
implied collection. It works in C++ because the STL iterators can be
compared for equality. Java iterators are not guaranteed to be comparable
to each other by contract, so the same signatures wouldn't work.
Typically, where an STL algorithm would take a pair of iterators, this facade
will take a single iterator and where an STL algorithm would return an
iterator, we'll return an iterator. In this facade, the iterator returned
will frequently be specialized in some way: either a ListIterator or one of
the iterators defined in jga.
It is best to assume that all of the methods in this class may (but are not
guaranteed to) advance the iterator argument. Also, once an iterator has
been passed to one of the methods of this class, it should not be used
again. On the other hand, unless otherwise noted, it is safe to pass the
iterator returned by one of the methods in this class back to the method
that returned it, or to any of the other methods.
Copyright © 2003-2005 David A. Hall
author: David A. Hall |
Method Summary | |
public static T | accumulate(Class<T> numtype, Iterator<T> iterator) Adds each number in the iterator, returning the sum.
the final sum. | public static T | accumulate(Class<T> numtype, Iterator<T> iterator, BinaryFunctor<T, T, T> bf) Applies the binary functor to each number in the iterator, returning
the final result. | public static T | accumulate(Iterator<T> iterator, T initial, BinaryFunctor<T, T, T> bf) Applies the binary functor to each element in the iterator, returning
the final result. | public static TransformAdjacentIterator<T, T> | adjacentDiff(Class<T> type, Iterator<? extends T> iter) | public static long | count(Iterator<? extends T> iterator, T value) Counts the number of occurrences of value in the iteration,
using the equals() method of the value. | public static long | count(Iterator<? extends T> iterator, Equality<T> eq, T value) Counts the number of occurrences of value in the iteration, using
the given equality operator. | public static long | count(Iterator<? extends T> iterator, UnaryFunctor<T, Boolean> eq) Counts the items in the collection for which the given function returns
TRUE. | public static boolean | equal(Iterator<? extends T> iterator1, Iterator<? extends T> iterator2) Returns true if the two iterations are equal, using the Comparable
interface to compare elements in the iterations. | public static boolean | equal(Iterator<? extends T> iterator1, Iterator<? extends T> iterator2, Comparator<T> comp) Returns true if the two iterations are equal, using the given Comparator
to compare elements in the iterations. | public static boolean | equal(Iterator<? extends T> iter1, Iterator<? extends T> iter2, BinaryFunctor<T, T, Boolean> eq) Returns true if the two iterations are equal, using the given
BinaryFunctor to compare elements in the iterations. | public static FindIterator<T> | find(Iterator<? extends T> iterator, T value) Finds an arbitrary value in an iteration using the equals() method.
an iterator based on the given iterator whose next() [if ithasNext()] will return the next instance of value in the iteration,using the equals() method of the value. | public static FindIterator<T> | find(Iterator<? extends T> iterator, T value, Equality<T> eq) Finds an arbitrary value in an iteration using the given Equality
operator.
an iterator based on the given iterator whose next() [if ithasNext()] will return the next instance of value in the iteration, usingthe given equality operator. | public static FindIterator<T> | find(Iterator<? extends T> iterator, UnaryFunctor<T, Boolean> fn) Finds a value in a collection for which the given function returns TRUE.
an iterator based on the given iterator whose next() [if ithasNext()] will return the next instance in the iteration for which thegiven function returns true. | public static LookAheadIterator<T> | findAdjacent(Iterator<? extends T> iterator) Finds adjacent pairs of equivalent values in an iteration using the
equals() method.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first of a pair of adjacent values. | public static LookAheadIterator<T> | findAdjacent(Iterator<? extends T> iterator, BinaryFunctor<T, T, Boolean> bf) Finds adjacent pairs of equivalent values in an iteration for which the
given function returns TRUE.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first of a pair of adjacent values. | public static FindIterator<T> | findElement(Iterator<? extends T> iterator, Collection<? extends T> desired) Finds any value from the given collection using the collection's contains() method.
an iterator based on the given iterator whose next() [if it hasNext()] will returnthe first instance of any value found in the second collection. | public static FindIterator<T> | findElement(Iterator<? extends T> i, Collection<? extends T> c, BinaryFunctor<T, T, Boolean> eq) Finds any value from the given collection using the given functor to determine equivalence.
Each item in the iteration will be compared to every item in the second collection using
the given functor, stopping when the iteration is exhausted or when any pair returns TRUE.
an iterator based on the given iterator whose next() [if it hasNext()] will returnthe first instance of any value in the second collection, where equivelency is determinedby the given functor. | public static LookAheadIterator<T> | findRepeated(Iterator<? extends T> iterator, int n, T value) Finds arbitrary length runs of a given value in an iteration using the
equals() method. | public static LookAheadIterator<T> | findRepeated(Iterator<? extends T> iterator, int n, T value, Equality<T> eq) Finds arbitrary length runs of a given value in an iteration using the
given equality operator. | public static LookAheadIterator<T> | findRepeated(Iterator<? extends T> iterator, int n, UnaryFunctor<T, Boolean> eq) Finds arbitrary length runs of a given value in an iteration for which the
given function returns TRUE. | public static UnaryFunctor<T, R> | forEach(Iterator<? extends T> iterator, UnaryFunctor<T, R> fn) Applies the given UnaryFunctor to every element in the iteration, and
returns the Functor. | public static boolean | lessThan(Iterator<? extends T> iter1, Iterator<? extends T> iter2) Returns true if the first iterator is lexically less than the second,
using the default comparison operation to compare the elements in each
iterator. | public static boolean | lessThan(Iterator<? extends T> iter1, Iterator<? extends T> iter2, Comparator<T> comp) Returns true if the first iterator is lexically less than the second,
using the given comparator to compare the elements in each iterator. | public static boolean | lessThan(Iterator<? extends T> i1, Iterator<? extends T> i2, BinaryFunctor<T, T, Boolean> lt) Returns true if the first iterator is lexically less than the second,
using the given operator to compare the elements in each iterator. | public static LookAheadIterator<T> | match(Iterator<? extends T> iterator, Collection<? extends T> pattern) Finds the given pattern in the iteration using the equals method.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first element of a sequence that matchesthe entire contents of the collection. | public static LookAheadIterator<T> | match(Iterator<? extends T> i, Collection<? extends T> pattern, BinaryFunctor<T, T, Boolean> eq) Finds the given pattern in the collection using the given functor
to determine equivalence.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first element of a sequence that matchesthe entire contents of the collection. | public static T | maximumValue(Iterator<? extends T> iterator) Finds the maximum value in an iteration using the natural ordering of
the iterator's elements. | public static T | maximumValue(Iterator<? extends T> iterator, Comparator<T> comp) Finds the maximum value in an iteration using the given comparator. | public static T | maximumValue(Iterator<? extends T> iterator, BinaryFunctor<T, T, T> bf) Finds the maximum value in an iteration using the given functor to
compare elements. | public static MergeIterator<T> | merge(Iterator<? extends T> iter1, Iterator<? extends T> iter2) Merges two iterations together. | public static MergeIterator<T> | merge(Iterator<? extends T> iter1, Iterator<? extends T> iter2, Comparator<T> comp) Merges two iterations together using the given comparator. | public static T | minimumValue(Iterator<? extends T> iterator) Finds the minimum value in an iteration using the natural ordering of
the iterator's elements. | public static T | minimumValue(Iterator<? extends T> iterator, Comparator<T> comp) Finds the minimum value in an iteration using the given comparator. | public static T | minimumValue(Iterator<? extends T> iterator, BinaryFunctor<T, T, T> bf) Finds the minimum value in an iteration using the given functor to
compare elements. | public static LookAheadIterator<T> | mismatch(Iterator<? extends T> iterator, Collection<? extends T> pattern) Finds the point at which two collections differ, using NotEqualTo.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first element in the iteration that doesnot equal the corresponding element in the pattern. | public static LookAheadIterator<T> | mismatch(Iterator<? extends T> iterator, Collection<? extends T> pattern, BinaryFunctor<T, T, Boolean> neq) Finds the point at which two collections differ, using the given functor
an iterator based on the given iterator whose next() [if ithasNext()] will return the first element in the iteration for which thegiven function returns TRUE when given the element and the correspondingelement in the pattern. | public static FilterIterator<T> | removeAll(Iterator<? extends T> iterator, T value) Filters an arbitrary value from an iteration using the equals() method. | public static FilterIterator<T> | removeAll(Iterator<? extends T> iterator, T value, Equality<T> eq) Filters an arbitrary value from an iteration using the given Equality
operator. | public static FilterIterator<T> | removeAll(Iterator<? extends T> iterator, UnaryFunctor<T, Boolean> eq) Filters values from an iteration for which the given function returns
TRUE. | public static Iterator<T> | replaceAll(Iterator<? extends T> iter, UnaryFunctor<T, Boolean> test, T value) Tests each element in an iterator, replacing those for which the test is
true with the replacement value. | public static TransformIterator<T, R> | transform(Iterator<? extends T> iter, UnaryFunctor<T, R> uf) Applies the UnaryFunctor to each element in the input, returning an
iterator over the results. | public static TransformBinaryIterator<T1, T2, R> | transform(Iterator<? extends T1> i1, Iterator<? extends T2> i2, BinaryFunctor<T1, T2, R> bf) Applies the BinaryFunctor to corresponding elements of the two input
iterators, and returns an iterator over the results. | public static UniqueIterator<T> | unique(Iterator<? extends T> iterator) Skips duplicate values in the given iteration. | public static UniqueIterator<T> | unique(Iterator<? extends T> iterator, BinaryFunctor<T, T, Boolean> eq) Skips duplicate values in the given iteration. |
accumulate | public static T accumulate(Class<T> numtype, Iterator<T> iterator)(Code) | | Adds each number in the iterator, returning the sum.
the final sum. If the iterator is empty, then zero isreturned |
accumulate | public static T accumulate(Class<T> numtype, Iterator<T> iterator, BinaryFunctor<T, T, T> bf)(Code) | | Applies the binary functor to each number in the iterator, returning
the final result. Along with each number is passed the result of the
previous call of the functor (or zero for the first call to the functor).
The elements in the iterator are always passed in the 2nd postion.
the final result. If the iterator is empty, then zero isreturned |
accumulate | public static T accumulate(Iterator<T> iterator, T initial, BinaryFunctor<T, T, T> bf)(Code) | | Applies the binary functor to each element in the iterator, returning
the final result. Along with each element is passed the result of the
previous call of the functor (or the initial value for the first call
to the functor). The elements in the iteration are always passed in the
2nd postion.
the final result. If the iteration is empty, then the initialvalue is returned |
count | public static long count(Iterator<? extends T> iterator, T value)(Code) | | Counts the number of occurrences of value in the iteration,
using the equals() method of the value.
the number of instances found |
count | public static long count(Iterator<? extends T> iterator, Equality<T> eq, T value)(Code) | | Counts the number of occurrences of value in the iteration, using
the given equality operator.
the number of instances found |
count | public static long count(Iterator<? extends T> iterator, UnaryFunctor<T, Boolean> eq)(Code) | | Counts the items in the collection for which the given function returns
TRUE.
the number of instances found |
equal | public static boolean equal(Iterator<? extends T> iterator1, Iterator<? extends T> iterator2)(Code) | | Returns true if the two iterations are equal, using the Comparable
interface to compare elements in the iterations.
true if the two iterations are equal |
equal | public static boolean equal(Iterator<? extends T> iterator1, Iterator<? extends T> iterator2, Comparator<T> comp)(Code) | | Returns true if the two iterations are equal, using the given Comparator
to compare elements in the iterations.
true if the two iterations are equal |
equal | public static boolean equal(Iterator<? extends T> iter1, Iterator<? extends T> iter2, BinaryFunctor<T, T, Boolean> eq)(Code) | | Returns true if the two iterations are equal, using the given
BinaryFunctor to compare elements in the iterations.
true if the two iterations are equal |
find | public static FindIterator<T> find(Iterator<? extends T> iterator, T value)(Code) | | Finds an arbitrary value in an iteration using the equals() method.
an iterator based on the given iterator whose next() [if ithasNext()] will return the next instance of value in the iteration,using the equals() method of the value. If the value is not in theiteration, then the returned iterator's hasNext() will report false. |
find | public static FindIterator<T> find(Iterator<? extends T> iterator, T value, Equality<T> eq)(Code) | | Finds an arbitrary value in an iteration using the given Equality
operator.
an iterator based on the given iterator whose next() [if ithasNext()] will return the next instance of value in the iteration, usingthe given equality operator. If the value is not in theiteration, then the returned iterator's hasNext() will report false. |
find | public static FindIterator<T> find(Iterator<? extends T> iterator, UnaryFunctor<T, Boolean> fn)(Code) | | Finds a value in a collection for which the given function returns TRUE.
an iterator based on the given iterator whose next() [if ithasNext()] will return the next instance in the iteration for which thegiven function returns true. If the value is not in theiteration, then the returned iterator's hasNext() will report false. |
findAdjacent | public static LookAheadIterator<T> findAdjacent(Iterator<? extends T> iterator)(Code) | | Finds adjacent pairs of equivalent values in an iteration using the
equals() method.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first of a pair of adjacent values. If nopair of values exists in the iteration, then the returned iterator'shasNext() will report false. |
findAdjacent | public static LookAheadIterator<T> findAdjacent(Iterator<? extends T> iterator, BinaryFunctor<T, T, Boolean> bf)(Code) | | Finds adjacent pairs of equivalent values in an iteration for which the
given function returns TRUE.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first of a pair of adjacent values. If nopair of values exists in the iteration, then the returned iterator'shasNext() will report false. |
findElement | public static FindIterator<T> findElement(Iterator<? extends T> iterator, Collection<? extends T> desired)(Code) | | Finds any value from the given collection using the collection's contains() method.
an iterator based on the given iterator whose next() [if it hasNext()] will returnthe first instance of any value found in the second collection. If no such value is foundin the iteration, then the returned iterator's hasNext() will report false. |
findElement | public static FindIterator<T> findElement(Iterator<? extends T> i, Collection<? extends T> c, BinaryFunctor<T, T, Boolean> eq)(Code) | | Finds any value from the given collection using the given functor to determine equivalence.
Each item in the iteration will be compared to every item in the second collection using
the given functor, stopping when the iteration is exhausted or when any pair returns TRUE.
an iterator based on the given iterator whose next() [if it hasNext()] will returnthe first instance of any value in the second collection, where equivelency is determinedby the given functor. If no such value is found in the iteration, then the returned iterator'shasNext() will report false. |
findRepeated | public static LookAheadIterator<T> findRepeated(Iterator<? extends T> iterator, int n, T value)(Code) | | Finds arbitrary length runs of a given value in an iteration using the
equals() method. Runs of length zero are well-defined: every iteration
begins with a run of length zero of all possible values.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first of n adjacent instances of value. If norun of values of the requested length exist in the iteration, then thereturned iterator's hasNext() will report false. |
findRepeated | public static LookAheadIterator<T> findRepeated(Iterator<? extends T> iterator, int n, T value, Equality<T> eq)(Code) | | Finds arbitrary length runs of a given value in an iteration using the
given equality operator. Runs of length zero are well-defined: every
iteration begins with a run of length zero of all possible values.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first of n adjacent instances of value. If norun of values of the requested length exist in the iteration, then thereturned iterator's hasNext() will report false. |
findRepeated | public static LookAheadIterator<T> findRepeated(Iterator<? extends T> iterator, int n, UnaryFunctor<T, Boolean> eq)(Code) | | Finds arbitrary length runs of a given value in an iteration for which the
given function returns TRUE. Runs of length zero are well-defined: every
iteration begins with a run of length zero of all possible values.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first of n adjacent instances of value. If norun of values of the requested length exist in the iteration, then thereturned iterator's hasNext() will report false. |
forEach | public static UnaryFunctor<T, R> forEach(Iterator<? extends T> iterator, UnaryFunctor<T, R> fn)(Code) | | Applies the given UnaryFunctor to every element in the iteration, and
returns the Functor. This is useful when the Functor gathers information
on each successive call.
the functor, after it has been called once for every element |
lessThan | public static boolean lessThan(Iterator<? extends T> iter1, Iterator<? extends T> iter2)(Code) | | Returns true if the first iterator is lexically less than the second,
using the default comparison operation to compare the elements in each
iterator.
true if the first iteration is less than the second |
lessThan | public static boolean lessThan(Iterator<? extends T> iter1, Iterator<? extends T> iter2, Comparator<T> comp)(Code) | | Returns true if the first iterator is lexically less than the second,
using the given comparator to compare the elements in each iterator.
true if the first iteration is less than the second |
lessThan | public static boolean lessThan(Iterator<? extends T> i1, Iterator<? extends T> i2, BinaryFunctor<T, T, Boolean> lt)(Code) | | Returns true if the first iterator is lexically less than the second,
using the given operator to compare the elements in each iterator. The
first is less than the second if it is not longer than the second and if
the first corresponding element that is not equal is less.
true if the first iteration is less than the second |
match | public static LookAheadIterator<T> match(Iterator<? extends T> iterator, Collection<? extends T> pattern)(Code) | | Finds the given pattern in the iteration using the equals method.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first element of a sequence that matchesthe entire contents of the collection. If no such match isfound in the iteration, then the returned iterator's hasNext()will report false. If the pattern is empty, then the iterator will notbe advanced. |
match | public static LookAheadIterator<T> match(Iterator<? extends T> i, Collection<? extends T> pattern, BinaryFunctor<T, T, Boolean> eq)(Code) | | Finds the given pattern in the collection using the given functor
to determine equivalence.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first element of a sequence that matchesthe entire contents of the collection. If no such match isfound in the iteration, then the returned iterator's hasNext()will report false. If the pattern is empty, then the iterator will notbe advanced. |
maximumValue | public static T maximumValue(Iterator<? extends T> iterator)(Code) | | Finds the maximum value in an iteration using the natural ordering of
the iterator's elements.
the maximum value found in the iteration |
maximumValue | public static T maximumValue(Iterator<? extends T> iterator, Comparator<T> comp)(Code) | | Finds the maximum value in an iteration using the given comparator.
the maximum value found in the iteration |
maximumValue | public static T maximumValue(Iterator<? extends T> iterator, BinaryFunctor<T, T, T> bf)(Code) | | Finds the maximum value in an iteration using the given functor to
compare elements. The functor is presumed to return the lesser of
its two arguments.
the maximum value found in the iteration |
merge | public static MergeIterator<T> merge(Iterator<? extends T> iter1, Iterator<? extends T> iter2)(Code) | | Merges two iterations together. Walks both iterators, choosing the
lesser of the two current values.
an iterator based on the two input iterators that contains theirmerged contents |
merge | public static MergeIterator<T> merge(Iterator<? extends T> iter1, Iterator<? extends T> iter2, Comparator<T> comp)(Code) | | Merges two iterations together using the given comparator. Walks both
iterators, choosing the lesser of the two current values.
an iterator based on the two input iterators that contains theirmerged contents |
minimumValue | public static T minimumValue(Iterator<? extends T> iterator)(Code) | | Finds the minimum value in an iteration using the natural ordering of
the iterator's elements.
the minimum value found in the iteration |
minimumValue | public static T minimumValue(Iterator<? extends T> iterator, Comparator<T> comp)(Code) | | Finds the minimum value in an iteration using the given comparator.
the minimum value found in the iteration |
minimumValue | public static T minimumValue(Iterator<? extends T> iterator, BinaryFunctor<T, T, T> bf)(Code) | | Finds the minimum value in an iteration using the given functor to
compare elements. The functor is presumed to return the lesser of
its two arguments.
the minimum value found in the iteration |
mismatch | public static LookAheadIterator<T> mismatch(Iterator<? extends T> iterator, Collection<? extends T> pattern)(Code) | | Finds the point at which two collections differ, using NotEqualTo.
an iterator based on the given iterator whose next() [if ithasNext()] will return the first element in the iteration that doesnot equal the corresponding element in the pattern. If the patternmatches the iteration but is longer, than the returned iterator'shasNext() will report false. If the pattern is empty, then theiteration is not advanced. |
mismatch | public static LookAheadIterator<T> mismatch(Iterator<? extends T> iterator, Collection<? extends T> pattern, BinaryFunctor<T, T, Boolean> neq)(Code) | | Finds the point at which two collections differ, using the given functor
an iterator based on the given iterator whose next() [if ithasNext()] will return the first element in the iteration for which thegiven function returns TRUE when given the element and the correspondingelement in the pattern. If the pattern matches the iteration but islonger, than the returned iterator's hasNext() will report false. If thepattern is empty, then the iteration is not advanced. |
removeAll | public static FilterIterator<T> removeAll(Iterator<? extends T> iterator, T value)(Code) | | Filters an arbitrary value from an iteration using the equals() method.
an iterator based on the given iterator that will return notinclude elements equal to the given value |
removeAll | public static FilterIterator<T> removeAll(Iterator<? extends T> iterator, T value, Equality<T> eq)(Code) | | Filters an arbitrary value from an iteration using the given Equality
operator.
an iterator based on the given iterator that will not includeelements that are equal to the value using the given Equality |
removeAll | public static FilterIterator<T> removeAll(Iterator<? extends T> iterator, UnaryFunctor<T, Boolean> eq)(Code) | | Filters values from an iteration for which the given function returns
TRUE.
an iterator based on the given iterator that will not includeelements that pass the given test. |
replaceAll | public static Iterator<T> replaceAll(Iterator<? extends T> iter, UnaryFunctor<T, Boolean> test, T value)(Code) | | Tests each element in an iterator, replacing those for which the test is
true with the replacement value.
|
transform | public static TransformIterator<T, R> transform(Iterator<? extends T> iter, UnaryFunctor<T, R> uf)(Code) | | Applies the UnaryFunctor to each element in the input, returning an
iterator over the results.
an iterator based on the given iterator that will return theresults obtained when passing each element of the input iteration to thegiven unary functor. |
transform | public static TransformBinaryIterator<T1, T2, R> transform(Iterator<? extends T1> i1, Iterator<? extends T2> i2, BinaryFunctor<T1, T2, R> bf)(Code) | | Applies the BinaryFunctor to corresponding elements of the two input
iterators, and returns an iterator over the results. The resulting
iterator will have the same number of elements as the shorter of the two
input iterations.
an iterator that will return the results obtained when passingeach pair of corresponding elements of the input iterations to thegiven binary functor. |
unique | public static UniqueIterator<T> unique(Iterator<? extends T> iterator)(Code) | | Skips duplicate values in the given iteration.
an iterator based on the given iterator that will not return thesame element twice in succession |
unique | public static UniqueIterator<T> unique(Iterator<? extends T> iterator, BinaryFunctor<T, T, Boolean> eq)(Code) | | Skips duplicate values in the given iteration.
an iterator based on the given iterator that will not return thesame element twice in succession, using the given functor to compareelements |
|
|