1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.inputmethodservice.cts.db; 18 19 import android.content.ContentValues; 20 import android.database.Cursor; 21 22 import androidx.annotation.NonNull; 23 24 import java.util.stream.Stream; 25 26 /** 27 * Abstraction of SQLite database table. 28 * @param <E> type of table entities. 29 */ 30 public abstract class Table<E> { 31 32 public final String mName; 33 private final Entity<E> mEntity; 34 Table(String name, Entity<E> entity)35 protected Table(String name, Entity<E> entity) { 36 mName = name; 37 mEntity = entity; 38 } 39 40 /** 41 * @return name of this table. 42 */ name()43 public String name() { 44 return mName; 45 } 46 47 /** 48 * Build {@link ContentValues} object from {@code entity}. 49 * 50 * @param entity an input data to be converted. 51 * @return a converted {@link ContentValues} object. 52 */ buildContentValues(E entity)53 public abstract ContentValues buildContentValues(E entity); 54 55 /** 56 * Build {@link Stream} object from {@link Cursor} comes from Content Provider. 57 * 58 * @param cursor a {@link Cursor} object to be converted. 59 * @return a converted {@link Stream} object. 60 */ buildStream(Cursor cursor)61 public abstract Stream<E> buildStream(Cursor cursor); 62 63 /** 64 * Returns SQL statement to create this table, such that 65 * "CREATE TABLE IF NOT EXISTS table_name \ 66 * (_id INTEGER PRIMARY KEY AUTOINCREMENT, column2_name column2_type, ...)" 67 */ 68 @NonNull createTableSql()69 public String createTableSql() { 70 return "CREATE TABLE IF NOT EXISTS " + mName + " " + mEntity.createEntitySql(); 71 } 72 getField(String fieldName)73 protected Field getField(String fieldName) { 74 return mEntity.getField(fieldName); 75 } 76 } 77