Back to project page androidata.
The source code is released under:
Apache License
If you think the Android project androidata listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.stanidesis.androidata.model; //from w ww . j a v a 2 s . co m import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.stanidesis.androidata.column.BooleanColumn; import com.stanidesis.androidata.column.Column; import com.stanidesis.androidata.column.DoubleColumn; import com.stanidesis.androidata.column.IntegerColumn; import com.stanidesis.androidata.column.StringColumn; import com.stanidesis.androidata.table.BaseTable; import com.stanidesis.androidata.table.ITable; import java.util.HashMap; import java.util.Map; /** * Created by Stanley Idesis on 8/19/14. */ public abstract class ReadOnlyModel<Table extends ITable, Updater extends IModelUpdateBuilder> { /** * Fields */ // A map of Column names to their respective Data private Map<String, Object> mData; //A table reference for this model private final Table mTable; //True during population private boolean mIsPopulating; /* * Constructor */ public ReadOnlyModel(Cursor cursor, Table table) { if (table == null || cursor == null || cursor.getColumnIndex(BaseTable.COLUMN_ROW_ID.getName()) == -1) { throw new IllegalArgumentException("Table:" + table + ", Cursor: " + cursor); } mData = new HashMap<String, Object>(); mTable = table; populateFromCursor(cursor); } /* * Public Methods */ /** * @return the row Id associated with this model */ public long getRowId() { return get(BaseTable.COLUMN_ROW_ID); } /** * @return an instance of a ModelUpdater capable of updating this exact model */ public abstract Updater createModelUpdater(); /** * Recover an instance of the table this model is associated with * @return */ public Table getTable() { return mTable; } /** * A typed getter * * @param column the column to whose data should be retrieved * @param <Type> the type that is expected of Object * @return the object requested, casted to the desired type or null if not found */ public <Type extends Object> Type get(Column<Type> column) { Type retValue = null; try { retValue = (Type) mData.get(column.getName()); } catch (Exception e) { e.printStackTrace(); } return retValue; } /** * A convenience accessor for Boolean columns. Booleans * must be represented by Integer types and therefore * this added work is required to determine logical truth. * * @param booleanColumn * @return true if the integer represented by the column is equal to `1`, false otherwise */ public boolean is(BooleanColumn booleanColumn) { Long longBool = get(booleanColumn); return longBool == null ? false : longBool == BooleanColumn.TRUE; } /** * Repopulate this model from the database. * This method performs synchronous database access. * @param readOnlyDB */ public void refresh(SQLiteDatabase readOnlyDB) { if (mIsPopulating) { return; } mIsPopulating = true; Cursor row = mTable.getRow(readOnlyDB, getRowId()); if (row == null || !row.moveToFirst()) { throw new RuntimeException("Model does not exist in database"); } populateFromCursor(row); row.close(); mIsPopulating = false; } /** * Merge updates into the ROM * * @param updateBuilder */ public final void commitUpdates(Updater updateBuilder) { for (String key : updateBuilder.getValues().keySet()) { mData.put(key, updateBuilder.getValues().get(key)); } } /* * Protected Methods */ /** * Recover information from a cursor object representing this model */ protected void populateFromCursor(Cursor cursor) { for (Column column : getTable().getColumns()) { setColumn(column, optColumn(cursor, column)); } } /** * Set a piece of data for a given column * @param column * @param data */ protected void setColumn(Column column, Object data) { setColumn(column.getName(), data); } /** * Set a piece of data for a given column with name * @param columnName * @param data */ protected void setColumn(String columnName, Object data) { mData.put(columnName, data); } /** * Empty all data */ protected void clearData() { mData.clear(); } protected <Type extends Object> Type optColumn(Cursor cursor, Column<Type> column) { if (column instanceof StringColumn) { try { return (Type) optString(cursor, column.getName()); } catch (Exception e) { return null; } } else if (column instanceof IntegerColumn) { try { return (Type) optLong(cursor, column.getName()); } catch (Exception e) { return null; } } else if (column instanceof DoubleColumn) { try { return (Type) optDouble(cursor, column.getName()); } catch (Exception e) { return null; } } return null; } protected String optString(Cursor cursor, String columnName) { int columnIndex = cursor.getColumnIndex(columnName); if (columnIndex < 0) { return null; } return cursor.getString(columnIndex); } protected Long optLong(Cursor cursor, String columnName) { int columnIndex = cursor.getColumnIndex(columnName); if (columnIndex < 0) { return null; } return cursor.getLong(columnIndex); } protected Double optDouble(Cursor cursor, String columnName) { int columnIndex = cursor.getColumnIndex(columnName); if (columnIndex < 0) { return null; } return cursor.getDouble(columnIndex); } }