diff options
author | Remi NGUYEN VAN <reminv@google.com> | 2021-03-02 09:27:09 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2021-03-02 09:27:09 +0000 |
commit | ef5eb5c41bccf989133be8490f3400ef12d4a43c (patch) | |
tree | a36961d785c4e52fd0028711631ff4f20d61e973 /common | |
parent | 5bb30ebaef097a52c80ca8018c650f72a8aaa4bb (diff) | |
parent | 0c7660d7f696d7e4c9bd820b349cda4f630c1d5d (diff) | |
download | net-ef5eb5c41bccf989133be8490f3400ef12d4a43c.tar.gz |
Merge "Add CollectionUtils.indexOf"
Diffstat (limited to 'common')
-rw-r--r-- | common/framework/com/android/net/module/util/CollectionUtils.java | 15 | ||||
-rw-r--r-- | common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt | 10 |
2 files changed, 23 insertions, 2 deletions
diff --git a/common/framework/com/android/net/module/util/CollectionUtils.java b/common/framework/com/android/net/module/util/CollectionUtils.java index e5bb58d4..115f19d0 100644 --- a/common/framework/com/android/net/module/util/CollectionUtils.java +++ b/common/framework/com/android/net/module/util/CollectionUtils.java @@ -76,15 +76,26 @@ public final class CollectionUtils { 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) { + return indexOf(elem, predicate) >= 0; + } + + /** + * @return The index of the first element that matches the predicate, or -1 if none. + */ + @Nullable + public static <T> int indexOf(@NonNull Collection<T> elem, @NonNull Predicate<T> predicate) { + int idx = 0; for (final T e : elem) { - if (predicate.test(e)) return true; + if (predicate.test(e)) return idx; + idx++; } - return false; + return -1; } /** diff --git a/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt b/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt index 00077420..08864265 100644 --- a/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt +++ b/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt @@ -20,6 +20,7 @@ import androidx.test.filters.SmallTest import androidx.test.runner.AndroidJUnit4 import org.junit.Test import org.junit.runner.RunWith +import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue @@ -40,6 +41,15 @@ class CollectionUtilsTest { } @Test + fun testIndexOf() { + assertEquals(4, CollectionUtils.indexOf(listOf("A", "B", "C", "D", "E")) { it == "E" }) + assertEquals(0, CollectionUtils.indexOf(listOf("A", "B", "C", "D", "E")) { it == "A" }) + assertEquals(1, CollectionUtils.indexOf(listOf("AA", "BBB", "CCCC")) { it.length >= 3 }) + assertEquals(1, CollectionUtils.indexOf(listOf("AA", null, "CCCC")) { it == null }) + assertEquals(1, CollectionUtils.indexOf(listOf(null, "CCCC")) { it != null }) + } + + @Test fun testAll() { assertFalse(CollectionUtils.all(listOf("A", "B", "C", "D", "E")) { it != "E" }) assertTrue(CollectionUtils.all(listOf("A", "B", "C", "D", "E")) { it != "F" }) |