Example #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());
 }
Example #2
0
 /**
  * Returns {@code true} if {@code iterable} contains any object for which {@code equals(element)}
  * is true.
  */
 public static boolean contains(Iterable<?> iterable, @Nullable Object element) {
   if (iterable instanceof Collection) {
     Collection<?> collection = (Collection<?>) iterable;
     return Collections2.safeContains(collection, element);
   }
   return Iterators.contains(iterable.iterator(), element);
 }
Example #3
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;
 }
Example #4
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;
 }
Example #5
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;
 }
Example #6
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);
  }
Example #7
0
 @Override
 public boolean containsAll(Collection<?> c) {
   synchronized (mutex) {
     return Collections2.containsAllImpl(delegate(), c);
   }
 }
 /**
  * A sensible definition of {@link #toString} in terms of {@link #iterator}. If you override
  * {@link #iterator}, you may wish to override {@link #toString} to forward to this
  * implementation.
  *
  * @since 7.0
  */
 protected String standardToString() {
   return Collections2.toStringImpl(this);
 }
 /**
  * A sensible definition of {@link #containsAll} in terms of {@link #contains} . If you override
  * {@link #contains}, you may wish to override {@link #containsAll} to forward to this
  * implementation.
  *
  * @since 7.0
  */
 protected boolean standardContainsAll(Collection<?> collection) {
   return Collections2.containsAllImpl(this, collection);
 }