public class PositionIterator implements Iterator {
protected List list; // the underlying list
protected Position cur; // the current (next) position
public PositionIterator() { } // default constructor
public PositionIterator(List L) { // preferred constructor
list = L;
if (list.isEmpty()) cur = null; // list is empty
else cur = list.first(); // start with the first position
}
public boolean hasNext() { return (cur != null); }
public Object next() throws NoSuchElementException {
if (!hasNext()) throw new NoSuchElementException("No next position");
Position toReturn = cur;
if (cur == list.last()) cur = null; // no positions left
else cur = list.next(cur); // move cursor to the next position
return toReturn;
}
}