Add indexOf method
PermissionMonitor is using ArrayUtils#indexOf often in checking
permission methods. It's better to have this method in
CollectionUtils.
Also implement contains() by indexOf method.
Bug: 174541037
Test: atest FrameworksNetTests
Change-Id: I32e2ca815dd35eae9c31188856068eaf3458d250
diff --git a/staticlibs/framework/com/android/net/module/util/CollectionUtils.java b/staticlibs/framework/com/android/net/module/util/CollectionUtils.java
index 74f738d..6e8c6fe 100644
--- a/staticlibs/framework/com/android/net/module/util/CollectionUtils.java
+++ b/staticlibs/framework/com/android/net/module/util/CollectionUtils.java
@@ -95,12 +95,17 @@
* @return true if the array contains the specified value.
*/
public static <T> boolean contains(@Nullable T[] array, @Nullable T value) {
- if (array == null) return false;
- for (T element : array) {
- if (Objects.equals(element, value)) {
- return true;
- }
+ return indexOf(array, value) != -1;
+ }
+
+ /**
+ * Return first index of value in given array, or -1 if not found.
+ */
+ public static <T> int indexOf(@Nullable T[] array, @Nullable T value) {
+ if (array == null) return -1;
+ for (int i = 0; i < array.length; i++) {
+ if (Objects.equals(array[i], value)) return i;
}
- return false;
+ return -1;
}
}