/** * A sensible definition of {@link #addAll} in terms of {@link #add}. If you override {@link * #add}, you may wish to override {@link #addAll} to forward to this implementation. * * @since 7.0 */ protected boolean standardAddAll(Collection<? extends E> collection) { return Iterators.addAll(this, collection.iterator()); }
/** * Creates a <i>mutable</i> {@code HashSet} instance containing the given elements. A very thin * convenience for creating an empty set and then calling {@link Iterators#addAll}. * * <p><b>Note:</b> if mutability is not required and the elements are non-null, use {@link * ImmutableSet#copyOf(Iterator)} instead. * * <p><b>Note:</b> if {@code E} is an {@link Enum} type, you should create an {@link EnumSet} * instead. * * <p>Overall, this method is not very useful and will likely be deprecated in the future. */ public static <E> HashSet<E> newHashSet(Iterator<? extends E> elements) { HashSet<E> set = newHashSet(); Iterators.addAll(set, elements); return set; }
private static <E> ArrayList<E> toArrayList(Collection<E> c) { // Avoid calling ArrayList(Collection), which may call back into toArray. ArrayList<E> result = new ArrayList<>(c.size()); Iterators.addAll(result, c.iterator()); return result; }
private static <E> ArrayList<E> toArrayList(Collection<E> c) { // Avoid calling ArrayList(Collection), which may call back into toArray. ArrayList<E> result = new ArrayList<E>(c.size()); Iterators.addAll(result, c.iterator()); return result; }
/** * A sensible definition of {@link #addAll} in terms of {@link #add}. If you override {@link * #add}, you may wish to override {@link #addAll} to forward to this implementation. * * @since 7.0 */ protected boolean standardAddAll(Collection<? extends E> collection) { return Iterators.addAll(this, collection.iterator()); }
private static <E> ArrayList<E> toArrayList(Collection<E> c) { // Avoid calling ArrayList(Collection), which may call back into toArray. ArrayList<E> result = new ArrayList<E>(c.size()); Iterators.addAll(result, c.iterator()); return result; }
private static <E> ArrayList<E> toArrayList(Collection<E> c) { // Avoid calling ArrayList(Collection), which may call back into toArray. ArrayList<E> result = new ArrayList<>(c.size()); Iterators.addAll(result, c.iterator()); return result; }
/** An implementation of {@link Multiset#addAll}. */ static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) { checkNotNull(self); checkNotNull(elements); if (elements instanceof Multiset) { return addAllImpl(self, cast(elements)); } else if (elements.isEmpty()) { return false; } else { return Iterators.addAll(self, elements.iterator()); } }
/** * Adds all elements in {@code iterable} to {@code collection}. * * @return {@code true} if {@code collection} was modified as a result of this operation. */ @CanIgnoreReturnValue 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()); }
@CollectionSize.Require(SEVERAL) public void testDescendingNavigation() { List<Entry<E>> ascending = new ArrayList<>(); Iterators.addAll(ascending, sortedMultiset.entrySet().iterator()); List<Entry<E>> descending = new ArrayList<>(); Iterators.addAll(descending, sortedMultiset.descendingMultiset().entrySet().iterator()); Collections.reverse(descending); assertEquals(ascending, descending); }
/** * Creates a <i>mutable</i> {@code ArrayList} instance containing the given elements; a very thin * shortcut for creating an empty list and then calling {@link Iterators#addAll}. * * <p><b>Note:</b> if mutability is not required and the elements are non-null, use {@link * ImmutableList#copyOf(Iterator)} instead. */ @CanIgnoreReturnValue // TODO(kak): Remove this @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements) { ArrayList<E> list = newArrayList(); Iterators.addAll(list, elements); return list; }
@CanIgnoreReturnValue @Override public boolean putAll(@Nullable K key, Iterable<? extends V> values) { checkNotNull(values); // make sure we only call values.iterator() once // and we only call get(key) if values is nonempty if (values instanceof Collection) { Collection<? extends V> valueCollection = (Collection<? extends V>) values; return !valueCollection.isEmpty() && get(key).addAll(valueCollection); } else { Iterator<? extends V> valueItr = values.iterator(); return valueItr.hasNext() && Iterators.addAll(get(key), valueItr); } }
@Override public Enumeration<URL> getResources(final String name) throws IOException { final List<URL> urls = new ArrayList<>(); Iterators.addAll(urls, Iterators.forEnumeration(super.getResources(name))); Iterators.addAll(urls, Iterators.forEnumeration(druidLoader.getResources(name))); return Iterators.asEnumeration(urls.iterator()); } }
if (itr.hasNext()) { EnumSet<E> enumSet = EnumSet.of(itr.next()); Iterators.addAll(enumSet, itr); return ImmutableEnumSet.asImmutable(enumSet); } else {
/** * Returns an immutable multiset containing the given elements, in the "grouped iteration order" * described in the class documentation. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableMultiset<E> copyOf(Iterator<? extends E> elements) { Multiset<E> multiset = LinkedHashMultiset.create(); Iterators.addAll(multiset, elements); return copyFromEntries(multiset.entrySet()); }
public static EntryArrayList of(Iterable<? extends Entry> i) { // This is adapted from Guava's Lists.newArrayList implementation EntryArrayList result; if (i instanceof Collection) { // Let ArrayList's sizing logic work, if possible result = new EntryArrayList((Collection)i); } else { // Unknown size result = new EntryArrayList(); Iterators.addAll(result, i.iterator()); } return result; }
public void testAddAllToList() { List<String> alreadyThere = Lists.newArrayList("already", "there"); List<String> freshlyAdded = Lists.newArrayList("freshly", "added"); boolean changed = Iterators.addAll(alreadyThere, freshlyAdded.iterator()); assertThat(alreadyThere).containsExactly("already", "there", "freshly", "added"); assertTrue(changed); }
public void testAddAllToSet() { Set<String> alreadyThere = Sets.newLinkedHashSet(asList("already", "there")); List<String> oneMore = Lists.newArrayList("there"); boolean changed = Iterators.addAll(alreadyThere, oneMore.iterator()); assertThat(alreadyThere).containsExactly("already", "there").inOrder(); assertFalse(changed); }
@Override public ListenableFuture<?> spill(Iterator<Page> pageIterator) { checkState(writing, "writing already finished"); if (failSpill) { return immediateFailedFuture(new PrestoException(GENERIC_INTERNAL_ERROR, "Spill failed")); } Iterators.addAll(spills, pageIterator); return immediateFuture(null); }
public void testAddAllWithEmptyIterator() { List<String> alreadyThere = Lists.newArrayList("already", "there"); boolean changed = Iterators.addAll(alreadyThere, Iterators.<String>emptyIterator()); assertThat(alreadyThere).containsExactly("already", "there").inOrder(); assertFalse(changed); }