AbstractWindowedCursor.java revision 7ce745248d4de0e6543a559c93423df899832100
1/* 2 * Copyright (C) 2006 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.database; 18 19/** 20 * A base class for Cursors that store their data in {@link CursorWindow}s. 21 * <p> 22 * Subclasses are responsible for filling the cursor window with data during 23 * {@link #onMove(int, int)}, allocating a new cursor window if necessary. 24 * During {@link #requery()}, the existing cursor window should be cleared and 25 * filled with new data. 26 * </p><p> 27 * If the contents of the cursor change or become invalid, the old window must be closed 28 * (because it is owned by the cursor) and set to null. 29 * </p> 30 */ 31public abstract class AbstractWindowedCursor extends AbstractCursor { 32 /** 33 * The cursor window owned by this cursor. 34 */ 35 protected CursorWindow mWindow; 36 37 @Override 38 public byte[] getBlob(int columnIndex) { 39 checkPosition(); 40 return mWindow.getBlob(mPos, columnIndex); 41 } 42 43 @Override 44 public String getString(int columnIndex) { 45 checkPosition(); 46 return mWindow.getString(mPos, columnIndex); 47 } 48 49 @Override 50 public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) { 51 checkPosition(); 52 mWindow.copyStringToBuffer(mPos, columnIndex, buffer); 53 } 54 55 @Override 56 public short getShort(int columnIndex) { 57 checkPosition(); 58 return mWindow.getShort(mPos, columnIndex); 59 } 60 61 @Override 62 public int getInt(int columnIndex) { 63 checkPosition(); 64 return mWindow.getInt(mPos, columnIndex); 65 } 66 67 @Override 68 public long getLong(int columnIndex) { 69 checkPosition(); 70 return mWindow.getLong(mPos, columnIndex); 71 } 72 73 @Override 74 public float getFloat(int columnIndex) { 75 checkPosition(); 76 return mWindow.getFloat(mPos, columnIndex); 77 } 78 79 @Override 80 public double getDouble(int columnIndex) { 81 checkPosition(); 82 return mWindow.getDouble(mPos, columnIndex); 83 } 84 85 @Override 86 public boolean isNull(int columnIndex) { 87 checkPosition(); 88 return mWindow.getType(mPos, columnIndex) == Cursor.FIELD_TYPE_NULL; 89 } 90 91 /** 92 * @deprecated Use {@link #getType} 93 */ 94 @Deprecated 95 public boolean isBlob(int columnIndex) { 96 return getType(columnIndex) == Cursor.FIELD_TYPE_BLOB; 97 } 98 99 /** 100 * @deprecated Use {@link #getType} 101 */ 102 @Deprecated 103 public boolean isString(int columnIndex) { 104 return getType(columnIndex) == Cursor.FIELD_TYPE_STRING; 105 } 106 107 /** 108 * @deprecated Use {@link #getType} 109 */ 110 @Deprecated 111 public boolean isLong(int columnIndex) { 112 return getType(columnIndex) == Cursor.FIELD_TYPE_INTEGER; 113 } 114 115 /** 116 * @deprecated Use {@link #getType} 117 */ 118 @Deprecated 119 public boolean isFloat(int columnIndex) { 120 return getType(columnIndex) == Cursor.FIELD_TYPE_FLOAT; 121 } 122 123 @Override 124 public int getType(int columnIndex) { 125 checkPosition(); 126 return mWindow.getType(mPos, columnIndex); 127 } 128 129 @Override 130 protected void checkPosition() { 131 super.checkPosition(); 132 133 if (mWindow == null) { 134 throw new StaleDataException("Attempting to access a closed CursorWindow." + 135 "Most probable cause: cursor is deactivated prior to calling this method."); 136 } 137 } 138 139 @Override 140 public CursorWindow getWindow() { 141 return mWindow; 142 } 143 144 /** 145 * Sets a new cursor window for the cursor to use. 146 * <p> 147 * The cursor takes ownership of the provided cursor window; the cursor window 148 * will be closed when the cursor is closed or when the cursor adopts a new 149 * cursor window. 150 * </p><p> 151 * If the cursor previously had a cursor window, then it is closed when the 152 * new cursor window is assigned. 153 * </p> 154 * 155 * @param window The new cursor window, typically a remote cursor window. 156 */ 157 public void setWindow(CursorWindow window) { 158 if (window != mWindow) { 159 closeWindow(); 160 mWindow = window; 161 } 162 } 163 164 /** 165 * Returns true if the cursor has an associated cursor window. 166 * 167 * @return True if the cursor has an associated cursor window. 168 */ 169 public boolean hasWindow() { 170 return mWindow != null; 171 } 172 173 /** 174 * Closes the cursor window and sets {@link #mWindow} to null. 175 * @hide 176 */ 177 protected void closeWindow() { 178 if (mWindow != null) { 179 mWindow.close(); 180 mWindow = null; 181 } 182 } 183} 184