use of android.support.annotation.NonNull in project storio by pushtorefresh.
the class UserWithTweetsGetResolver method mapFromCursor.
@NonNull
@Override
public UserWithTweets mapFromCursor(@NonNull Cursor cursor) {
final StorIOSQLite storIOSQLite = storIOSQLiteFromPerformGet.get();
// Or you can manually parse cursor (it will be sliiightly faster)
final User user = userGetResolver.mapFromCursor(cursor);
// Yep, you can reuse StorIO here!
// Or, you can do manual low level requests here
// BTW, if you profiled your app and found that such queries are not very fast
// You can always add some optimized version for particular queries to improve the performance
final List<Tweet> tweetsOfTheUser = storIOSQLite.get().listOfObjects(Tweet.class).withQuery(Query.builder().table(TweetsTable.TABLE).where(TweetsTable.COLUMN_AUTHOR + "=?").whereArgs(user.nick()).build()).prepare().executeAsBlocking();
return new UserWithTweets(user, tweetsOfTheUser);
}
use of android.support.annotation.NonNull in project storio by pushtorefresh.
the class BoxedTypesMethodsConstructorIgnoreNullStorIOSQLiteGetResolver method mapFromCursor.
/**
* {@inheritDoc}
*/
@Override
@NonNull
public BoxedTypesMethodsConstructorIgnoreNull mapFromCursor(@NonNull Cursor cursor) {
Boolean field1 = null;
if (!cursor.isNull(cursor.getColumnIndex("field1"))) {
field1 = cursor.getInt(cursor.getColumnIndex("field1")) == 1;
}
Short field2 = null;
if (!cursor.isNull(cursor.getColumnIndex("field2"))) {
field2 = cursor.getShort(cursor.getColumnIndex("field2"));
}
Integer field3 = null;
if (!cursor.isNull(cursor.getColumnIndex("field3"))) {
field3 = cursor.getInt(cursor.getColumnIndex("field3"));
}
Long field4 = null;
if (!cursor.isNull(cursor.getColumnIndex("field4"))) {
field4 = cursor.getLong(cursor.getColumnIndex("field4"));
}
Float field5 = null;
if (!cursor.isNull(cursor.getColumnIndex("field5"))) {
field5 = cursor.getFloat(cursor.getColumnIndex("field5"));
}
Double field6 = null;
if (!cursor.isNull(cursor.getColumnIndex("field6"))) {
field6 = cursor.getDouble(cursor.getColumnIndex("field6"));
}
BoxedTypesMethodsConstructorIgnoreNull object = new BoxedTypesMethodsConstructorIgnoreNull(field1, field2, field3, field4, field5, field6);
return object;
}
use of android.support.annotation.NonNull in project storio by pushtorefresh.
the class DefaultDeleteResolverTest method performDelete.
@Test
public void performDelete() {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
final int expectedNumberOfRowsDeleted = 1;
when(lowLevel.delete(any(DeleteQuery.class))).thenReturn(expectedNumberOfRowsDeleted);
final Uri expectedUri = mock(Uri.class);
final DeleteQuery expectedDeleteQuery = DeleteQuery.builder().uri(expectedUri).where("test where clause").whereArgs("test").build();
final TestItem testItem = TestItem.newInstance();
final DefaultDeleteResolver<TestItem> defaultDeleteResolver = new DefaultDeleteResolver<TestItem>() {
@NonNull
@Override
protected DeleteQuery mapToDeleteQuery(@NonNull TestItem object) {
assertThat(object).isSameAs(testItem);
return expectedDeleteQuery;
}
};
// Performing Delete Operation
final DeleteResult deleteResult = defaultDeleteResolver.performDelete(storIOContentResolver, testItem);
// checks that required delete was performed
verify(lowLevel, times(1)).delete(expectedDeleteQuery);
// only one delete should be performed
verify(lowLevel, times(1)).delete(any(DeleteQuery.class));
// delete result checks
assertThat(deleteResult.numberOfRowsDeleted()).isEqualTo(expectedNumberOfRowsDeleted);
assertThat(deleteResult.affectedUris()).hasSize(1);
assertThat(deleteResult.affectedUris()).contains(expectedUri);
}
use of android.support.annotation.NonNull in project storio by pushtorefresh.
the class TweetWithUserGetResolver method mapFromCursor.
// We expect that cursor will contain both Tweet and User: SQL JOIN
@NonNull
@Override
public TweetWithUser mapFromCursor(@NonNull Cursor cursor) {
final Tweet tweet = Tweet.newTweet(cursor.getLong(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_TWEET_ID)), cursor.getString(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_TWEET_AUTHOR)), cursor.getString(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_TWEET_CONTENT)));
final User user = User.newUser(cursor.getLong(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_USER_ID)), cursor.getString(cursor.getColumnIndexOrThrow(Relations.QUERY_COLUMN_USER_NICK)));
return new TweetWithUser(tweet, user);
}
use of android.support.annotation.NonNull in project storio by pushtorefresh.
the class DefaultPutResolverTest method shouldCloseCursorIfInsertThrowsException.
@Test
public void shouldCloseCursorIfInsertThrowsException() {
final StorIOContentResolver storIOContentResolver = mock(StorIOContentResolver.class);
final StorIOContentResolver.LowLevel lowLevel = mock(StorIOContentResolver.LowLevel.class);
when(storIOContentResolver.lowLevel()).thenReturn(lowLevel);
// item without id, should be inserted
final TestItem testItem = new TestItem(null);
final Query expectedQuery = Query.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(testItem.getId()).build();
final Cursor cursor = mock(Cursor.class);
when(lowLevel.query(eq(expectedQuery))).thenReturn(cursor);
when(cursor.getCount()).thenReturn(// No results -> insert should be performed
0);
final InsertQuery expectedInsertQuery = InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
when(lowLevel.insert(eq(expectedInsertQuery), any(ContentValues.class))).thenThrow(new IllegalStateException("Fake exception from ContentResolver"));
final PutResolver<TestItem> putResolver = new DefaultPutResolver<TestItem>() {
@NonNull
@Override
protected InsertQuery mapToInsertQuery(@NonNull TestItem object) {
return InsertQuery.builder().uri(TestItem.CONTENT_URI).build();
}
@NonNull
@Override
protected UpdateQuery mapToUpdateQuery(@NonNull TestItem object) {
return UpdateQuery.builder().uri(TestItem.CONTENT_URI).where(TestItem.COLUMN_ID + " = ?").whereArgs(object.getId()).build();
}
@NonNull
@Override
protected ContentValues mapToContentValues(@NonNull TestItem object) {
return TestItem.MAP_TO_CONTENT_VALUES.call(object);
}
};
try {
putResolver.performPut(storIOContentResolver, testItem);
failBecauseExceptionWasNotThrown(IllegalStateException.class);
} catch (IllegalStateException expected) {
assertThat(expected).hasMessage("Fake exception from ContentResolver");
verify(storIOContentResolver).lowLevel();
// Checks that it asks actual ContentResolver for results
verify(lowLevel).query(eq(expectedQuery));
verify(cursor).getCount();
// Cursor must be closed in case of exception!
verify(cursor).close();
verify(lowLevel).insert(eq(expectedInsertQuery), any(ContentValues.class));
verifyNoMoreInteractions(storIOContentResolver, lowLevel, cursor);
}
}
Aggregations