001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import static com.google.common.base.Preconditions.checkNotNull; 020 021import com.google.common.annotations.Beta; 022import com.google.common.annotations.GwtCompatible; 023import com.google.common.annotations.GwtIncompatible; 024import com.google.common.base.Function; 025import com.google.common.base.Optional; 026import com.google.common.base.Predicate; 027 028import java.util.Collection; 029import java.util.Comparator; 030import java.util.Iterator; 031import java.util.List; 032import java.util.SortedSet; 033 034import javax.annotation.CheckReturnValue; 035import javax.annotation.Nullable; 036 037/** 038 * {@code FluentIterable} provides a rich interface for manipulating {@code Iterable} instances in a 039 * chained fashion. A {@code FluentIterable} can be created from an {@code Iterable}, or from a set 040 * of elements. The following types of methods are provided on {@code FluentIterable}: 041 * <ul> 042 * <li>chained methods which return a new {@code FluentIterable} based in some way on the contents 043 * of the current one (for example {@link #transform}) 044 * <li>conversion methods which copy the {@code FluentIterable}'s contents into a new collection or 045 * array (for example {@link #toList}) 046 * <li>element extraction methods which facilitate the retrieval of certain elements (for example 047 * {@link #last}) 048 * <li>query methods which answer questions about the {@code FluentIterable}'s contents (for example 049 * {@link #anyMatch}) 050 * </ul> 051 * 052 * <p>Here is an example that merges the lists returned by two separate database calls, transforms 053 * it by invoking {@code toString()} on each element, and returns the first 10 elements as an 054 * {@code ImmutableList}: <pre> {@code 055 * 056 * FluentIterable 057 * .from(database.getClientList()) 058 * .filter(activeInLastMonth()) 059 * .transform(Functions.toStringFunction()) 060 * .limit(10) 061 * .toList();}</pre> 062 * 063 * <p>Anything which can be done using {@code FluentIterable} could be done in a different fashion 064 * (often with {@link Iterables}), however the use of {@code FluentIterable} makes many sets of 065 * operations significantly more concise. 066 * 067 * @author Marcin Mikosik 068 * @since 12.0 069 */ 070@GwtCompatible(emulated = true) 071public abstract class FluentIterable<E> implements Iterable<E> { 072 // We store 'iterable' and use it instead of 'this' to allow Iterables to perform instanceof 073 // checks on the _original_ iterable when FluentIterable.from is used. 074 private final Iterable<E> iterable; 075 076 /** Constructor for use by subclasses. */ 077 protected FluentIterable() { 078 this.iterable = this; 079 } 080 081 FluentIterable(Iterable<E> iterable) { 082 this.iterable = checkNotNull(iterable); 083 } 084 085 /** 086 * Returns a fluent iterable that wraps {@code iterable}, or {@code iterable} itself if it 087 * is already a {@code FluentIterable}. 088 */ 089 public static <E> FluentIterable<E> from(final Iterable<E> iterable) { 090 return (iterable instanceof FluentIterable) ? (FluentIterable<E>) iterable 091 : new FluentIterable<E>(iterable) { 092 @Override 093 public Iterator<E> iterator() { 094 return iterable.iterator(); 095 } 096 }; 097 } 098 099 /** 100 * Construct a fluent iterable from another fluent iterable. This is obviously never necessary, 101 * but is intended to help call out cases where one migration from {@code Iterable} to 102 * {@code FluentIterable} has obviated the need to explicitly convert to a {@code FluentIterable}. 103 * 104 * @deprecated instances of {@code FluentIterable} don't need to be converted to 105 * {@code FluentIterable} 106 */ 107 @Deprecated 108 public static <E> FluentIterable<E> from(FluentIterable<E> iterable) { 109 return checkNotNull(iterable); 110 } 111 112 /** 113 * Returns a string representation of this fluent iterable, with the format 114 * {@code [e1, e2, ..., en]}. 115 */ 116 @Override 117 public String toString() { 118 return Iterables.toString(iterable); 119 } 120 121 /** 122 * Returns the number of elements in this fluent iterable. 123 */ 124 public final int size() { 125 return Iterables.size(iterable); 126 } 127 128 /** 129 * Returns {@code true} if this fluent iterable contains any object for which 130 * {@code equals(element)} is true. 131 */ 132 public final boolean contains(@Nullable Object element) { 133 return Iterables.contains(iterable, element); 134 } 135 136 /** 137 * Returns a fluent iterable whose {@code Iterator} cycles indefinitely over the elements of 138 * this fluent iterable. 139 * 140 * <p>That iterator supports {@code remove()} if {@code iterable.iterator()} does. After 141 * {@code remove()} is called, subsequent cycles omit the removed element, which is no longer in 142 * this fluent iterable. The iterator's {@code hasNext()} method returns {@code true} until 143 * this fluent iterable is empty. 144 * 145 * <p><b>Warning:</b> Typical uses of the resulting iterator may produce an infinite loop. You 146 * should use an explicit {@code break} or be certain that you will eventually remove all the 147 * elements. 148 */ 149 @CheckReturnValue 150 public final FluentIterable<E> cycle() { 151 return from(Iterables.cycle(iterable)); 152 } 153 154 /** 155 * Returns the elements from this fluent iterable that satisfy a predicate. The 156 * resulting fluent iterable's iterator does not support {@code remove()}. 157 */ 158 @CheckReturnValue 159 public final FluentIterable<E> filter(Predicate<? super E> predicate) { 160 return from(Iterables.filter(iterable, predicate)); 161 } 162 163 /** 164 * Returns the elements from this fluent iterable that are instances of class {@code type}. 165 * 166 * @param type the type of elements desired 167 */ 168 @GwtIncompatible("Class.isInstance") 169 @CheckReturnValue 170 public final <T> FluentIterable<T> filter(Class<T> type) { 171 return from(Iterables.filter(iterable, type)); 172 } 173 174 /** 175 * Returns {@code true} if any element in this fluent iterable satisfies the predicate. 176 */ 177 public final boolean anyMatch(Predicate<? super E> predicate) { 178 return Iterables.any(iterable, predicate); 179 } 180 181 /** 182 * Returns {@code true} if every element in this fluent iterable satisfies the predicate. 183 * If this fluent iterable is empty, {@code true} is returned. 184 */ 185 public final boolean allMatch(Predicate<? super E> predicate) { 186 return Iterables.all(iterable, predicate); 187 } 188 189 /** 190 * Returns an {@link Optional} containing the first element in this fluent iterable that 191 * satisfies the given predicate, if such an element exists. 192 * 193 * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code null}. If {@code null} 194 * is matched in this fluent iterable, a {@link NullPointerException} will be thrown. 195 */ 196 public final Optional<E> firstMatch(Predicate<? super E> predicate) { 197 return Iterables.tryFind(iterable, predicate); 198 } 199 200 /** 201 * Returns a fluent iterable that applies {@code function} to each element of this 202 * fluent iterable. 203 * 204 * <p>The returned fluent iterable's iterator supports {@code remove()} if this iterable's 205 * iterator does. After a successful {@code remove()} call, this fluent iterable no longer 206 * contains the corresponding element. 207 */ 208 public final <T> FluentIterable<T> transform(Function<? super E, T> function) { 209 return from(Iterables.transform(iterable, function)); 210 } 211 212 /** 213 * Applies {@code function} to each element of this fluent iterable and returns 214 * a fluent iterable with the concatenated combination of results. {@code function} 215 * returns an Iterable of results. 216 * 217 * <p>The returned fluent iterable's iterator supports {@code remove()} if this 218 * function-returned iterables' iterator does. After a successful {@code remove()} call, 219 * the returned fluent iterable no longer contains the corresponding element. 220 * 221 * @since 13.0 (required {@code Function<E, Iterable<T>>} until 14.0) 222 */ 223 public <T> FluentIterable<T> transformAndConcat( 224 Function<? super E, ? extends Iterable<? extends T>> function) { 225 return from(Iterables.concat(transform(function))); 226 } 227 228 /** 229 * Returns an {@link Optional} containing the first element in this fluent iterable. 230 * If the iterable is empty, {@code Optional.absent()} is returned. 231 * 232 * @throws NullPointerException if the first element is null; if this is a possibility, use 233 * {@code iterator().next()} or {@link Iterables#getFirst} instead. 234 */ 235 public final Optional<E> first() { 236 Iterator<E> iterator = iterable.iterator(); 237 return iterator.hasNext() 238 ? Optional.of(iterator.next()) 239 : Optional.<E>absent(); 240 } 241 242 /** 243 * Returns an {@link Optional} containing the last element in this fluent iterable. 244 * If the iterable is empty, {@code Optional.absent()} is returned. 245 * 246 * @throws NullPointerException if the last element is null; if this is a possibility, use 247 * {@link Iterables#getLast} instead. 248 */ 249 public final Optional<E> last() { 250 // Iterables#getLast was inlined here so we don't have to throw/catch a NSEE 251 252 // TODO(kevinb): Support a concurrently modified collection? 253 if (iterable instanceof List) { 254 List<E> list = (List<E>) iterable; 255 if (list.isEmpty()) { 256 return Optional.absent(); 257 } 258 return Optional.of(list.get(list.size() - 1)); 259 } 260 Iterator<E> iterator = iterable.iterator(); 261 if (!iterator.hasNext()) { 262 return Optional.absent(); 263 } 264 265 /* 266 * TODO(kevinb): consider whether this "optimization" is worthwhile. Users 267 * with SortedSets tend to know they are SortedSets and probably would not 268 * call this method. 269 */ 270 if (iterable instanceof SortedSet) { 271 SortedSet<E> sortedSet = (SortedSet<E>) iterable; 272 return Optional.of(sortedSet.last()); 273 } 274 275 while (true) { 276 E current = iterator.next(); 277 if (!iterator.hasNext()) { 278 return Optional.of(current); 279 } 280 } 281 } 282 283 /** 284 * Returns a view of this fluent iterable that skips its first {@code numberToSkip} 285 * elements. If this fluent iterable contains fewer than {@code numberToSkip} elements, 286 * the returned fluent iterable skips all of its elements. 287 * 288 * <p>Modifications to this fluent iterable before a call to {@code iterator()} are 289 * reflected in the returned fluent iterable. That is, the its iterator skips the first 290 * {@code numberToSkip} elements that exist when the iterator is created, not when {@code skip()} 291 * is called. 292 * 293 * <p>The returned fluent iterable's iterator supports {@code remove()} if the 294 * {@code Iterator} of this fluent iterable supports it. Note that it is <i>not</i> 295 * possible to delete the last skipped element by immediately calling {@code remove()} on the 296 * returned fluent iterable's iterator, as the {@code Iterator} contract states that a call 297 * to {@code * remove()} before a call to {@code next()} will throw an 298 * {@link IllegalStateException}. 299 */ 300 @CheckReturnValue 301 public final FluentIterable<E> skip(int numberToSkip) { 302 return from(Iterables.skip(iterable, numberToSkip)); 303 } 304 305 /** 306 * Creates a fluent iterable with the first {@code size} elements of this 307 * fluent iterable. If this fluent iterable does not contain that many elements, 308 * the returned fluent iterable will have the same behavior as this fluent iterable. 309 * The returned fluent iterable's iterator supports {@code remove()} if this 310 * fluent iterable's iterator does. 311 * 312 * @param size the maximum number of elements in the returned fluent iterable 313 * @throws IllegalArgumentException if {@code size} is negative 314 */ 315 @CheckReturnValue 316 public final FluentIterable<E> limit(int size) { 317 return from(Iterables.limit(iterable, size)); 318 } 319 320 /** 321 * Determines whether this fluent iterable is empty. 322 */ 323 public final boolean isEmpty() { 324 return !iterable.iterator().hasNext(); 325 } 326 327 /** 328 * Returns an {@code ImmutableList} containing all of the elements from this fluent iterable in 329 * proper sequence. 330 * 331 * @since 14.0 (since 12.0 as {@code toImmutableList()}). 332 */ 333 public final ImmutableList<E> toList() { 334 return ImmutableList.copyOf(iterable); 335 } 336 337 /** 338 * Returns an {@code ImmutableList} containing all of the elements from this {@code 339 * FluentIterable} in the order specified by {@code comparator}. To produce an {@code 340 * ImmutableList} sorted by its natural ordering, use {@code toSortedList(Ordering.natural())}. 341 * 342 * @param comparator the function by which to sort list elements 343 * @throws NullPointerException if any element is null 344 * @since 14.0 (since 13.0 as {@code toSortedImmutableList()}). 345 */ 346 @Beta 347 public final ImmutableList<E> toSortedList(Comparator<? super E> comparator) { 348 return Ordering.from(comparator).immutableSortedCopy(iterable); 349 } 350 351 /** 352 * Returns an {@code ImmutableSet} containing all of the elements from this fluent iterable with 353 * duplicates removed. 354 * 355 * @since 14.0 (since 12.0 as {@code toImmutableSet()}). 356 */ 357 public final ImmutableSet<E> toSet() { 358 return ImmutableSet.copyOf(iterable); 359 } 360 361 /** 362 * Returns an {@code ImmutableSortedSet} containing all of the elements from this {@code 363 * FluentIterable} in the order specified by {@code comparator}, with duplicates (determined by 364 * {@code comparator.compare(x, y) == 0}) removed. To produce an {@code ImmutableSortedSet} sorted 365 * by its natural ordering, use {@code toSortedSet(Ordering.natural())}. 366 * 367 * @param comparator the function by which to sort set elements 368 * @throws NullPointerException if any element is null 369 * @since 14.0 (since 12.0 as {@code toImmutableSortedSet()}). 370 */ 371 public final ImmutableSortedSet<E> toSortedSet(Comparator<? super E> comparator) { 372 return ImmutableSortedSet.copyOf(comparator, iterable); 373 } 374 375 /** 376 * Returns an immutable map for which the elements of this {@code FluentIterable} are the keys in 377 * the same order, mapped to values by the given function. If this iterable contains duplicate 378 * elements, the returned map will contain each distinct element once in the order it first 379 * appears. 380 * 381 * @throws NullPointerException if any element of this iterable is {@code null}, or if {@code 382 * valueFunction} produces {@code null} for any key 383 * @since 14.0 384 */ 385 public final <V> ImmutableMap<E, V> toMap(Function<? super E, V> valueFunction) { 386 return Maps.toMap(iterable, valueFunction); 387 } 388 389 /** 390 * Creates an index {@code ImmutableListMultimap} that contains the results of applying a 391 * specified function to each item in this {@code FluentIterable} of values. Each element of this 392 * iterable will be stored as a value in the resulting multimap, yielding a multimap with the same 393 * size as this iterable. The key used to store that value in the multimap will be the result of 394 * calling the function on that value. The resulting multimap is created as an immutable snapshot. 395 * In the returned multimap, keys appear in the order they are first encountered, and the values 396 * corresponding to each key appear in the same order as they are encountered. 397 * 398 * @param keyFunction the function used to produce the key for each value 399 * @throws NullPointerException if any of the following cases is true: 400 * <ul> 401 * <li>{@code keyFunction} is null 402 * <li>An element in this fluent iterable is null 403 * <li>{@code keyFunction} returns {@code null} for any element of this iterable 404 * </ul> 405 * @since 14.0 406 */ 407 public final <K> ImmutableListMultimap<K, E> index(Function<? super E, K> keyFunction) { 408 return Multimaps.index(iterable, keyFunction); 409 } 410 411 /** 412 * Returns an immutable map for which the {@link java.util.Map#values} are the elements of this 413 * {@code FluentIterable} in the given order, and each key is the product of invoking a supplied 414 * function on its corresponding value. 415 * 416 * @param keyFunction the function used to produce the key for each value 417 * @throws IllegalArgumentException if {@code keyFunction} produces the same key for more than one 418 * value in this fluent iterable 419 * @throws NullPointerException if any element of this fluent iterable is null, or if 420 * {@code keyFunction} produces {@code null} for any value 421 * @since 14.0 422 */ 423 public final <K> ImmutableMap<K, E> uniqueIndex(Function<? super E, K> keyFunction) { 424 return Maps.uniqueIndex(iterable, keyFunction); 425 } 426 427 /** 428 * Returns an array containing all of the elements from this fluent iterable in iteration order. 429 * 430 * @param type the type of the elements 431 * @return a newly-allocated array into which all the elements of this fluent iterable have 432 * been copied 433 */ 434 @GwtIncompatible("Array.newArray(Class, int)") 435 public final E[] toArray(Class<E> type) { 436 return Iterables.toArray(iterable, type); 437 } 438 439 /** 440 * Copies all the elements from this fluent iterable to {@code collection}. This is equivalent to 441 * calling {@code Iterables.addAll(collection, this)}. 442 * 443 * @param collection the collection to copy elements to 444 * @return {@code collection}, for convenience 445 * @since 14.0 446 */ 447 public final <C extends Collection<? super E>> C copyInto(C collection) { 448 checkNotNull(collection); 449 if (iterable instanceof Collection) { 450 collection.addAll(Collections2.cast(iterable)); 451 } else { 452 for (E item : iterable) { 453 collection.add(item); 454 } 455 } 456 return collection; 457 } 458 459 /** 460 * Returns the element at the specified position in this fluent iterable. 461 * 462 * @param position position of the element to return 463 * @return the element at the specified position in this fluent iterable 464 * @throws IndexOutOfBoundsException if {@code position} is negative or greater than or equal to 465 * the size of this fluent iterable 466 */ 467 public final E get(int position) { 468 return Iterables.get(iterable, position); 469 } 470 471 /** 472 * Function that transforms {@code Iterable<E>} into a fluent iterable. 473 */ 474 private static class FromIterableFunction<E> 475 implements Function<Iterable<E>, FluentIterable<E>> { 476 @Override 477 public FluentIterable<E> apply(Iterable<E> fromObject) { 478 return FluentIterable.from(fromObject); 479 } 480 } 481}