Class AbstractQueue<E>
- Type Parameters:
E- the type of elements held in this queue
- All Implemented Interfaces:
Iterable<E>, Collection<E>, Queue<E>
- Direct Known Subclasses:
ArrayBlockingQueue, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedTransferQueue, PriorityBlockingQueue, PriorityQueue, SynchronousQueue
Queue
operations. The implementations in this class are appropriate when
the base implementation does not allow null
elements. Methods add, remove, and
element are based on offer, poll, and peek, respectively, but throw
exceptions instead of indicating failure via false or
null returns.
A Queue implementation that extends this class must
minimally define a method Queue.offer(E) which does not permit
insertion of null elements, along with methods Queue.peek(), Queue.poll(), Collection.size(), and
Collection.iterator(). Typically, additional methods will be
overridden as well. If these requirements cannot be met, consider
instead subclassing AbstractCollection.
This class is a member of the Java Collections Framework.
- Since:
- 1.5
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanInserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returningtrueupon success and throwing anIllegalStateExceptionif no space is currently available.booleanaddAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this queue.voidclear()Removes all of the elements from this queue.element()Retrieves, but does not remove, the head of this queue.remove()Retrieves and removes the head of this queue.Methods inherited from class AbstractCollection
contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toStringModifier and TypeMethodDescriptionbooleanReturnstrueif this collection contains the specified element.booleancontainsAll(Collection<?> c) Returnstrueif this collection contains all of the elements in the specified collection.booleanisEmpty()Returnstrueif this collection contains no elements.iterator()Returns an iterator over the elements contained in this collection.booleanRemoves a single instance of the specified element from this collection, if it is present (optional operation).booleanremoveAll(Collection<?> c) Removes all of this collection's elements that are also contained in the specified collection (optional operation).booleanretainAll(Collection<?> c) Retains only the elements in this collection that are contained in the specified collection (optional operation).abstract intsize()Returns the number of elements in this collection.Object[]toArray()Returns an array containing all of the elements in this collection.<T> T[]toArray(T[] a) Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.toString()Returns a string representation of this collection.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Creates and returns a copy of this object.booleanIndicates whether some other object is "equal to" this one.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.Finalization is deprecated and subject to removal in a future release.final Class<?> getClass()Returns the runtime class of thisObject.inthashCode()Returns a hash code value for this object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.final voidwait()Causes the current thread to wait until it is awakened, typically by being notified or interrupted.final voidwait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.final voidwait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.Methods inherited from interface Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArrayModifier and TypeMethodDescriptionbooleanCompares the specified object with this collection for equality.inthashCode()Returns the hash code value for this collection.Returns a possibly parallelStreamwith this collection as its source.default booleanRemoves all of the elements of this collection that satisfy the given predicate (optional operation).default Spliterator<E> Creates aSpliteratorover the elements in this collection.stream()Returns a sequentialStreamwith this collection as its source.default <T> T[]toArray(IntFunction<T[]> generator) Returns an array containing all of the elements in this collection, using the providedgeneratorfunction to allocate the returned array.Methods inherited from interface Queue
offer, peek, pollModifier and TypeMethodDescriptionbooleanInserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.peek()Retrieves, but does not remove, the head of this queue, or returnsnullif this queue is empty.poll()Retrieves and removes the head of this queue, or returnsnullif this queue is empty.
-
Constructor Details
-
AbstractQueue
protected AbstractQueue()Constructor for use by subclasses.
-
-
Method Details
-
add
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returningtrueupon success and throwing anIllegalStateExceptionif no space is currently available.This implementation returns
trueifoffersucceeds, else throws anIllegalStateException.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceQueue<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
e- the element to add- Returns:
true(as specified byCollection.add(E))- Throws:
IllegalStateException- if the element cannot be added at this time due to capacity restrictionsClassCastException- if the class of the specified element prevents it from being added to this queueNullPointerException- if the specified element is null and this queue does not permit null elementsIllegalArgumentException- if some property of this element prevents it from being added to this queue
-
remove
Retrieves and removes the head of this queue. This method differs frompollonly in that it throws an exception if this queue is empty.This implementation returns the result of
pollunless the queue is empty.- Specified by:
removein interfaceQueue<E>- Returns:
- the head of this queue
- Throws:
NoSuchElementException- if this queue is empty
-
element
Retrieves, but does not remove, the head of this queue. This method differs frompeekonly in that it throws an exception if this queue is empty.This implementation returns the result of
peekunless the queue is empty.- Specified by:
elementin interfaceQueue<E>- Returns:
- the head of this queue
- Throws:
NoSuchElementException- if this queue is empty
-
clear
public void clear()Removes all of the elements from this queue. The queue will be empty after this call returns.This implementation repeatedly invokes
polluntil it returnsnull.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>
-
addAll
Adds all of the elements in the specified collection to this queue. Attempts to addAll of a queue to itself result inIllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.This implementation iterates over the specified collection, and adds each element returned by the iterator to this queue, in turn. A runtime exception encountered while trying to add an element (including, in particular, a
nullelement) may result in only some of the elements having been successfully added when the associated exception is thrown.- Specified by:
addAllin interfaceCollection<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
c- collection containing elements to be added to this queue- Returns:
trueif this queue changed as a result of the call- Throws:
ClassCastException- if the class of an element of the specified collection prevents it from being added to this queueNullPointerException- if the specified collection contains a null element and this queue does not permit null elements, or if the specified collection is nullIllegalArgumentException- if some property of an element of the specified collection prevents it from being added to this queue, or if the specified collection is this queueIllegalStateException- if not all the elements can be added at this time due to insertion restrictions- See Also:
-