com.google.common.collect
Class Queues

java.lang.Object
  extended by com.google.common.collect.Queues

@Beta
public final class Queues
extends Object

Static utility methods pertaining to Queue instances. Also see this class's counterparts Lists, Sets, and Maps.

Since:
11.0
Author:
Kurt Alfred Kluever

Method Summary
static
<E> int
drain(BlockingQueue<E> q, Collection<? super E> buffer, int numElements, long timeout, TimeUnit unit)
          Drains the queue as BlockingQueue.drainTo(Collection, int), but if the requested numElements elements are not available, it will wait for them up to the specified timeout.
static
<E> int
drainUninterruptibly(BlockingQueue<E> q, Collection<? super E> buffer, int numElements, long timeout, TimeUnit unit)
          Drains the queue as drain(BlockingQueue, Collection, int, long, TimeUnit), but with a different behavior in case it is interrupted while waiting.
static
<E> ArrayBlockingQueue<E>
newArrayBlockingQueue(int capacity)
          Creates an empty ArrayBlockingQueue instance.
static
<E> ConcurrentLinkedQueue<E>
newConcurrentLinkedQueue()
          Creates an empty ConcurrentLinkedQueue instance.
static
<E> ConcurrentLinkedQueue<E>
newConcurrentLinkedQueue(Iterable<? extends E> elements)
          Creates an ConcurrentLinkedQueue instance containing the given elements.
static
<E> LinkedBlockingQueue<E>
newLinkedBlockingQueue()
          Creates an empty LinkedBlockingQueue instance.
static
<E> LinkedBlockingQueue<E>
newLinkedBlockingQueue(int capacity)
          Creates a LinkedBlockingQueue with the given (fixed) capacity.
static
<E> LinkedBlockingQueue<E>
newLinkedBlockingQueue(Iterable<? extends E> elements)
          Creates an LinkedBlockingQueue instance containing the given elements.
static
<E> PriorityBlockingQueue<E>
newPriorityBlockingQueue()
          Creates an empty PriorityBlockingQueue instance.
static
<E> PriorityBlockingQueue<E>
newPriorityBlockingQueue(Iterable<? extends E> elements)
          Creates an PriorityBlockingQueue instance containing the given elements.
static
<E> PriorityQueue<E>
newPriorityQueue()
          Creates an empty PriorityQueue instance.
static
<E> PriorityQueue<E>
newPriorityQueue(Iterable<? extends E> elements)
          Creates an PriorityQueue instance containing the given elements.
static
<E> SynchronousQueue<E>
newSynchronousQueue()
          Creates an empty SynchronousQueue instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newArrayBlockingQueue

public static <E> ArrayBlockingQueue<E> newArrayBlockingQueue(int capacity)
Creates an empty ArrayBlockingQueue instance.

Returns:
a new, empty ArrayBlockingQueue

newConcurrentLinkedQueue

public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue()
Creates an empty ConcurrentLinkedQueue instance.

Returns:
a new, empty ConcurrentLinkedQueue

newConcurrentLinkedQueue

public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue(Iterable<? extends E> elements)
Creates an ConcurrentLinkedQueue instance containing the given elements.

Parameters:
elements - the elements that the queue should contain, in order
Returns:
a new ConcurrentLinkedQueue containing those elements

newLinkedBlockingQueue

public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue()
Creates an empty LinkedBlockingQueue instance.

Returns:
a new, empty LinkedBlockingQueue

newLinkedBlockingQueue

public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(int capacity)
Creates a LinkedBlockingQueue with the given (fixed) capacity.

Parameters:
capacity - the capacity of this queue
Returns:
a new, empty LinkedBlockingQueue
Throws:
IllegalArgumentException - if capacity is less than 1

newLinkedBlockingQueue

public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(Iterable<? extends E> elements)
Creates an LinkedBlockingQueue instance containing the given elements.

Parameters:
elements - the elements that the queue should contain, in order
Returns:
a new LinkedBlockingQueue containing those elements

newPriorityBlockingQueue

public static <E> PriorityBlockingQueue<E> newPriorityBlockingQueue()
Creates an empty PriorityBlockingQueue instance.

Returns:
a new, empty PriorityBlockingQueue

newPriorityBlockingQueue

public static <E> PriorityBlockingQueue<E> newPriorityBlockingQueue(Iterable<? extends E> elements)
Creates an PriorityBlockingQueue instance containing the given elements.

Parameters:
elements - the elements that the queue should contain, in order
Returns:
a new PriorityBlockingQueue containing those elements

newPriorityQueue

public static <E> PriorityQueue<E> newPriorityQueue()
Creates an empty PriorityQueue instance.

Returns:
a new, empty PriorityQueue

newPriorityQueue

public static <E> PriorityQueue<E> newPriorityQueue(Iterable<? extends E> elements)
Creates an PriorityQueue instance containing the given elements.

Parameters:
elements - the elements that the queue should contain, in order
Returns:
a new PriorityQueue containing those elements

newSynchronousQueue

public static <E> SynchronousQueue<E> newSynchronousQueue()
Creates an empty SynchronousQueue instance.

Returns:
a new, empty SynchronousQueue

drain

public static <E> int drain(BlockingQueue<E> q,
                            Collection<? super E> buffer,
                            int numElements,
                            long timeout,
                            TimeUnit unit)
                 throws InterruptedException
Drains the queue as BlockingQueue.drainTo(Collection, int), but if the requested numElements elements are not available, it will wait for them up to the specified timeout.

Parameters:
q - the blocking queue to be drained
buffer - where to add the transferred elements
numElements - the number of elements to be waited for
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
the number of elements transferred
Throws:
InterruptedException - if interrupted while waiting

drainUninterruptibly

public static <E> int drainUninterruptibly(BlockingQueue<E> q,
                                           Collection<? super E> buffer,
                                           int numElements,
                                           long timeout,
                                           TimeUnit unit)
Drains the queue as drain(BlockingQueue, Collection, int, long, TimeUnit), but with a different behavior in case it is interrupted while waiting. In that case, the operation will continue as usual, and in the end the thread's interruption status will be set (no InterruptedException is thrown).

Parameters:
q - the blocking queue to be drained
buffer - where to add the transferred elements
numElements - the number of elements to be waited for
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
the number of elements transferred


Copyright © 2010-2012. All Rights Reserved.