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 androidx.room.solver.binderprovider
18 
19 import androidx.room.ext.PagingTypeNames
20 import androidx.room.parser.ParsedQuery
21 import androidx.room.processor.Context
22 import androidx.room.processor.ProcessorErrors
23 import androidx.room.solver.QueryResultBinderProvider
24 import androidx.room.solver.query.result.ListQueryResultAdapter
25 import androidx.room.solver.query.result.PositionalDataSourceQueryResultBinder
26 import androidx.room.solver.query.result.QueryResultBinder
27 import javax.lang.model.type.DeclaredType
28 import javax.lang.model.type.TypeMirror
29 
30 class DataSourceQueryResultBinderProvider(val context: Context) : QueryResultBinderProvider {
<lambda>null31     private val dataSourceTypeMirror: TypeMirror? by lazy {
32         context.processingEnv.elementUtils
33                 .getTypeElement(PagingTypeNames.DATA_SOURCE.toString())?.asType()
34     }
35 
<lambda>null36     private val positionalDataSourceTypeMirror: TypeMirror? by lazy {
37         context.processingEnv.elementUtils
38                 .getTypeElement(PagingTypeNames.POSITIONAL_DATA_SOURCE.toString())?.asType()
39     }
40 
providenull41     override fun provide(declared: DeclaredType, query: ParsedQuery): QueryResultBinder {
42         if (query.tables.isEmpty()) {
43             context.logger.e(ProcessorErrors.OBSERVABLE_QUERY_NOTHING_TO_OBSERVE)
44         }
45         val typeArg = declared.typeArguments.last()
46         val listAdapter = context.typeAdapterStore.findRowAdapter(typeArg, query)?.let {
47             ListQueryResultAdapter(it)
48         }
49         val tableNames = ((listAdapter?.accessedTableNames() ?: emptyList())
50                 + query.tables.map { it.name }).toSet()
51         return PositionalDataSourceQueryResultBinder(listAdapter, tableNames)
52     }
53 
matchesnull54     override fun matches(declared: DeclaredType): Boolean {
55         if (dataSourceTypeMirror == null || positionalDataSourceTypeMirror == null) {
56             return false
57         }
58         if (declared.typeArguments.isEmpty()) {
59             return false
60         }
61         val erasure = context.processingEnv.typeUtils.erasure(declared)
62         val isDataSource = context.processingEnv.typeUtils
63                 .isAssignable(erasure, dataSourceTypeMirror)
64         if (!isDataSource) {
65             return false
66         }
67         val isPositional = context.processingEnv.typeUtils
68                 .isAssignable(erasure, positionalDataSourceTypeMirror)
69         if (!isPositional) {
70             context.logger.e(ProcessorErrors.PAGING_SPECIFY_DATA_SOURCE_TYPE)
71         }
72         return true
73     }
74 }
75