public class Cupboard
extends java.lang.Object
The entry point of Cupboard is this class. The typical way to get an instance of this class is to use CupboardFactory using a static import:
import static nl.qbusict.cupboard.CupboardFactory.cupboard;
public class Example {
static {
cupboard().register(MyEntity.class);
}
public void storeMyEntity(SQLiteDatabase db, MyEntity entity) {
cupboard().withDataBase(db).put(entity);
}
}
Entities that are used with Cupboard should be registered using the register(Class) function. If an entity class isn't registered an IllegalArgumentException is thrown at runtime,
for example when attempting to call DatabaseCompartment.createTables()
ContentResolver. That's why there are different entry points:
withDatabase(SQLiteDatabase) for interacting with a databasewithContext(Context) for interacting with a ContentProviderwithCursor(Cursor) for getting results from Cursor objectswithEntity(Class) for converting entities to ContentValueswithOperations(ArrayList) for working on a list of ContentProviderOperationsColumn annotation can be used on an entity field. By default, these
annotations are not processed, processing needs to be enabled explicitly by creating a Cupboard instance using CupboardBuilder
Cupboard cupboard = new CupboardBuilder().useAnnotations().build(); cupboard.setUseAnnotations(true);Above would configure a local instance of Cupboard to use the
Column annotation for mapping fields to columns.
If you would like to make this the global default, use CupboardBuilder as well.
static {
new CupboardBuilder().useAnnotations().asGlobalInstance().build();
cupboard().register(MyEntity.class);
}
| Constructor and Description |
|---|
Cupboard() |
| Modifier and Type | Method and Description |
|---|---|
<T> EntityConverter<T> |
getDelegateEntityConverter(EntityConverterFactory skipPast,
java.lang.Class<T> entityClass)
Get an entity converter to be used as a delegate.
|
FieldConverter<?> |
getDelegateFieldConverter(FieldConverterFactory skipPast,
java.lang.reflect.Type type)
Get a field converter to be used as a delegate.
|
<T> EntityConverter<T> |
getEntityConverter(java.lang.Class<T> entityClass)
Get an entity converter for an entity class.
|
FieldConverter<?> |
getFieldConverter(java.lang.reflect.Type type)
Get a field converter for the specified
Type |
java.util.Collection<java.lang.Class<?>> |
getRegisteredEntities()
Get the classes that are registered with this instance
|
<T> java.lang.String |
getTable(java.lang.Class<T> clz)
Get the database table for an entity
|
boolean |
isRegisteredEntity(java.lang.Class<?> entityClass)
Check if an entity class is registered.
|
boolean |
isUseAnnotations()
Return if annotations are enabled
|
<T> void |
register(java.lang.Class<T> clz)
Register an entity class.
|
ProviderCompartment |
withContext(android.content.Context context)
Operate on a
ContentResolver |
CursorCompartment |
withCursor(android.database.Cursor cursor)
Operate on a
Cursor |
DatabaseCompartment |
withDatabase(CupboardDatabase db)
Operate on a
CupboardDatabase |
DatabaseCompartment |
withDatabase(android.database.sqlite.SQLiteDatabase db)
Operate on a
SQLiteDatabase |
<T> EntityCompartment<T> |
withEntity(java.lang.Class<T> entityClass)
Operate on an entity
|
ProviderOperationsCompartment |
withOperations(java.util.ArrayList<android.content.ContentProviderOperation> operations)
Operate on a list of
ContentProviderOperations |
public <T> void register(java.lang.Class<T> clz)
clz - the entity class to register.public DatabaseCompartment withDatabase(android.database.sqlite.SQLiteDatabase db)
SQLiteDatabasedb - the database to wrapDatabaseCompartment wrapping the database for chaining.public DatabaseCompartment withDatabase(CupboardDatabase db)
CupboardDatabasedb - the database to wrapDatabaseCompartment wrapping the database for chaining.public CursorCompartment withCursor(android.database.Cursor cursor)
Cursorcursor - the cursor to wrapCursorCompartment wrapping the cursor for chaining.public ProviderCompartment withContext(android.content.Context context)
ContentResolvercontext - the Context to retrieve the ContentResolver fromProviderCompartment to interact with the ContentResolverpublic ProviderOperationsCompartment withOperations(java.util.ArrayList<android.content.ContentProviderOperation> operations)
ContentProviderOperationsoperations - the (empty) list of operations to append toProviderOperationsCompartment for chainingpublic <T> EntityCompartment<T> withEntity(java.lang.Class<T> entityClass)
entityClass - the entity classEntityCompartment to interact with the entity.public <T> java.lang.String getTable(java.lang.Class<T> clz)
clz - the entity classpublic java.util.Collection<java.lang.Class<?>> getRegisteredEntities()
public boolean isUseAnnotations()
public FieldConverter<?> getFieldConverter(java.lang.reflect.Type type) throws java.lang.IllegalArgumentException
Typetype - the typejava.lang.IllegalArgumentException - if a field of this type cannot be converted by this instancepublic <T> EntityConverter<T> getEntityConverter(java.lang.Class<T> entityClass) throws java.lang.IllegalArgumentException
EntityConverter.fromCursor(Cursor)
expects the cursor passed in to have the columns in the expected order.
When not using this as part of delegation from an EntityConverter, the recommended way is to use withCursor(Cursor) and use CursorCompartment.get(Class)
to convert from a cursor to an entity.T - the entity typeentityClass - the entity class, must have been previous registered using register(Class)java.lang.IllegalArgumentException - if an entity of this type cannot be converted by this instancepublic FieldConverter<?> getDelegateFieldConverter(FieldConverterFactory skipPast, java.lang.reflect.Type type) throws java.lang.IllegalArgumentException
FieldConverter to delegate
to the default converter for example.skipPast - the FieldConverterFactory to skip when searching for the delegate convertertype - the type for the fieldjava.lang.IllegalArgumentException - if a field of this type cannot be converted by this instancepublic <T> EntityConverter<T> getDelegateEntityConverter(EntityConverterFactory skipPast, java.lang.Class<T> entityClass) throws java.lang.IllegalArgumentException
EntityConverter to delegate
to the default converter for example.T - the entity typeskipPast - The EntityConverterFactory to skip when searching for the delegate converterentityClass - the entity classjava.lang.IllegalArgumentException - if an entity of this type cannot be converted by this instancepublic boolean isRegisteredEntity(java.lang.Class<?> entityClass)
entityClass - the entity class