| 1185 | */ |
| 1186 | // TODO(kevinb): how to omit that last sentence when building GWT javadoc? |
| 1187 | public static <E extends @Nullable Object> Set<E> filter( |
| 1188 | Set<E> unfiltered, Predicate<? super E> predicate) { |
| 1189 | if (unfiltered instanceof SortedSet) { |
| 1190 | return filter((SortedSet<E>) unfiltered, predicate); |
| 1191 | } |
| 1192 | if (unfiltered instanceof FilteredSet) { |
| 1193 | // Support clear(), removeAll(), and retainAll() when filtering a filtered |
| 1194 | // collection. |
| 1195 | FilteredSet<E> filtered = (FilteredSet<E>) unfiltered; |
| 1196 | Predicate<E> combinedPredicate = Predicates.and(filtered.predicate, predicate); |
| 1197 | return new FilteredSet<>((Set<E>) filtered.unfiltered, combinedPredicate); |
| 1198 | } |
| 1199 | |
| 1200 | return new FilteredSet<>(checkNotNull(unfiltered), checkNotNull(predicate)); |
| 1201 | } |
| 1202 | |
| 1203 | /** |
| 1204 | * Returns the elements of a {@code SortedSet}, {@code unfiltered}, that satisfy a predicate. The |