package aalg.algebra;

import java.math.BigInteger;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Iterator that iterates all prime field elements.
 * @author Ing. Petr Ležák
 */
public class PrimeFieldIterator implements Iterator<PrimeFieldElement> {
	
	private final BigInteger prime;
	private BigInteger number;
	
	/**
	 * Creates prime field iterator.
	 * @param prime prime number
	 */
	public PrimeFieldIterator (final BigInteger prime) {
		this.prime = prime;
		this.number = BigInteger.ZERO;
	}

	@Override
	public boolean hasNext() {
		return number.compareTo(prime) < 0;
	}

	@Override
	public PrimeFieldElement next() {
		if (!hasNext())
			throw new NoSuchElementException();
		
		final PrimeFieldElement result = new PrimeFieldElement(number);
		number = number.add(BigInteger.ONE);
		
		return result;
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}

}
