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 declared in class AbstractCollection
contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, 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).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 declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.Methods declared in interface Collection
equals, hashCode, parallelStream, removeIf, size, 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).intsize()Returns the number of elements in this collection.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 declared in 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:
-