to top
public class

ConditionPriorityBlockingQueue

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

Class Overview

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)
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()
Retrieves and removes the head of this queue.
T poll(long timeout, TimeUnit unit)
Retrieves and removes the minimum (as judged by the provided 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 Comparator element T in the queue where matcher.matches(T) is true.
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.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.lang.Iterable

Public Constructors

public ConditionPriorityBlockingQueue ()

Creates a ConditionPriorityBlockingQueue

Elements will be prioritized in FIFO order.

public ConditionPriorityBlockingQueue (Comparator<T> c)

Parameters
c the Comparator used to prioritize the queue.

Public Methods

public boolean add (T addedElement)

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

Parameters
addedElement the element to add
Returns
  • 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

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

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

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

public void clear ()

Removes all elements from this queue.

public boolean contains (T object)

Determine if an object is currently contained in this queue.

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

public List<T> getCopy ()

Get a copy of the contents of the queue.

public Iterator<T> iterator ()

public 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 the ConditionPriorityBlockingQueue.IMatcher to use to evaluate elements
Returns
  • the minimum matched element or null if there are no matching elements

public T poll ()

Retrieves and removes the head of this queue.

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

public 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 the amount of time to wait for an element to become available
unit the TimeUnit of timeout
Returns
  • the minimum matched element or null if there are no matching elements
Throws
InterruptedException

public 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 the amount of time to wait for an element to become available
unit the TimeUnit of timeout
matcher the ConditionPriorityBlockingQueue.IMatcher to use to evaluate elements
Returns
  • the minimum matched element or null if there are no matching elements
Throws
InterruptedException

public boolean remove (T object)

Removes an item from this queue.

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

public int size ()

Returns
  • the number of elements in queue

public 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
  • the head of this queue
Throws
InterruptedException if interrupted while waiting

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 the ConditionPriorityBlockingQueue.IMatcher to use to evaluate elements
Returns
  • the matched element
Throws
InterruptedException if interrupted while waiting