All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.common.collect.Iterables Maven / Gradle / Ivy

Go to download

Google Collections Library is a suite of new collections and collection-related goodness for Java 5.0

There is a newer version: snapshot-20080530
Show newest version
/*
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import com.google.common.base.Function;
import com.google.common.base.Nullable;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Predicate;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedSet;

/**
 * This class contains static utility methods that operate on or return objects
 * of type {@code Iterable}. Also see the parallel implementations in {@link
 * Iterators}.
 *
 * @author Kevin Bourrillion
 * @author Scott Bonneau
 */
public final class Iterables {
  private Iterables() {}

  private static final Iterable EMPTY_ITERABLE = new Iterable()
  {
    public Iterator iterator() {
      return Iterators.EMPTY_ITERATOR;
    }
  };

  /** Returns the empty Iterable. */
  // Casting to any type is safe since there are no actual elements.
  @SuppressWarnings("unchecked")
  public static  Iterable emptyIterable() {
    return (Iterable) EMPTY_ITERABLE;
  }

  /** Returns an unmodifiable view of {@code iterable}. */
  public static  Iterable unmodifiableIterable(final Iterable iterable)
  {
    checkNotNull(iterable);
    return new Iterable() {
      public Iterator iterator() {
        return Iterators.unmodifiableIterator(iterable.iterator());
      }
      @Override public String toString() {
        return iterable.toString();
      }
      // no equals and hashCode; it would break the contract!
    };
  }

  /**
   * Returns the number of elements present in {@code iterable}.
   */
  public static int size(Iterable iterable) {
    return (iterable instanceof Collection)
        ? ((Collection) iterable).size()
        : Iterators.size(iterable.iterator());
  }

  /**
   * Determines whether the two Iterables contain equal elements. More
   * specifically, this method returns {@code true} if {@code iterable1} and
   * {@code iterable2} contain the same number of elements and every element of
   * {@code iterable1} is equal to the corresponding element of {@code
   * iterable2}.
   */
  public static boolean elementsEqual(
      Iterable iterable1, Iterable iterable2) {
    return Iterators.elementsEqual(iterable1.iterator(), iterable2.iterator());
  }

  /**
   * Returns a string representation of {@code iterable} in the same format as
   * {@code Iterators#toString(java.util.Iterator)}.
   */
  public static String toString(Iterable iterable) {
    return Iterators.toString(iterable.iterator());
  }

  /**
   * Returns the single element contained in {@code iterable}.
   *
   * @throws NoSuchElementException if the iterable is empty
   * @throws IllegalArgumentException if the iterable contains multiple
   *     elements
   */
  public static  T getOnlyElement(Iterable iterable) {
    return Iterators.getOnlyElement(iterable.iterator());
  }

  /**
   * Returns the single element contained in {@code iterator}, or {@code
   * defaultValue} if the iterator is empty.
   *
   * @throws IllegalArgumentException if the iterator contains multiple
   *     elements
   */
  public static  T getOnlyElement(
      Iterable iterable, @Nullable T defaultValue) {
    return Iterators.getOnlyElement(iterable.iterator(), defaultValue);
  }

  /**
   * Converts an {@code Iterable} into an array.
   *
   * @param iterable any instance of {@code Iterable} (will not be modified)
   * @param type the type of the elements
   * @return a newly-allocated array into which all the elements of the iterable
   *     have been copied. May be empty but never null.
   */
  public static  T[] newArray(Iterable iterable, Class type) {
    Collection collection = (iterable instanceof Collection)
        ? (Collection) iterable
        : Lists.newArrayList(iterable);
    T[] array = ObjectArrays.newArray(type, collection.size());
    return collection.toArray(array);
  }

