Mango provides nine new iterators
- Array Iterator
- Bounded Iterator
- NullIterator
- ReverseIterator
- Selecting Iterator
- Singleton Iterator
- Skipping Iterator
- StringIterator
- Transform Iterator
All the iterators implement java.util.Iterator. They are all constructed using factory static methods on the Iterators class.
Array Iterator
An Array Iterator puts a java.util.Iterator face on an object array, allowing it be treated as you would a java.util.Collection.
[Javadoc]
Bounded Iterator
A conventional java.util.Iterator, obtained by a call to say
java.util.List.iterator(), travels the entire sequence of the
java.util.Collection it points to. It starts at the beginning and
keeps on going until you hit the end or get bored.
A BoundedIterator enumerates of a subset of a collection, in the range [start, end) - a normal java.util.Iterator traverses [0, collection.size()). A BoundedIterator therefore allows you to pick out a sub-set without using list.subList() or equivalent.
A BoundedIterator implements the java.util.Iterator interface, and can be constructed by wrapping around an existing iterator. There's also a version which is optimized for java.util.Lists.
[Javadoc]
Selecting Iterator
A SelectingIterator picks out selected elements from a sequence. It takes a Predicate which encapsulates some test, and only returns those Objects in the sequence which pass the test.
Say you have a list of strings, myStringList and you are only interested in those that begin with 'S'. What you need is
Iterator iter = Mango.SelectingIterator(myStringList.iterator(),
new Predicate() {
boolean test(Object o) {
String s = (String)o;
return s.charAt(0) == 'S';
}
});
A SelectingIterator implements the java.util.Iterator interface, and is constructed by wrapping around an existing iterator.
[Javadoc]
Singleton Iterator
Usually an iterator moves over some sequence. A SingletonIterator treats a single object as it if it were a list containing one object. Since SingletonIterator implements the java.util.Iterator interface, it provides a convienent way of passing a single object to an algorithm or other iterator consumer.
[Javadoc]
Skipping Iterator
A SkippingIterator enumerates a sequence, stepping over the elements that match the supplied Predicate. Is it equivalent to SelectingIterator(iter, Not(predicate))
[Javadoc]
Transform Iterator
A TransformIterator applies a UnaryFunction to each element in the sequence, returning the function result at each step.
Say you have a list of some complex type, and you want to print their names You could (caution! trivial example follows)
Iterator i = list.iterator();
while(i.hasNext()) {
MyComplexObject mco = (MyComplexObject)i.next();
System.out.println(mco.getName());
}
or you could
Iterator i = Iterators.TransformIterator(list.iterator(),
Adapt.ArgumentMethod("GetName"));
while(i.hasNext())
System.out.println(i.next());
[Javadoc]