1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 17:56: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 * Filter the elements of a collection down to just the ones that match the given predicate.
* that match the given predicate.
* @since 5.6 * @since 5.6
*/ */
public static <T> Collection<T> filter(Collection<T> collection, IUnaryPredicate<T> predicate) { public static <T> Collection<T> filter(Collection<T> collection, IUnaryPredicate<T> predicate) {
if (collection.isEmpty()) if (collection.isEmpty())
return collection; return collection;
Collection<T> result = new ArrayList<T>(); Collection<T> result = null;
for (T t : collection) int n = 0;
if (predicate.apply(t)) for (T t : collection) {
if (predicate.apply(t)) {
if (result != null) {
result.add(t); result.add(t);
return result; } 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;
} }
} }