| 1 | /* |
| 2 | * Copyright (C) 2014 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 android.hardware.camera2.utils; |
| 18 | |
| 19 | import android.util.Log; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | /** |
| 26 | * Various assortment of array utilities. |
| 27 | */ |
| 28 | public class ArrayUtils { |
| 29 | |
| 30 | private static final String TAG = "ArrayUtils"; |
| 31 | private static final boolean DEBUG = false; |
| 32 | |
| 33 | /** Return the index of {@code needle} in the {@code array}, or else {@code -1} */ |
| 34 | public static <T> int getArrayIndex(T[] array, T needle) { |
| 35 | if (array == null) { |
| 36 | return -1; |
| 37 | } |
| 38 | |
| 39 | int index = 0; |
| 40 | for (T elem : array) { |
| 41 | if (Objects.equals(elem, needle)) { |
| 42 | return index; |
| 43 | } |
| 44 | index++; |
| 45 | } |
| 46 | |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | /** Return the index of {@code needle} in the {@code array}, or else {@code -1} */ |
| 51 | public static int getArrayIndex(int[] array, int needle) { |
| 52 | if (array == null) { |
| 53 | return -1; |
| 54 | } |
| 55 | for (int i = 0; i < array.length; ++i) { |
| 56 | if (array[i] == needle) { |
| 57 | return i; |
| 58 | } |
| 59 | } |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Create an {@code int[]} from the {@code List<>} by using {@code convertFrom} and |
| 65 | * {@code convertTo} as a one-to-one map (via the index). |
| 66 | * |
| 67 | * <p>Strings not appearing in {@code convertFrom} are ignored (with a logged warning); |
| 68 | * strings appearing in {@code convertFrom} but not {@code convertTo} are silently |
| 69 | * dropped.</p> |
| 70 | * |
| 71 | * @param list Source list of strings |
| 72 | * @param convertFrom Conversion list of strings |
| 73 | * @param convertTo Conversion list of ints |
| 74 | * @return An array of ints where the values correspond to the ones in {@code convertTo} |
| 75 | * or {@code null} if {@code list} was {@code null} |
| 76 | */ |
| 77 | public static int[] convertStringListToIntArray( |
| 78 | List<String> list, String[] convertFrom, int[] convertTo) { |
| 79 | if (list == null) { |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | List<Integer> convertedList = convertStringListToIntList(list, convertFrom, convertTo); |
| 84 | |
| 85 | int[] returnArray = new int[convertedList.size()]; |
| 86 | for (int i = 0; i < returnArray.length; ++i) { |
| 87 | returnArray[i] = convertedList.get(i); |
| 88 | } |
| 89 | |
| 90 | return returnArray; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Create an {@code List<Integer>} from the {@code List<>} by using {@code convertFrom} and |
| 95 | * {@code convertTo} as a one-to-one map (via the index). |
| 96 | * |
| 97 | * <p>Strings not appearing in {@code convertFrom} are ignored (with a logged warning); |
| 98 | * strings appearing in {@code convertFrom} but not {@code convertTo} are silently |
| 99 | * dropped.</p> |
| 100 | * |
| 101 | * @param list Source list of strings |
| 102 | * @param convertFrom Conversion list of strings |
| 103 | * @param convertTo Conversion list of ints |
| 104 | * @return A list of ints where the values correspond to the ones in {@code convertTo} |
| 105 | * or {@code null} if {@code list} was {@code null} |
| 106 | */ |
| 107 | public static List<Integer> convertStringListToIntList( |
| 108 | List<String> list, String[] convertFrom, int[] convertTo) { |
| 109 | if (list == null) { |
| 110 | return null; |
| 111 | } |
| 112 | |
| 113 | List<Integer> convertedList = new ArrayList<>(list.size()); |
| 114 | |
| 115 | for (String str : list) { |
| 116 | int strIndex = getArrayIndex(convertFrom, str); |
| 117 | |
| 118 | // Guard against unexpected values |
| 119 | if (strIndex < 0) { |
| 120 | if (DEBUG) Log.v(TAG, "Ignoring invalid value " + str); |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | // Ignore values we can't map into (intentional) |
| 125 | if (strIndex < convertTo.length) { |
| 126 | convertedList.add(convertTo[strIndex]); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return convertedList; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Convert the list of integers in {@code list} to an {@code int} array. |
| 135 | * |
| 136 | * <p>Every element in {@code list} must be non-{@code null}.</p> |
| 137 | * |
| 138 | * @param list a list of non-{@code null} integers |
| 139 | * |
| 140 | * @return a new int array containing all the elements from {@code list} |
| 141 | * |
| 142 | * @throws NullPointerException if any of the elements in {@code list} were {@code null} |
| 143 | */ |
| 144 | public static int[] toIntArray(List<Integer> list) { |
| 145 | if (list == null) { |
| 146 | return null; |
| 147 | } |
| 148 | |
| 149 | int[] arr = new int[list.size()]; |
| 150 | int i = 0; |
| 151 | for (int elem : list) { |
| 152 | arr[i] = elem; |
| 153 | i++; |
| 154 | } |
| 155 | |
| 156 | return arr; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns true if the given {@code array} contains the given element. |
| 161 | * |
| 162 | * @param array {@code array} to check for {@code elem} |
| 163 | * @param elem {@code elem} to test for |
| 164 | * @return {@code true} if the given element is contained |
| 165 | */ |
| 166 | public static boolean contains(int[] array, int elem) { |
| 167 | return getArrayIndex(array, elem) != -1; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns true if the given {@code array} contains the given element. |
| 172 | * |
| 173 | * @param array {@code array} to check for {@code elem} |
| 174 | * @param elem {@code elem} to test for |
| 175 | * @return {@code true} if the given element is contained |
| 176 | */ |
| 177 | public static <T> boolean contains(T[] array, T elem) { |
| 178 | return getArrayIndex(array, elem) != -1; |
| 179 | } |
| 180 | |
| 181 | private ArrayUtils() { |
| 182 | throw new AssertionError(); |
| 183 | } |
| 184 | } |