Skip to content

Commit f830a5e

Browse files
committed
(feat) MemoizingSupplier.isInitialized
1 parent 0e2fa3e commit f830a5e

File tree

4 files changed

+74
-20
lines changed

4 files changed

+74
-20
lines changed

android/guava-tests/test/com/google/common/base/SuppliersTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ static class SerializableThrowingSupplier extends ThrowingSupplier implements Se
8181
static void checkMemoize(CountingSupplier countingSupplier, Supplier<Integer> memoizedSupplier) {
8282
// the underlying supplier hasn't executed yet
8383
assertEquals(0, countingSupplier.calls);
84-
84+
assertFalse(memoizedSupplier.isInitialized());
8585
assertEquals(10, (int) memoizedSupplier.get());
8686

8787
// now it has
8888
assertEquals(1, countingSupplier.calls);
89-
89+
assertTrue(memoizedSupplier.isInitialized());
9090
assertEquals(10, (int) memoizedSupplier.get());
9191

9292
// it still should only have executed once due to memoization
@@ -165,7 +165,7 @@ public void testMemoizeSerializable() throws Exception {
165165
Object unused2 = memoizedSupplier.get();
166166

167167
CountingSupplier countingCopy =
168-
(CountingSupplier) ((Suppliers.MemoizingSupplier<Integer>) copy).delegate;
168+
(CountingSupplier) ((Suppliers.SerializableMemoizingSupplier<Integer>) copy).delegate;
169169
checkMemoize(countingCopy, copy);
170170
}
171171

android/guava/src/com/google/common/base/Suppliers.java

+34-7
Original file line numberDiff line numberDiff line change
@@ -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;

guava-tests/test/com/google/common/base/SuppliersTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ static class SerializableThrowingSupplier extends ThrowingSupplier implements Se
8181
static void checkMemoize(CountingSupplier countingSupplier, Supplier<Integer> memoizedSupplier) {
8282
// the underlying supplier hasn't executed yet
8383
assertEquals(0, countingSupplier.calls);
84-
84+
assertFalse(memoizedSupplier.isInitialized());
8585
assertEquals(10, (int) memoizedSupplier.get());
8686

8787
// now it has
8888
assertEquals(1, countingSupplier.calls);
89-
89+
assertTrue(memoizedSupplier.isInitialized());
9090
assertEquals(10, (int) memoizedSupplier.get());
9191

9292
// it still should only have executed once due to memoization
@@ -165,7 +165,7 @@ public void testMemoizeSerializable() throws Exception {
165165
Object unused2 = memoizedSupplier.get();
166166

167167
CountingSupplier countingCopy =
168-
(CountingSupplier) ((Suppliers.MemoizingSupplier<Integer>) copy).delegate;
168+
(CountingSupplier) ((Suppliers.SerializableMemoizingSupplier<Integer>) copy).delegate;
169169
checkMemoize(countingCopy, copy);
170170
}
171171

guava/src/com/google/common/base/Suppliers.java

+34-7
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)