The next example shows how to create and use a collection of mock integers
in Java. The collection, named ints, is created with a range of
[10,20]2, which gives the collection even values from 10 to 20.
Two iterators, named iter0 and iter1, are created
against the collection. The next value from each iterator is printed, while
iter0 has elements.
System.out.println ("Ints0");
Ints ints = new Ints ("[10,20]2");
Iterator iter0 = ints.iterator ();
Iterator iter1 = ints.iterator ();
while (iter0.hasNext ())
{
Integer i0 = (Integer) iter0.next ();
Integer i1 = (Integer) iter1.next ();
System.out.print (i0.intValue () + " " + i1.intValue () + " ");
}
System.out.println ();
The result of running this code is:
10 10 12 12 14 14 16 16 18 18 20 20
Equivalent code, using XML configuration, is:
<dm:ints range="[10,20]2">
<dm:iterator id="iter0"/>
<dm:iterator id="iter1"/>
</dm:ints>
Note that because this collection has an upper bound, it is safe to use a
while loop with iter.hasNext(). If the collection
did not have an upper bound, then hasNext() would never have
returned true, and the while would have been
infinite. In this case it would be safer to use a for loop to
get a specific number of values.
The Java code to load and run this XML is shown next. The two iterators are looked up from the context, and then printed using code identical to the Java example above.
DatamixerContext context = root.getContext ();
DatamixerIterator iter0 = context.getDatamixerIterator ("iter0");
DatamixerIterator iter1 = context.getDatamixerIterator ("iter1");
while (iter0.hasNext ())
{
Integer i0 = (Integer) iter0.next ();
Integer i1 = (Integer) iter1.next ();
System.out.print (i0.intValue () + " " + i1.intValue () + " ");
}
System.out.println ();