T[] toArray(T[] array) {
lock.lock();
try {
return q.toArray(array);
}
finally {
lock.unlock();
}
}
/**
* Removes a single instance of the specified element from this
* queue, if it is present. More formally,
* removes an element e such that (o==null ? e==null :
* o.equals(e)), if the queue contains one or more such
* elements. Returns true if the queue contained the
* specified element (or equivalently, if the queue changed as a
* result of the call).
*
* This implementation iterates over the queue looking for the
* specified element. If it finds the element, it removes the element
* from the queue using the iterator's remove method.
*
*/
public boolean remove(Object o) {
lock.lock();
try {
return q.remove(o);
}
finally {
lock.unlock();
}
}
/**
* Returns an iterator over the elements in this queue. The iterator
* does not return the elements in any particular order.
*
* @return an iterator over the elements in this queue.
*/
public Iterator iterator() {
lock.lock();
try {
return new Itr(q.iterator());
}
finally {
lock.unlock();
}
}
private class Itr implements Iterator {
private final Iterator iter;
Itr(Iterator i) {
iter = i;
}
public boolean hasNext() {
return iter.hasNext();
}
public E next() {
lock.lock();
try {
return iter.next();
}
finally {
lock.unlock();
}
}
public void remove() {
lock.lock();
try {
iter.remove();
}
finally {
lock.unlock();
}
}
}
}