1384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy/*
2384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy * Copyright (C) 2013 The Android Open Source Project
3384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy *
4384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy * Licensed under the Apache License, Version 2.0 (the "License");
5384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy * you may not use this file except in compliance with the License.
6384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy * You may obtain a copy of the License at
7384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy *
8384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
9384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy *
10384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy * Unless required by applicable law or agreed to in writing, software
11384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
12384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy * See the License for the specific language governing permissions and
14384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy * limitations under the License.
15384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy */
16384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedypackage com.android.mail.utils;
17384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy
18384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedyimport android.database.MatrixCursor;
19384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy
20384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedyimport java.util.HashMap;
21384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedyimport java.util.Map;
22384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy
23384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedypublic class MatrixCursorWithCachedColumns extends MatrixCursor {
24384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy    /** A mapping of column names to column indices, to speed up lookups */
25384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy    private final Map<String, Integer> mColumnNameMap;
26384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy
27384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy    public MatrixCursorWithCachedColumns(final String[] columnNames, final int initialCapacity) {
28384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy        super(columnNames, initialCapacity);
29384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy
30384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy        final int columnCount = columnNames.length;
31384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy        mColumnNameMap = new HashMap<String, Integer>(columnCount, 1);
32384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy        for (int i = 0; i < columnCount; i++) {
33384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy            mColumnNameMap.put(columnNames[i], i);
34384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy        }
35384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy    }
36384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy
37384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy    public MatrixCursorWithCachedColumns(final String[] columnNames) {
38384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy        this(columnNames, 16);
39384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy    }
40384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy
41384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy    @Override
42384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy    public int getColumnIndex(String columnName) {
43384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy        final Integer i = mColumnNameMap.get(columnName);
44384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy        if (i != null) {
45384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy            return i.intValue();
46384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy        }
47384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy
48384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy        return -1;
49384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy    }
50384b8de1a1e18b29fe392151b06ce6fbe07be4bdScott Kennedy}
51