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
17package androidx.room.migration.bundle;
18
19import androidx.annotation.RestrictTo;
20
21import com.google.gson.annotations.SerializedName;
22
23import java.util.List;
24
25/**
26 * Data class that holds the schema information about a table Index.
27 *
28 * @hide
29 */
30@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
31public class IndexBundle implements SchemaEquality<IndexBundle> {
32    // should match Index.kt
33    public static final String DEFAULT_PREFIX = "index_";
34    @SerializedName("name")
35    private String mName;
36    @SerializedName("unique")
37    private boolean mUnique;
38    @SerializedName("columnNames")
39    private List<String> mColumnNames;
40    @SerializedName("createSql")
41    private String mCreateSql;
42
43    public IndexBundle(String name, boolean unique, List<String> columnNames,
44            String createSql) {
45        mName = name;
46        mUnique = unique;
47        mColumnNames = columnNames;
48        mCreateSql = createSql;
49    }
50
51    public String getName() {
52        return mName;
53    }
54
55    public boolean isUnique() {
56        return mUnique;
57    }
58
59    public List<String> getColumnNames() {
60        return mColumnNames;
61    }
62
63    /**
64     * @hide
65     */
66    @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
67    public String create(String tableName) {
68        return BundleUtil.replaceTableName(mCreateSql, tableName);
69    }
70
71    @Override
72    public boolean isSchemaEqual(IndexBundle other) {
73        if (mUnique != other.mUnique) return false;
74        if (mName.startsWith(DEFAULT_PREFIX)) {
75            if (!other.mName.startsWith(DEFAULT_PREFIX)) {
76                return false;
77            }
78        } else if (other.mName.startsWith(DEFAULT_PREFIX)) {
79            return false;
80        } else if (!mName.equals(other.mName)) {
81            return false;
82        }
83
84        // order matters
85        if (mColumnNames != null ? !mColumnNames.equals(other.mColumnNames)
86                : other.mColumnNames != null) {
87            return false;
88        }
89        return true;
90    }
91}
92