/** * Inserts the specified element at the end of this queue. * * @param e the element to add * @return <tt>true</tt> (as specified by {@link Queue#offer}) * @throws NullPointerException if the specified element is null */ @Override public boolean offer(E e) { return add(e); }
/** * Constructs a queue containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. (The first element returned by the collection's * iterator becomes the first element, or <i>front</i> of the * queue.) * * @param c the collection whose elements are to be placed into the queue * @throws NullPointerException if the specified collection is null */ public ArrayQueue(Collection<? extends E> c) { allocateElements(c.size()); addAll(c); }
/** * Returns an array containing all of the elements in this queue * in proper sequence (from first to last element). * * <p>The returned array will be "safe" in that no references to it are * maintained by this queue. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. * * <p>This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this queue */ @Override public Object[] toArray() { return toArray(new Object[size()]); }
/** * Constructs an empty array queue with an initial capacity * sufficient to hold the specified number of elements. * * @param numElements lower bound on initial capacity of the queue */ public ArrayQueue(int numElements) { allocateElements(numElements); }
/** * Serialize this queue. * * @serialData The current size (<tt>int</tt>) of the queue, * followed by all of its elements (each an object reference) in * first-to-last order. */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { s.defaultWriteObject(); // Write out size s.writeInt(size()); // Write out elements in order. int mask = elements.length - 1; for (int i = head; i != tail; i = (i + 1) & mask) s.writeObject(elements[i]); }
/** * Inserts the specified element at the end of this queue. * * <p>This method is equivalent to {@link #offer}. * * @param e the element to add * @return <tt>true</tt> (as specified by {@link Collection#add}) * @throws NullPointerException if the specified element is null */ @Override public boolean add(E e) { if (e == null) throw new NullPointerException("e == null"); elements[tail] = e; if ((tail = (tail + 1) & (elements.length - 1)) == head) doubleCapacity(); return true; }
/** * Retrieves and removes the head of the queue represented by this queue. * * This method differs from {@link #poll poll} only in that it throws an * exception if this queue is empty. * * @return the head of the queue represented by this queue * @throws NoSuchElementException {@inheritDoc} */ @Override public E remove() { E x = poll(); if (x == null) throw new NoSuchElementException(); return x; }
/** * Removes a single instance of the specified element from this queue. * If the queue does not contain the element, it is unchanged. * More formally, removes the first element <tt>e</tt> such that * <tt>o.equals(e)</tt> (if such an element exists). * Returns <tt>true</tt> if this queue contained the specified element * (or equivalently, if this queue changed as a result of the call). * * @param o element to be removed from this queue, if present * @return <tt>true</tt> if this queue contained the specified element */ @Override public boolean remove(Object o) { if (o == null) return false; int mask = elements.length - 1; int i = head; Object x; while ((x = elements[i]) != null) { if (o.equals(x)) { delete(i); return true; } i = (i + 1) & mask; } return false; }
/** * Deserialize this queue. */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { s.defaultReadObject(); // Read in size and allocate array int size = s.readInt(); allocateElements(size); head = 0; tail = size; // Read in all elements in the proper order. for (int i = 0; i < size; i++) elements[i] = s.readObject(); } }