FileDocCategorySizeDatePackage
MatrixCursor.javaAPI DocAndroid 1.5 API8374Wed May 06 22:41:54 BST 2009android.database

MatrixCursor

public class MatrixCursor extends AbstractCursor
A mutable cursor implementation backed by an array of {@code Object}s. Use {@link #newRow()} to add rows. Automatically expands internal capacity as needed.

Fields Summary
private final String[]
columnNames
private Object[]
data
private int
rowCount
private final int
columnCount
Constructors Summary
public MatrixCursor(String[] columnNames, int initialCapacity)
Constructs a new cursor with the given initial capacity.

param
columnNames names of the columns, the ordering of which determines column ordering elsewhere in this cursor
param
initialCapacity in rows


                                        
         
        this.columnNames = columnNames;
        this.columnCount = columnNames.length;

        if (initialCapacity < 1) {
            initialCapacity = 1;
        }

        this.data = new Object[columnCount * initialCapacity];
    
public MatrixCursor(String[] columnNames)
Constructs a new cursor.

param
columnNames names of the columns, the ordering of which determines column ordering elsewhere in this cursor

        this(columnNames, 16);
    
Methods Summary
public voidaddRow(java.lang.Object[] columnValues)
Adds a new row to the end with the given column values. Not safe for concurrent use.

throws
IllegalArgumentException if {@code columnValues.length != columnNames.length}
param
columnValues in the same order as the the column names specified at cursor construction time

        if (columnValues.length != columnCount) {
            throw new IllegalArgumentException("columnNames.length = "
                    + columnCount + ", columnValues.length = "
                    + columnValues.length);
        }

        int start = rowCount++ * columnCount;
        ensureCapacity(start + columnCount);
        System.arraycopy(columnValues, 0, data, start, columnCount);
    
public voidaddRow(java.lang.Iterable columnValues)
Adds a new row to the end with the given column values. Not safe for concurrent use.

throws
IllegalArgumentException if {@code columnValues.size() != columnNames.length}
param
columnValues in the same order as the the column names specified at cursor construction time

        int start = rowCount * columnCount;
        int end = start + columnCount;
        ensureCapacity(end);

        if (columnValues instanceof ArrayList<?>) {
            addRow((ArrayList<?>) columnValues, start);
            return;
        }

        int current = start;
        Object[] localData = data;
        for (Object columnValue : columnValues) {
            if (current == end) {
                // TODO: null out row?
                throw new IllegalArgumentException(
                        "columnValues.size() > columnNames.length");
            }
            localData[current++] = columnValue;
        }

        if (current != end) {
            // TODO: null out row?
            throw new IllegalArgumentException(
                    "columnValues.size() < columnNames.length");
        }

        // Increase row count here in case we encounter an exception.
        rowCount++;
    
private voidaddRow(java.util.ArrayList columnValues, int start)
Optimization for {@link ArrayList}.

        int size = columnValues.size();
        if (size != columnCount) {
            throw new IllegalArgumentException("columnNames.length = "
                    + columnCount + ", columnValues.size() = " + size);
        }

        rowCount++;
        Object[] localData = data;
        for (int i = 0; i < size; i++) {
            localData[start + i] = columnValues.get(i);
        }
    
private voidensureCapacity(int size)
Ensures that this cursor has enough capacity.

        if (size > data.length) {
            Object[] oldData = this.data;
            int newSize = data.length * 2;
            if (newSize < size) {
                newSize = size;
            }
            this.data = new Object[newSize];
            System.arraycopy(oldData, 0, this.data, 0, oldData.length);
        }
    
private java.lang.Objectget(int column)
Gets value at the given column for the current row.

        if (column < 0 || column >= columnCount) {
            throw new CursorIndexOutOfBoundsException("Requested column: "
                    + column + ", # of columns: " +  columnCount);
        }
        if (mPos < 0) {
            throw new CursorIndexOutOfBoundsException("Before first row.");
        }
        if (mPos >= rowCount) {
            throw new CursorIndexOutOfBoundsException("After last row.");
        }
        return data[mPos * columnCount + column];
    
public java.lang.String[]getColumnNames()

        return columnNames;
    
public intgetCount()

        return rowCount;
    
public doublegetDouble(int column)

        Object value = get(column);
        return (value instanceof String)
                ? Double.valueOf((String) value)
                : ((Number) value).doubleValue();
    
public floatgetFloat(int column)

        Object value = get(column);
        return (value instanceof String)
                ? Float.valueOf((String) value)
                : ((Number) value).floatValue();
    
public intgetInt(int column)

        Object value = get(column);
        return (value instanceof String)
                ? Integer.valueOf((String) value)
                : ((Number) value).intValue();
    
public longgetLong(int column)

        Object value = get(column);
        return (value instanceof String)
                ? Long.valueOf((String) value)
                : ((Number) value).longValue();
    
public shortgetShort(int column)

        Object value = get(column);
        return (value instanceof String)
                ? Short.valueOf((String) value)
                : ((Number) value).shortValue();
    
public java.lang.StringgetString(int column)

        return String.valueOf(get(column));
    
public booleanisNull(int column)

        return get(column) == null;
    
public android.database.MatrixCursor$RowBuildernewRow()
Adds a new row to the end and returns a builder for that row. Not safe for concurrent use.

return
builder which can be used to set the column values for the new row

        rowCount++;
        int endIndex = rowCount * columnCount;
        ensureCapacity(endIndex);
        int start = endIndex - columnCount;
        return new RowBuilder(start, endIndex);