Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License
  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  * or implied. See the License for the specific language governing permissions and limitations under
  * the License.
  */
 
 package com.google.common.collect;
 
 
Static utility methods pertaining to java.util.Queue and java.util.Deque instances. Also see this class's counterparts com.google.common.collect.Lists, Sets, and com.google.common.collect.Maps.

Author(s):
Kurt Alfred Kluever
Since:
11.0
 
 public final class Queues {
   private Queues() {}
 
   // ArrayBlockingQueue
 
  
Creates an empty ArrayBlockingQueue instance.

Returns:
a new, empty ArrayBlockingQueue
 
   public static <E> ArrayBlockingQueue<E> newArrayBlockingQueue(int capacity) {
     return new ArrayBlockingQueue<E>(capacity);
   }
 
   // ArrayDeque
 
  
Creates an empty ArrayDeque instance.

Returns:
a new, empty ArrayDeque
Since:
12.0
 
   public static <E> ArrayDeque<E> newArrayDeque() {
     return new ArrayDeque<E>();
   }

  
Creates an ArrayDeque instance containing the given elements.

Parameters:
elements the elements that the queue should contain, in order
Returns:
a new 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(dequeelements);
     return deque;
   }
 
   // ConcurrentLinkedQueue
 
  
Creates an empty ConcurrentLinkedQueue instance.

Returns:
a new, empty ConcurrentLinkedQueue
 
   public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue() {
     return new ConcurrentLinkedQueue<E>();
   }

  
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
      Iterable<? extends E> elements) {
    if (elements instanceof Collection) {
      return new ConcurrentLinkedQueue<E>(Collections2.cast(elements));
    }
    ConcurrentLinkedQueue<E> queue = new ConcurrentLinkedQueue<E>();
    Iterables.addAll(queueelements);
    return queue;
  }
  // LinkedBlockingDeque

  
Creates an empty LinkedBlockingDeque instance.

Returns:
a new, empty LinkedBlockingDeque
Since:
12.0
  public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque() {
    return new LinkedBlockingDeque<E>();
  }

  
Creates a LinkedBlockingDeque with the given (fixed) capacity.

Parameters:
capacity the capacity of this deque
Returns:
a new, empty LinkedBlockingDeque
Throws:
java.lang.IllegalArgumentException if capacity is less than 1
Since:
12.0
  public static <E> LinkedBlockingDeque<E> newLinkedBlockingDeque(int capacity) {
    return new LinkedBlockingDeque<E>(capacity);
  }

  
Creates an LinkedBlockingDeque instance containing the given elements.

Parameters:
elements the elements that the queue should contain, in order
Returns:
a new 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(dequeelements);
    return deque;
  }
  // LinkedBlockingQueue

  
Creates an empty LinkedBlockingQueue instance.

Returns:
a new, empty LinkedBlockingQueue
  public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue() {
    return new LinkedBlockingQueue<E>();
  }

  
Creates a LinkedBlockingQueue with the given (fixed) capacity.

Parameters:
capacity the capacity of this queue
Returns:
a new, empty LinkedBlockingQueue
Throws:
java.lang.IllegalArgumentException if capacity is less than 1
  public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(int capacity) {
    return new LinkedBlockingQueue<E>(capacity);
  }

  
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
  public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(Iterable<? extends E> elements) {
    if (elements instanceof Collection) {
      return new LinkedBlockingQueue<E>(Collections2.cast(elements));
    }
    LinkedBlockingQueue<E> queue = new LinkedBlockingQueue<E>();
    Iterables.addAll(queueelements);
    return queue;
  }
  // LinkedList: see {@link com.google.common.collect.Lists}
  // PriorityBlockingQueue

  
Creates an empty PriorityBlockingQueue instance.

Returns:
a new, empty PriorityBlockingQueue
  public static <E> PriorityBlockingQueue<E> newPriorityBlockingQueue() {
    return new PriorityBlockingQueue<E>();
  }

  
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
      Iterable<? extends E> elements) {
    if (elements instanceof Collection) {
      return new PriorityBlockingQueue<E>(Collections2.cast(elements));
    }
    PriorityBlockingQueue<E> queue = new PriorityBlockingQueue<E>();
    Iterables.addAll(queueelements);
    return queue;
  }
  // PriorityQueue

  
Creates an empty PriorityQueue instance.

Returns:
a new, empty PriorityQueue
  public static <E> PriorityQueue<E> newPriorityQueue() {
    return new PriorityQueue<E>();
  }

  
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
  public static <E> PriorityQueue<E> newPriorityQueue(Iterable<? extends E> elements) {
    if (elements instanceof Collection) {
      return new PriorityQueue<E>(Collections2.cast(elements));
    }
    PriorityQueue<E> queue = new PriorityQueue<E>();
    Iterables.addAll(queueelements);
    return queue;
  }
  // SynchronousQueue

  
Creates an empty SynchronousQueue instance.

Returns:
a new, empty SynchronousQueue
  public static <E> SynchronousQueue<E> newSynchronousQueue() {
    return new SynchronousQueue<E>();
  }
  
  
Drains the queue as java.util.concurrent.BlockingQueue.drainTo(java.util.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:
java.lang.InterruptedException if interrupted while waiting
  public static <E> int drain(BlockingQueue<E> qCollection<? super E> bufferint numElements,
      long timeoutTimeUnit unitthrows InterruptedException {
    Preconditions.checkNotNull(buffer);
    /*
     * This code performs one System.nanoTime() more than necessary, and in return, the time to
     * execute Queue#drainTo is not added *on top* of waiting for the timeout (which could make
     * the timeout arbitrarily inaccurate, given a queue that is slow to drain).
     */
    long deadline = System.nanoTime() + unit.toNanos(timeout);
    int added = 0;
    while (added < numElements) {
      // 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(buffernumElements - added);
      if (added < numElements) { // not enough elements immediately available; will have to poll
        E e = q.poll(deadline - System.nanoTime(), .);
        if (e == null) {
          break// we already waited enough, and there are no more elements in sight
        }
        buffer.add(e);
        added++;
      }
    }
    return added;
  }
  
  
Drains the queue as drain(java.util.concurrent.BlockingQueue,java.util.Collection,int,long,java.util.concurrent.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
  public static <E> int drainUninterruptibly(BlockingQueue<E> qCollection<? super E> buffer
      int numElementslong timeoutTimeUnit unit) {
    Preconditions.checkNotNull(buffer);
    long deadline = System.nanoTime() + unit.toNanos(timeout);
    int added = 0;
    boolean interrupted = false;
    try {
      while (added < numElements) {
        // 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(buffernumElements - added);
        if (added < numElements) { // not enough elements immediately available; will have to poll
          E e// written exactly once, by a successful (uninterrupted) invocation of #poll
          while (true) {
            try {
              e = q.poll(deadline - System.nanoTime(), .);
              break;
            } catch (InterruptedException ex) {
              interrupted = true// note interruption and retry
            }
          }
          if (e == null) {
            break// we already waited enough, and there are no more elements in sight
          }
          buffer.add(e);
          added++;
        }
      }
    } finally {
      if (interrupted) {
        Thread.currentThread().interrupt();
      }
    }
    return added;
  }
New to GrepCode? Check out our FAQ X