package queue;

/**
 * Array based implementation of Queue
 * @author Winston Prakash
 */

public class ArrayQueue<E> implements Queue<E>{
    int size = 0;
    private transient E[] queueData;
    
    ArrayQueue(){
        this(11);
    }
    
    /**
     * Check if the queue is empty
     */
    public boolean isEmpty(){
        return size == 0;
    }
    
    ArrayQueue(int initialCapacity){
        queueData = (E[]) new Object[initialCapacity];
    }
    
    /**
     * Add the data to the bottom of the queueu
     */
    public void enqueue(E item) {
        ensureCapacity();
        queueData[size++] = item;
    }
    
    /**
     * Get the item from the head of the queue
     */
    public E dequeue() {
        if(isEmpty()){
            throw new EmptyQueueException();
        }
        E item = (E) queueData[0];
        size--;
        System.arraycopy(queueData, 1, queueData, 0, size);
        queueData[size] = null;
        return item;
    }
    
    /**
     * Get the size of the queue
     */
    public int size() {
        return size;
    }
    
    private void ensureCapacity(){
        if (size > queueData.length - 1){
            Object[] oldData = queueData;
            queueData = (E[]) new Object[oldData.length * 2];
            System.arraycopy(oldData, 0, queueData, 0, oldData.length);
        }
    }
}