1 /* 2 * Copyright (C) 2010 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 package com.android.quicksearchbox; 17 18 import android.database.AbstractCursor; 19 20 import java.util.ArrayList; 21 22 /** 23 * Mock cursor providing suggestions data. 24 */ 25 public class MockSuggestionProviderCursor extends AbstractCursor { 26 27 private final String[] mColumns; 28 private final ArrayList<Row> mRows; 29 MockSuggestionProviderCursor(String[] columns)30 public MockSuggestionProviderCursor(String[] columns) { 31 mColumns = columns; 32 mRows = new ArrayList<Row>(); 33 } 34 addRow(Object... values)35 public Row addRow(Object... values) { 36 Row row = new Row(values); 37 mRows.add(row); 38 return row; 39 } 40 get(int column)41 public Object get(int column) { 42 return mRows.get(getPosition()).get(column); 43 } 44 45 @Override getColumnNames()46 public String[] getColumnNames() { 47 return mColumns; 48 } 49 50 @Override getCount()51 public int getCount() { 52 return mRows.size(); 53 } 54 55 @Override getDouble(int column)56 public double getDouble(int column) { 57 return mRows.get(getPosition()).getDouble(column); 58 } 59 60 @Override getFloat(int column)61 public float getFloat(int column) { 62 return mRows.get(getPosition()).getFloat(column); 63 } 64 65 @Override getInt(int column)66 public int getInt(int column) { 67 return mRows.get(getPosition()).getInt(column); 68 } 69 70 @Override getLong(int column)71 public long getLong(int column) { 72 return mRows.get(getPosition()).getLong(column); 73 } 74 75 @Override getShort(int column)76 public short getShort(int column) { 77 return mRows.get(getPosition()).getShort(column); 78 } 79 80 @Override getString(int column)81 public String getString(int column) { 82 return mRows.get(getPosition()).getString(column); 83 } 84 85 @Override isNull(int column)86 public boolean isNull(int column) { 87 return mRows.get(getPosition()).isNull(column); 88 } 89 90 private class Row { 91 private final Object[] mValues; Row(Object... values)92 public Row(Object... values) { 93 if (values.length > mColumns.length) { 94 throw new IllegalArgumentException("Too many columns"); 95 } 96 mValues = new Object[mColumns.length]; 97 for (int i = 0; i < values.length; ++i) { 98 setColumn(i, values[i]); 99 } 100 } 101 setColumn(int column, Object value)102 public Row setColumn(int column, Object value) { 103 mValues[column] = value; 104 return this; 105 } 106 get(int column)107 public Object get(int column) { 108 return mValues[column]; 109 } 110 getDouble(int column)111 public double getDouble(int column) { 112 Object o = mValues[column]; 113 if (o == null) return 0d; 114 if (o instanceof Double) return (Double) o; 115 return Double.valueOf(o.toString()); 116 } 117 getFloat(int column)118 public float getFloat(int column) { 119 Object o = mValues[column]; 120 if (o == null) return 0f; 121 if (o instanceof Float) return (Float) o; 122 return Float.valueOf(o.toString()); 123 } 124 getInt(int column)125 public int getInt(int column) { 126 Object o = mValues[column]; 127 if (o == null) return 0; 128 return Integer.valueOf(o.toString()); 129 } 130 getLong(int column)131 public long getLong(int column) { 132 Object o = mValues[column]; 133 if (o == null) return 0; 134 return Long.valueOf(o.toString()); 135 } 136 getShort(int column)137 public short getShort(int column) { 138 Object o = mValues[column]; 139 if (o == null) return 0; 140 return Short.valueOf(o.toString()); 141 } 142 getString(int column)143 public String getString(int column) { 144 Object o = mValues[column]; 145 if (o == null) return null; 146 if (o instanceof String) return (String) o; 147 return o.toString(); 148 } 149 isNull(int column)150 public boolean isNull(int column) { 151 return (mValues[column] == null); 152 } 153 154 } 155 156 } 157