summaryrefslogtreecommitdiff
path: root/common/framework/com/android/net/module/util/CollectionUtils.java
diff options
context:
space:
mode:
authorlucaslin <lucaslin@google.com>2021-02-01 15:22:56 +0800
committerlucaslin <lucaslin@google.com>2021-02-01 15:22:56 +0800
commita7af478653f62e5e3c210691eef814453d9a8c90 (patch)
tree1f8d4b956d3d3094edf15be5c4ea698a452bed77 /common/framework/com/android/net/module/util/CollectionUtils.java
parent8e555572f01547fc2928cdf86d7ecec0dd0db3ac (diff)
downloadnet-a7af478653f62e5e3c210691eef814453d9a8c90.tar.gz
Add common CollectionUtils.{any,all} methods.
Test: new tests for this Change-Id: I482c958af499bf8cdc8824c9586e9da619cd1f39
Diffstat (limited to 'common/framework/com/android/net/module/util/CollectionUtils.java')
-rw-r--r--common/framework/com/android/net/module/util/CollectionUtils.java22
1 files changed, 22 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 74f738da..cb1e3e74 100644
--- a/common/framework/com/android/net/module/util/CollectionUtils.java
+++ b/common/framework/com/android/net/module/util/CollectionUtils.java
@@ -66,6 +66,28 @@ public final class CollectionUtils {
}
/**
+ * @return True if all elements satisfy the predicate, false otherwise.
+ * Note that means this always returns true for empty collections.
+ */
+ public static <T> boolean all(@NonNull Collection<T> elem, @NonNull Predicate<T> predicate) {
+ for (final T e : elem) {
+ if (!predicate.test(e)) return false;
+ }
+ return true;
+
+ }
+ /**
+ * @return True if any element satisfies the predicate, false otherwise.
+ * Note that means this always returns false for empty collections.
+ */
+ public static <T> boolean any(@NonNull Collection<T> elem, @NonNull Predicate<T> predicate) {
+ for (final T e : elem) {
+ if (predicate.test(e)) return true;
+ }
+ return false;
+ }
+
+ /**
* @return True if there exists at least one element in the sparse array for which
* condition {@code predicate}
*/