From 92bf80f3e4dff7b1f3edaa32c5979b415d5a5954 Mon Sep 17 00:00:00 2001 From: Cheng Xing Date: Sun, 20 Oct 2024 00:05:06 +1100 Subject: [PATCH 1/3] Add isMemoized() method to MemoizingSupplier -allowing check whether the value has been memoized without triggering the memoization process. -updated toString() to reflect state Resolves issue#7292 --- guava/src/com/google/common/base/Suppliers.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/guava/src/com/google/common/base/Suppliers.java b/guava/src/com/google/common/base/Suppliers.java index 9476c21872fc..1d9a6e6939e6 100644 --- a/guava/src/com/google/common/base/Suppliers.java +++ b/guava/src/com/google/common/base/Suppliers.java @@ -154,11 +154,18 @@ public T get() { return uncheckedCastNullableTToT(value); } + /** + * Returns whether the value has been memoized without triggering memoization. + */ + public boolean isMemoized() { + return initialized; + } + @Override public String toString() { return "Suppliers.memoize(" - + (initialized ? "" : delegate) - + ")"; + + (initialized ? "" : delegate) + + ")"; } @GwtIncompatible // serialization @@ -171,6 +178,7 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE private static final long serialVersionUID = 0; } + @VisibleForTesting static class NonSerializableMemoizingSupplier implements Supplier { private final Object lock = new Object(); From 16ebce0ae97b9dc62a23dfcb65cd249801319f54 Mon Sep 17 00:00:00 2001 From: Cheng Xing Date: Sun, 20 Oct 2024 00:17:47 +1100 Subject: [PATCH 2/3] Add reset() and getIfMemoized() method to MemoizingSupplier - reset() method allowing resetting the cached value, enabling the supplier to recompute the value. - getIfMemoized() method returns the cached value if has been initialized, otherwise returns null. Resolves issue#7292 --- .../src/com/google/common/base/Suppliers.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/guava/src/com/google/common/base/Suppliers.java b/guava/src/com/google/common/base/Suppliers.java index 1d9a6e6939e6..478f78fa378d 100644 --- a/guava/src/com/google/common/base/Suppliers.java +++ b/guava/src/com/google/common/base/Suppliers.java @@ -161,6 +161,26 @@ public boolean isMemoized() { return initialized; } + /** + * Resets the memoized value, allowing the supplier to recompute its value. + * This can be useful in cases where the value needs to be refreshed. + */ + public void reset() { + synchronized (lock) { + initialized = false; + value = null; + } + } + + /** + * Returns the memoized value if it has been initialized, otherwise returns {@code null}. + * This method does not trigger memoization. + */ + @CheckForNull + public T getIfMemoized() { + return initialized ? value : null; + } + @Override public String toString() { return "Suppliers.memoize(" @@ -179,6 +199,7 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE } + @VisibleForTesting static class NonSerializableMemoizingSupplier implements Supplier { private final Object lock = new Object(); From 29ac5c21bed61d67d3e9a551e8a3782fd48d14bc Mon Sep 17 00:00:00 2001 From: Cheng Xing Date: Sun, 20 Oct 2024 00:40:45 +1100 Subject: [PATCH 3/3] Add refresh() method to MemoizingSupplier - refresh() which forces the recomputation of the cached value even if it has already been memoized. This will ensure the value is up to date. Resolves issue#7292 --- guava/src/com/google/common/base/Suppliers.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/guava/src/com/google/common/base/Suppliers.java b/guava/src/com/google/common/base/Suppliers.java index 478f78fa378d..74d632df8c21 100644 --- a/guava/src/com/google/common/base/Suppliers.java +++ b/guava/src/com/google/common/base/Suppliers.java @@ -181,6 +181,21 @@ public T getIfMemoized() { return initialized ? value : null; } + /** + * Attempts to refresh the memoized value by recomputing it. This method forces recomputation + * even if the value was previously memoized, and it can be used to ensure the value is up to date. + * The recomputation occurs only if the current thread successfully acquires the lock. + */ + public void refresh() { + synchronized (lock) { + T oldValue = value; + value = delegate.get(); + initialized = true; + if (!value.equals(oldValue)) { + System.out.println("Memoized value has been refreshed from " + oldValue + " to " + value); + } + } + } @Override public String toString() { return "Suppliers.memoize("