An iterator returns values from a collection. A collection can have many iterators, and each iterator is independent of all the others. In the next example, a list of integers is printed twice, by two iterators. The result is:
10 10 12 12 14 14 16 16 18 18 20 20
XML:
<dm:ints range="[0,20]2">
<dm:iterator id="iter1"/>
<dm:iterator id="iter2"/>
</dm:ints>
Java:
DatamixerIterator iter0 = // get iterator from XML context
DatamixerIterator iter1 = // get iterator from XML context
while (iter0.hasNext ())
System.out.println (iter0.getNextValue () + " " + iter1.getNextValue ());
Iterators can also refer to a collection by its id:
<dm:ints id="even-ints" range="[0,20]2"/> <dm:iterator id="iter1" collection="even-ints"/> <dm:iterator id="iter2" collection="even-ints"/>
The Java implementation of a datamixer iterator is DatamixerIterator, which implements
java.util.Iterator. Each call to next() calls getNextValue(), which gets the
next value as a Value object.
The iterator stores this value as its current value. The current value can be accessed any number of times with
getCurrentValue(), or as a Java Object with getCurrent().
next() returns this value as a Java Object, by calling value.getObject(). If
the value is desired as a Value, then getNextValue() can be called directly instead of
next().
A datamixer iterator supports several methods in addition to hasNext() and next():
reset() resets the iterator to an initial state. The first call to next() after
reset() will return the first element in the collection.
getRepeat() and setRepeat() set the value of the repeat flag. If true, then when an
iterator reaches the end of the collection, it is automatically reset.
getCollection() and setCollection() return the collection that produced the iterator.
getFormatPattern() and setFormatPattern() allow a format pattern to be applied to each
next value.
getId() and setId() associate an identifier with an iterator, which is used when printing
error messages.