1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.packageinstaller.permission.utils; 18 19 import java.util.Objects; 20 21 public final class ArrayUtils { ArrayUtils()22 private ArrayUtils() { /* cannot be instantiated */ } 23 24 /** 25 * Checks that value is present as at least one of the elements of the array. 26 * @param array the array to check in 27 * @param value the value to check for 28 * @return true if the value is present in the array 29 */ contains(T[] array, T value)30 public static <T> boolean contains(T[] array, T value) { 31 return indexOf(array, value) != -1; 32 } 33 34 /** 35 * Return first index of {@code value} in {@code array}, or {@code -1} if 36 * not found. 37 */ indexOf(T[] array, T value)38 public static <T> int indexOf(T[] array, T value) { 39 if (array == null) return -1; 40 for (int i = 0; i < array.length; i++) { 41 if (Objects.equals(array[i], value)) return i; 42 } 43 return -1; 44 } 45 } 46