| 1 | package Queue; |
| 2 | |
| 3 | public class ArrayQueue<E> implements IQueue<E> { |
| 4 | private static final int CAPACITY = 1000; |
| 5 | private E[] data; |
| 6 | private int front = 0; |
| 7 | private int size = 0; |
| 8 | |
| 9 | public ArrayQueue() { |
| 10 | this(CAPACITY); |
| 11 | } |
| 12 | |
| 13 | public ArrayQueue(int capacity) { |
| 14 | data = (E[]) new Object[capacity]; |
| 15 | } |
| 16 | |
| 17 | @Override |
| 18 | public int size() { |
| 19 | return size; |
| 20 | } |
| 21 | |
| 22 | @Override |
| 23 | public boolean isEmpty() { |
| 24 | return size == 0; |
| 25 | } |
| 26 | |
| 27 | @Override |
| 28 | public void enqueue(E e) { |
| 29 | if (size == data.length) throw new IllegalStateException("Queue is full"); |
| 30 | int avail = (front + size) % data.length; |
| 31 | data[avail] = e; |
| 32 | size ++; |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public E dequeue() { |
| 37 | if (isEmpty()) return null; |
| 38 | E target = data[front]; |
| 39 | data[front] = null; // dereference to help garbage collection |
| 40 | front = (front + 1) % data.length; |
| 41 | size --; |
| 42 | return target; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public E peek() { |
| 47 | if (isEmpty()) return null; |
| 48 | return data[front]; |
| 49 | } |
| 50 | } |
nothing calls this directly
no outgoing calls
no test coverage detected