1 /*
2  * Copyright (C) 2013 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.provider.cts.contacts;
18 
19 import android.content.ContentValues;
20 import android.database.Cursor;
21 
22 import junit.framework.Assert;
23 
24 import java.util.ArrayList;
25 import java.util.List;
26 
27 /**
28  * Common database methods.
29  */
30 public class CommonDatabaseUtils {
31 
32     // primitive value used when record is not found.
33     public static final long NOT_FOUND = -1;
34 
singleRecordToArray(Cursor cursor)35     public static String[] singleRecordToArray(Cursor cursor) {
36         String[] result = null;
37         try {
38             if (cursor.moveToNext()) {
39                 result = new String[cursor.getColumnCount()];
40                 fillArray(cursor, result);
41             }
42         } finally {
43             closeQuietly(cursor);
44         }
45         return result;
46     }
47 
multiRecordToArray(Cursor cursor)48     public static List<String[]> multiRecordToArray(Cursor cursor) {
49         ArrayList<String[]> result = new ArrayList<String[]>();
50         try {
51             while (cursor.moveToNext()) {
52                 String[] record = new String[cursor.getColumnCount()];
53                 fillArray(cursor, record);
54                 result.add(record);
55             }
56         } finally {
57             closeQuietly(cursor);
58         }
59         return result;
60     }
61 
fillArray(Cursor cursor, String[] array)62     private static void fillArray(Cursor cursor, String[] array) {
63         for (int i = 0; i < array.length; i++) {
64             array[i] = cursor.getString(i);
65         }
66     }
67 
closeQuietly(Cursor cursor)68     public static void closeQuietly(Cursor cursor) {
69         if (cursor != null) {
70             cursor.close();
71         }
72     }
73 
74     /**
75      * Verifies that the number of string parameters is either zero or even, and inserts them
76      * into the provided ContentValues object as a set of name-value pairs. Throws an exception if
77      * the number of string parameters is odd, or a single null parameter was provided.
78      *
79      * @param values ContentValues object to insert name-value pairs into
80      * @param extras Zero or even number of string parameters
81      */
extrasVarArgsToValues(ContentValues values, String... extras)82     public static void extrasVarArgsToValues(ContentValues values, String... extras) {
83         Assert.assertNotNull(extras);
84         // Check that the number of provided string parameters is even.
85         Assert.assertEquals(0, extras.length % 2);
86         for (int i = 0; i < extras.length; i += 2) {
87             values.put(extras[i], extras[i + 1]);
88         }
89     }
90 }
91