Filter filter = findOnlyOperator(plan, Filter.class); Assert.assertTrue(filter.getInput() instanceof Scan); Project[] projects = Iterables.toArray(findOperator(plan, Project.class), Project.class); Assert.assertEquals(2, projects.length); Assert.assertEquals(1, projects[0].getSelections().size());
/** * Copies an iterable's elements into an array. * * @param iterable the iterable to copy * @param type the type of the elements * @return a newly-allocated array into which all the elements of the iterable * have been copied */ @GwtIncompatible // Array.newInstance(Class, int) public static <T> T[] toArray(Iterable<? extends T> iterable, Class<T> type) { return toArray(iterable, ObjectArrays.newArray(type, 0)); }
/** * Copies an iterator's elements into an array. The iterator will be left * exhausted: its {@code hasNext()} method will return {@code false}. * * @param iterator the iterator to copy * @param type the type of the elements * @return a newly-allocated array into which all the elements of the iterator * have been copied */ @GwtIncompatible // Array.newInstance(Class, int) public static <T> T[] toArray(Iterator<? extends T> iterator, Class<T> type) { List<T> list = Lists.newArrayList(iterator); return Iterables.toArray(list, type); }
/** * Returns a <b>mutable</b> list containing {@code elements} sorted by this ordering; use this * only when the resulting list may need further modification, or may contain {@code null}. The * input is not modified. The returned list is serializable and has random access. * * <p>Unlike {@link Sets#newTreeSet(Iterable)}, this method does not discard elements that are * duplicates according to the comparator. The sort performed is <i>stable</i>, meaning that such * elements will appear in the returned list in the same order they appeared in {@code elements}. * * <p><b>Performance note:</b> According to our * benchmarking * on Open JDK 7, {@link #immutableSortedCopy} generally performs better (in both time and space) * than this method, and this method in turn generally performs better than copying the list and * calling {@link Collections#sort(List)}. */ // TODO(kevinb): rerun benchmarks including new options @CanIgnoreReturnValue // TODO(kak): Consider removing this public <E extends T> List<E> sortedCopy(Iterable<E> elements) { @SuppressWarnings("unchecked") // does not escape, and contains only E's E[] array = (E[]) Iterables.toArray(elements); Arrays.sort(array, this); return Lists.newArrayList(Arrays.asList(array)); }
/** * Returns an immutable list containing the given elements, sorted according to their natural * order. The sorting algorithm used is stable, so elements that compare as equal will stay in the * order in which they appear in the input. * * <p>If your data has no duplicates, or you wish to deduplicate elements, use {@code * ImmutableSortedSet.copyOf(elements)}; if you want a {@code List} you can use its {@code * asList()} view. * * <p><b>Java 8 users:</b> If you want to convert a {@link java.util.stream.Stream} to a sorted * {@code ImmutableList}, use {@code stream.sorted().collect(toImmutableList())}. * * @throws NullPointerException if any element in the input is null * @since 21.0 */ public static <E extends Comparable<? super E>> ImmutableList<E> sortedCopyOf( Iterable<? extends E> elements) { Comparable<?>[] array = Iterables.toArray(elements, new Comparable<?>[0]); checkElementsNotNull((Object[]) array); Arrays.sort(array); return asImmutableList(array); }
/** * Returns an array containing all of the elements from this fluent iterable in iteration order. * * <p><b>{@code Stream} equivalent:</b> if an object array is acceptable, use * {@code stream.toArray()}; if {@code type} is a class literal such as {@code MyType.class}, use * {@code stream.toArray(MyType[]::new)}. Otherwise use {@code stream.toArray( * len -> (E[]) Array.newInstance(type, len))}. * * @param type the type of the elements * @return a newly-allocated array into which all the elements of this fluent iterable have been * copied */ @GwtIncompatible // Array.newArray(Class, int) public final E[] toArray(Class<E> type) { return Iterables.toArray(getDelegate(), type); }
/** * Returns an immutable list containing the given elements, in sorted order relative to the * specified comparator. The sorting algorithm used is stable, so elements that compare as equal * will stay in the order in which they appear in the input. * * <p>If your data has no duplicates, or you wish to deduplicate elements, use {@code * ImmutableSortedSet.copyOf(comparator, elements)}; if you want a {@code List} you can use its * {@code asList()} view. * * <p><b>Java 8 users:</b> If you want to convert a {@link java.util.stream.Stream} to a sorted * {@code ImmutableList}, use {@code stream.sorted(comparator).collect(toImmutableList())}. * * @throws NullPointerException if any element in the input is null * @since 21.0 */ public static <E> ImmutableList<E> sortedCopyOf( Comparator<? super E> comparator, Iterable<? extends E> elements) { checkNotNull(comparator); @SuppressWarnings("unchecked") // all supported methods are covariant E[] array = (E[]) Iterables.toArray(elements); checkElementsNotNull(array); Arrays.sort(array, comparator); return asImmutableList(array); }
/** * Returns an immutable bimap containing the given entries. The returned bimap iterates over * entries in the same order as the original iterable. * * @throws IllegalArgumentException if two keys have the same value or two * values have the same key * @throws NullPointerException if any key, value, or entry is null * @since 19.0 */ @Beta public static <K, V> ImmutableBiMap<K, V> copyOf( Iterable<? extends Entry<? extends K, ? extends V>> entries) { @SuppressWarnings("unchecked") // we'll only be using getKey and getValue, which are covariant Entry<K, V>[] entryArray = (Entry<K, V>[]) Iterables.toArray(entries, EMPTY_ENTRY_ARRAY); switch (entryArray.length) { case 0: return of(); case 1: Entry<K, V> entry = entryArray[0]; return of(entry.getKey(), entry.getValue()); default: /* * The current implementation will end up using entryArray directly, though it will write * over the (arbitrary, potentially mutable) Entry objects actually stored in entryArray. */ return RegularImmutableBiMap.fromEntries(entryArray); } }
/** * Returns an immutable map containing the specified entries. The returned * map iterates over entries in the same order as the original iterable. * * @throws NullPointerException if any key, value, or entry is null * @throws IllegalArgumentException if two entries have the same key * @since 19.0 */ @Beta public static <K, V> ImmutableMap<K, V> copyOf( Iterable<? extends Entry<? extends K, ? extends V>> entries) { @SuppressWarnings("unchecked") // we'll only be using getKey and getValue, which are covariant Entry<K, V>[] entryArray = (Entry<K, V>[]) Iterables.toArray(entries, EMPTY_ENTRY_ARRAY); switch (entryArray.length) { case 0: return of(); case 1: Entry<K, V> onlyEntry = entryArray[0]; return of(onlyEntry.getKey(), onlyEntry.getValue()); default: /* * The current implementation will end up using entryArray directly, though it will write * over the (arbitrary, potentially mutable) Entry objects actually stored in entryArray. */ return RegularImmutableMap.fromEntries(entryArray); } }
final Object[] array = Iterables.toArray(keys, Object.class); if (array.length == 0) { return ImmutableList.of();
/** * Returns an immutable sorted set containing the given elements sorted by * the given {@code Comparator}. When multiple elements are equivalent * according to {@code compare()}, only the first one specified is * included. This method iterates over {@code elements} at most once. * * <p>Despite the method name, this method attempts to avoid actually copying * the data when it is safe to do so. The exact circumstances under which a * copy will or will not be performed are undocumented and subject to change. * * @throws NullPointerException if {@code comparator} or any of {@code * elements} is null */ public static <E> ImmutableSortedSet<E> copyOf( Comparator<? super E> comparator, Iterable<? extends E> elements) { checkNotNull(comparator); boolean hasSameComparator = SortedIterables.hasSameComparator(comparator, elements); if (hasSameComparator && (elements instanceof ImmutableSortedSet)) { @SuppressWarnings("unchecked") ImmutableSortedSet<E> original = (ImmutableSortedSet<E>) elements; if (!original.isPartialView()) { return original; } } @SuppressWarnings("unchecked") // elements only contains E's; it's safe. E[] array = (E[]) Iterables.toArray(elements); return construct(comparator, array.length, array); }
private static <K, V> ImmutableSortedMap<K, V> fromEntries( final Comparator<? super K> comparator, boolean sameComparator, Entry<K, V>[] entryArray, int size) { switch (size) { case 0: return emptyMap(comparator); case 1: return ImmutableSortedMap.<K, V>of( comparator, entryArray[0].getKey(), entryArray[0].getValue()); default: Object[] keys = new Object[size]; Object[] values = new Object[size]; if (sameComparator) { // Need to check for nulls, but don't need to sort or validate. for (int i = 0; i < size; i++) { Object key = entryArray[i].getKey(); Object value = entryArray[i].getValue(); checkEntryNotNull(key, value); keys[i] = key; values[i] = value; } } else { // Need to sort and check for nulls and dupes. // Inline the Comparator implementation rather than transforming with a Function // to save code size. Arrays.sort(entryArray, 0, size, new Comparator<Entry<K, V>>() { @Override public int compare(Entry<K, V> e1, Entry<K, V> e2) {