/** * This method fully traverses an iterator of this iterable and returns * a new iterable with the same contents, but without any reference * to the originating iterables and/or iterators. * <p> * Calling this method is equivalent to: * <pre> * FluentIterable<E> someIterable = ...; * FluentIterable.of(someIterable.toList()); * </pre> * * @return a new iterable with the same contents as this iterable */ public FluentIterable<E> eval() { return of(toList()); }
/** * Returns a new FluentIterable whose iterator will first traverse * the elements of the current iterable, followed by the provided * elements. * * @param elements the elements to append to the iterable * @return a new iterable, combining this iterable with the elements */ public FluentIterable<E> append(final E... elements) { return append(Arrays.asList(elements)); }
/** * Constructs a new <code>ZippingIterator</code> that will provide * interleaved iteration of the specified iterators. * * @param iterators the array of iterators * @throws NullPointerException if any iterator is null */ public ZippingIterator(final Iterator<? extends E>... iterators) { // create a mutable list to be able to remove exhausted iterators final List<Iterator<? extends E>> list = new ArrayList<>(); for (final Iterator<? extends E> iterator : iterators) { if (iterator == null) { throw new NullPointerException("Iterator must not be null."); } list.add(iterator); } this.iterators = FluentIterable.of(list).loop().iterator(); }
/** * Creates a new FluentIterable from the provided elements. * <p> * The returned iterable's iterator does not support {@code remove()}. * * @param <T> the element type * @param elements the elements to be contained in the FluentIterable * @return a new FluentIterable containing the provided elements */ public static <T> FluentIterable<T> of(final T... elements) { return of(Arrays.asList(elements)); }
@Override public boolean putAll(final K key, final Iterable<? extends V> values) { if (values == null) { throw new NullPointerException("Values must not be null."); } final Iterable<V> transformedValues = FluentIterable.of(values).transform(valueTransformer); final Iterator<? extends V> it = transformedValues.iterator(); return it.hasNext() && CollectionUtils.addAll(decorated().get(transformKey(key)), it); }
/** * Returns an Enumeration that will enumerate all elements contained * in this iterable. * * @return an Enumeration over the elements of this iterable */ public Enumeration<E> asEnumeration() { return IteratorUtils.asEnumeration(iterator()); }
/** * Construct a new FluentIterable from the provided iterable. If the * iterable is already an instance of FluentIterable, the instance * will be returned instead. * <p> * The returned iterable's iterator supports {@code remove()} when the * corresponding input iterator supports it. * * @param <T> the element type * @param iterable the iterable to wrap into a FluentIterable, may not be null * @return a new FluentIterable wrapping the provided iterable * @throws NullPointerException if iterable is null */ public static <T> FluentIterable<T> of(final Iterable<T> iterable) { IterableUtils.checkNotNull(iterable); if (iterable instanceof FluentIterable<?>) { return (FluentIterable<T>) iterable; } return new FluentIterable<>(iterable); }
/** * Returns a new FluentIterable whose iterator will return at most * the provided maximum number of elements from this iterable. * * @param maxSize the maximum number of elements * @return a new iterable, providing a bounded view of this iterable * @throws IllegalArgumentException if maxSize is negative */ public FluentIterable<E> limit(final long maxSize) { return of(IterableUtils.boundedIterable(iterable, maxSize)); }
/** * Returns an array containing all elements of this iterable by traversing * its iterator. * * @param arrayClass the class of array to create * @return an array of the iterable contents * @throws ArrayStoreException if arrayClass is invalid */ public E[] toArray(final Class<E> arrayClass) { return IteratorUtils.toArray(iterator(), arrayClass); }
/** * Returns a new FluentIterable whose iterator will traverse the * elements from this iterable in reverse order. * * @return a new iterable, providing a reversed view of this iterable */ public FluentIterable<E> reverse() { return of(IterableUtils.reversedIterable(iterable)); }
/** * Returns a new FluentIterable whose iterator will return an unmodifiable * view of this iterable. * * @return a new iterable, providing an unmodifiable view of this iterable */ public FluentIterable<E> unmodifiable() { return of(IterableUtils.unmodifiableIterable(iterable)); }
/** * Returns a new FluentIterable whose iterator will traverse * the elements of this iterable and the other iterable in * alternating order. * * @param other the other iterable to interleave, may not be null * @return a new iterable, interleaving this iterable with others * @throws NullPointerException if other is null */ public FluentIterable<E> zip(final Iterable<? extends E> other) { return of(IterableUtils.zippingIterable(iterable, other)); }
/** * Returns a new FluentIterable whose iterator will first traverse * the elements of the current iterable, followed by the elements * of the provided iterable. * * @param other the other iterable to combine, may not be null * @return a new iterable, combining this iterable with other * @throws NullPointerException if other is null */ public FluentIterable<E> append(final Iterable<? extends E> other) { return of(IterableUtils.chainedIterable(iterable, other)); }
/** * Returns a new FluentIterable whose iterator will only return * elements from this iterable matching the provided predicate. * * @param predicate the predicate used to filter elements * @return a new iterable, providing a filtered view of this iterable * @throws NullPointerException if predicate is null */ public FluentIterable<E> filter(final Predicate<? super E> predicate) { return of(IterableUtils.filteredIterable(iterable, predicate)); }
/** * Returns a new FluentIterable whose iterator will skip the first * N elements from this iterable. * * @param elementsToSkip the number of elements to skip * @return a new iterable, providing a view of this iterable by skipping * the first N elements * @throws IllegalArgumentException if elementsToSkip is negative */ public FluentIterable<E> skip(final long elementsToSkip) { return of(IterableUtils.skippingIterable(iterable, elementsToSkip)); }
/** * Returns a new FluentIterable whose iterator will return all elements * of this iterable transformed by the provided transformer. * * @param <O> the output element type * @param transformer the transformer applied to each element * @return a new iterable, providing a transformed view of this iterable * @throws NullPointerException if transformer is null */ public <O> FluentIterable<O> transform(final Transformer<? super E, ? extends O> transformer) { return of(IterableUtils.transformedIterable(iterable, transformer)); }