ConditionPriorityBlockingQueue

public class ConditionPriorityBlockingQueue
extends Object

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


A thread-safe class with ERROR(/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 ERROR(/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( 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.

getCopy()

Get a copy of the contents of the queue.

iterator()

T poll(long timeout, TimeUnit unit)

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

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

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

T poll(IMatcher<T> matcher)

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

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 ERROR(/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

public ConditionPriorityBlockingQueue ()

Creates a ConditionPriorityBlockingQueue

Elements will be prioritized in FIFO order.

ConditionPriorityBlockingQueue

public ConditionPriorityBlockingQueue ( c)

Creates a ConditionPriorityBlockingQueue

Parameters
c : the ERROR(/Comparator) used to prioritize the queue.

Public methods

add

public 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

public 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

public void clear ()

Removes all elements from this queue.

contains

public 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

public  getCopy ()

Get a copy of the contents of the queue.

Returns

iterator

public  iterator ()

Returns

poll

public T poll (long timeout, 
                TimeUnit unit)

Retrieves and removes the minimum (as judged by the provided ERROR(/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 ERROR(/TimeUnit) of timeout

Returns
T the minimum matched element or null if there are no matching elements

poll

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

Retrieves and removes the minimum (as judged by the provided ERROR(/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 ERROR(/TimeUnit) of timeout

matcher IMatcher: the IMatcher to use to evaluate elements

Returns
T the minimum matched element or null if there are no matching elements

poll

public T poll (IMatcher<T> matcher)

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

Parameters
matcher IMatcher: the IMatcher to use to evaluate elements

Returns
T the minimum matched element or null if there are no matching elements

poll

public 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

public 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

public int size ()

Returns
int the number of elements in queue

take

public T take ()

Retrieves and removes the minimum (as judged by the provided ERROR(/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

public 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 IMatcher to use to evaluate elements

Returns
T the matched element

Throws
InterruptedException if interrupted while waiting