package utp;

import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;

public class ActiveListGenerator<T> implements IActiveGenerator<T> {
	private T m_value = null;
	private IActiveGenerator<T> m_next = null;
	private ListIterator<T> m_iter;

	private ActiveListGenerator(ListIterator<T> iter) {
		this.m_iter = iter;
	}

	public ActiveListGenerator(List<T> list) {
		this.m_iter = list.listIterator();
	}

	private void force() throws StopException {
		try {
			if (m_iter != null) {
				m_value = m_iter.next();
				m_next = new ActiveListGenerator<T>(m_iter);
				m_iter = null;
			}
		} catch (NoSuchElementException e) {
			throw new StopException();
		}
	}

	public IActiveGenerator<T> next() {
		try {

			force();
		} catch (StopException e) {
			return this;
		}
		return m_next;
	}

	public T value() throws StopException {
		force();
		return m_value;
	}

}
