@@ -110,18 +110,34 @@ public String toString() {
110110 * <p>If {@code delegate} is an instance created by an earlier call to {@code memoize}, it is
111111 * returned directly.
112112 */
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 ) {
114114 if (delegate instanceof NonSerializableMemoizingSupplier
115- || delegate instanceof MemoizingSupplier ) {
116- return delegate ;
115+ || delegate instanceof SerializableMemoizingSupplier ) {
116+ return ( MemoizingSupplier < T >) delegate ;
117117 }
118118 return delegate instanceof Serializable
119- ? new MemoizingSupplier <T >(delegate )
119+ ? new SerializableMemoizingSupplier <T >(delegate )
120120 : new NonSerializableMemoizingSupplier <T >(delegate );
121121 }
122122
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+
123138 @ 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 {
125141 private transient Object lock = new Object ();
126142
127143 final Supplier <T > delegate ;
@@ -130,7 +146,7 @@ static class MemoizingSupplier<T extends @Nullable Object> implements Supplier<T
130146 // on volatile read of "initialized".
131147 @ CheckForNull transient T value ;
132148
133- MemoizingSupplier (Supplier <T > delegate ) {
149+ SerializableMemoizingSupplier (Supplier <T > delegate ) {
134150 this .delegate = checkNotNull (delegate );
135151 }
136152
@@ -154,6 +170,11 @@ public T get() {
154170 return uncheckedCastNullableTToT (value );
155171 }
156172
173+ @ Override
174+ public boolean isInitialized () {
175+ return initialized ;
176+ }
177+
157178 @ Override
158179 public String toString () {
159180 return "Suppliers.memoize("
@@ -172,7 +193,8 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
172193 }
173194
174195 @ VisibleForTesting
175- static class NonSerializableMemoizingSupplier <T extends @ Nullable Object > implements Supplier <T > {
196+ static class NonSerializableMemoizingSupplier <T extends @ Nullable Object >
197+ implements MemoizingSupplier <T > {
176198 private final Object lock = new Object ();
177199
178200 @ SuppressWarnings ("UnnecessaryLambda" ) // Must be a fixed singleton object
@@ -208,6 +230,11 @@ public T get() {
208230 return uncheckedCastNullableTToT (value );
209231 }
210232
233+ @ Override
234+ public boolean isInitialized () {
235+ return delegate == SUCCESSFULLY_COMPUTED ;
236+ }
237+
211238 @ Override
212239 public String toString () {
213240 Supplier <T > delegate = this .delegate ;
0 commit comments