diff options
author | Chalard Jean <jchalard@google.com> | 2021-03-31 19:33:09 +0900 |
---|---|---|
committer | Chalard Jean <jchalard@google.com> | 2021-03-31 19:33:09 +0900 |
commit | fb615019f75486644c563619813fee91dc997e00 (patch) | |
tree | 3efd3f996930118001c20dfdbfc2d4bd929c304d /common | |
parent | 54fcb1500eb1dc05f82392885a4e9f603de65fa6 (diff) | |
download | net-fb615019f75486644c563619813fee91dc997e00.tar.gz |
Add a filter function to CollectionUtils
This is generally useful, and specifically useful in
NetworkRanker
Test: Used in NetworkRanker, ConnectivityServiceTest
Change-Id: I896e7e5bcb2931ce43a5b88967bfd6fcd1de0ce2
Diffstat (limited to 'common')
-rw-r--r-- | common/framework/com/android/net/module/util/CollectionUtils.java | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/common/framework/com/android/net/module/util/CollectionUtils.java b/common/framework/com/android/net/module/util/CollectionUtils.java index 2223443d..f2afc757 100644 --- a/common/framework/com/android/net/module/util/CollectionUtils.java +++ b/common/framework/com/android/net/module/util/CollectionUtils.java @@ -20,6 +20,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.util.SparseArray; +import java.util.ArrayList; import java.util.Collection; import java.util.Objects; import java.util.function.Predicate; @@ -148,4 +149,21 @@ public final class CollectionUtils { } return -1; } + + /** + * Returns a new collection of elements that match the passed predicate. + * @param source the elements to filter. + * @param test the predicate to test for. + * @return a new collection containing only the source elements that satisfy the predicate. + */ + @NonNull private static <T> ArrayList<T> filter(@NonNull final Collection<T> source, + @NonNull final Predicate<T> test) { + final ArrayList<T> matches = new ArrayList<>(); + for (final T e : source) { + if (test.test(e)) { + matches.add(e); + } + } + return matches; + } } |