1 /*
2  * Copyright (C) 2024 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.nfc;
18 
19 import android.annotation.Nullable;
20 
21 import java.util.List;
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      * Return first index of {@code value} in {@code array}, or {@code -1} if
32      * not found.
33      */
indexOf(@ullable T[] array, T value)34     public static <T> int indexOf(@Nullable T[] array, T value) {
35         if (array == null) return -1;
36         for (int i = 0; i < array.length; i++) {
37             if (Objects.equals(array[i], value)) return i;
38         }
39         return -1;
40     }
41 
42     /**
43      * Checks if given array is null or has zero elements.
44      */
isEmpty(@ullable int[] array)45     public static boolean isEmpty(@Nullable int[] array) {
46         return array == null || array.length == 0;
47     }
48 
49     /**
50      * True if the byte array is null or has length 0.
51      */
isEmpty(@ullable byte[] array)52     public static boolean isEmpty(@Nullable byte[] array) {
53         return array == null || array.length == 0;
54     }
55 
56     /**
57      * Converts from List of bytes to byte array
58      * @param list
59      * @return byte[]
60      */
toPrimitive(List<byte[]> list)61     public static byte[] toPrimitive(List<byte[]> list) {
62         if (list.size() == 0) {
63             return new byte[0];
64         }
65         int byteLen = list.get(0).length;
66         byte[] array = new byte[list.size() * byteLen];
67         for (int i = 0; i < list.size(); i++) {
68             for (int j = 0; j < list.get(i).length; j++) {
69                 array[i * byteLen + j] = list.get(i)[j];
70             }
71         }
72         return array;
73     }
74 }
75