ConditionPriorityBlockingQueue

public class ConditionPriorityBlockingQueue
extends Object implements Iterable<T>

java.lang.Object
   ↳ com.android.tradefed.util.ConditionPriorityBlockingQueue<T>


A thread-safe class with PriorityBlockingQueue-like operations that can retrieve objects that match a certain condition.

Iteration is also thread-safe, but not consistent. A copy of the queue is made at iterator creation time, and that copy is used as the iteration target. If queue is modified during iteration, a ConcurrentModificationException will not be thrown, but the iterator will also not reflect the modified contents.

Summary

Nested classes

class ConditionPriorityBlockingQueue.AlwaysMatch<T>

A ConditionPriorityBlockingQueue.IMatcher that matches any object. 

interface ConditionPriorityBlockingQueue.IMatcher<T>

An interface for determining if elements match some sort of condition. 

Public constructors

ConditionPriorityBlockingQueue()

Creates a ConditionPriorityBlockingQueue

Elements will be prioritized in FIFO order.

ConditionPriorityBlockingQueue(Comparator<T> c)

Creates a ConditionPriorityBlockingQueue

Public methods

boolean add(T addedElement)

Inserts the specified element into this queue.

T addUnique(IMatcher<T> matcher, T object)

Adds a item to this queue, replacing any existing object that matches given condition

void clear()

Removes all elements from this queue.

boolean contains(T object)

Determine if an object is currently contained in this queue.

List<T> getCopy()

Get a copy of the contents of the queue.

Iterator<T> iterator()

T poll(IMatcher<T> matcher)

Retrieves and removes the minimum (as judged by the provided Comparator element T in the queue where matcher.matches(T) is true.

T poll(long timeout, TimeUnit unit, IMatcher<T> matcher)

Retrieves and removes the minimum (as judged by the provided Comparator element T in the queue where matcher.matches(T) is true.

T poll(long timeout, TimeUnit unit)

Retrieves and removes the minimum (as judged by the provided Comparator element T in the queue.

T poll()

Retrieves and removes the head of this queue.

boolean remove(T object)

Removes an item from this queue.

int size()
T take()

Retrieves and removes the minimum (as judged by the provided Comparator element T in the queue.

T take(IMatcher<T> matcher)

Retrieves and removes the first element T in the queue where matcher.matches(T) is true, waiting if necessary until such an element becomes available.

Public constructors

ConditionPriorityBlockingQueue

ConditionPriorityBlockingQueue ()

Creates a ConditionPriorityBlockingQueue

Elements will be prioritized in FIFO order.

ConditionPriorityBlockingQueue

ConditionPriorityBlockingQueue (Comparator<T> c)

Creates a ConditionPriorityBlockingQueue

Parameters
c Comparator: the Comparator used to prioritize the queue.

Public methods

add

boolean add (T addedElement)

Inserts the specified element into this queue. As the queue is unbounded this method will never block.

Parameters
addedElement T: the element to add
Returns
boolean true
Throws
ClassCastException if the specified element cannot be compared with elements currently in the priority queue according to the priority queue's ordering
NullPointerException if the specified element is null

addUnique

T addUnique (IMatcher<T> matcher, 
                T object)

Adds a item to this queue, replacing any existing object that matches given condition

Parameters
matcher IMatcher: the matcher to evaluate existing objects
object T: the object to add
Returns
T the replaced object or null if none exist

clear

void clear ()

Removes all elements from this queue.

contains

boolean contains (T object)

Determine if an object is currently contained in this queue.

Parameters
object T: the object to find
Returns
boolean true if given object is contained in queue. false> otherwise.

getCopy

List<T> getCopy ()

Get a copy of the contents of the queue.

Returns
List<T>

iterator

Iterator<T> iterator ()

Returns
Iterator<T>

poll

T poll (IMatcher<T> matcher)

Retrieves and removes the minimum (as judged by the provided Comparator element T in the queue where matcher.matches(T) is true.

Parameters
matcher IMatcher: the ConditionPriorityBlockingQueue.IMatcher to use to evaluate elements
Returns
T the minimum matched element or null if there are no matching elements

poll

T poll (long timeout, 
                TimeUnit unit, 
                IMatcher<T> matcher)

Retrieves and removes the minimum (as judged by the provided Comparator element T in the queue where matcher.matches(T) is true.

Blocks up to timeout time for an element to become available.

Parameters
timeout long: the amount of time to wait for an element to become available
unit TimeUnit: the TimeUnit of timeout
matcher IMatcher: the ConditionPriorityBlockingQueue.IMatcher to use to evaluate elements
Returns
T the minimum matched element or null if there are no matching elements
Throws
InterruptedException

poll

T poll (long timeout, 
                TimeUnit unit)

Retrieves and removes the minimum (as judged by the provided Comparator element T in the queue.

Blocks up to timeout time for an element to become available.

Parameters
timeout long: the amount of time to wait for an element to become available
unit TimeUnit: the TimeUnit of timeout
Returns
T the minimum matched element or null if there are no matching elements
Throws
InterruptedException

poll

T poll ()

Retrieves and removes the head of this queue.

Returns
T the head of this queue, or null if the queue is empty

remove

boolean remove (T object)

Removes an item from this queue.

Parameters
object T: the object to remove
Returns
boolean true if given object was removed from queue. false> otherwise.

size

int size ()

Returns
int the number of elements in queue

take

T take ()

Retrieves and removes the minimum (as judged by the provided Comparator element T in the queue.

Blocks indefinitely for an element to become available.

Returns
T the head of this queue
Throws
InterruptedException if interrupted while waiting

take

T take (IMatcher<T> matcher)

Retrieves and removes the first element T in the queue where matcher.matches(T) is true, waiting if necessary until such an element becomes available.

Parameters
matcher IMatcher: the ConditionPriorityBlockingQueue.IMatcher to use to evaluate elements
Returns
T the matched element
Throws
InterruptedException if interrupted while waiting