1 /* 2 * Copyright (C) 2023 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.healthconnect.cts.database; 18 19 import androidx.annotation.NonNull; 20 import androidx.annotation.Nullable; 21 22 import java.util.ArrayList; 23 import java.util.HashMap; 24 import java.util.List; 25 import java.util.Objects; 26 27 /** TableInfo contains information about all the attributes that a table cna hold. */ 28 class TableInfo { 29 private final String mTableName; 30 private final List<String> mPrimaryKey; 31 32 private final HashMap<String, ColumnInfo> mColumnNameColumnInfoMap; 33 34 private final HashMap<String, ForeignKeyInfo> mForeignKeyNameForeignKeyInfoMap; 35 36 private final HashMap<String, IndexInfo> mIndexNameIndexInfoMap; 37 38 /** Creates an instance for TableInfo. */ TableInfo(Builder builder)39 TableInfo(Builder builder) { 40 mTableName = builder.mTableName; 41 mColumnNameColumnInfoMap = builder.mColumnNameColumnInfoMap; 42 mForeignKeyNameForeignKeyInfoMap = builder.mForeignKeyNameForeignKeyInfoMap; 43 mIndexNameIndexInfoMap = builder.mIndexNameIndexInfoMap; 44 mPrimaryKey = builder.mPrimaryKey; 45 } 46 47 /** Builder pattern for TableInfo. */ 48 public static class Builder { 49 private final String mTableName; 50 51 private final List<String> mPrimaryKey; 52 53 private final HashMap<String, ColumnInfo> mColumnNameColumnInfoMap; 54 55 private final HashMap<String, ForeignKeyInfo> mForeignKeyNameForeignKeyInfoMap; 56 57 private final HashMap<String, IndexInfo> mIndexNameIndexInfoMap; 58 Builder(String tableName)59 Builder(String tableName) { 60 mTableName = tableName; 61 mColumnNameColumnInfoMap = new HashMap<>(); 62 mForeignKeyNameForeignKeyInfoMap = new HashMap<>(); 63 mIndexNameIndexInfoMap = new HashMap<>(); 64 mPrimaryKey = new ArrayList<>(); 65 } 66 67 /** Adds primary key column to the existing list of column. */ addPrimaryKeyColumn(@onNull String primaryKey)68 public Builder addPrimaryKeyColumn(@NonNull String primaryKey) { 69 Objects.requireNonNull(primaryKey); 70 mPrimaryKey.add(primaryKey); 71 return this; 72 } 73 74 /** Adds mapping for ColumnName and corresponding ColumnInfo. */ addColumnInfoMapping( @onNull String columnName, @NonNull ColumnInfo columnInfo)75 public Builder addColumnInfoMapping( 76 @NonNull String columnName, @NonNull ColumnInfo columnInfo) { 77 Objects.requireNonNull(columnName); 78 Objects.requireNonNull(columnInfo); 79 mColumnNameColumnInfoMap.put(columnName, columnInfo); 80 return this; 81 } 82 83 /** Adds mapping for ForeignKeyName and corresponding ForeignKeyInfo. */ addForeignKeyInfoMapping( @onNull String foreignKeyName, @NonNull ForeignKeyInfo foreignKeyInfo)84 public Builder addForeignKeyInfoMapping( 85 @NonNull String foreignKeyName, @NonNull ForeignKeyInfo foreignKeyInfo) { 86 Objects.requireNonNull(foreignKeyName); 87 Objects.requireNonNull(foreignKeyInfo); 88 mForeignKeyNameForeignKeyInfoMap.put(foreignKeyName, foreignKeyInfo); 89 return this; 90 } 91 92 /** Adds mapping for IndexName and corresponding IndexInfo. */ addIndexInfoMapping( @onNull String indexName, @NonNull IndexInfo indexInfo)93 public Builder addIndexInfoMapping( 94 @NonNull String indexName, @NonNull IndexInfo indexInfo) { 95 Objects.requireNonNull(indexName); 96 Objects.requireNonNull(indexInfo); 97 mIndexNameIndexInfoMap.put(indexName, indexInfo); 98 return this; 99 } 100 101 /** Builds the TableInfo object. */ build()102 public TableInfo build() { 103 return new TableInfo(this); 104 } 105 } 106 107 /** 108 * @return name of the table. 109 */ 110 @NonNull getTableName()111 public String getTableName() { 112 return mTableName; 113 } 114 115 /** 116 * @return primary key of a table. 117 */ 118 @Nullable getPrimaryKey()119 public List<String> getPrimaryKey() { 120 return mPrimaryKey; 121 } 122 123 /** 124 * @return the columnName-ColumnInfo HashMap. 125 */ 126 @NonNull getColumnInfoMapping()127 public HashMap<String, ColumnInfo> getColumnInfoMapping() { 128 return mColumnNameColumnInfoMap; 129 } 130 131 /** 132 * @return the ForeignKeyName-ForeignKeyInfo HashMap. 133 */ 134 @Nullable getForeignKeyMapping()135 public HashMap<String, ForeignKeyInfo> getForeignKeyMapping() { 136 return mForeignKeyNameForeignKeyInfoMap; 137 } 138 139 /** 140 * @return the indexName-IndexInfo HashMap. 141 */ getIndexInfoMapping()142 public HashMap<String, IndexInfo> getIndexInfoMapping() { 143 return mIndexNameIndexInfoMap; 144 } 145 } 146