Search in sources :

Example 1 with EMPTY_LIST

use of java.util.Collections.EMPTY_LIST in project storio by pushtorefresh.

the class PreparedGetListOfObjects method executeAsBlocking.

/**
     * Executes Prepared Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null, immutable {@link List} with mapped results, list can be empty.
     */
@WorkerThread
@NonNull
@Override
public List<T> executeAsBlocking() {
    try {
        final GetResolver<T> getResolver;
        if (explicitGetResolver != null) {
            getResolver = explicitGetResolver;
        } else {
            final ContentResolverTypeMapping<T> typeMapping = storIOContentResolver.lowLevel().typeMapping(type);
            if (typeMapping == null) {
                throw new IllegalStateException("This type does not have type mapping: " + "type = " + type + "," + "ContentProvider was not touched by this operation, please add type mapping for this type");
            }
            getResolver = typeMapping.getResolver();
        }
        final Cursor cursor = getResolver.performGet(storIOContentResolver, query);
        try {
            final int count = cursor.getCount();
            if (count == 0) {
                // it's immutable
                return EMPTY_LIST;
            } else {
                final List<T> list = new ArrayList<T>(count);
                while (cursor.moveToNext()) {
                    list.add(getResolver.mapFromCursor(cursor));
                }
                return unmodifiableList(list);
            }
        } finally {
            cursor.close();
        }
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Get operation. query = " + query, exception);
    }
}
Also used : EMPTY_LIST(java.util.Collections.EMPTY_LIST) StorIOException(com.pushtorefresh.storio.StorIOException) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Example 2 with EMPTY_LIST

use of java.util.Collections.EMPTY_LIST in project storio by pushtorefresh.

the class PreparedGetListOfObjects method executeAsBlocking.

/**
     * Executes Get Operation immediately in current thread.
     * <p>
     * Notice: This is blocking I/O operation that should not be executed on the Main Thread,
     * it can cause ANR (Activity Not Responding dialog), block the UI and drop animations frames.
     * So please, call this method on some background thread. See {@link WorkerThread}.
     *
     * @return non-null, immutable {@link List} with mapped results, list can be empty.
     */
@WorkerThread
@SuppressWarnings({ "TryFinallyCanBeTryWithResources", "unchecked" })
// Min SDK :( unchecked for empty list
@NonNull
@Override
public List<T> executeAsBlocking() {
    try {
        final GetResolver<T> getResolver;
        if (explicitGetResolver != null) {
            getResolver = explicitGetResolver;
        } else {
            final SQLiteTypeMapping<T> typeMapping = storIOSQLite.lowLevel().typeMapping(type);
            if (typeMapping == null) {
                throw new IllegalStateException("This type does not have type mapping: " + "type = " + type + "," + "db was not touched by this operation, please add type mapping for this type");
            }
            getResolver = typeMapping.getResolver();
        }
        final Cursor cursor;
        if (query != null) {
            cursor = getResolver.performGet(storIOSQLite, query);
        } else if (rawQuery != null) {
            cursor = getResolver.performGet(storIOSQLite, rawQuery);
        } else {
            throw new IllegalStateException("Please specify query");
        }
        try {
            final int count = cursor.getCount();
            if (count == 0) {
                // it's immutable
                return EMPTY_LIST;
            }
            final List<T> list = new ArrayList<T>(count);
            while (cursor.moveToNext()) {
                list.add(getResolver.mapFromCursor(cursor));
            }
            return unmodifiableList(list);
        } finally {
            cursor.close();
        }
    } catch (Exception exception) {
        throw new StorIOException("Error has occurred during Get operation. query = " + (query != null ? query : rawQuery), exception);
    }
}
Also used : EMPTY_LIST(java.util.Collections.EMPTY_LIST) StorIOException(com.pushtorefresh.storio.StorIOException) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) StorIOException(com.pushtorefresh.storio.StorIOException) WorkerThread(android.support.annotation.WorkerThread) NonNull(android.support.annotation.NonNull)

Aggregations

Cursor (android.database.Cursor)2 NonNull (android.support.annotation.NonNull)2 WorkerThread (android.support.annotation.WorkerThread)2 StorIOException (com.pushtorefresh.storio.StorIOException)2 ArrayList (java.util.ArrayList)2 EMPTY_LIST (java.util.Collections.EMPTY_LIST)2