/**
* Returns the elements of <tt>unfiltered</tt> that satisfy a predicate.
*
* @param unfiltered An iterable containing objects of any type
* that will be filtered and used as the result.
* @param predicate The predicate to use for evaluation.
* @return An iterable containing all objects which passed the predicate's evaluation.
*/
public static <T> Iterable<T> filter(Iterable<T> unfiltered, Predicate<T> predicate) {
checkNotNull(unfiltered);
checkNotNull(predicate);
List<T> result = new ArrayList<T>();
Iterator<T> iterator = unfiltered.iterator();
while (iterator.hasNext()) {
T next = iterator.next();
if (predicate.evaluate(next)) {
result.add(next);
}
}
return result;
}
/**
* Returns the elements of <tt>unfiltered</tt> that satisfy a predicate.
*
* @param unfiltered An array containing objects of any type
* that will be filtered and used as the result.
* @param predicate The predicate to use for evaluation.
* @return An iterable containing all objects which passed the predicate's evaluation.
*/
public static <T> Iterable<T> filter(T[] unfiltered, Predicate<T> predicate) {
return filter(Arrays.asList(unfiltered), predicate);
}