• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import org.jspecify.annotations.Nullable;
23 
24 /**
25  * A {@link Provider} implementation that memoizes the result of another {@link Provider} using
26  * simple lazy initialization, not the double-checked lock pattern.
27  */
28 public final class SingleCheck<T extends @Nullable Object> implements Provider<T> {
29   private static final Object UNINITIALIZED = new Object();
30 
31   private volatile @Nullable Provider<T> provider;
32   private volatile @Nullable Object instance = UNINITIALIZED;
33 
SingleCheck(Provider<T> provider)34   private SingleCheck(Provider<T> provider) {
35     assert provider != null;
36     this.provider = provider;
37   }
38 
39   @SuppressWarnings("unchecked") // cast only happens when result comes from the delegate provider
40   @Override
get()41   public T get() {
42     @Nullable Object local = instance;
43     if (local == UNINITIALIZED) {
44       // provider is volatile and might become null after the check, so retrieve the provider first
45       @Nullable Provider<T> providerReference = provider;
46       if (providerReference == null) {
47         // The provider was null, so the instance must already be set
48         local = instance;
49       } else {
50         local = providerReference.get();
51         instance = local;
52 
53         // Null out the reference to the provider. We are never going to need it again, so we can
54         // make it eligible for GC.
55         provider = null;
56       }
57     }
58     return (T) local;
59   }
60 
61   /** Returns a {@link Provider} that caches the value from the given delegate provider. */
provider(Provider<T> provider)62   public static <T> Provider<T> provider(Provider<T> 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