guava icon indicating copy to clipboard operation
guava copied to clipboard

Suppliers.memoize should return a MemoizingSupplier with isMemoized()

Open alexey-pelykh opened this issue 1 year ago • 4 comments

API(s)

`com.google.common.base.Suppliers::memoize`

How do you want it to be improved?

Get memoizing supplier state without causing memoization

Why do we need it to be improved?

To check the memoizing supplier state without changing it

Example

N/A as there's no such feature

Current Behavior

MemoizingSupplier::initialized is not exposed

Desired Behavior

MemoizingSupplier::initialized is exposed

Concrete Use Cases

e.g. in toString() of a class that needs to print a memoized supplier value without causing the memoization

Checklist

alexey-pelykh avatar Jul 07 '24 19:07 alexey-pelykh

e.g. in toString() of a class that needs to print a memoized supplier value without causing the memoization

Does the default toString() of the memoizing supplier not work for you? It doesn't trigger the memoization if it hasn't occurred yet.

My inclination is that this is niche enough that I'd suggest just writing your own, perhaps even wrapping a single-element LoadingCache or something.

cgdecker avatar Jul 11 '24 18:07 cgdecker

While toString() use-case could be mitigated, I fail to see how exposing a property that's already there can be harmful. Currently I have to resort to using Apache Commons because there's LazyInitializer.html#isInitialized - however I find it's usage bulky in comparison to MemoizingSupplier.

alexey-pelykh avatar Jul 11 '24 18:07 alexey-pelykh

Hi, I want to solve this issue. Could you please assign to me?

xc-x avatar Oct 17 '24 07:10 xc-x

@xc-x I already have a draft implementation that I wanted to share if/when the suggestion would be agreed upon, yet here it is #7450

alexey-pelykh avatar Oct 17 '24 08:10 alexey-pelykh