|
16 | 16 |
|
17 | 17 | package dagger; |
18 | 18 |
|
| 19 | +import org.jspecify.annotations.Nullable; |
| 20 | + |
19 | 21 | /** |
20 | | - * A handle to a lazily-computed value. Each {@code Lazy} computes its value on |
21 | | - * the first call to {@link #get()} and remembers that same value for all |
22 | | - * subsequent calls to {@code get()}. |
| 22 | + * A handle to a lazily-computed value. Each {@code Lazy} computes its value on the first call to |
| 23 | + * {@link #get()} and remembers that same value for all subsequent calls to {@code get()}. |
23 | 24 | * |
24 | 25 | * <p>All implementations are expected to be thread-safe and compute their value at most once. |
25 | 26 | * |
26 | 27 | * <h2>Example</h2> |
27 | | - * The differences between <strong>direct injection</strong>, <strong>provider |
28 | | - * injection</strong> and <strong>lazy injection</strong> are best demonstrated |
29 | | - * with an example. Start with a module that computes a different integer for |
30 | | - * each use:<pre><code> |
| 28 | + * |
| 29 | + * The differences between <strong>direct injection</strong>, <strong>provider injection</strong> |
| 30 | + * and <strong>lazy injection</strong> are best demonstrated with an example. Start with a module |
| 31 | + * that computes a different integer for each use: |
| 32 | + * |
| 33 | + * <pre><code> |
31 | 34 | * {@literal @Module} |
32 | 35 | * final class CounterModule { |
33 | 36 | * int next = 100; |
|
40 | 43 | * </code></pre> |
41 | 44 | * |
42 | 45 | * <h3>Direct Injection</h3> |
43 | | - * This class injects that integer and prints it 3 times:<pre><code> |
| 46 | + * |
| 47 | + * This class injects that integer and prints it 3 times: |
| 48 | + * |
| 49 | + * <pre><code> |
44 | 50 | * final class DirectCounter { |
45 | 51 | * {@literal @Inject} Integer value; |
46 | 52 | * |
|
52 | 58 | * } |
53 | 59 | * } |
54 | 60 | * </code></pre> |
55 | | - * Injecting a {@code DirectCounter} and invoking {@code print()} reveals that |
56 | | - * the value is computed <i>before</i> it is required:<pre><code> |
| 61 | + * |
| 62 | + * Injecting a {@code DirectCounter} and invoking {@code print()} reveals that the value is computed |
| 63 | + * <i>before</i> it is required: |
| 64 | + * |
| 65 | + * <pre><code> |
57 | 66 | * computing... |
58 | 67 | * printing... |
59 | 68 | * 100 |
|
62 | 71 | * </code></pre> |
63 | 72 | * |
64 | 73 | * <h3>Provider Injection</h3> |
65 | | - * This class injects a {@linkplain javax.inject.Provider provider} for the |
66 | | - * integer. It calls {@code Provider.get()} 3 times and prints each result: |
| 74 | + * |
| 75 | + * This class injects a {@linkplain javax.inject.Provider provider} for the integer. It calls {@code |
| 76 | + * Provider.get()} 3 times and prints each result: |
| 77 | + * |
67 | 78 | * <pre><code> |
68 | 79 | * final class ProviderCounter { |
69 | 80 | * {@literal @Inject Provider<Integer> provider;} |
|
76 | 87 | * } |
77 | 88 | * } |
78 | 89 | * </code></pre> |
79 | | - * Injecting a {@code ProviderCounter} and invoking {@code print()} shows that |
80 | | - * a new value is computed each time {@code Provider.get()} is used:<pre><code> |
| 90 | + * |
| 91 | + * Injecting a {@code ProviderCounter} and invoking {@code print()} shows that a new value is |
| 92 | + * computed each time {@code Provider.get()} is used: |
| 93 | + * |
| 94 | + * <pre><code> |
81 | 95 | * printing... |
82 | 96 | * computing... |
83 | 97 | * 100 |
|
88 | 102 | * </code></pre> |
89 | 103 | * |
90 | 104 | * <h3>Lazy Injection</h3> |
91 | | - * This class injects a {@code Lazy} for the integer. Like the provider above, |
92 | | - * it calls {@code Lazy.get()} 3 times and prints each result:<pre><code> |
| 105 | + * |
| 106 | + * This class injects a {@code Lazy} for the integer. Like the provider above, it calls {@code |
| 107 | + * Lazy.get()} 3 times and prints each result: |
| 108 | + * |
| 109 | + * <pre><code> |
93 | 110 | * final class LazyCounter { |
94 | 111 | * {@literal @Inject Lazy<Integer> lazy;} |
95 | 112 | * |
|
101 | 118 | * } |
102 | 119 | * } |
103 | 120 | * </code></pre> |
104 | | - * Injecting a {@code LazyCounter} and invoking {@code print()} shows that a new |
105 | | - * value is computed immediately before it is needed. The same value is returned |
106 | | - * for all subsequent uses:<pre><code> |
| 121 | + * |
| 122 | + * Injecting a {@code LazyCounter} and invoking {@code print()} shows that a new value is computed |
| 123 | + * immediately before it is needed. The same value is returned for all subsequent uses: |
| 124 | + * |
| 125 | + * <pre><code> |
107 | 126 | * printing... |
108 | 127 | * computing... |
109 | 128 | * 100 |
|
112 | 131 | * </code></pre> |
113 | 132 | * |
114 | 133 | * <h3>Lazy != Singleton</h3> |
115 | | - * Note that each injected {@code Lazy} is independent, and remembers its value |
116 | | - * in isolation of other {@code Lazy} instances. In this example, two {@code |
117 | | - * LazyCounter} objects are created and {@code print()} is called on each: |
| 134 | + * |
| 135 | + * Note that each injected {@code Lazy} is independent, and remembers its value in isolation of |
| 136 | + * other {@code Lazy} instances. In this example, two {@code LazyCounter} objects are created and |
| 137 | + * {@code print()} is called on each: |
| 138 | + * |
118 | 139 | * <pre><code> |
119 | 140 | * final class LazyCounters { |
120 | 141 | * {@literal @Inject} LazyCounter counter1; |
|
126 | 147 | * } |
127 | 148 | * } |
128 | 149 | * </code></pre> |
| 150 | + * |
129 | 151 | * The output demonstrates that each {@code Lazy} works independently: |
| 152 | + * |
130 | 153 | * <pre><code> |
131 | 154 | * printing... |
132 | 155 | * computing... |
|
139 | 162 | * 101 |
140 | 163 | * 101 |
141 | 164 | * </code></pre> |
142 | | - * Use {@link javax.inject.Singleton @Singleton} to share one instance among all |
143 | | - * clients, and {@code Lazy} for lazy computation in a single client. |
| 165 | + * |
| 166 | + * Use {@link javax.inject.Singleton @Singleton} to share one instance among all clients, and {@code |
| 167 | + * Lazy} for lazy computation in a single client. |
144 | 168 | */ |
145 | | -public interface Lazy<T> { |
| 169 | +public interface Lazy<T extends @Nullable Object> { |
146 | 170 | /** |
147 | 171 | * Return the underlying value, computing the value if necessary. All calls to |
148 | 172 | * the same {@code Lazy} instance will return the same result. |
|
0 commit comments