Skip to content

Commit

Permalink
Throw exception if ContentResover#query returns null. Add custom exce…
Browse files Browse the repository at this point in the history
…ption.
  • Loading branch information
nikitin-da committed Jul 18, 2015
1 parent 79e995b commit 0801a0d
Show file tree
Hide file tree
Showing 33 changed files with 786 additions and 500 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.pushtorefresh.storio;

/**
* Common {@link RuntimeException} for all errors and exceptions occurred during StorIO operations.
*/
public class StorIOException extends RuntimeException {

/**
* {@inheritDoc}
*/
public StorIOException() {
super();
}

/**
* {@inheritDoc}
*/
public StorIOException(String detailMessage) {
super(detailMessage);
}

/**
* {@inheritDoc}
*/
public StorIOException(String detailMessage, Throwable throwable) {
super(detailMessage, throwable);
}

/**
* {@inheritDoc}
*/
public StorIOException(Throwable throwable) {
super(throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
Expand Down Expand Up @@ -263,9 +262,11 @@ public Cursor query(@NonNull Query query) {
nullableString(query.sortOrder())
);

return cursor == null
? new MatrixCursor(null, 0)
: cursor;
if (cursor == null) {
throw new IllegalStateException("Cursor returned by content provider is null");
}

return cursor;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;

import com.pushtorefresh.storio.StorIOException;
import com.pushtorefresh.storio.contentresolver.StorIOContentResolver;
import com.pushtorefresh.storio.contentresolver.queries.DeleteQuery;
import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking;
Expand Down Expand Up @@ -47,7 +48,11 @@ public final class PreparedDeleteByQuery extends PreparedDelete<DeleteResult> {
@NonNull
@Override
public DeleteResult executeAsBlocking() {
return deleteResolver.performDelete(storIOContentResolver, deleteQuery);
try {
return deleteResolver.performDelete(storIOContentResolver, deleteQuery);
} catch (Throwable throwable) {
throw new StorIOException(throwable);
}
}

/**
Expand All @@ -67,11 +72,15 @@ public DeleteResult executeAsBlocking() {
@NonNull
@Override
public Observable<DeleteResult> createObservable() {
throwExceptionIfRxJavaIsNotAvailable("createObservable()");

return Observable
.create(OnSubscribeExecuteAsBlocking.newInstance(this))
.subscribeOn(Schedulers.io());
try {
throwExceptionIfRxJavaIsNotAvailable("createObservable()");

return Observable
.create(OnSubscribeExecuteAsBlocking.newInstance(this))
.subscribeOn(Schedulers.io());
} catch (Throwable throwable) {
throw new StorIOException(throwable);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;

import com.pushtorefresh.storio.StorIOException;
import com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping;
import com.pushtorefresh.storio.contentresolver.StorIOContentResolver;
import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking;
Expand Down Expand Up @@ -55,51 +56,56 @@ public final class PreparedDeleteCollectionOfObjects<T> extends PreparedDelete<D
@NonNull
@Override
public DeleteResults<T> executeAsBlocking() {
final StorIOContentResolver.Internal internal = storIOContentResolver.internal();

// Nullable
final List<SimpleImmutableEntry> objectsAndDeleteResolvers;

if (explicitDeleteResolver != null) {
objectsAndDeleteResolvers = null;
} else {
objectsAndDeleteResolvers = new ArrayList<SimpleImmutableEntry>(objects.size());
try {
final StorIOContentResolver.Internal internal = storIOContentResolver.internal();

// Nullable
final List<SimpleImmutableEntry> objectsAndDeleteResolvers;

if (explicitDeleteResolver != null) {
objectsAndDeleteResolvers = null;
} else {
objectsAndDeleteResolvers = new ArrayList<SimpleImmutableEntry>(objects.size());

for (final T object : objects) {
final ContentResolverTypeMapping<T> typeMapping
= (ContentResolverTypeMapping<T>) internal.typeMapping(object.getClass());

if (typeMapping == null) {
throw new IllegalStateException("One of the objects from the collection does not have type mapping: " +
"object = " + object + ", object.class = " + object.getClass() + "," +
"ContentProvider was not affected by this operation, please add type mapping for this type");
}

objectsAndDeleteResolvers.add(new SimpleImmutableEntry(
object,
typeMapping.deleteResolver()
));
}
}

for (final T object : objects) {
final ContentResolverTypeMapping<T> typeMapping
= (ContentResolverTypeMapping<T>) internal.typeMapping(object.getClass());
final Map<T, DeleteResult> results = new HashMap<T, DeleteResult>(objects.size());

if (typeMapping == null) {
throw new IllegalStateException("One of the objects from the collection does not have type mapping: " +
"object = " + object + ", object.class = " + object.getClass() + "," +
"ContentProvider was not affected by this operation, please add type mapping for this type");
if (explicitDeleteResolver != null) {
for (final T object : objects) {
final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOContentResolver, object);
results.put(object, deleteResult);
}
} else {
for (final SimpleImmutableEntry<T, DeleteResolver<T>> objectAndDeleteResolver : objectsAndDeleteResolvers) {
final T object = objectAndDeleteResolver.getKey();
final DeleteResolver<T> deleteResolver = objectAndDeleteResolver.getValue();

objectsAndDeleteResolvers.add(new SimpleImmutableEntry(
object,
typeMapping.deleteResolver()
));
final DeleteResult deleteResult = deleteResolver.performDelete(storIOContentResolver, object);
results.put(object, deleteResult);
}
}
}

final Map<T, DeleteResult> results = new HashMap<T, DeleteResult>(objects.size());
return DeleteResults.newInstance(results);

if (explicitDeleteResolver != null) {
for (final T object : objects) {
final DeleteResult deleteResult = explicitDeleteResolver.performDelete(storIOContentResolver, object);
results.put(object, deleteResult);
}
} else {
for (final SimpleImmutableEntry<T, DeleteResolver<T>> objectAndDeleteResolver : objectsAndDeleteResolvers) {
final T object = objectAndDeleteResolver.getKey();
final DeleteResolver<T> deleteResolver = objectAndDeleteResolver.getValue();

final DeleteResult deleteResult = deleteResolver.performDelete(storIOContentResolver, object);
results.put(object, deleteResult);
}
} catch (Throwable throwable) {
throw new StorIOException(throwable);
}

return DeleteResults.newInstance(results);
}

/**
Expand All @@ -119,11 +125,15 @@ public DeleteResults<T> executeAsBlocking() {
@NonNull
@Override
public Observable<DeleteResults<T>> createObservable() {
throwExceptionIfRxJavaIsNotAvailable("createObservable()");

return Observable
.create(OnSubscribeExecuteAsBlocking.newInstance(this))
.subscribeOn(Schedulers.io());
try {
throwExceptionIfRxJavaIsNotAvailable("createObservable()");

return Observable
.create(OnSubscribeExecuteAsBlocking.newInstance(this))
.subscribeOn(Schedulers.io());
} catch (Throwable throwable) {
throw new StorIOException(throwable);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;

import com.pushtorefresh.storio.StorIOException;
import com.pushtorefresh.storio.contentresolver.ContentResolverTypeMapping;
import com.pushtorefresh.storio.contentresolver.StorIOContentResolver;
import com.pushtorefresh.storio.operations.internal.OnSubscribeExecuteAsBlocking;
Expand Down Expand Up @@ -48,24 +49,29 @@ public final class PreparedDeleteObject<T> extends PreparedDelete<DeleteResult>
@NonNull
@Override
public DeleteResult executeAsBlocking() {
final DeleteResolver<T> deleteResolver;

if (explicitDeleteResolver != null) {
deleteResolver = explicitDeleteResolver;
} else {
final ContentResolverTypeMapping<T> typeMapping
= storIOContentResolver.internal().typeMapping((Class<T>) object.getClass());

if (typeMapping == null) {
throw new IllegalStateException("Object does not have type mapping: " +
"object = " + object + ", object.class = " + object.getClass() + "," +
"ContentProvider was not affected by this operation, please add type mapping for this type");
try {
final DeleteResolver<T> deleteResolver;

if (explicitDeleteResolver != null) {
deleteResolver = explicitDeleteResolver;
} else {
final ContentResolverTypeMapping<T> typeMapping
= storIOContentResolver.internal().typeMapping((Class<T>) object.getClass());

if (typeMapping == null) {
throw new IllegalStateException("Object does not have type mapping: " +
"object = " + object + ", object.class = " + object.getClass() + "," +
"ContentProvider was not affected by this operation, please add type mapping for this type");
}

deleteResolver = typeMapping.deleteResolver();
}

deleteResolver = typeMapping.deleteResolver();
}
return deleteResolver.performDelete(storIOContentResolver, object);

return deleteResolver.performDelete(storIOContentResolver, object);
} catch (Throwable throwable) {
throw new StorIOException(throwable);
}
}

/**
Expand All @@ -85,11 +91,15 @@ public DeleteResult executeAsBlocking() {
@NonNull
@Override
public Observable<DeleteResult> createObservable() {
throwExceptionIfRxJavaIsNotAvailable("createObservable()");

return Observable
.create(OnSubscribeExecuteAsBlocking.newInstance(this))
.subscribeOn(Schedulers.io());
try {
throwExceptionIfRxJavaIsNotAvailable("createObservable()");

return Observable
.create(OnSubscribeExecuteAsBlocking.newInstance(this))
.subscribeOn(Schedulers.io());
} catch (Throwable throwable) {
throw new StorIOException(throwable);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;

import com.pushtorefresh.storio.StorIOException;
import com.pushtorefresh.storio.contentresolver.StorIOContentResolver;
import com.pushtorefresh.storio.contentresolver.queries.Query;
import com.pushtorefresh.storio.operations.internal.MapSomethingToExecuteAsBlocking;
Expand Down Expand Up @@ -50,7 +51,11 @@ public final class PreparedGetCursor extends PreparedGet<Cursor> {
@NonNull
@Override
public Cursor executeAsBlocking() {
return getResolver.performGet(storIOContentResolver, query);
try {
return getResolver.performGet(storIOContentResolver, query);
} catch (Throwable throwable) {
throw new StorIOException(throwable);
}
}

/**
Expand All @@ -73,13 +78,17 @@ public Cursor executeAsBlocking() {
@NonNull
@Override
public Observable<Cursor> createObservable() {
throwExceptionIfRxJavaIsNotAvailable("createObservable()");

return storIOContentResolver
.observeChangesOfUri(query.uri()) // each change triggers executeAsBlocking
.map(MapSomethingToExecuteAsBlocking.newInstance(this))
.startWith(Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))) // start stream with first query result
.subscribeOn(Schedulers.io());
try {
throwExceptionIfRxJavaIsNotAvailable("createObservable()");

return storIOContentResolver
.observeChangesOfUri(query.uri()) // each change triggers executeAsBlocking
.map(MapSomethingToExecuteAsBlocking.newInstance(this))
.startWith(Observable.create(OnSubscribeExecuteAsBlocking.newInstance(this))) // start stream with first query result
.subscribeOn(Schedulers.io());
} catch (Throwable throwable) {
throw new StorIOException(throwable);
}
}

/**
Expand Down
Loading

0 comments on commit 0801a0d

Please sign in to comment.