All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codeslap.persistence.PersistenceConfig Maven / Gradle / Ivy

/*
 * Copyright 2012 CodeSlap
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.codeslap.persistence;

import android.content.Context;

import java.util.HashMap;
import java.util.Map;

public class PersistenceConfig {

    private static final Map SQL = new HashMap();
    private static final Map PREFS = new HashMap();
    static String sFirstDatabase;

    public static SqlPersistence getDatabase(String name, int version) {
        return getDatabase(name, version, null);
    }

    public static SqlPersistence getDatabase(DbOpenHelper openHelper) {
        return getDatabase(openHelper.getName(), openHelper.getVersion(), openHelper);
    }

    /**
     * @param context used to find the databases' path
     * @param name the name of the database to check
     * @return true if the database already exist
     */
    public static boolean isDatabaseAlreadyCreated(Context context, String name) {
        return context.getDatabasePath(name).exists();
    }

    private static SqlPersistence getDatabase(String name, int version, DbOpenHelper openHelper) {
        if (name == null) {
            throw new IllegalArgumentException("You must provide a valid database name");
        }
        if (sFirstDatabase == null) {
            sFirstDatabase = name;
        }
        if (SQL.containsKey(name)) {
            return SQL.get(name);
        }
        SqlPersistence sqlPersistence = new SqlPersistence(name, version, openHelper);
        SQL.put(name, sqlPersistence);
        return sqlPersistence;
    }

    static SqlPersistence getDatabase(String name) {
        if (SQL.containsKey(name)) {
            return SQL.get(name);
        }
        throw new IllegalStateException(String.format("There is no sql persistence for \"%s\"", name));
    }

    public static PrefsPersistence getPreference(String name) {
        if (PREFS.containsKey(name)) {
            return PREFS.get(name);
        }
        PrefsPersistence persistence = new PrefsPersistence();
        PREFS.put(name, persistence);
        return persistence;
    }

    public static PrefsPersistence getPreference() {
        return getPreference(PreferencesAdapter.DEFAULT_PREFS);
    }

    public static void clear() {
        SQL.clear();
        PREFS.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy