• 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 androidx.annotation.Nullable;
22 
23 import java.util.Objects;
24 
25 public final class ArrayUtils {
ArrayUtils()26     private ArrayUtils() { /* cannot be instantiated */ }
27 
28     /**
29      * Checks if an array is null or has no elements.
30      *
31      * @param array the array to check for
32      *
33      * @return whether the array is null or has no elements.
34      */
isEmpty(@ullable T[] array)35     public static <T> boolean isEmpty(@Nullable T[] array) {
36         return array == null || array.length == 0;
37     }
38 
39     /**
40      * Checks that value is present as at least one of the elements of the array.
41      * @param array the array to check in
42      * @param value the value to check for
43      * @return true if the value is present in the array
44      */
contains(T[] array, T value)45     public static <T> boolean contains(T[] array, T value) {
46         return indexOf(array, value) != -1;
47     }
48 
49     /**
50      * Return first index of {@code value} in {@code array}, or {@code -1} if
51      * not found.
52      */
indexOf(T[] array, T value)53     public static <T> int indexOf(T[] array, T value) {
54         if (array == null) return -1;
55         for (int i = 0; i < array.length; i++) {
56             if (Objects.equals(array[i], value)) return i;
57         }
58         return -1;
59     }
60 
appendString(String[] cur, String val)61     public static String[] appendString(String[] cur, String val) {
62         if (cur == null) {
63             return new String[] { val };
64         }
65         final int N = cur.length;
66         for (int i = 0; i < N; i++) {
67             if (TextUtils.equals(cur[i], val)) {
68                 return cur;
69             }
70         }
71         String[] ret = new String[N + 1];
72         System.arraycopy(cur, 0, ret, 0, N);
73         ret[N] = val;
74         return ret;
75     }
76 }
77