Ejemplo n.º 1
0
 /**
  * Adds all elements in {@code iterable} to {@code collection}.
  *
  * @return {@code true} if {@code collection} was modified as a result of this operation.
  */
 public static <T> boolean addAll(Collection<T> addTo, Iterable<? extends T> elementsToAdd) {
   if (elementsToAdd instanceof Collection) {
     Collection<? extends T> c = Collections2.cast(elementsToAdd);
     return addTo.addAll(c);
   }
   return Iterators.addAll(addTo, checkNotNull(elementsToAdd).iterator());
 }
Ejemplo n.º 2
0
 /**
  * Creates an {@code ArrayDeque} containing the elements of the specified iterable, in the order
  * they are returned by the iterable's iterator.
  *
  * @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;
 }
Ejemplo n.º 3
0
 /**
  * Creates a {@code LinkedBlockingQueue} with a capacity of {@link Integer#MAX_VALUE}, containing
  * the elements of the specified iterable, in the order they are returned by the iterable's
  * iterator.
  *
  * @param elements the elements that the queue should contain, in order
  * @return a new {@code 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(queue, elements);
   return queue;
 }
Ejemplo n.º 4
0
 /**
  * Creates a {@code PriorityQueue} containing the given elements.
  *
  * <p><b>Note:</b> If the specified iterable is a {@code SortedSet} or a {@code PriorityQueue},
  * this priority queue will be ordered according to the same ordering.
  *
  * @since 11.0 (requires that {@code E} be {@code Comparable} since 15.0).
  */
 public static <E extends Comparable> 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(queue, elements);
   return queue;
 }
Ejemplo n.º 5
0
  /**
   * Returns the last element of {@code iterable} or {@code defaultValue} if the iterable is empty.
   *
   * @param defaultValue the value to return if {@code iterable} is empty
   * @return the last element of {@code iterable} or the default value
   * @since 3.0
   */
  @Nullable
  public static <T> T getLast(Iterable<? extends T> iterable, @Nullable T defaultValue) {
    if (iterable instanceof Collection) {
      Collection<? extends T> c = Collections2.cast(iterable);
      if (c.isEmpty()) {
        return defaultValue;
      } else if (iterable instanceof List) {
        return getLastInNonemptyList(Lists.cast(iterable));
      }
    }

    return Iterators.getLast(iterable.iterator(), defaultValue);
  }