import java.util.Iterator; // An example True-Iterator implemented using the IteratorHelper class. // This class implements the Iterator interface but does not support the // remove method. public class Range implements Runnable, Iterator { private int low; private int high; private IteratorHelper ih; // Constructor. public Range(int low, int high) { this.low = low; this.high = high; ih = new IteratorHelper(this); ih.start(); } // Checks if we have reached the end of the iterator. public boolean hasNext() { return !ih.done(); } // Gives the next element. If no such element exists it throws // NoSuchElementException. public Integer next() { return ih.get(); } // Not supported. public void remove() { throw new UnsupportedOperationException(); } // Implements the method of the Runnable interface. public void run() { for (int i = low; i <= high; i++) { ih.yield(new Integer(i)); } } }