  /**
   * 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  boolean addAll(
      Collection collection, Iterable iterable) {
    if (iterable instanceof Collection) {
      @SuppressWarnings("unchecked")
      Collection c = (Collection) iterable;
      return collection.addAll(c);
    }
    return Iterators.addAll(collection, iterable.iterator());
  }

  /** Variant of {@code Collections.frequency} for iterables. */
  public static int frequency(Iterable iterable, @Nullable Object element) {
    if ((iterable instanceof Multiset)) {
      return ((Multiset) iterable).count(element);
    }
    if ((iterable instanceof Set)) {
      return ((Set) iterable).contains(element) ? 1 : 0;
    }
    return Iterators.frequency(iterable.iterator(), element);
  }

  /**
   * Variant of {@code Iterators.cycle} which returns an {@code Iterable}.
   *
   * @see Iterators#cycle(Iterable)
   */
  public static  Iterable cycle(final Iterable iterable) {
    checkNotNull(iterable);
    return new Iterable() {
      public Iterator iterator() {
        return Iterators.cycle(iterable);
      }
      @Override public String toString() {
        return iterable.toString() + " (cycled)";
      }
    };
  }

  /** Variant of {@code cycle(Iterable)} accepting varargs parameters. */
  public static  Iterable cycle(T... elements) {
    return cycle(Lists.newArrayList(elements));
  }

  /**
   * Variant of {@code Iterators.concat} that acts on and returns instances of
   * {@code Iterable}.
   */
  @SuppressWarnings("unchecked")
  public static  Iterable concat(
      Iterable firstElements, Iterable nextElements) {
    checkNotNull(firstElements);
    checkNotNull(nextElements);
    return concat(Arrays.asList(firstElements, nextElements));
  }

  /**
   * Variant of {@code Iterators.concat} that acts on and returns instances of
   * {@code Iterable}.
   */
  public static  Iterable concat(Iterable... iterables) {
    return concat(Arrays.asList(iterables));
  }

  /**
   * Variant of {@code Iterators.concat} that acts on and returns instances of
   * {@code Iterable}.
   */
  public static  Iterable concat(
      Iterable> iterables) {
    checkNotNull(iterables);

    /*
     * Hint: if you let A represent Iterable and B represent
     * Iterator, then this Function would look simply like:
     *
     *   Function function = new Function {
     *     public B apply(A from) {
     *       return from.iterator();
     *     }
     *   }
     *
     * TODO: there may be a better way to do this.
     */

    Function, Iterator> function
        = new Function, Iterator>() {
      public Iterator apply(Iterable from) {
        return from.iterator();
      }
    };
    final Iterable> iterators
        = transform(iterables, function);
    return new AbstractIterable() {
      public Iterator iterator() {
        return Iterators.concat(iterators.iterator());
      }
    };
  }

