Provides static methods for working with Collection instances. @author Chris Povirk @author Mike Bostock @author Jared Levy @since 2.0
| 52 | |
| 53 | |
| 54 | @GwtCompatible |
| 55 | public final class Collections2 { |
| 56 | private Collections2() {} |
| 57 | |
| 58 | /** |
| 59 | * Returns the elements of {@code unfiltered} that satisfy a predicate. The |
| 60 | * returned collection is a live view of {@code unfiltered}; changes to one |
| 61 | * affect the other. |
| 62 | * |
| 63 | * <p>The resulting collection's iterator does not support {@code remove()}, |
| 64 | * but all other collection methods are supported. When given an element that |
| 65 | * doesn't satisfy the predicate, the collection's {@code add()} and {@code |
| 66 | * addAll()} methods throw an {@link IllegalArgumentException}. When methods |
| 67 | * such as {@code removeAll()} and {@code clear()} are called on the filtered |
| 68 | * collection, only elements that satisfy the filter will be removed from the |
| 69 | * underlying collection. |
| 70 | * |
| 71 | * <p>The returned collection isn't threadsafe or serializable, even if |
| 72 | * {@code unfiltered} is. |
| 73 | * |
| 74 | * <p>Many of the filtered collection's methods, such as {@code size()}, |
| 75 | * iterate across every element in the underlying collection and determine |
| 76 | * which elements satisfy the filter. When a live view is <i>not</i> needed, |
| 77 | * it may be faster to copy {@code Iterables.filter(unfiltered, predicate)} |
| 78 | * and use the copy. |
| 79 | * |
| 80 | * <p><b>Warning:</b> {@code predicate} must be <i>consistent with equals</i>, |
| 81 | * as documented at {@link Predicate#apply}. Do not provide a predicate such |
| 82 | * as {@code Predicates.instanceOf(ArrayList.class)}, which is inconsistent |
| 83 | * with equals. (See {@link Iterables#filter(Iterable, Class)} for related |
| 84 | * functionality.) |
| 85 | */ |
| 86 | // TODO(kevinb): how can we omit that Iterables link when building gwt |
| 87 | // javadoc? |
| 88 | |
| 89 | |
| 90 | public static <E> Collection<E> filter(Collection<E> unfiltered, Predicate<? super E> predicate) { |
| 91 | if (unfiltered instanceof FilteredCollection) { |
| 92 | // Support clear(), removeAll(), and retainAll() when filtering a filtered |
| 93 | // collection. |
| 94 | return ((FilteredCollection<E>) unfiltered).createCombined(predicate); |
| 95 | } |
| 96 | return new FilteredCollection<E>(checkNotNull(unfiltered), checkNotNull(predicate)); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Delegates to {@link Collection#contains}. Returns {@code false} if the |
| 101 | * {@code contains} method throws a {@code ClassCastException} or |
| 102 | * {@code NullPointerException}. |
| 103 | */ |
| 104 | |
| 105 | static boolean safeContains(Collection<?> collection, @Nullable Object object) { |
| 106 | checkNotNull(collection); |
| 107 | try { |
| 108 | return collection.contains(object); |
| 109 | } catch (ClassCastException e) { |
| 110 | return false; |
| 111 | } catch (NullPointerException e) { |
nothing calls this directly
no test coverage detected