1 /*
2  * Copyright 2024 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 // @exportToFramework:skipFile()
17 package androidx.appsearch.app;
18 
19 import androidx.annotation.RestrictTo;
20 import androidx.annotation.VisibleForTesting;
21 
22 import org.jspecify.annotations.NonNull;
23 
24 /**
25  * This is a factory class for implementations needed based on the environment.
26  *
27  * @exportToFramework:hide
28  */
29 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
30 public class AppSearchEnvironmentFactory {
31     private static volatile AppSearchEnvironment sAppSearchEnvironment;
32 
33     /** Returns the singleton instance of {@link AppSearchEnvironment}. */
getEnvironmentInstance()34     public static @NonNull AppSearchEnvironment getEnvironmentInstance() {
35         AppSearchEnvironment localRef = sAppSearchEnvironment;
36         if (localRef == null) {
37             synchronized (AppSearchEnvironmentFactory.class) {
38                 localRef = sAppSearchEnvironment;
39                 if (localRef == null) {
40                     sAppSearchEnvironment = localRef =
41                             new JetpackAppSearchEnvironment();
42                 }
43             }
44         }
45         return localRef;
46     }
47 
48     /** Sets an instance of {@link AppSearchEnvironment}. for testing.*/
49     @VisibleForTesting
setEnvironmentInstanceForTest( @onNull AppSearchEnvironment appSearchEnvironment)50     public static void setEnvironmentInstanceForTest(
51             @NonNull AppSearchEnvironment appSearchEnvironment) {
52         synchronized (AppSearchEnvironmentFactory.class) {
53             sAppSearchEnvironment = appSearchEnvironment;
54         }
55     }
56 
AppSearchEnvironmentFactory()57     private AppSearchEnvironmentFactory() {
58     }
59 }
60