/**
 * Queue implementation using an array
 * 
 * @author dave
 */
public class ArrayQueue<E> implements Queue<E>, Linear<E>{
	private E[] data;
	private int headIndex = 0;
	private int size = 0;
	
	public ArrayQueue(int capacity){
		data = (E[])new Object[capacity];
	}

	public E dequeue() {
		assert !empty();

		E returnMe = data[headIndex];
		data[headIndex] = null; // just to be safe
		headIndex = (headIndex + 1) % data.length;
		size--;
		
		return returnMe;
	}

	public boolean empty() {
		return size == 0;
	}

	public void enqueue(E item) {
		assert size < data.length;
		
		int tail = (headIndex + size) % data.length;
		data[tail] = item;
		size++;
	}

	public E peek() {
		assert !empty();
		
		return data[headIndex];
	}

	// ---------------- Just added for compatibility with Linear
	public void add(E item){
		enqueue(item);
	}

	public E remove() {
		return dequeue();
	}
}