ArrayUtils.java revision 83d8639e901a24e59c9886dd6910faf3ba7adae1
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 17package android.hardware.camera2.utils; 18 19import android.util.Log; 20 21import java.util.ArrayList; 22import java.util.List; 23import java.util.Objects; 24 25/** 26 * Various assortment of array utilities. 27 */ 28public class ArrayUtils { 29 30 private static final String TAG = "ArrayUtils"; 31 private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); 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 (needle == 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 /** 51 * Create an {@code int[]} from the {@code List<>} by using {@code convertFrom} and 52 * {@code convertTo} as a one-to-one map (via the index). 53 * 54 * <p>Strings not appearing in {@code convertFrom} are ignored (with a logged warning); 55 * strings appearing in {@code convertFrom} but not {@code convertTo} are silently 56 * dropped.</p> 57 * 58 * @param list Source list of strings 59 * @param convertFrom Conversion list of strings 60 * @param convertTo Conversion list of ints 61 * @return An array of ints where the values correspond to the ones in {@code convertTo} 62 * or {@code null} if {@code list} was {@code null} 63 */ 64 public static int[] convertStringListToIntArray( 65 List<String> list, String[] convertFrom, int[] convertTo) { 66 if (list == null) { 67 return null; 68 } 69 70 List<Integer> convertedList = convertStringListToIntList(list, convertFrom, convertTo); 71 72 int[] returnArray = new int[convertedList.size()]; 73 for (int i = 0; i < returnArray.length; ++i) { 74 returnArray[i] = convertedList.get(i); 75 } 76 77 return returnArray; 78 } 79 80 /** 81 * Create an {@code List<Integer>} from the {@code List<>} by using {@code convertFrom} and 82 * {@code convertTo} as a one-to-one map (via the index). 83 * 84 * <p>Strings not appearing in {@code convertFrom} are ignored (with a logged warning); 85 * strings appearing in {@code convertFrom} but not {@code convertTo} are silently 86 * dropped.</p> 87 * 88 * @param list Source list of strings 89 * @param convertFrom Conversion list of strings 90 * @param convertTo Conversion list of ints 91 * @return A list of ints where the values correspond to the ones in {@code convertTo} 92 * or {@code null} if {@code list} was {@code null} 93 */ 94 public static List<Integer> convertStringListToIntList( 95 List<String> list, String[] convertFrom, int[] convertTo) { 96 if (list == null) { 97 return null; 98 } 99 100 List<Integer> convertedList = new ArrayList<>(list.size()); 101 102 for (String str : list) { 103 int strIndex = getArrayIndex(convertFrom, str); 104 105 // Guard against unexpected values 106 if (strIndex < 0) { 107 Log.w(TAG, "Ignoring invalid value " + str); 108 continue; 109 } 110 111 // Ignore values we can't map into (intentional) 112 if (strIndex < convertTo.length) { 113 convertedList.add(convertTo[strIndex]); 114 } 115 } 116 117 return convertedList; 118 } 119 120 /** 121 * Convert the list of integers in {@code list} to an {@code int} array. 122 * 123 * <p>Every element in {@code list} must be non-{@code null}.</p> 124 * 125 * @param list a list of non-{@code null} integers 126 * 127 * @return a new int array containing all the elements from {@code list} 128 * 129 * @throws NullPointerException if any of the elements in {@code list} were {@code null} 130 */ 131 public static int[] toIntArray(List<Integer> list) { 132 if (list == null) { 133 return null; 134 } 135 136 int[] arr = new int[list.size()]; 137 int i = 0; 138 for (int elem : list) { 139 arr[i] = elem; 140 i++; 141 } 142 143 return arr; 144 } 145 146 private ArrayUtils() { 147 throw new AssertionError(); 148 } 149} 150