public class

Room

extends java.lang.Object

 java.lang.Object

↳androidx.room.Room

Gradle dependencies

compile group: 'androidx.room', name: 'room-runtime', version: '2.5.0-alpha01'

  • groupId: androidx.room
  • artifactId: room-runtime
  • version: 2.5.0-alpha01

Artifact androidx.room:room-runtime:2.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.room:room-runtime android.arch.persistence.room:runtime

Androidx class mapping:

androidx.room.Room android.arch.persistence.room.Room

Overview

Utility class for Room.

Summary

Fields
public static final java.lang.StringMASTER_TABLE_NAME

The master table where room keeps its metadata information.

Constructors
publicRoom()

Methods
public static RoomDatabase.Builder<RoomDatabase>databaseBuilder(Context context, java.lang.Class<RoomDatabase> klass, java.lang.String name)

Creates a RoomDatabase.Builder for a persistent database.

public static java.lang.ObjectgetGeneratedImplementation(java.lang.Class<java.lang.Object> klass, java.lang.String suffix)

public static RoomDatabase.Builder<RoomDatabase>inMemoryDatabaseBuilder(Context context, java.lang.Class<RoomDatabase> klass)

Creates a RoomDatabase.Builder for an in memory database.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final java.lang.String MASTER_TABLE_NAME

The master table where room keeps its metadata information.

Constructors

public Room()

Deprecated: This type should not be instantiated as it contains only static methods.

Methods

public static RoomDatabase.Builder<RoomDatabase> databaseBuilder(Context context, java.lang.Class<RoomDatabase> klass, java.lang.String name)

Creates a RoomDatabase.Builder for a persistent database. Once a database is built, you should keep a reference to it and re-use it.

Parameters:

context: The context for the database. This is usually the Application context.
klass: The abstract class which is annotated with Database and extends RoomDatabase.
name: The name of the database file.

Returns:

A RoomDatabaseBuilder which you can use to create the database.

public static RoomDatabase.Builder<RoomDatabase> inMemoryDatabaseBuilder(Context context, java.lang.Class<RoomDatabase> klass)

Creates a RoomDatabase.Builder for an in memory database. Information stored in an in memory database disappears when the process is killed. Once a database is built, you should keep a reference to it and re-use it.

Parameters:

context: The context for the database. This is usually the Application context.
klass: The abstract class which is annotated with Database and extends RoomDatabase.

Returns:

A RoomDatabaseBuilder which you can use to create the database.

public static java.lang.Object getGeneratedImplementation(java.lang.Class<java.lang.Object> klass, java.lang.String suffix)

Source

/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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 androidx.room;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;

/**
 * Utility class for Room.
 */
@SuppressWarnings("unused")
public class Room {
    static final String LOG_TAG = "ROOM";
    /**
     * The master table where room keeps its metadata information.
     */
    public static final String MASTER_TABLE_NAME = RoomMasterTable.TABLE_NAME;
    private static final String CURSOR_CONV_SUFFIX = "_CursorConverter";

    /**
     * Creates a RoomDatabase.Builder for a persistent database. Once a database is built, you
     * should keep a reference to it and re-use it.
     *
     * @param context The context for the database. This is usually the Application context.
     * @param klass   The abstract class which is annotated with {@link Database} and extends
     *                {@link RoomDatabase}.
     * @param name    The name of the database file.
     * @param <T>     The type of the database class.
     * @return A {@code RoomDatabaseBuilder<T>} which you can use to create the database.
     */
    @SuppressWarnings("WeakerAccess")
    @NonNull
    public static <T extends RoomDatabase> RoomDatabase.Builder<T> databaseBuilder(
            @NonNull Context context, @NonNull Class<T> klass, @NonNull String name) {
        //noinspection ConstantConditions
        if (name == null || name.trim().length() == 0) {
            throw new IllegalArgumentException("Cannot build a database with null or empty name."
                    + " If you are trying to create an in memory database, use Room"
                    + ".inMemoryDatabaseBuilder");
        }
        return new RoomDatabase.Builder<>(context, klass, name);
    }

    /**
     * Creates a RoomDatabase.Builder for an in memory database. Information stored in an in memory
     * database disappears when the process is killed.
     * Once a database is built, you should keep a reference to it and re-use it.
     *
     * @param context The context for the database. This is usually the Application context.
     * @param klass   The abstract class which is annotated with {@link Database} and extends
     *                {@link RoomDatabase}.
     * @param <T>     The type of the database class.
     * @return A {@code RoomDatabaseBuilder<T>} which you can use to create the database.
     */
    @NonNull
    public static <T extends RoomDatabase> RoomDatabase.Builder<T> inMemoryDatabaseBuilder(
            @NonNull Context context, @NonNull Class<T> klass) {
        return new RoomDatabase.Builder<>(context, klass, null);
    }

    @SuppressWarnings({"TypeParameterUnusedInFormals", "ClassNewInstance"})
    @NonNull
    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    public static <T, C> T getGeneratedImplementation(@NonNull Class<C> klass,
            @NonNull String suffix) {
        final String fullPackage = klass.getPackage().getName();
        String name = klass.getCanonicalName();
        final String postPackageName = fullPackage.isEmpty()
                ? name
                : name.substring(fullPackage.length() + 1);
        final String implName = postPackageName.replace('.', '_') + suffix;
        //noinspection TryWithIdenticalCatches
        try {

            final String fullClassName = fullPackage.isEmpty()
                    ? implName
                    : fullPackage + "." + implName;
            @SuppressWarnings("unchecked")
            final Class<T> aClass = (Class<T>) Class.forName(
                    fullClassName, true, klass.getClassLoader());
            return aClass.newInstance();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("cannot find implementation for "
                    + klass.getCanonicalName() + ". " + implName + " does not exist");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Cannot access the constructor"
                    + klass.getCanonicalName());
        } catch (InstantiationException e) {
            throw new RuntimeException("Failed to create an instance of "
                    + klass.getCanonicalName());
        }
    }

    /** @deprecated This type should not be instantiated as it contains only static methods. */
    @Deprecated
    @SuppressWarnings("PrivateConstructorForUtilityClass")
    public Room() {
    }
}