Source:
List<Migration> extractMigrations() { Cursor batchesCursor = database.rawQuery(BATCHES_QUERY); List<Migration> migrations = new ArrayList<>(); while (batchesCursor.moveToNext()) { String batchId = batchesCursor.getString(BATCH_ID_COLUMN); String batchTitle = batchesCursor.getString(TITLE_COLUMN); long downloadedDateTimeInMillis = batchesCursor.getLong(MODIFIED_TIMESTAMP_COLUMN); Cursor downloadsCursor = database.rawQuery(DOWNLOADS_QUERY, batchId); Batch.Builder newBatchBuilder = null; List<Migration.FileMetadata> fileMetadataList = new ArrayList<>(); Set<String> uris = new HashSet<>(); Set<String> fileIds = new HashSet<>(); DownloadBatchId downloadBatchId = null; while (downloadsCursor.moveToNext()) { String originalFileId = downloadsCursor.getString(FILE_ID_COLUMN); String uri = downloadsCursor.getString(URI_COLUMN); String originalFileLocation = downloadsCursor.getString(FILE_LOCATION_COLUMN); String sanitizedOriginalFileLocation = MigrationStoragePathSanitizer.sanitize(originalFileLocation); FilePath originalFilePath = new LiteFilePath(sanitizedOriginalFileLocation); if (downloadsCursor.isFirst()) { downloadBatchId = createDownloadBatchIdFrom(originalFileId, batchId); newBatchBuilder = Batch.with(downloadBatchId, batchTitle); } if (uris.contains(uri) && fileIds.contains(originalFileId)) { continue; } else { uris.add(uri); fileIds.add(originalFileId); } if (originalFileId != null) { newBatchBuilder.addFile(uri) .withDownloadFileId(DownloadFileIdCreator.createFrom(originalFileId)) .apply(); } else { newBatchBuilder.addFile(uri) .apply(); } FilePath newFilePath = MigrationPathExtractor.extractMigrationPath(basePath, originalFilePath.path(), downloadBatchId); Migration.FileMetadata fileMetadata = new Migration.FileMetadata( originalFileId, originalFilePath, newFilePath, FileSizeCreator.unknownFileSize(), uri ); fileMetadataList.add(fileMetadata); } downloadsCursor.close(); Batch batch = newBatchBuilder.build(); migrations.add(new Migration(batch, fileMetadataList, downloadedDateTimeInMillis, Migration.Type.PARTIAL)); } batchesCursor.close(); return migrations; }

Target:
List<Migration> extractMigrations() { Cursor batchesCursor = database.rawQuery(BATCHES_QUERY); List<Migration> migrations = new ArrayList<>(); while (batchesCursor.moveToNext()) { String batchId = batchesCursor.getString(BATCH_ID_COLUMN); String batchTitle = batchesCursor.getString(TITLE_COLUMN); long downloadedDateTimeInMillis = batchesCursor.getLong(MODIFIED_TIMESTAMP_COLUMN); Cursor downloadsCursor = database.rawQuery(DOWNLOADS_QUERY, batchId); Batch.Builder newBatchBuilder = null; List<Migration.FileMetadata> fileMetadataList = new ArrayList<>(); Set<String> uris = new HashSet<>(); Set<String> fileIds = new HashSet<>(); DownloadBatchId downloadBatchId = null; while (downloadsCursor.moveToNext()) { String originalFileId = downloadsCursor.getString(FILE_ID_COLUMN); String uri = downloadsCursor.getString(URI_COLUMN); String originalFileLocation = downloadsCursor.getString(FILE_LOCATION_COLUMN); String sanitizedOriginalFileLocation = MigrationStoragePathSanitizer.sanitize(originalFileLocation); FilePath originalFilePath = new LiteFilePath(sanitizedOriginalFileLocation); if (downloadsCursor.isFirst()) { downloadBatchId = createDownloadBatchIdFrom(originalFileId, batchId); newBatchBuilder = Batch.with(downloadBatchId, batchTitle); } if (uris.contains(uri) && fileIds.contains(originalFileId)) { continue; } else { uris.add(uri); fileIds.add(originalFileId); } if (originalFileId == null) { newBatchBuilder.addFile(uri) .apply(); } else { newBatchBuilder.addFile(uri) .withDownloadFileId(DownloadFileIdCreator.createFrom(originalFileId)) .apply(); } FilePath newFilePath = MigrationPathExtractor.extractMigrationPath(basePath, originalFilePath.path(), downloadBatchId); Migration.FileMetadata fileMetadata = new Migration.FileMetadata( originalFileId, originalFilePath, newFilePath, FileSizeCreator.unknownFileSize(), uri ); fileMetadataList.add(fileMetadata); } downloadsCursor.close(); Batch batch = newBatchBuilder.build(); migrations.add(new Migration(batch, fileMetadataList, downloadedDateTimeInMillis, Migration.Type.PARTIAL)); } batchesCursor.close(); return migrations; }