If you think the Android project spades listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/**
* Copyright (C) 2013 Pau Picas Sans <pau.picas@gmail.com>
*//fromwww.java2s.com
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/package cat.picas.spades;
import android.content.ContentValues;
import android.database.Cursor;
publicabstractclass EntityMapper<T extends Entity> {
private Table mTable;
public EntityMapper(Table table) {
table.validate();
mTable = table;
}
public Table getTable() {
return mTable;
}
public CursorInfo getDefaultCursorInfo() {
returnnew CursorInfoBuilder().add(mTable).build();
}
publicvoid putContentValues(T entity, ContentValues values) {
mapContentValues(entity, values);
}
public T createFromCursor(Cursor cursor, CursorInfo cursorInfo) {
if (!cursorInfo.hasTable(mTable)) {
return null;
}
T entity = newInstance(cursor, cursorInfo);
// Set the entity ID.
int colIdIndex = cursorInfo.getColumnIndex(mTable.getColumnId());
if (colIdIndex != CursorInfo.INVALID_INDEX) {
if (cursor.isNull(colIdIndex)) {
// As the entity ID is selected (the cursor has the column) and the value is null,
// we should return null as this row doesn't contain any entity instance.
return null;
} else {
entity.setEntityId(cursor.getLong(colIdIndex));
}
}
// Manual population of the entity implemented by user.
mapCursorValues(entity, cursor, cursorInfo);
return entity;
}
protectedabstract T newInstance(Cursor cursor, CursorInfo cursorInfo);
protectedabstractvoid mapContentValues(T entity, ContentValues values);
protectedabstractvoid mapCursorValues(T entity, Cursor cursor, CursorInfo cursorInfo);
}