package iteratorFun;


public class Generator<Any> implements Iterable<Any> {
	Operator<Any> operator;
	Terminator<Any> terminator;
	Any current;
	public Generator(Any initial, Operator<Any> op, Terminator<Any> term) {
		current = initial;
		operator = op;
		terminator = term;
	}
	private class GeneratorIterator implements java.util.Iterator<Any> {
		public Any next() {
			if (terminator.done(current)) {
				throw new java.util.NoSuchElementException();
			} else {
				Any oldCurrent = current;
				current = operator.op(current);
				return oldCurrent;
			}
		}
		public boolean hasNext() {
			return ! terminator.done(current);
		}
		public void remove() {
			throw new java.util.NoSuchElementException();
		}
	}
	public java.util.Iterator<Any> iterator() {
		return new GeneratorIterator();
	}

}

