Skip to content

Commit 4f20e76

Browse files
schmidt-sebastianqdpham13
authored andcommitted
Use tryWithResources (#3421)
1 parent 4c138d7 commit 4f20e76

File tree

1 file changed

+3
-21
lines changed

1 file changed

+3
-21
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLitePersistence.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -505,18 +505,12 @@ int forEach(Consumer<Cursor> consumer) {
505505
* @return The number of rows processed (either zero or one).
506506
*/
507507
int first(Consumer<Cursor> consumer) {
508-
Cursor cursor = null;
509-
try {
510-
cursor = startQuery();
508+
try (Cursor cursor = startQuery()) {
511509
if (cursor.moveToFirst()) {
512510
consumer.accept(cursor);
513511
return 1;
514512
}
515513
return 0;
516-
} finally {
517-
if (cursor != null) {
518-
cursor.close();
519-
}
520514
}
521515
}
522516

@@ -530,30 +524,18 @@ int first(Consumer<Cursor> consumer) {
530524
*/
531525
@Nullable
532526
<T> T firstValue(Function<Cursor, T> function) {
533-
Cursor cursor = null;
534-
try {
535-
cursor = startQuery();
527+
try (Cursor cursor = startQuery()) {
536528
if (cursor.moveToFirst()) {
537529
return function.apply(cursor);
538530
}
539531
return null;
540-
} finally {
541-
if (cursor != null) {
542-
cursor.close();
543-
}
544532
}
545533
}
546534

547535
/** Runs the query and returns true if the result was nonempty. */
548536
boolean isEmpty() {
549-
Cursor cursor = null;
550-
try {
551-
cursor = startQuery();
537+
try (Cursor cursor = startQuery()) {
552538
return !cursor.moveToFirst();
553-
} finally {
554-
if (cursor != null) {
555-
cursor.close();
556-
}
557539
}
558540
}
559541

0 commit comments

Comments
 (0)