package graph; import java.util.Iterator; /** An Iteration is an Iterator that may also be used in a foreach * loop. That is, it implements the Interable interface by simply * returning itself. For example, this allows one to write * for (Edge e: G.edges()) { * ... * } * @author P. N. Hilfinger */ public abstract class Iteration implements Iterator, Iterable { @Override public Iterator iterator() { return this; } @Override public void remove() { throw new UnsupportedOperationException("remove not supported"); } /** A wrapper class that turns an Iterator into an Iteration. */ private static class SimpleIteration extends Iteration { /** ITER as an iteration. */ SimpleIteration(Iterator iter) { _iter = iter; } @Override public boolean hasNext() { return _iter.hasNext(); } @Override public Type next() { return _iter.next(); } /** The iterator with which I was constructed. */ private Iterator _iter; } /** Returns an Iteration that delegates to IT. */ static Iteration iteration(Iterator it) { return new SimpleIteration<>(it); } /** Returns an Iteration that delegates to ITERABLE. */ static Iteration iteration(Iterable iterable) { return new SimpleIteration<>(iterable.iterator()); } }