  /**
   * Partition an iterable into sub iterables of the given size like so: {A, B,
   * C, D, E, F} with partition size 3 => {A, B, C} and {D, E, F}.
   *
   * 

NOTE: You must read partitions one at a time from the returned iterable * Once you read forward any iterables from previous partitions will become * invalid. * *

NOTE: This is optimized for a the simple case of iterating through each * sub-iterable in order only once. Other operations will succeed, but will * suffer a performance penalty to maintain correctness. * * @param iterable the iterable to partition * @param partitionSize the size of each partition * @param padToSize whether to pad the last partition to the partition size * with {@code null}. * @return an iterable of partitioned iterables */ public static Iterable> partition( final Iterable iterable, final int partitionSize, final boolean padToSize) { checkNotNull(iterable); return new AbstractIterable>() { public Iterator> iterator() { final Iterator> iterator = Iterators.partition( iterable.iterator(), partitionSize, padToSize); return new AbstractIterator>() { int howFarIn; @Override protected Iterable computeNext() { howFarIn++; if (!iterator.hasNext()) { return endOfData(); } return new AbstractIterable() { Iterator innerIter = iterator.next(); boolean firstIteratorRequest = true; public Iterator iterator() { if (firstIteratorRequest) { firstIteratorRequest = false; return innerIter; } else { Iterator> iterator = Iterators.partition( iterable.iterator(), partitionSize, padToSize); for (int i = 0; i < howFarIn; i++) { innerIter = iterator.next(); } return innerIter; } } }; } }; } }; } /** * Variant of {@code Iterators.filter(Iterator,Predicate)}, which accepts and * returns an iterable instead of an iterator. * * @see Iterators#filter(Iterator, Predicate) */ public static Iterable filter( final Iterable unfiltered, final Predicate predicate) { checkNotNull(unfiltered); checkNotNull(predicate); return new AbstractIterable() { public Iterator iterator() { return Iterators.filter(unfiltered.iterator(), predicate); } }; } /** * Returns all instances of {@code type} found in {@code unfiltered}. Similar * to {@link #filter(Iterable, Predicate)}. * * @param unfiltered an iterable containing objects of any type * @param type the type of elements desired * @return an unmodifiable iterable containing all elements of the original * iterable that were of the requested type */ public static Iterable filter( final Iterable unfiltered, final Class type) { checkNotNull(unfiltered); checkNotNull(type); return new AbstractIterable() { public Iterator iterator() { return Iterators.filter(unfiltered.iterator(), type); } }; } /** * Returns {@code true} if some element in {@code iterable} evaluates to * {@code true} under {@code predicate}. Returns {@code false} if {@code * iterable} is empty. */ public static boolean any( Iterable iterable, Predicate predicate) { return Iterators.any(iterable.iterator(), predicate); } /** * Returns {@code true} if no element in {@code iterable} evaluates to {@code * false} under {@code predicate}. Returns {@code true} if {@code iterable} is * empty. */ public static boolean all( Iterable iterable, Predicate predicate) { return Iterators.all(iterable.iterator(), predicate); } /** * Returns the first element in {@code iterable} for which the given predicate * matches. * * @throws NoSuchElementException if no element in {@code iterable} matches * the given predicate */ public static E find(Iterable iterable, Predicate predicate) { return Iterators.find(iterable.iterator(), predicate); } /** * Returns an iterable that applies {@code function} to each element of {@code * fromIterable}. */ public static Iterable transform(final Iterable fromIterable, final Function function) { checkNotNull(fromIterable); checkNotNull(function); return new AbstractIterable() { public Iterator iterator() { return Iterators.transform(fromIterable.iterator(), function); } }; } // Methods only in Iterables, not in Iterators /** * Adapt a list to an Iterable over a reversed version of the list. Requires a * List so that we can reverse it with no copying required. Especially useful * in foreach-style loops: *

