package queues; import java.util.NoSuchElementException; public class ArrayQueue<E> implements Queue<E> { private final E[] items; private int head; private int tail; private int size; public ArrayQueue(int capacity) { items = (E[])new Object[capacity]; } final int inc(int i) { return (++i == items.length)? 0 : i; } public boolean add(E e) { //throws IllegalStateException, NullPointerException throw new UnsupportedOperationException(); } public boolean offer(E e) { //throws NullPointerException if(size == items.length) return false; items[tail] = e; tail = inc(tail); size++; return true; } public E remove() { //throws NoSuchElementException throw new UnsupportedOperationException(); } public E poll() { if(size == 0) return null; E result = items[head]; items[head] = null; head = inc(head); size--; return result; } public E element() { //throws NoSuchElementException throw new UnsupportedOperationException(); } public E peek() { if(size == 0) return null; return items[head]; } public boolean contains(Object o) { throw new UnsupportedOperationException(); } public boolean remove(Object e) { //throws NoSuchElementException, UnsupportedOperationException throw new UnsupportedOperationException(); } public Iterator<E> iterator() { return new ArrayQueueIterator(); } public int size() { return size; } public boolean isEmpty() { return size == 0; } public void clear() { //throws UnsupportedOperationException throw new UnsupportedOperationException(); } public String toString() { StringBuffer stb = new StringBuffer("[ "); if(size > 0) { Iterator<E> it = iterator(); while(it.hasNext()) stb.append(it.next().toString() + " ; "); stb.delete(stb.length()-2, stb.length()-1); stb.append("]"); } return stb.toString(); } public <T> T[] toArray(T[] a) { throw new UnsupportedOperationException(); } /** * Iterator for ArrayQueue */ private class ArrayQueueIterator implements Iterator<E> { /** * Index of element to be returned by next, * or a negative number if no such. */ private int nextIndex; /** * nextItem holds on to item fields because once we claim * that an element exists in hasNext(), we must return it in * the following next() call even if it was in the process of * being removed when hasNext() was called. */ private E nextItem; /** * Index of element returned by most recent call to next. * Reset to -1 if this element is deleted by a call to remove. */ private int lastRet; ArrayQueueIterator() { lastRet = -1; if (size == 0) nextIndex = -1; else { nextIndex = head; nextItem = items[head]; } } public boolean hasNext() { return nextIndex >= 0; } /** * Checks whether nextIndex is valid; if so setting nextItem. * Stops iterator when either hits putIndex or sees null item. */ private void checkNext() { if (nextIndex == head) { nextIndex = -1; nextItem = null; } else { nextItem = items[nextIndex]; if (nextItem == null) nextIndex = -1; } } public E next() { if (nextIndex < 0) throw new NoSuchElementException(); lastRet = nextIndex; E e = nextItem; nextIndex = inc(nextIndex); checkNext(); return e; } public void remove() { throw new UnsupportedOperationException(); } } }