diff options
Diffstat (limited to 'guava/src/com/google/common/collect/Queues.java')
-rw-r--r-- | guava/src/com/google/common/collect/Queues.java | 127 |
1 files changed, 15 insertions, 112 deletions
diff --git a/guava/src/com/google/common/collect/Queues.java b/guava/src/com/google/common/collect/Queues.java index d2bf4ad..d68146a 100644 --- a/guava/src/com/google/common/collect/Queues.java +++ b/guava/src/com/google/common/collect/Queues.java @@ -19,7 +19,6 @@ import com.google.common.base.Preconditions; import java.util.ArrayDeque; import java.util.Collection; -import java.util.Deque; import java.util.PriorityQueue; import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; @@ -32,12 +31,14 @@ import java.util.concurrent.SynchronousQueue; import java.util.concurrent.TimeUnit; /** - * Static utility methods pertaining to {@link Queue} and {@link Deque} instances. - * Also see this class's counterparts {@link Lists}, {@link Sets}, and {@link Maps}. + * Static utility methods pertaining to {@link Queue} + * instances. Also see this class's counterparts + * {@link Lists}, {@link Sets}, and {@link Maps}. * * @author Kurt Alfred Kluever * @since 11.0 */ +@Beta public final class Queues { private Queues() {} @@ -54,32 +55,6 @@ public final class Queues { // ArrayDeque - /** - * Creates an empty {@code ArrayDeque} instance. - * - * @return a new, empty {@code ArrayDeque} - * @since 12.0 - */ - public static <E> ArrayDeque<E> newArrayDeque() { - return new ArrayDeque<E>(); - } - - /** - * Creates an {@code ArrayDeque} instance containing the given elements. - * - * @param elements the elements that the queue should contain, in order - * @return a new {@code ArrayDeque} containing those elements - * @since 12.0 - */ - public static <E> ArrayDeque<E> newArrayDeque(Iterable<? extends E> elements) { - if (elements instanceof Collection) { - return new ArrayDeque<E>(Collections2.cast(elements)); - } - ArrayDeque<E> deque = new ArrayDeque<E>(); - Iterables.addAll(deque, elements); - return deque; - } - // ConcurrentLinkedQueue /** @@ -109,44 +84,6 @@ public final class Queues { // LinkedBlockingDeque - /** - * Creates an empty {@code LinkedBlockingDeque} instance. - * - * @return a new, empty {@code LinkedBlockingDeque} - * @since 12.0 - */ - public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque() { - return new LinkedBlockingDeque<E>(); - } - - /** - * Creates a {@code LinkedBlockingDeque} with the given (fixed) capacity. - * - * @param capacity the capacity of this deque - * @return a new, empty {@code LinkedBlockingDeque} - * @throws IllegalArgumentException if {@code capacity} is less than 1 - * @since 12.0 - */ - public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque(int capacity) { - return new LinkedBlockingDeque<E>(capacity); - } - - /** - * Creates an {@code LinkedBlockingDeque} instance containing the given elements. - * - * @param elements the elements that the queue should contain, in order - * @return a new {@code LinkedBlockingDeque} containing those elements - * @since 12.0 - */ - public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque(Iterable<? extends E> elements) { - if (elements instanceof Collection) { - return new LinkedBlockingDeque<E>(Collections2.cast(elements)); - } - LinkedBlockingDeque<E> deque = new LinkedBlockingDeque<E>(); - Iterables.addAll(deque, elements); - return deque; - } - // LinkedBlockingQueue /** @@ -249,12 +186,12 @@ public final class Queues { public static <E> SynchronousQueue<E> newSynchronousQueue() { return new SynchronousQueue<E>(); } - + /** - * Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested + * Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested * {@code numElements} elements are not available, it will wait for them up to the specified * timeout. - * + * * @param q the blocking queue to be drained * @param buffer where to add the transferred elements * @param numElements the number of elements to be waited for @@ -263,7 +200,6 @@ public final class Queues { * @return the number of elements transferred * @throws InterruptedException if interrupted while waiting */ - @Beta public static <E> int drain(BlockingQueue<E> q, Collection<? super E> buffer, int numElements, long timeout, TimeUnit unit) throws InterruptedException { Preconditions.checkNotNull(buffer); @@ -289,13 +225,13 @@ public final class Queues { } return added; } - + /** - * Drains the queue as {@linkplain #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 {@code InterruptedException} is thrown). - * + * Drains the queue as {@linkplain #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 {@code InterruptedException} is thrown). + * * @param q the blocking queue to be drained * @param buffer where to add the transferred elements * @param numElements the number of elements to be waited for @@ -303,8 +239,7 @@ public final class Queues { * @param unit a {@code TimeUnit} determining how to interpret the timeout parameter * @return the number of elements transferred */ - @Beta - public static <E> int drainUninterruptibly(BlockingQueue<E> q, Collection<? super E> buffer, + public static <E> int drainUninterruptibly(BlockingQueue<E> q, Collection<? super E> buffer, int numElements, long timeout, TimeUnit unit) { Preconditions.checkNotNull(buffer); long deadline = System.nanoTime() + unit.toNanos(timeout); @@ -312,7 +247,7 @@ public final class Queues { boolean interrupted = false; try { while (added < numElements) { - // we could rely solely on #poll, but #drainTo might be more efficient when there are + // we could rely solely on #poll, but #drainTo might be more efficient when there are // multiple elements already available (e.g. LinkedBlockingQueue#drainTo locks only once) added += q.drainTo(buffer, numElements - added); if (added < numElements) { // not enough elements immediately available; will have to poll @@ -339,36 +274,4 @@ public final class Queues { } return added; } - - /** - * Returns a synchronized (thread-safe) queue backed by the specified queue. In order to - * guarantee serial access, it is critical that <b>all</b> access to the backing queue is - * accomplished through the returned queue. - * - * <p>It is imperative that the user manually synchronize on the returned queue when accessing - * the queue's iterator: <pre> {@code - * - * Queue<E> queue = Queues.synchronizedQueue(MinMaxPriorityQueue<E>.create()); - * ... - * queue.add(element); // Needn't be in synchronized block - * ... - * synchronized (queue) { // Must synchronize on queue! - * Iterator<E> i = queue.iterator(); // Must be in synchronized block - * while (i.hasNext()) { - * foo(i.next()); - * } - * }}</pre> - * - * Failure to follow this advice may result in non-deterministic behavior. - * - * <p>The returned queue will be serializable if the specified queue is serializable. - * - * @param queue the queue to be wrapped in a synchronized view - * @return a synchronized view of the specified queue - * @since 14.0 - */ - @Beta - public static <E> Queue<E> synchronizedQueue(Queue<E> queue) { - return Synchronized.queue(queue, null); - } } |