xref: /aosp_15_r20/external/dagger2/java/dagger/internal/SingleCheck.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2014 The Dagger Authors.
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 dagger.internal;
18 
19 import static dagger.internal.Preconditions.checkNotNull;
20 import static dagger.internal.Providers.asDaggerProvider;
21 
22 /**
23  * A {@link Provider} implementation that memoizes the result of another {@link Provider} using
24  * simple lazy initialization, not the double-checked lock pattern.
25  */
26 public final class SingleCheck<T> implements Provider<T> {
27   private static final Object UNINITIALIZED = new Object();
28 
29   private volatile Provider<T> provider;
30   private volatile Object instance = UNINITIALIZED;
31 
SingleCheck(Provider<T> provider)32   private SingleCheck(Provider<T> provider) {
33     assert provider != null;
34     this.provider = provider;
35   }
36 
37   @SuppressWarnings("unchecked") // cast only happens when result comes from the delegate provider
38   @Override
get()39   public T get() {
40     Object local = instance;
41     if (local == UNINITIALIZED) {
42       // provider is volatile and might become null after the check, so retrieve the provider first
43       Provider<T> providerReference = provider;
44       if (providerReference == null) {
45         // The provider was null, so the instance must already be set
46         local = instance;
47       } else {
48         local = providerReference.get();
49         instance = local;
50 
51         // Null out the reference to the provider. We are never going to need it again, so we can
52         // make it eligible for GC.
53         provider = null;
54       }
55     }
56     return (T) local;
57   }
58 
59   /** Returns a {@link Provider} that caches the value from the given delegate provider. */
60   // This method is declared this way instead of "<T> Provider<T> provider(Provider<T> provider)"
61   // to work around an Eclipse type inference bug: https://github.com/google/dagger/issues/949.
provider(P provider)62   public static <P extends Provider<T>, T> Provider<T> provider(P provider) {
63     // If a scoped @Binds delegates to a scoped binding, don't cache the value again.
64     if (provider instanceof SingleCheck || provider instanceof DoubleCheck) {
65       return provider;
66     }
67     return new SingleCheck<T>(checkNotNull(provider));
68   }
69 
70   /**
71    * Legacy javax version of the method to support libraries compiled with an older version of
72    * Dagger. Do not use directly.
73    */
provider( P delegate)74   public static <P extends javax.inject.Provider<T>, T> javax.inject.Provider<T> provider(
75       P delegate) {
76     return provider(asDaggerProvider(delegate));
77   }
78 }
79