• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Objects;
25 
26 /** IndexInfo contains information about all the attributes that an index can hold. */
27 class IndexInfo {
28     private final String mIndexName;
29     private final String mTableName;
30     private final List<String> mColumnList;
31     private final boolean mCheckUniqueFlag;
32 
33     /** Creates an instance for IndexInfo. */
IndexInfo(Builder builder)34     IndexInfo(Builder builder) {
35         mIndexName = builder.mIndexName;
36         mTableName = builder.mTableName;
37         mColumnList = builder.mColumnList;
38         mCheckUniqueFlag = builder.mCheckUniqueFlag;
39     }
40 
41     /** Builder pattern for IndexInfo. */
42     public static class Builder {
43         private final String mIndexName;
44         private final String mTableName;
45         private final List<String> mColumnList;
46         private boolean mCheckUniqueFlag;
47 
Builder(String indexName, String tableName)48         Builder(String indexName, String tableName) {
49             mIndexName = indexName;
50             mTableName = tableName;
51             mColumnList = new ArrayList<>();
52         }
53 
54         /**
55          * Sets the unique constraint flag of the index(if index has been made on unique flag then
56          * it is set to true otherwise false).
57          */
setUniqueFlag(boolean flag)58         public Builder setUniqueFlag(boolean flag) {
59             mCheckUniqueFlag = flag;
60             return this;
61         }
62 
63         /** Appends column to the existing list of columns. */
addIndexCols(@onNull String column)64         public Builder addIndexCols(@NonNull String column) {
65             Objects.requireNonNull(column);
66             mColumnList.add(column);
67             return this;
68         }
69 
70         /** Builds the IndexInfo object. */
build()71         public IndexInfo build() {
72             return new IndexInfo(this);
73         }
74     }
75 
76     /**
77      * @return name of the index.
78      */
79     @NonNull
getIndexName()80     public String getIndexName() {
81         return mIndexName;
82     }
83 
84     /**
85      * @return name of the table of the index.
86      */
87     @NonNull
getTableName()88     public String getTableName() {
89         return mTableName;
90     }
91 
92     /**
93      * @return list of the columns of the table of the index.
94      */
95     @NonNull
getColumnList()96     public List<String> getColumnList() {
97         return mColumnList;
98     }
99 
100     /**
101      * @return boolean value which shows whether index is made unique or not.
102      */
103     @NonNull
isUnique()104     public boolean isUnique() {
105         return mCheckUniqueFlag;
106     }
107 
108     /**
109      * @return true if the objects of the two IndexInfo are same otherwise false
110      */
111     @NonNull
isEqual(IndexInfo expectedIndex)112     public boolean isEqual(IndexInfo expectedIndex) {
113         if (mIndexName.equals(expectedIndex.mIndexName)
114                 && mTableName.equals(expectedIndex.mTableName)
115                 && mCheckUniqueFlag == expectedIndex.mCheckUniqueFlag) {
116             List<String> columnListCheck = mColumnList;
117             List<String> expectedColumnList = expectedIndex.mColumnList;
118             Collections.sort(columnListCheck);
119             Collections.sort(expectedColumnList);
120             return columnListCheck.equals(expectedColumnList);
121         }
122         return false;
123     }
124 
125     /**
126      * Compares two IndexInfo and stores any backward incompatible change to the corresponding
127      * ErrorInfo of index.
128      */
checkIndexDiff( IndexInfo expectedIndex, List<String> modificationOfIndex, String tableName)129     public void checkIndexDiff(
130             IndexInfo expectedIndex, List<String> modificationOfIndex, String tableName) {
131 
132         for (String columnName : mColumnList) {
133             if (!expectedIndex.mColumnList.contains(columnName)) {
134                 modificationOfIndex.add(
135                         "Column: "
136                                 + columnName
137                                 + " has been deleted from the  index : "
138                                 + mIndexName
139                                 + " of table: "
140                                 + tableName);
141             }
142         }
143 
144         for (String columnName : expectedIndex.mColumnList) {
145             if (!mColumnList.contains(columnName)) {
146                 modificationOfIndex.add(
147                         "Column: "
148                                 + columnName
149                                 + " has been added to the index : "
150                                 + mIndexName
151                                 + " of table: "
152                                 + tableName);
153             }
154         }
155 
156         if (mCheckUniqueFlag != expectedIndex.mCheckUniqueFlag) {
157             if (mCheckUniqueFlag) {
158                 modificationOfIndex.add(
159                         "Unique flag has been removed from the index: "
160                                 + mIndexName
161                                 + " of table: "
162                                 + tableName);
163             } else {
164                 modificationOfIndex.add(
165                         "Unique flag has been added to the index: "
166                                 + mIndexName
167                                 + " of table: "
168                                 + tableName);
169             }
170         }
171     }
172 }
173