com.venmo.cursor.CursorUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of library Show documentation
Show all versions of library Show documentation
A library that encapsulates the repeatable actions of Android Cursors.
package com.venmo.cursor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;
/**
* Utility class of default implementations for {@link IterableCursor} methods. If Android gets
* support for Java 8, these can become default methods in the interface.
*/
public final class CursorUtils {
private CursorUtils() {
throw new UnsupportedOperationException("Non-instantiable class");
}
/**
* Add each item of this {@link android.database.Cursor} to the {@code collection} parameter.
* Closes the cursor once completed.
*
* @return the same collection as the parameter.
* @see #consumeToArrayList(com.venmo.cursor.IterableCursor)
* @see #consumeToLinkedList(com.venmo.cursor.IterableCursor)
* @see #consumeToLinkedHashSet(com.venmo.cursor.IterableCursor)
*/
public static > C consumeToCollection(IterableCursor cursor,
C collection) {
for (T t : cursor) {
collection.add(t);
}
cursor.close();
return collection;
}
/**
* Returns an {@link java.util.ArrayList} of the {@link android.database.Cursor} and closes it.
*/
public static ArrayList consumeToArrayList(IterableCursor cursor) {
return consumeToCollection(cursor, new ArrayList(cursor.getCount()));
}
/**
* Returns an {@link java.util.LinkedList} of the {@link android.database.Cursor} and closes
* it.
*/
public static LinkedList consumeToLinkedList(IterableCursor cursor) {
return consumeToCollection(cursor, new LinkedList());
}
/**
* Returns an {@link java.util.LinkedHashSet} of the {@link android.database.Cursor} and closes
* it.
*/
public static LinkedHashSet consumeToLinkedHashSet(IterableCursor cursor) {
return consumeToCollection(cursor, new LinkedHashSet(cursor.getCount()));
}
public static T nextDocumentHelper(IterableCursor cursor) {
T t = cursor.peek();
cursor.moveToNext();
return t;
}
public static T previousDocumentHelper(IterableCursor cursor) {
cursor.moveToPrevious();
return cursor.peek();
}
}