<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.yobidashi.libs.db

import android.content.Context
import androidx.room.Room

/**
 * @author toastkidjp
 */
class DatabaseFinder {

    fun invoke(context: Context): AppDatabase {
        return Room.databaseBuilder(
                context.applicationContext,
                AppDatabase::class.java,
                DATABASE_FILE_NAME
        )
                .fallbackToDestructiveMigration()
                .build()
    }

    companion object {
        private const val DATABASE_FILE_NAME = "yobidashi2.db"
    }
}<commit_msg>feature #412 Hold singleton database instance.<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.yobidashi.libs.db

import android.content.Context
import androidx.annotation.VisibleForTesting
import androidx.room.Room

/**
 * @author toastkidjp
 */
class DatabaseFinder {

    fun invoke(context: Context): AppDatabase {
        synchronized(DatabaseFinder) {
            if (instance != null) {
                return@synchronized
            }

            instance = makeAppDatabase(context)
        }

        return instance?: makeAppDatabase(context)
    }

    private fun makeAppDatabase(context: Context): AppDatabase {
        return Room.databaseBuilder(
            context.applicationContext,
            AppDatabase::class.java,
            DATABASE_FILE_NAME
        )
            .fallbackToDestructiveMigration()
            .build()
    }

    companion object {
        private const val DATABASE_FILE_NAME = "yobidashi2.db"

        private var instance: AppDatabase? = null

        /**
         * For reset instance each test.
         */
        @VisibleForTesting
        fun clearInstance() {
            instance = null
        }

    }
}
{'fraction_non_alphanumeric': 0.08058294042006, 'fraction_numerical': 0.009001285897985427, 'mean_word_length': 2.3875181422351233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7249508', 'n_tokens_mistral': 650, 'n_tokens_neox': 608, 'n_words': 169}