示例#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());
 }
示例#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;
 }
示例#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;
 }
示例#4
0
 /**
  * Creates a <i>mutable</i> {@code ArrayList} instance containing the given elements; a very thin
  * shortcut for creating an empty list then calling {@link Iterables#addAll}.
  *
  * <p><b>Note:</b> if mutability is not required and the elements are non-null, use {@link
  * ImmutableList#copyOf(Iterable)} instead. (Or, change {@code elements} to be a {@link
  * FluentIterable} and call {@code elements.toList()}.)
  *
  * <p><b>Note for Java 7 and later:</b> if {@code elements} is a {@link Collection}, you don't
  * need this method. Use the {@code ArrayList} {@linkplain ArrayList#ArrayList(Collection)
  * constructor} directly, taking advantage of the new <a href="http://goo.gl/iz2Wi">"diamond"
  * syntax</a>.
  */
 @GwtCompatible(serializable = true)
 public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) {
   checkNotNull(elements); // for GWT
   // Let ArrayList's sizing logic work, if possible
   return (elements instanceof Collection)
       ? new ArrayList<E>(Collections2.cast(elements))
       : newArrayList(elements.iterator());
 }
示例#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;
 }
示例#6
0
 /**
  * Creates a {@code CopyOnWriteArrayList} instance containing the given elements.
  *
  * @param elements the elements that the list should contain, in order
  * @return a new {@code CopyOnWriteArrayList} containing those elements
  * @since 12.0
  */
 @GwtIncompatible("CopyOnWriteArrayList")
 public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(
     Iterable<? extends E> elements) {
   // We copy elements to an ArrayList first, rather than incurring the
   // quadratic cost of adding them to the COWAL directly.
   Collection<? extends E> elementsCollection =
       (elements instanceof Collection) ? Collections2.cast(elements) : newArrayList(elements);
   return new CopyOnWriteArrayList<E>(elementsCollection);
 }
示例#7
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);
  }
示例#8
0
 /**
  * Returns an immutable list containing the given elements, in order. If {@code elements} is a
  * {@link java.util.Collection}, this method behaves exactly as {@link
  * #copyOf(java.util.Collection)}; otherwise, it behaves exactly as {@code
  * copyOf(elements.iterator()}.
  *
  * @throws NullPointerException if any of {@code elements} is null
  */
 public static <E> ImmutableList<E> copyOf(Iterable<? extends E> elements) {
   checkNotNull(elements); // TODO(kevinb): is this here only for GWT?
   return (elements instanceof Collection)
       ? copyOf(Collections2.cast(elements))
       : copyOf(elements.iterator());
 }