<commit_before>/*
 * Copyright (c) 2019 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package jp.toastkid.image.list

import android.content.ContentResolver
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import jp.toastkid.image.Image

/**
 * @author toastkidjp
 */
class ImageLoader(private val contentResolver: ContentResolver) {

    operator fun invoke(sort: Sort, bucket: String): List<Image> {
        val externalContentUri =
            ResolvingUriFinder().invoke()
        return extractImages(
                contentResolver.query(
                        externalContentUri,
                        columns,
                        "bucket_display_name = ?",
                        arrayOf(bucket),
                        sort.imageSort
                )
        )
    }

    fun filterBy(name: String?): List<Image> {
        val externalContentUri =
            ResolvingUriFinder().invoke()
        return extractImages(
                contentResolver.query(
                        externalContentUri,
                        columns,
                        "${MediaStore.Images.Media.DISPLAY_NAME} LIKE ?",
                        arrayOf("%$name%"),
                        Sort.NAME.imageSort
                )
        )
    }

    private fun findContentResolveUri(): Uri {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
            MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
        else
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    }

    private fun extractImages(cursor: Cursor?): MutableList<Image> {
        val images = mutableListOf<Image>()

        val dataIndex = cursor?.getColumnIndex(MediaStore.Images.Media.DATA) ?: 0
        val displayNameIndex = cursor?.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME) ?: 0

        while (cursor?.moveToNext() == true) {
            images.add(Image(cursor.getString(dataIndex), cursor.getString(displayNameIndex)))
        }
        return images
    }

    companion object {
        private val columns = arrayOf(
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Images.Media.DATE_TAKEN
        )
    }
}<commit_msg>refactor #506 Remove useless line break.<commit_after>/*
 * Copyright (c) 2019 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package jp.toastkid.image.list

import android.content.ContentResolver
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import jp.toastkid.image.Image

/**
 * @author toastkidjp
 */
class ImageLoader(private val contentResolver: ContentResolver) {

    operator fun invoke(sort: Sort, bucket: String): List<Image> {
        val externalContentUri =
            ResolvingUriFinder().invoke()
        return extractImages(
                contentResolver.query(
                        externalContentUri,
                        columns,
                        "bucket_display_name = ?",
                        arrayOf(bucket),
                        sort.imageSort
                )
        )
    }

    fun filterBy(name: String?): List<Image> {
        val externalContentUri = ResolvingUriFinder().invoke()
        return extractImages(
                contentResolver.query(
                        externalContentUri,
                        columns,
                        "${MediaStore.Images.Media.DISPLAY_NAME} LIKE ?",
                        arrayOf("%$name%"),
                        Sort.NAME.imageSort
                )
        )
    }

    private fun findContentResolveUri(): Uri {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
            MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
        else
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    }

    private fun extractImages(cursor: Cursor?): MutableList<Image> {
        val images = mutableListOf<Image>()

        val dataIndex = cursor?.getColumnIndex(MediaStore.Images.Media.DATA) ?: 0
        val displayNameIndex = cursor?.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME) ?: 0

        while (cursor?.moveToNext() == true) {
            images.add(Image(cursor.getString(dataIndex), cursor.getString(displayNameIndex)))
        }
        return images
    }

    companion object {
        private val columns = arrayOf(
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Images.Media.DATE_TAKEN
        )
    }
}
{'fraction_non_alphanumeric': 0.08772265849454128, 'fraction_numerical': 0.004405286343612335, 'mean_word_length': 2.0167533217793183, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5828615', 'n_tokens_mistral': 1392, 'n_tokens_neox': 1320, 'n_words': 279}