   * List mylist = ...
   * for (String str : Iterables.reverse(mylist)) {
   *   ...
   * }
   * 
* * @return an Iterable with the same elements as the list, in reverse. */ public static Iterable reverse(final List list) { checkNotNull(list); return new AbstractIterable() { public Iterator iterator() { final ListIterator listIter = list.listIterator(list.size()); return new Iterator() { public boolean hasNext() { return listIter.hasPrevious(); } public T next() { return listIter.previous(); } public void remove() { listIter.remove(); } }; } }; } /** * Provides a rotated view of a list. Differs from {@link Collections#rotate} * only in that it leaves the underlying list unchanged. Note that this is a * "live" view of the list that will change as the list changes. However, the * behavior of an {@link Iterator} constructed from a rotated view of the list * is undefined if the list is changed after the Iterator is constructed. * * @param list the list to return a rotated view of. * @param distance the distance to rotate the list. There are no constraints * on this value; it may be zero, negative, or greater than {@code * list.size()}. * @return a rotated view of the given list */ public static Iterable rotate(final List list, final int distance) { checkNotNull(list); // If no rotation is requested, just return the original list if (distance == 0) { return list; } return new AbstractIterable() { /** * Determines the actual distance we need to rotate (distance provided * might be larger than the size of the list or negative). */ int calcActualDistance(int size) { // we already know distance and size are non-zero int actualDistance = distance % size; if (actualDistance < 0) { // distance must have been negative actualDistance += size; } return actualDistance; } public Iterator iterator() { int size = list.size(); if (size <= 1) { return list.iterator(); } int actualDistance = calcActualDistance(size); // lists of a size that go into the distance evenly don't need rotation if (actualDistance == 0) { return list.iterator(); } @SuppressWarnings("unchecked") Iterable rotated = concat(list.subList(actualDistance, size), list.subList(0, actualDistance)); return rotated.iterator(); } }; } /** * Variant of {@code Iterators.limit(Iterator,int)}, which accepts and * returns an iterable instead of an iterator. * * @see Iterators#limit(Iterator, int) */ public static Iterable limit( final Iterable iterable, final int limit) { checkNotNull(iterable); return new AbstractIterable() { public Iterator iterator() { return Iterators.limit(iterable.iterator(), limit); } }; } /** * Returns whether the given iterable contains no elements. * * @return {@code true} if the iterable has no elements, {@code false} if the * iterable has one or more elements */ public static boolean isEmpty(Iterable iterable) { return !iterable.iterator().hasNext(); } /** * Returns a view of {@code iterable} whose {@link Iterator} skips its first * {@code numberToSkip} elements or, if {@code iterable} contains fewer than * {@code numberToSkip} elements, skips all its elements. *

* Structural modifications to the underlying {@link Iterable} * before a call to {@link Iterable#iterator() iterator()} * are reflected in the returned {@code Iterator}. That is, the returned * {@code Iterator} skips the first {@code numberToSkip} elements that exist * when the {@code Iterator} is created, not when {@code skip()} is called. *

* Structural modifications to the underlying {@link Iterable} * after a call to {@code iterator()} may result in undefined * behavior by the returned {@code Iterator}, depending upon the * concurrent-modification policy of the underlying {@code Iterable}. * Non-structural changes to the underlying {@code Iterable} are always * reflected in the returned {@code Iterable}. *

* {@link Iterator#remove()} is supported if iterators of the underlying * {@code Iterable} supports it. (Note that it is not * possible to delete the last skipped element by immediately calling * {@code remove()} on a new iterator, as the {@code Iterator} contract * requires that a call to {@code remove()} before a call to * {@link Iterator#next() next()} will throw an * {@link IllegalStateException}.) */ public static Iterable skip(final Iterable iterable, final int numberToSkip) { checkNotNull(iterable); checkArgument(numberToSkip >= 0, "number to skip cannot be negative"); if (iterable instanceof List) { final List list = (List) iterable; return new Iterable() { public Iterator iterator() { return (numberToSkip >= list.size()) ? Iterators.emptyIterator() : list.subList(numberToSkip, list.size()).iterator(); } }; } return new Iterable() { public Iterator iterator() { final Iterator iterator = iterable.iterator(); Iterators.skip(iterator, numberToSkip); /* * We can't just return the iterator because an immediate call to its * remove() method would remove one of the skipped elements instead of * throwing an IllegalStateException. */ return new Iterator() { boolean atStart = true; public boolean hasNext() { return iterator.hasNext(); } public T next() { if (!hasNext()) { throw new NoSuchElementException(); } try { return iterator.next(); } finally { atStart = false; } } public void remove() { if (atStart) { throw new IllegalStateException(); } iterator.remove(); } }; } }; } /** * Returns the last element of {@code iterable}, or throws a * {@link NoSuchElementException} if it has no elements. */ public static T getLast(Iterable iterable) { if (iterable instanceof List) { List list = (List) iterable; if (list.isEmpty()) { throw new NoSuchElementException(); } return list.get(list.size() - 1); } if (iterable instanceof SortedSet) { SortedSet sortedSet = (SortedSet) iterable; return sortedSet.last(); } return Iterators.getLast(iterable.iterator()); } }