1 /*
2  * Copyright 2022 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.compiler.processing
18 
19 import java.util.ServiceLoader
20 
21 /**
22  * This interface can be implemented by XProcessingTesting clients to provide a default
23  * configuration for all runProcessingTest calls.
24  *
25  * XProcessingTesting will find the implementation via a [ServiceLoader]. To register your
26  * implementation, add the following file
27  * `androidx.room.compiler.processing.XProcessingEnvironmentTestConfigProvider` into
28  * `resources/META-INF/services/` folder (test source path). The contents of the file should have
29  * the fully qualified name of your implementation class.
30  */
31 interface XProcessingEnvironmentTestConfigProvider {
configurenull32     fun configure(options: Map<String, String>): XProcessingEnvConfig
33 
34     companion object {
35         private val instance: XProcessingEnvironmentTestConfigProvider? by lazy {
36             val implementations =
37                 ServiceLoader.load(XProcessingEnvironmentTestConfigProvider::class.java).toList()
38             if (implementations.size >= 2) {
39                 error(
40                     "Multiple XProcessingEnvironmentTestConfigProvider implementations " +
41                         "were found: $implementations. There can be only 1 or 0."
42                 )
43             }
44             implementations.firstOrNull()
45         }
46 
47         internal fun createConfig(options: Map<String, String>): XProcessingEnvConfig {
48             return instance?.configure(options) ?: XProcessingEnvConfig.DEFAULT
49         }
50     }
51 }
52