Reduce unecessary allocation churn in MdnsServiceTypeClient.processResponse
Bug: 413371501 Test: atest ConnectivityCoverageTests:android.net.connectivity.com.android.server.connectivity.mdns.MdnsServiceTypeClientTests Test: atest ConnectivityCoverageTests:android.net.connectivity.com.android.net.module.util.CollectionUtilsTest Flag: EXEMPT bugfix Change-Id: Id9abf0b79f6db155e6895b0dd7661d0aafb6b0a7
This commit is contained in:
parent
b8a76f540c
commit
532b263b45
2 changed files with 60 additions and 6 deletions
|
|
@ -113,6 +113,16 @@ public final class CollectionUtils {
|
|||
return indexOf(elem, predicate) >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return True if any element satisfies the predicate, false otherwise.
|
||||
* Note that means this always returns false for empty collections.
|
||||
*
|
||||
* This is an overload for List that avoids unnecessary iterator allocation.
|
||||
*/
|
||||
public static <T> boolean any(@NonNull List<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.
|
||||
*/
|
||||
|
|
@ -126,6 +136,21 @@ public final class CollectionUtils {
|
|||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The index of the first element that matches the predicate, or -1 if none.
|
||||
*
|
||||
* This is an overload for List that avoids unnecessary iterator allocation.
|
||||
*/
|
||||
public static <T> int indexOf(@NonNull final List<T> elem,
|
||||
@NonNull final Predicate<? super T> predicate) {
|
||||
for (int idx = 0; idx < elem.size(); ++idx) {
|
||||
if (predicate.test(elem.get(idx))) {
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return True if there exists at least one element in the sparse array for which
|
||||
* condition {@code predicate}
|
||||
|
|
|
|||
|
|
@ -53,6 +53,12 @@ class CollectionUtilsTest {
|
|||
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 })
|
||||
|
||||
assertEquals(4, CollectionUtils.indexOf(setOf("A", "B", "C", "D", "E")) { it == "E" })
|
||||
assertEquals(0, CollectionUtils.indexOf(setOf("A", "B", "C", "D", "E")) { it == "A" })
|
||||
assertEquals(1, CollectionUtils.indexOf(setOf("AA", "BBB", "CCCC")) { it.length >= 3 })
|
||||
assertEquals(1, CollectionUtils.indexOf(setOf("AA", null, "CCCC")) { it == null })
|
||||
assertEquals(1, CollectionUtils.indexOf(setOf(null, "CCCC")) { it != null })
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in a new issue