From fa30bc5447498d928a2ec3508ef93f2bb7c4821e Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Fri, 29 May 2020 14:38:53 -0400 Subject: [PATCH] Convert try finally into try with resources --- .../antennapod/core/storage/DBReader.java | 133 +++--------------- 1 file changed, 18 insertions(+), 115 deletions(-) diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java index c30f46315..4ec9cce63 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java @@ -72,19 +72,13 @@ public final class DBReader { @NonNull private static List getFeedList(PodDBAdapter adapter) { - Cursor cursor = null; - try { - cursor = adapter.getAllFeedsCursor(); + try (Cursor cursor = adapter.getAllFeedsCursor()) { List feeds = new ArrayList<>(cursor.getCount()); while (cursor.moveToNext()) { Feed feed = extractFeedFromCursorRow(cursor); feeds.add(feed); } return feeds; - } finally { - if (cursor != null) { - cursor.close(); - } } } @@ -96,18 +90,13 @@ public final class DBReader { public static List getFeedListDownloadUrls() { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getFeedCursorDownloadUrls(); + try (Cursor cursor = adapter.getFeedCursorDownloadUrls()) { List result = new ArrayList<>(cursor.getCount()); while (cursor.moveToNext()) { result.add(cursor.getString(1)); } return result; } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } } @@ -172,9 +161,7 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getAllItemsOfFeedCursor(feed); + try (Cursor cursor = adapter.getAllItemsOfFeedCursor(feed)) { List items = extractItemlistFromCursor(adapter, cursor); Collections.sort(items, new FeedItemPubdateComparator()); for (FeedItem item : items) { @@ -182,9 +169,6 @@ public final class DBReader { } return items; } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } } @@ -226,16 +210,10 @@ public final class DBReader { @NonNull static List getQueue(PodDBAdapter adapter) { Log.d(TAG, "getQueue()"); - Cursor cursor = null; - try { - cursor = adapter.getQueueCursor(); + try (Cursor cursor = adapter.getQueueCursor()) { List items = extractItemlistFromCursor(adapter, cursor); loadAdditionalFeedItemListData(items); return items; - } finally { - if (cursor != null) { - cursor.close(); - } } } @@ -257,18 +235,12 @@ public final class DBReader { } private static LongList getQueueIDList(PodDBAdapter adapter) { - Cursor cursor = null; - try { - cursor = adapter.getQueueIDCursor(); + try (Cursor cursor = adapter.getQueueIDCursor()) { LongList queueIds = new LongList(cursor.getCount()); while (cursor.moveToNext()) { queueIds.add(cursor.getLong(0)); } return queueIds; - } finally { - if (cursor != null) { - cursor.close(); - } } } @@ -302,17 +274,12 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getDownloadedItemsCursor(); + try (Cursor cursor = adapter.getDownloadedItemsCursor()) { List items = extractItemlistFromCursor(adapter, cursor); loadAdditionalFeedItemListData(items); Collections.sort(items, new FeedItemPubdateComparator()); return items; } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } } @@ -328,16 +295,11 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getPlayedItemsCursor(); + try (Cursor cursor = adapter.getPlayedItemsCursor()) { List items = extractItemlistFromCursor(adapter, cursor); loadAdditionalFeedItemListData(items); return items; } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } } @@ -355,16 +317,11 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getNewItemsCursor(offset, limit); + try (Cursor cursor = adapter.getNewItemsCursor(offset, limit)) { List items = extractItemlistFromCursor(adapter, cursor); loadAdditionalFeedItemListData(items); return items; } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } } @@ -381,16 +338,11 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getFavoritesCursor(offset, limit); + try (Cursor cursor = adapter.getFavoritesCursor(offset, limit)) { List items = extractItemlistFromCursor(adapter, cursor); loadAdditionalFeedItemListData(items); return items; } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } } @@ -400,18 +352,13 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getFavoritesCursor(0, Integer.MAX_VALUE); + try (Cursor cursor = adapter.getFavoritesCursor(0, Integer.MAX_VALUE)) { LongList favoriteIDs = new LongList(cursor.getCount()); while (cursor.moveToNext()) { favoriteIDs.add(cursor.getLong(0)); } return favoriteIDs; } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } } @@ -428,16 +375,11 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getRecentlyPublishedItemsCursor(offset, limit); + try (Cursor cursor = adapter.getRecentlyPublishedItemsCursor(offset, limit)) { List items = extractItemlistFromCursor(adapter, cursor); loadAdditionalFeedItemListData(items); return items; } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } } @@ -492,9 +434,7 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getDownloadLogCursor(DOWNLOAD_LOG_SIZE); + try (Cursor cursor = adapter.getDownloadLogCursor(DOWNLOAD_LOG_SIZE)) { List downloadLog = new ArrayList<>(cursor.getCount()); while (cursor.moveToNext()) { downloadLog.add(DownloadStatus.fromCursor(cursor)); @@ -502,9 +442,6 @@ public final class DBReader { Collections.sort(downloadLog, new DownloadStatusComparator()); return downloadLog; } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } } @@ -521,9 +458,7 @@ public final class DBReader { PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getDownloadLog(Feed.FEEDFILETYPE_FEED, feedId); + try (Cursor cursor = adapter.getDownloadLog(Feed.FEEDFILETYPE_FEED, feedId)) { List downloadLog = new ArrayList<>(cursor.getCount()); while (cursor.moveToNext()) { downloadLog.add(DownloadStatus.fromCursor(cursor)); @@ -531,9 +466,6 @@ public final class DBReader { Collections.sort(downloadLog, new DownloadStatusComparator()); return downloadLog; } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } } @@ -560,9 +492,7 @@ public final class DBReader { @Nullable static Feed getFeed(final long feedId, PodDBAdapter adapter) { Feed feed = null; - Cursor cursor = null; - try { - cursor = adapter.getFeedCursor(feedId); + try (Cursor cursor = adapter.getFeedCursor(feedId)) { if (cursor.moveToNext()) { feed = extractFeedFromCursorRow(cursor); feed.setItems(getFeedItemList(feed)); @@ -570,10 +500,6 @@ public final class DBReader { Log.e(TAG, "getFeed could not find feed with id " + feedId); } return feed; - } finally { - if (cursor != null) { - cursor.close(); - } } } @@ -582,9 +508,7 @@ public final class DBReader { Log.d(TAG, "Loading feeditem with id " + itemId); FeedItem item = null; - Cursor cursor = null; - try { - cursor = adapter.getFeedItemCursor(Long.toString(itemId)); + try (Cursor cursor = adapter.getFeedItemCursor(Long.toString(itemId))) { if (cursor.moveToNext()) { List list = extractItemlistFromCursor(adapter, cursor); if (!list.isEmpty()) { @@ -593,10 +517,6 @@ public final class DBReader { } } return item; - } finally { - if (cursor != null) { - cursor.close(); - } } } @@ -631,9 +551,7 @@ public final class DBReader { @Nullable private static FeedItem getFeedItemByUrl(final String podcastUrl, final String episodeUrl, PodDBAdapter adapter) { Log.d(TAG, "Loading feeditem with podcast url " + podcastUrl + " and episode url " + episodeUrl); - Cursor cursor = null; - try { - cursor = adapter.getFeedItemCursor(podcastUrl, episodeUrl); + try (Cursor cursor = adapter.getFeedItemCursor(podcastUrl, episodeUrl)) { if (!cursor.moveToNext()) { return null; } @@ -642,10 +560,6 @@ public final class DBReader { return list.get(0); } return null; - } finally { - if (cursor != null) { - cursor.close(); - } } } @@ -669,9 +583,7 @@ public final class DBReader { private static String getImageAuthentication(final String imageUrl, PodDBAdapter adapter) { String credentials = null; - Cursor cursor = null; - try { - cursor = adapter.getImageAuthenticationCursor(imageUrl); + try (Cursor cursor = adapter.getImageAuthenticationCursor(imageUrl)) { if (cursor.moveToFirst()) { String username = cursor.getString(0); String password = cursor.getString(1); @@ -683,10 +595,6 @@ public final class DBReader { } else { credentials = ""; } - } finally { - if (cursor != null) { - cursor.close(); - } } return credentials; } @@ -720,9 +628,7 @@ public final class DBReader { Log.d(TAG, "loadDescriptionOfFeedItem() called with: " + "item = [" + item + "]"); PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - Cursor cursor = null; - try { - cursor = adapter.getDescriptionOfItem(item); + try (Cursor cursor = adapter.getDescriptionOfItem(item)) { if (cursor.moveToFirst()) { int indexDescription = cursor.getColumnIndex(PodDBAdapter.KEY_DESCRIPTION); String description = cursor.getString(indexDescription); @@ -732,9 +638,6 @@ public final class DBReader { item.setContentEncoded(contentEncoded); } } finally { - if (cursor != null) { - cursor.close(); - } adapter.close(); } }