@@ -110,18 +110,34 @@ public String toString() {
110
110
* <p>If {@code delegate} is an instance created by an earlier call to {@code memoize}, it is
111
111
* returned directly.
112
112
*/
113
- public static <T extends @ Nullable Object > Supplier <T > memoize (Supplier <T > delegate ) {
113
+ public static <T extends @ Nullable Object > MemoizingSupplier <T > memoize (Supplier <T > delegate ) {
114
114
if (delegate instanceof NonSerializableMemoizingSupplier
115
- || delegate instanceof MemoizingSupplier ) {
116
- return delegate ;
115
+ || delegate instanceof SerializableMemoizingSupplier ) {
116
+ return ( MemoizingSupplier < T >) delegate ;
117
117
}
118
118
return delegate instanceof Serializable
119
- ? new MemoizingSupplier <T >(delegate )
119
+ ? new SerializableMemoizingSupplier <T >(delegate )
120
120
: new NonSerializableMemoizingSupplier <T >(delegate );
121
121
}
122
122
123
+ /**
124
+ * A supplier that memoizes the result of the first call to {@link #get()} and returns the same
125
+ * result on subsequent calls to {@link #get()}.
126
+ *
127
+ * @author Alexey Pelykh
128
+ */
129
+ @ ElementTypesAreNonnullByDefault
130
+ public interface MemoizingSupplier <T extends @ Nullable Object > extends Supplier <T > {
131
+ /**
132
+ * Returns {@code true} if the supplier has been initialized, i.e. if the first call to
133
+ * {@link #get()} has been made or if the supplier has been explicitly initialized.
134
+ */
135
+ boolean isInitialized ();
136
+ }
137
+
123
138
@ VisibleForTesting
124
- static class MemoizingSupplier <T extends @ Nullable Object > implements Supplier <T >, Serializable {
139
+ static class SerializableMemoizingSupplier <T extends @ Nullable Object >
140
+ implements MemoizingSupplier <T >, Serializable {
125
141
private transient Object lock = new Object ();
126
142
127
143
final Supplier <T > delegate ;
@@ -130,7 +146,7 @@ static class MemoizingSupplier<T extends @Nullable Object> implements Supplier<T
130
146
// on volatile read of "initialized".
131
147
@ CheckForNull transient T value ;
132
148
133
- MemoizingSupplier (Supplier <T > delegate ) {
149
+ SerializableMemoizingSupplier (Supplier <T > delegate ) {
134
150
this .delegate = checkNotNull (delegate );
135
151
}
136
152
@@ -154,6 +170,11 @@ public T get() {
154
170
return uncheckedCastNullableTToT (value );
155
171
}
156
172
173
+ @ Override
174
+ public boolean isInitialized () {
175
+ return initialized ;
176
+ }
177
+
157
178
@ Override
158
179
public String toString () {
159
180
return "Suppliers.memoize("
@@ -172,7 +193,8 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
172
193
}
173
194
174
195
@ VisibleForTesting
175
- static class NonSerializableMemoizingSupplier <T extends @ Nullable Object > implements Supplier <T > {
196
+ static class NonSerializableMemoizingSupplier <T extends @ Nullable Object >
197
+ implements MemoizingSupplier <T > {
176
198
private final Object lock = new Object ();
177
199
178
200
@ SuppressWarnings ("UnnecessaryLambda" ) // Must be a fixed singleton object
@@ -208,6 +230,11 @@ public T get() {
208
230
return uncheckedCastNullableTToT (value );
209
231
}
210
232
233
+ @ Override
234
+ public boolean isInitialized () {
235
+ return delegate == SUCCESSFULLY_COMPUTED ;
236
+ }
237
+
211
238
@ Override
212
239
public String toString () {
213
240
Supplier <T > delegate = this .delegate ;
0 commit comments