ArrayUtils.java revision df6242e374b81e802a38cb891477f05d3e4b3cbc
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 = new ArrayList<>(list.size());
71
72        for (String str : list) {
73            int strIndex = getArrayIndex(convertFrom, str);
74
75            // Guard against unexpected values
76            if (strIndex < 0) {
77                Log.w(TAG, "Ignoring invalid value " + str);
78                continue;
79            }
80
81            // Ignore values we can't map into (intentional)
82            if (strIndex < convertTo.length) {
83                convertedList.add(convertTo[strIndex]);
84            }
85        }
86
87        int[] returnArray = new int[convertedList.size()];
88        for (int i = 0; i < returnArray.length; ++i) {
89            returnArray[i] = convertedList.get(i);
90        }
91
92        return returnArray;
93    }
94
95    private ArrayUtils() {
96        throw new AssertionError();
97    }
98}
99