<commit_before>package com.tonyodev.fetch2.fetch

import android.os.Handler
import com.tonyodev.fetch2.*
import com.tonyodev.fetch2.database.DatabaseManager
import com.tonyodev.fetch2.database.DownloadInfo
import com.tonyodev.fetch2.downloader.DownloadManager
import com.tonyodev.fetch2.exception.FetchException
import com.tonyodev.fetch2.helper.PriorityListProcessor
import com.tonyodev.fetch2.util.*
import com.tonyodev.fetch2core.*
import java.io.File
import java.util.*

/**
 * This handlerWrapper class handles all tasks and operations of Fetch.
 * */
class FetchHandlerImpl(private val namespace: String,
                       private val databaseManager: DatabaseManager,
                       private val downloadManager: DownloadManager,
                       private val priorityListProcessor: PriorityListProcessor<Download>,
                       private val logger: Logger,
                       private val autoStart: Boolean,
                       private val httpDownloader: Downloader,
                       private val fileServerDownloader: FileServerDownloader,
                       private val fileTempDir: String,
                       private val listenerCoordinator: ListenerCoordinator,
                       private val uiHandler: Handler) : FetchHandler {

    private val listenerId = UUID.randomUUID().hashCode()
    private val listenerSet = mutableSetOf<FetchListener>()

    @Volatile
    private var isTerminating = false

    override fun init() {
        databaseManager.sanitizeOnFirstEntry()
        if (autoStart) {
            priorityListProcessor.start()
        }
    }

    override fun enqueue(request: Request): Download {
        return enqueueRequests(listOf(request)).first()
    }

    override fun enqueue(requests: List<Request>): List<Download> {
        return enqueueRequests(requests)
    }

    private fun enqueueRequests(requests: List<Request>): List<Download> {
        val downloadInfoList = requests.map {
            val downloadInfo = it.toDownloadInfo()
            downloadInfo.namespace = namespace
            downloadInfo.status = if (it.downloadOnEnqueue) {
                Status.QUEUED
            } else {
                Status.ADDED
            }
            prepareDownloadInfoForEnqueue(downloadInfo)
            downloadInfo
        }
        val results = databaseManager.insert(downloadInfoList)
                .filter { it.second }
                .map {
                    logger.d("Enqueued download ${it.first}")
                    it.first
                }
        startPriorityQueueIfNotStarted()
        return results<commit_msg>enqueue action bug fix<commit_after>package com.tonyodev.fetch2.fetch

import android.os.Handler
import com.tonyodev.fetch2.*
import com.tonyodev.fetch2.database.DatabaseManager
import com.tonyodev.fetch2.database.DownloadInfo
import com.tonyodev.fetch2.downloader.DownloadManager
import com.tonyodev.fetch2.exception.FetchException
import com.tonyodev.fetch2.helper.PriorityListProcessor
import com.tonyodev.fetch2.util.*
import com.tonyodev.fetch2core.*
import java.io.File
import java.util.*

/**
 * This handlerWrapper class handles all tasks and operations of Fetch.
 * */
class FetchHandlerImpl(private val namespace: String,
                       private val databaseManager: DatabaseManager,
                       private val downloadManager: DownloadManager,
                       private val priorityListProcessor: PriorityListProcessor<Download>,
                       private val logger: Logger,
                       private val autoStart: Boolean,
                       private val httpDownloader: Downloader,
                       private val fileServerDownloader: FileServerDownloader,
                       private val fileTempDir: String,
                       private val listenerCoordinator: ListenerCoordinator,
                       private val uiHandler: Handler) : FetchHandler {

    private val listenerId = UUID.randomUUID().hashCode()
    private val listenerSet = mutableSetOf<FetchListener>()

    @Volatile
    private var isTerminating = false

    override fun init() {
        databaseManager.sanitizeOnFirstEntry()
        if (autoStart) {
            priorityListProcessor.start()
        }
    }

    override fun enqueue(request: Request): Download {
        return enqueueRequests(listOf(request)).first()
    }

    override fun enqueue(requests: List<Request>): List<Download> {
        return enqueueRequests(requests)
    }

    private fun enqueueRequests(requests: List<Request>): List<Download> {
        val downloadInfoList = requests.map {
            val downloadInfo = it.toDownloadInfo()
            downloadInfo.namespace = namespace
            prepareDownloadInfoForEnqueue(downloadInfo)
            downloadInfo.status = if (it.downloadOnEnqueue) {
                Status.QUEUED
            } else {
                Status.ADDED
            }
            downloadInfo
        }
        val results = databaseManager.insert(downloadInfoList)
                .filter { it.second }
                .map {
                    logger.d("Enqueued download ${it.first}")
                    it.first
                }
        startPriorityQueueIfNotStarted()
        return results
{'fraction_non_alphanumeric': 0.07171314741035857, 'fraction_numerical': 0.003414911781445646, 'mean_word_length': 2.2029161603888214, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2404447', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1272, 'n_words': 298}