1 /* 2 * Copyright (C) 2019 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.server.wifi.util; 18 19 import android.annotation.Nullable; 20 21 import java.util.Collection; 22 import java.util.Objects; 23 24 /** 25 * Copied over from frameworks/base/core/java/com/android/internal/util/ArrayUtils.java 26 */ 27 public class ArrayUtils { ArrayUtils()28 private ArrayUtils() { /* cannot be instantiated */ } 29 30 /** 31 * Checks if the beginnings of two byte arrays are equal. 32 * 33 * @param array1 the first byte array 34 * @param array2 the second byte array 35 * @param length the number of bytes to check 36 * @return true if they're equal, false otherwise 37 */ equals(byte[] array1, byte[] array2, int length)38 public static boolean equals(byte[] array1, byte[] array2, int length) { 39 if (length < 0) { 40 throw new IllegalArgumentException(); 41 } 42 43 if (array1 == array2) { 44 return true; 45 } 46 if (array1 == null || array2 == null || array1.length < length || array2.length < length) { 47 return false; 48 } 49 for (int i = 0; i < length; i++) { 50 if (array1[i] != array2[i]) { 51 return false; 52 } 53 } 54 return true; 55 } 56 57 /** 58 * Checks if given array is null or has zero elements. 59 */ isEmpty(@ullable Collection<?> array)60 public static boolean isEmpty(@Nullable Collection<?> array) { 61 return array == null || array.isEmpty(); 62 } 63 64 /** 65 * Checks if given array is null or has zero elements. 66 */ isEmpty(@ullable T[] array)67 public static <T> boolean isEmpty(@Nullable T[] array) { 68 return array == null || array.length == 0; 69 } 70 71 /** 72 * Checks if given array is null or has zero elements. 73 */ isEmpty(@ullable int[] array)74 public static boolean isEmpty(@Nullable int[] array) { 75 return array == null || array.length == 0; 76 } 77 78 /** 79 * Checks if given array is null or has zero elements. 80 */ isEmpty(@ullable long[] array)81 public static boolean isEmpty(@Nullable long[] array) { 82 return array == null || array.length == 0; 83 } 84 85 /** 86 * Checks if given array is null or has zero elements. 87 */ isEmpty(@ullable byte[] array)88 public static boolean isEmpty(@Nullable byte[] array) { 89 return array == null || array.length == 0; 90 } 91 92 /** 93 * Checks if given array is null or has zero elements. 94 */ isEmpty(@ullable boolean[] array)95 public static boolean isEmpty(@Nullable boolean[] array) { 96 return array == null || array.length == 0; 97 } 98 99 /** 100 * Length of the given array or 0 if it's null. 101 */ size(@ullable Object[] array)102 public static int size(@Nullable Object[] array) { 103 return array == null ? 0 : array.length; 104 } 105 106 /** 107 * Length of the given collection or 0 if it's null. 108 */ size(@ullable Collection<?> collection)109 public static int size(@Nullable Collection<?> collection) { 110 return collection == null ? 0 : collection.size(); 111 } 112 113 /** 114 * Checks that value is present as at least one of the elements of the array. 115 * 116 * @param array the array to check in 117 * @param value the value to check for 118 * @return true if the value is present in the array 119 */ contains(@ullable T[] array, T value)120 public static <T> boolean contains(@Nullable T[] array, T value) { 121 return indexOf(array, value) != -1; 122 } 123 124 /** 125 * Checks that value is present as at least one of the elements of the array. 126 * 127 * @param array the array to check in 128 * @param value the value to check for 129 * @return true if the value is present in the array 130 */ contains(@ullable int[] array, int value)131 public static boolean contains(@Nullable int[] array, int value) { 132 if (array == null) return false; 133 for (int element : array) { 134 if (element == value) { 135 return true; 136 } 137 } 138 return false; 139 } 140 141 /** 142 * Checks that value is present as at least one of the elements of the array. 143 * 144 * @param array the array to check in 145 * @param value the value to check for 146 * @return true if the value is present in the array 147 */ contains(@ullable long[] array, long value)148 public static boolean contains(@Nullable long[] array, long value) { 149 if (array == null) return false; 150 for (long element : array) { 151 if (element == value) { 152 return true; 153 } 154 } 155 return false; 156 } 157 158 /** 159 * Checks that value is present as at least one of the elements of the array. 160 * 161 * @param array the array to check in 162 * @param value the value to check for 163 * @return true if the value is present in the array 164 */ contains(@ullable char[] array, char value)165 public static boolean contains(@Nullable char[] array, char value) { 166 if (array == null) return false; 167 for (char element : array) { 168 if (element == value) { 169 return true; 170 } 171 } 172 return false; 173 } 174 175 /** 176 * Return first index of {@code value} in {@code array}, or {@code -1} if 177 * not found. 178 */ indexOf(@ullable T[] array, T value)179 public static <T> int indexOf(@Nullable T[] array, T value) { 180 if (array == null) return -1; 181 for (int i = 0; i < array.length; i++) { 182 if (Objects.equals(array[i], value)) return i; 183 } 184 return -1; 185 } 186 } 187 188