Skip to content

Use tryWithResources #3421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -505,18 +505,12 @@ int forEach(Consumer<Cursor> consumer) {
* @return The number of rows processed (either zero or one).
*/
int first(Consumer<Cursor> consumer) {
Cursor cursor = null;
try {
cursor = startQuery();
try (Cursor cursor = startQuery()) {
if (cursor.moveToFirst()) {
consumer.accept(cursor);
return 1;
}
return 0;
} finally {
if (cursor != null) {
cursor.close();
}
}
}

Expand All @@ -530,30 +524,18 @@ int first(Consumer<Cursor> consumer) {
*/
@Nullable
<T> T firstValue(Function<Cursor, T> function) {
Cursor cursor = null;
try {
cursor = startQuery();
try (Cursor cursor = startQuery()) {
if (cursor.moveToFirst()) {
return function.apply(cursor);
}
return null;
} finally {
if (cursor != null) {
cursor.close();
}
}
}

/** Runs the query and returns true if the result was nonempty. */
boolean isEmpty() {
Cursor cursor = null;
try {
cursor = startQuery();
try (Cursor cursor = startQuery()) {
return !cursor.moveToFirst();
} finally {
if (cursor != null) {
cursor.close();
}
}
}

Expand Down