Top Description Fields Constructors Methods
org.apache.commons.collections4

public Class QueueUtils

extends Object
Class Inheritance
Imports
java.util.LinkedList, .Queue, org.apache.commons.collections4.queue.PredicatedQueue, .SynchronizedQueue, .TransformedQueue, .UnmodifiableQueue

Provides utility methods and decorators for Queue instances.
Since
4.0

Field Summary

Modifier and TypeField and Description
public static final Queue<E>
EMPTY_QUEUE

An empty unmodifiable queue.

Constructor Summary

AccessConstructor and Description
private
QueueUtils()

QueueUtils should not normally be instantiated.

Method Summary

Modifier and TypeMethod and Description
public static <
the type of the elements in the queue
E
>
Queue<E>

Returns:

an empty Queue
emptyQueue
()

Get an empty Queue.

public static <
the type of the elements in the queue
E
>
Queue<E>

Returns:

a predicated queue
predicatedQueue
(final Queue<E>
the queue to predicate, must not be null
queue
,
final Predicate<? super E>
the predicate used to evaluate new elements, must not be null
predicate
)

Returns a predicated (validating) queue backed by the given queue.

public static <
the element type
E
>
Queue<E>

Returns:

a synchronized queue backed by that queue
synchronizedQueue
(final Queue<E>
the queue to synchronize, must not be null
queue
)

Returns a synchronized (thread-safe) queue backed by the given queue.

public static <
the type of the elements in the queue
E
>
Queue<E>

Returns:

a transformed queue backed by the given queue
transformingQueue
(final Queue<E>
the queue to predicate, must not be null
queue
,
final Transformer<? super E, ? extends E>
the transformer for the queue, must not be null
transformer
)

Returns a transformed queue backed by the given queue.

public static <
the type of the elements in the queue
E
>
Queue<E>

Returns:

an unmodifiable queue backed by that queue
unmodifiableQueue
(final Queue<? extends E>
the queue to make unmodifiable, must not be null
queue
)

Returns an unmodifiable queue backed by the given queue.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_QUEUEback to summary
public static final Queue<E> EMPTY_QUEUE

An empty unmodifiable queue.

Annotations
@SuppressWarnings:rawtypes

Constructor Detail

QueueUtilsback to summary
private QueueUtils()

QueueUtils should not normally be instantiated.

Method Detail

emptyQueueback to summary
public static <E> Queue<E> emptyQueue()

Get an empty Queue.

Parameters
<E>
the type of the elements in the queue
Returns:Queue<E>

an empty Queue

Annotations
@SuppressWarnings:unchecked
predicatedQueueback to summary
public static <E> Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate)

Returns a predicated (validating) queue backed by the given queue.

Only objects that pass the test in the given predicate can be added to the queue. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original queue after invoking this method, as it is a backdoor for adding invalid objects.

Parameters
<E>
the type of the elements in the queue
queue:Queue<E>

the queue to predicate, must not be null

predicate:Predicate<? super E>

the predicate used to evaluate new elements, must not be null

Returns:Queue<E>

a predicated queue

Exceptions
NullPointerException:
if the queue or predicate is null
synchronizedQueueback to summary
public static <E> Queue<E> synchronizedQueue(final Queue<E> queue)

Returns a synchronized (thread-safe) queue backed by the given queue. In order to guarantee serial access, it is critical that all access to the backing queue is accomplished through the returned queue.

It is imperative that the user manually synchronize on the returned queue when iterating over it:

Queue queue = QueueUtils.synchronizedQueue(new CircularFifoQueue());
...
synchronized(queue) {
    Iterator i = queue.iterator(); // Must be in synchronized block
    while (i.hasNext())
        foo(i.next());
    }
}
Failure to follow this advice may result in non-deterministic behavior.
Parameters
<E>
the element type
queue:Queue<E>

the queue to synchronize, must not be null

Returns:Queue<E>

a synchronized queue backed by that queue

Exceptions
NullPointerException:
if the queue is null
Since
4.2
transformingQueueback to summary
public static <E> Queue<E> transformingQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer)

Returns a transformed queue backed by the given queue.

Each object is passed through the transformer as it is added to the Queue. It is important not to use the original queue after invoking this method, as it is a backdoor for adding untransformed objects.

Existing entries in the specified queue will not be transformed. If you want that behaviour, see TransformedQueue#transformedQueue.

Parameters
<E>
the type of the elements in the queue
queue:Queue<E>

the queue to predicate, must not be null

transformer:Transformer<? super E, ? extends E>

the transformer for the queue, must not be null

Returns:Queue<E>

a transformed queue backed by the given queue

Exceptions
NullPointerException:
if the queue or transformer is null
unmodifiableQueueback to summary
public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue)

Returns an unmodifiable queue backed by the given queue.

Parameters
<E>
the type of the elements in the queue
queue:Queue<? extends E>

the queue to make unmodifiable, must not be null

Returns:Queue<E>

an unmodifiable queue backed by that queue

Exceptions
NullPointerException:
if the queue is null