• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.text.TextUtils;
20 
21 import java.util.Objects;
22 
23 public final class ArrayUtils {
ArrayUtils()24     private ArrayUtils() { /* cannot be instantiated */ }
25 
26     /**
27      * Checks that value is present as at least one of the elements of the array.
28      * @param array the array to check in
29      * @param value the value to check for
30      * @return true if the value is present in the array
31      */
contains(T[] array, T value)32     public static <T> boolean contains(T[] array, T value) {
33         return indexOf(array, value) != -1;
34     }
35 
36     /**
37      * Return first index of {@code value} in {@code array}, or {@code -1} if
38      * not found.
39      */
indexOf(T[] array, T value)40     public static <T> int indexOf(T[] array, T value) {
41         if (array == null) return -1;
42         for (int i = 0; i < array.length; i++) {
43             if (Objects.equals(array[i], value)) return i;
44         }
45         return -1;
46     }
47 
appendString(String[] cur, String val)48     public static String[] appendString(String[] cur, String val) {
49         if (cur == null) {
50             return new String[] { val };
51         }
52         final int N = cur.length;
53         for (int i = 0; i < N; i++) {
54             if (TextUtils.equals(cur[i], val)) {
55                 return cur;
56             }
57         }
58         String[] ret = new String[N + 1];
59         System.arraycopy(cur, 0, ret, 0, N);
60         ret[N] = val;
61         return ret;
62     }
63 }
64