Methods Summary |
---|
public boolean | add(E e)Inserts the specified element at the end of this deque unless it would
violate capacity restrictions. When using a capacity-restricted deque,
it is generally preferable to use method {@link #offer(Object) offer}.
This method is equivalent to {@link #addLast}.
addLast(e);
return true;
|
public void | addFirst(E e)
if (!offerFirst(e))
throw new IllegalStateException("Deque full");
|
public void | addLast(E e)
if (!offerLast(e))
throw new IllegalStateException("Deque full");
|
public void | clear()Atomically removes all of the elements from this deque.
The deque will be empty after this call returns.
lock.lock();
try {
first = last = null;
count = 0;
notFull.signalAll();
} finally {
lock.unlock();
}
|
public boolean | contains(java.lang.Object o)Returns true if this deque contains the specified element.
More formally, returns true if and only if this deque contains
at least one element e such that o.equals(e).
if (o == null) return false;
lock.lock();
try {
for (Node<E> p = first; p != null; p = p.next)
if (o.equals(p.item))
return true;
return false;
} finally {
lock.unlock();
}
|
public java.util.Iterator | descendingIterator()Returns an iterator over the elements in this deque in reverse
sequential order. The elements will be returned in order from
last (tail) to first (head).
The returned Iterator is a "weakly consistent" iterator that
will never throw {@link ConcurrentModificationException},
and guarantees to traverse elements as they existed upon
construction of the iterator, and may (but is not guaranteed to)
reflect any modifications subsequent to construction.
return new DescendingItr();
|
public int | drainTo(java.util.Collection c)
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
lock.lock();
try {
for (Node<E> p = first; p != null; p = p.next)
c.add(p.item);
int n = count;
count = 0;
first = last = null;
notFull.signalAll();
return n;
} finally {
lock.unlock();
}
|
public int | drainTo(java.util.Collection c, int maxElements)
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
lock.lock();
try {
int n = 0;
while (n < maxElements && first != null) {
c.add(first.item);
first.prev = null;
first = first.next;
--count;
++n;
}
if (first == null)
last = null;
notFull.signalAll();
return n;
} finally {
lock.unlock();
}
|
public E | element()Retrieves, but does not remove, the head of the queue represented by
this deque. This method differs from {@link #peek peek} only in that
it throws an exception if this deque is empty.
This method is equivalent to {@link #getFirst() getFirst}.
return getFirst();
|
public E | getFirst()
E x = peekFirst();
if (x == null) throw new NoSuchElementException();
return x;
|
public E | getLast()
E x = peekLast();
if (x == null) throw new NoSuchElementException();
return x;
|
public java.util.Iterator | iterator()Returns an iterator over the elements in this deque in proper sequence.
The elements will be returned in order from first (head) to last (tail).
The returned Iterator is a "weakly consistent" iterator that
will never throw {@link ConcurrentModificationException},
and guarantees to traverse elements as they existed upon
construction of the iterator, and may (but is not guaranteed to)
reflect any modifications subsequent to construction.
return new Itr();
|
private boolean | linkFirst(E e)Links e as first element, or returns false if full.
if (count >= capacity)
return false;
++count;
Node<E> f = first;
Node<E> x = new Node<E>(e, null, f);
first = x;
if (last == null)
last = x;
else
f.prev = x;
notEmpty.signal();
return true;
|
private boolean | linkLast(E e)Links e as last element, or returns false if full.
if (count >= capacity)
return false;
++count;
Node<E> l = last;
Node<E> x = new Node<E>(e, l, null);
last = x;
if (first == null)
first = x;
else
l.next = x;
notEmpty.signal();
return true;
|
public boolean | offer(E e)
return offerLast(e);
|
public boolean | offer(E e, long timeout, java.util.concurrent.TimeUnit unit)
return offerLast(e, timeout, unit);
|
public boolean | offerFirst(E e)
if (e == null) throw new NullPointerException();
lock.lock();
try {
return linkFirst(e);
} finally {
lock.unlock();
}
|
public boolean | offerFirst(E e, long timeout, java.util.concurrent.TimeUnit unit)
if (e == null) throw new NullPointerException();
long nanos = unit.toNanos(timeout);
lock.lockInterruptibly();
try {
for (;;) {
if (linkFirst(e))
return true;
if (nanos <= 0)
return false;
nanos = notFull.awaitNanos(nanos);
}
} finally {
lock.unlock();
}
|
public boolean | offerLast(E e)
if (e == null) throw new NullPointerException();
lock.lock();
try {
return linkLast(e);
} finally {
lock.unlock();
}
|
public boolean | offerLast(E e, long timeout, java.util.concurrent.TimeUnit unit)
if (e == null) throw new NullPointerException();
long nanos = unit.toNanos(timeout);
lock.lockInterruptibly();
try {
for (;;) {
if (linkLast(e))
return true;
if (nanos <= 0)
return false;
nanos = notFull.awaitNanos(nanos);
}
} finally {
lock.unlock();
}
|
public E | peek()
return peekFirst();
|
public E | peekFirst()
lock.lock();
try {
return (first == null) ? null : first.item;
} finally {
lock.unlock();
}
|
public E | peekLast()
lock.lock();
try {
return (last == null) ? null : last.item;
} finally {
lock.unlock();
}
|
public E | poll()
return pollFirst();
|
public E | poll(long timeout, java.util.concurrent.TimeUnit unit)
return pollFirst(timeout, unit);
|
public E | pollFirst()
lock.lock();
try {
return unlinkFirst();
} finally {
lock.unlock();
}
|
public E | pollFirst(long timeout, java.util.concurrent.TimeUnit unit)
long nanos = unit.toNanos(timeout);
lock.lockInterruptibly();
try {
for (;;) {
E x = unlinkFirst();
if (x != null)
return x;
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
} finally {
lock.unlock();
}
|
public E | pollLast()
lock.lock();
try {
return unlinkLast();
} finally {
lock.unlock();
}
|
public E | pollLast(long timeout, java.util.concurrent.TimeUnit unit)
long nanos = unit.toNanos(timeout);
lock.lockInterruptibly();
try {
for (;;) {
E x = unlinkLast();
if (x != null)
return x;
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
} finally {
lock.unlock();
}
|
public E | pop()
return removeFirst();
|
public void | push(E e)
addFirst(e);
|
public void | put(E e)
putLast(e);
|
public void | putFirst(E e)
if (e == null) throw new NullPointerException();
lock.lock();
try {
while (!linkFirst(e))
notFull.await();
} finally {
lock.unlock();
}
|
public void | putLast(E e)
if (e == null) throw new NullPointerException();
lock.lock();
try {
while (!linkLast(e))
notFull.await();
} finally {
lock.unlock();
}
|
private void | readObject(java.io.ObjectInputStream s)Reconstitute this deque from a stream (that is,
deserialize it).
s.defaultReadObject();
count = 0;
first = null;
last = null;
// Read in all elements and place in queue
for (;;) {
E item = (E)s.readObject();
if (item == null)
break;
add(item);
}
|
public int | remainingCapacity()Returns the number of additional elements that this deque can ideally
(in the absence of memory or resource constraints) accept without
blocking. This is always equal to the initial capacity of this deque
less the current size of this deque.
Note that you cannot always tell if an attempt to insert
an element will succeed by inspecting remainingCapacity
because it may be the case that another thread is about to
insert or remove an element.
lock.lock();
try {
return capacity - count;
} finally {
lock.unlock();
}
|
public E | remove()Retrieves and removes the head of the queue represented by this deque.
This method differs from {@link #poll poll} only in that it throws an
exception if this deque is empty.
This method is equivalent to {@link #removeFirst() removeFirst}.
return removeFirst();
|
public boolean | remove(java.lang.Object o)Removes the first occurrence of the specified element from this deque.
If the deque does not contain the element, it is unchanged.
More formally, removes the first element e such that
o.equals(e) (if such an element exists).
Returns true if this deque contained the specified element
(or equivalently, if this deque changed as a result of the call).
This method is equivalent to
{@link #removeFirstOccurrence(Object) removeFirstOccurrence}.
return removeFirstOccurrence(o);
|
public E | removeFirst()
E x = pollFirst();
if (x == null) throw new NoSuchElementException();
return x;
|
public boolean | removeFirstOccurrence(java.lang.Object o)
if (o == null) return false;
lock.lock();
try {
for (Node<E> p = first; p != null; p = p.next) {
if (o.equals(p.item)) {
unlink(p);
return true;
}
}
return false;
} finally {
lock.unlock();
}
|
public E | removeLast()
E x = pollLast();
if (x == null) throw new NoSuchElementException();
return x;
|
public boolean | removeLastOccurrence(java.lang.Object o)
if (o == null) return false;
lock.lock();
try {
for (Node<E> p = last; p != null; p = p.prev) {
if (o.equals(p.item)) {
unlink(p);
return true;
}
}
return false;
} finally {
lock.unlock();
}
|
boolean | removeNode(java.util.concurrent.LinkedBlockingDeque$Node e)Variant of removeFirstOccurrence needed by iterator.remove.
Searches for the node, not its contents.
lock.lock();
try {
for (Node<E> p = first; p != null; p = p.next) {
if (p == e) {
unlink(p);
return true;
}
}
return false;
} finally {
lock.unlock();
}
|
public int | size()Returns the number of elements in this deque.
lock.lock();
try {
return count;
} finally {
lock.unlock();
}
|
public E | take()
return takeFirst();
|
public E | takeFirst()
lock.lock();
try {
E x;
while ( (x = unlinkFirst()) == null)
notEmpty.await();
return x;
} finally {
lock.unlock();
}
|
public E | takeLast()
lock.lock();
try {
E x;
while ( (x = unlinkLast()) == null)
notEmpty.await();
return x;
} finally {
lock.unlock();
}
|
public java.lang.Object[] | toArray()Returns an array containing all of the elements in this deque, in
proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are
maintained by this deque. (In other words, this method must allocate
a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based
APIs.
lock.lock();
try {
Object[] a = new Object[count];
int k = 0;
for (Node<E> p = first; p != null; p = p.next)
a[k++] = p.item;
return a;
} finally {
lock.unlock();
}
|
public T[] | toArray(T[] a)Returns an array containing all of the elements in this deque, in
proper sequence; the runtime type of the returned array is that of
the specified array. If the deque fits in the specified array, it
is returned therein. Otherwise, a new array is allocated with the
runtime type of the specified array and the size of this deque.
If this deque fits in the specified array with room to spare
(i.e., the array has more elements than this deque), the element in
the array immediately following the end of the deque is set to
null.
Like the {@link #toArray()} method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
Suppose x is a deque known to contain only strings.
The following code can be used to dump the deque into a newly
allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().
lock.lock();
try {
if (a.length < count)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(),
count
);
int k = 0;
for (Node<E> p = first; p != null; p = p.next)
a[k++] = (T)p.item;
if (a.length > k)
a[k] = null;
return a;
} finally {
lock.unlock();
}
|
public java.lang.String | toString()
lock.lock();
try {
return super.toString();
} finally {
lock.unlock();
}
|
private void | unlink(java.util.concurrent.LinkedBlockingDeque$Node x)Unlink e
Node<E> p = x.prev;
Node<E> n = x.next;
if (p == null) {
if (n == null)
first = last = null;
else {
n.prev = null;
first = n;
}
} else if (n == null) {
p.next = null;
last = p;
} else {
p.next = n;
n.prev = p;
}
--count;
notFull.signalAll();
|
private E | unlinkFirst()Removes and returns first element, or null if empty.
Node<E> f = first;
if (f == null)
return null;
Node<E> n = f.next;
first = n;
if (n == null)
last = null;
else
n.prev = null;
--count;
notFull.signal();
return f.item;
|
private E | unlinkLast()Removes and returns last element, or null if empty.
Node<E> l = last;
if (l == null)
return null;
Node<E> p = l.prev;
last = p;
if (p == null)
first = null;
else
p.next = null;
--count;
notFull.signal();
return l.item;
|
private void | writeObject(java.io.ObjectOutputStream s)Save the state of this deque to a stream (that is, serialize it).
lock.lock();
try {
// Write out capacity and any hidden stuff
s.defaultWriteObject();
// Write out all elements in the proper order.
for (Node<E> p = first; p != null; p = p.next)
s.writeObject(p.item);
// Use trailing null as sentinel
s.writeObject(null);
} finally {
lock.unlock();
}
|