1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-06 17:26:01 +02:00

An attempt at performance optimization.

Change-Id: Ia4b6839626381935af859ae468de418fa004c867
Reviewed-on: https://git.eclipse.org/r/19934
Reviewed-by: Nathan Ridge <zeratul976@hotmail.com>
Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
IP-Clean: Sergey Prigogin <eclipse.sprigogin@gmail.com>
Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
This commit is contained in:
Sergey Prigogin 2013-12-17 20:18:42 -08:00
parent d9c4584257
commit 227b03e6b8

View file

@ -154,17 +154,30 @@ public final class CollectionUtils {
}
/**
* Filter the elements of a collection down to just the ones
* that match the given predicate.
* Filter the elements of a collection down to just the ones that match the given predicate.
* @since 5.6
*/
public static <T> Collection<T> filter(Collection<T> collection, IUnaryPredicate<T> predicate) {
if (collection.isEmpty())
return collection;
Collection<T> result = new ArrayList<T>();
for (T t : collection)
if (predicate.apply(t))
result.add(t);
return result;
Collection<T> result = null;
int n = 0;
for (T t : collection) {
if (predicate.apply(t)) {
if (result != null) {
result.add(t);
} else {
++n;
}
} else if (result == null) {
result = new ArrayList<T>(collection.size() - 1);
for (T u : collection) {
if (--n < 0)
break;
result.add(u);
}
}
}
return result == null ? collection : result;
}
}