1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2008 The Guava Authors
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkArgument;
201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport static com.google.common.base.Preconditions.checkNotNull;
211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Function;
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Joiner;
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Predicate;
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.base.Predicates;
271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.primitives.Ints;
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.AbstractCollection;
301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.ArrayList;
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Collection;
321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.Collections;
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.Iterator;
341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.List;
35bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Provides static methods for working with {@code Collection} instances.
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Chris Povirk
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Mike Bostock
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Jared Levy
421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 2.0 (imported from Google Collections Library)
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpublic final class Collections2 {
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private Collections2() {}
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns the elements of {@code unfiltered} that satisfy a predicate. The
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * returned collection is a live view of {@code unfiltered}; changes to one
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * affect the other.
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The resulting collection's iterator does not support {@code remove()},
541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * but all other collection methods are supported. When given an element that
551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * doesn't satisfy the predicate, the collection's {@code add()} and {@code
561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * addAll()} methods throw an {@link IllegalArgumentException}. When methods
571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * such as {@code removeAll()} and {@code clear()} are called on the filtered
581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * collection, only elements that satisfy the filter will be removed from the
591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * underlying collection.
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned collection isn't threadsafe or serializable, even if
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code unfiltered} is.
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>Many of the filtered collection's methods, such as {@code size()},
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * iterate across every element in the underlying collection and determine
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * which elements satisfy the filter. When a live view is <i>not</i> needed,
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * it may be faster to copy {@code Iterables.filter(unfiltered, predicate)}
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * and use the copy.
691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p><b>Warning:</b> {@code predicate} must be <i>consistent with equals</i>,
711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * as documented at {@link Predicate#apply}. Do not provide a predicate such
721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * as {@code Predicates.instanceOf(ArrayList.class)}, which is inconsistent
731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * with equals. (See {@link Iterables#filter(Iterable, Class)} for related
741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * functionality.)
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // TODO(kevinb): how can we omit that Iterables link when building gwt
771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // javadoc?
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <E> Collection<E> filter(
79090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Collection<E> unfiltered, Predicate<? super E> predicate) {
80090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (unfiltered instanceof FilteredCollection) {
81090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // Support clear(), removeAll(), and retainAll() when filtering a filtered
82090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // collection.
83090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return ((FilteredCollection<E>) unfiltered).createCombined(predicate);
84090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
85090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
86090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new FilteredCollection<E>(
87090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        checkNotNull(unfiltered), checkNotNull(predicate));
88090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
89090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
90bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  /**
911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Delegates to {@link Collection#contains}. Returns {@code false} if the
921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code contains} method throws a {@code ClassCastException}.
93bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   */
94bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  static boolean safeContains(Collection<?> collection, Object object) {
95bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    try {
96bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      return collection.contains(object);
97bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    } catch (ClassCastException e) {
98bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor      return false;
99bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    }
100bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  }
101bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
102090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static class FilteredCollection<E> implements Collection<E> {
103090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final Collection<E> unfiltered;
104090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final Predicate<? super E> predicate;
105090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
106090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    FilteredCollection(Collection<E> unfiltered,
107090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        Predicate<? super E> predicate) {
108090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.unfiltered = unfiltered;
109090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.predicate = predicate;
110090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
111090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
112090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    FilteredCollection<E> createCombined(Predicate<? super E> newPredicate) {
113090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return new FilteredCollection<E>(unfiltered,
114090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          Predicates.<E>and(predicate, newPredicate));
115090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // .<E> above needed to compile in JDK 5
116090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
117090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
119090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean add(E element) {
120090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      checkArgument(predicate.apply(element));
121090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return unfiltered.add(element);
122090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
123090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
125090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean addAll(Collection<? extends E> collection) {
126090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      for (E element : collection) {
127090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        checkArgument(predicate.apply(element));
128090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
129090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return unfiltered.addAll(collection);
130090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
131090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
133090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public void clear() {
134090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Iterables.removeIf(unfiltered, predicate);
135090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
136090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
138090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean contains(Object element) {
139090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      try {
140090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        // unsafe cast can result in a CCE from predicate.apply(), which we
141090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        // will catch
142090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        @SuppressWarnings("unchecked")
143090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        E e = (E) element;
1441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        /*
1461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert         * We check whether e satisfies the predicate, when we really mean to
1471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert         * check whether the element contained in the set does. This is ok as
1481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert         * long as the predicate is consistent with equals, as required.
1491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert         */
150090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return predicate.apply(e) && unfiltered.contains(element);
151090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      } catch (NullPointerException e) {
152090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
153090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      } catch (ClassCastException e) {
154090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
155090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
156090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
157090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
159090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean containsAll(Collection<?> collection) {
160090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      for (Object element : collection) {
161090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        if (!contains(element)) {
162090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return false;
163090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
164090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
165090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return true;
166090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
167090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
169090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean isEmpty() {
170090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return !Iterators.any(unfiltered.iterator(), predicate);
171090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
172090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
174090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Iterator<E> iterator() {
175090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Iterators.filter(unfiltered.iterator(), predicate);
176090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
177090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
179090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean remove(Object element) {
180090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      try {
181090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        // unsafe cast can result in a CCE from predicate.apply(), which we
182090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        // will catch
183090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        @SuppressWarnings("unchecked")
184090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        E e = (E) element;
1851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
1861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        // See comment in contains() concerning predicate.apply(e)
187090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return predicate.apply(e) && unfiltered.remove(element);
188090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      } catch (NullPointerException e) {
189090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
190090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      } catch (ClassCastException e) {
191090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        return false;
192090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      }
193090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
194090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
1951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
196090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean removeAll(final Collection<?> collection) {
197090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      checkNotNull(collection);
198090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Predicate<E> combinedPredicate = new Predicate<E>() {
1991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override
200090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        public boolean apply(E input) {
201090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return predicate.apply(input) && collection.contains(input);
202090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
203090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      };
204090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Iterables.removeIf(unfiltered, combinedPredicate);
205090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
206090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
208090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public boolean retainAll(final Collection<?> collection) {
209090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      checkNotNull(collection);
210090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Predicate<E> combinedPredicate = new Predicate<E>() {
2111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        @Override
212090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        public boolean apply(E input) {
2131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          // See comment in contains() concerning predicate.apply(e)
214090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson          return predicate.apply(input) && !collection.contains(input);
215090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        }
216090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      };
217090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Iterables.removeIf(unfiltered, combinedPredicate);
218090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
219090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
221090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public int size() {
222090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Iterators.size(iterator());
223090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
224090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
226090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public Object[] toArray() {
227090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      // creating an ArrayList so filtering happens once
228090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Lists.newArrayList(iterator()).toArray();
229090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
230090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    @Override
232090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    public <T> T[] toArray(T[] array) {
233090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Lists.newArrayList(iterator()).toArray(array);
234090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
235090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
236090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public String toString() {
237090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Iterators.toString(iterator());
238090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
239090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
240090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
241090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
242090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a collection that applies {@code function} to each element of
243090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code fromCollection}. The returned collection is a live view of {@code
244090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * fromCollection}; changes to one affect the other.
245090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
246090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned collection's {@code add()} and {@code addAll()} methods
247090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * throw an {@link UnsupportedOperationException}. All other collection
248090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * methods are supported, as long as {@code fromCollection} supports them.
249090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
250090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>The returned collection isn't threadsafe or serializable, even if
251090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * {@code fromCollection} is.
252090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
253090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * <p>When a live view is <i>not</i> needed, it may be faster to copy the
254090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * transformed collection and use the copy.
2551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>If the input {@code Collection} is known to be a {@code List}, consider
2571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link Lists#transform}. If only an {@code Iterable} is available, use
2581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@link Iterables#transform}.
259090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
260090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  public static <F, T> Collection<T> transform(Collection<F> fromCollection,
261090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      Function<? super F, T> function) {
262090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return new TransformedCollection<F, T>(fromCollection, function);
263090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
264090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
265090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static class TransformedCollection<F, T> extends AbstractCollection<T> {
266090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final Collection<F> fromCollection;
267090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    final Function<? super F, ? extends T> function;
268090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
269090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    TransformedCollection(Collection<F> fromCollection,
270090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson        Function<? super F, ? extends T> function) {
271090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.fromCollection = checkNotNull(fromCollection);
272090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      this.function = checkNotNull(function);
273090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
274090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
275090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public void clear() {
276090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      fromCollection.clear();
277090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
278090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
279090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public boolean isEmpty() {
280090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return fromCollection.isEmpty();
281090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
282090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
283090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public Iterator<T> iterator() {
284090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return Iterators.transform(fromCollection.iterator(), function);
285090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
286090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
287090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @Override public int size() {
288090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return fromCollection.size();
289090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
290090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
291090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
2921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
2931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns {@code true} if the collection {@code self} contains all of the
2941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * elements in the collection {@code c}.
2951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
2961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * <p>This method iterates over the specified collection {@code c}, checking
2971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * each element returned by the iterator in turn to see if it is contained in
2981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * the specified collection {@code self}. If all elements are so contained,
2991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * {@code true} is returned, otherwise {@code false}.
3001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   *
3011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param self a collection which might contain all elements in {@code c}
3021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * @param c a collection whose elements might be contained by {@code self}
3031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
3041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static boolean containsAllImpl(Collection<?> self, Collection<?> c) {
3051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkNotNull(self);
3061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    for (Object o : c) {
3071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      if (!self.contains(o)) {
3081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        return false;
3091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert      }
310090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
3111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return true;
312090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
313090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
3141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
3151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * An implementation of {@link Collection#toString()}.
3161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
3171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static String toStringImpl(final Collection<?> collection) {
3181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    StringBuilder sb
3191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        = newStringBuilderForCollection(collection.size()).append('[');
3201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    STANDARD_JOINER.appendTo(
3211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        sb, Iterables.transform(collection, new Function<Object, Object>() {
3221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          @Override public Object apply(Object input) {
3231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert            return input == collection ? "(this Collection)" : input;
3241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert          }
3251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert        }));
3261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return sb.append(']').toString();
3271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
3301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Returns best-effort-sized StringBuilder based on the given collection size.
3311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
3321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static StringBuilder newStringBuilderForCollection(int size) {
3331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    checkArgument(size >= 0, "size must be non-negative");
3341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return new StringBuilder((int) Math.min(size * 8L, Ints.MAX_POWER_OF_TWO));
3351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  /**
3381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   * Used to avoid http://bugs.sun.com/view_bug.do?bug_id=6558557
3391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert   */
3401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static <T> Collection<T> cast(Iterable<T> iterable) {
3411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert    return (Collection<T>) iterable;
3421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  }
3431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  static final Joiner STANDARD_JOINER = Joiner.on(", ");
3451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert
3461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // TODO(user): Maybe move the mathematical methods to a separate
3471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  // package-permission class.
348090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
349