On Tue, 5 Aug 2025 13:45:00 GMT, Alexey Ushakov <a...@openjdk.org> wrote:

>> src/java.desktop/share/classes/sun/java2d/pipe/BufferedContext.java line 103:
>> 
>>> 101:     protected static BufferedContext currentContext;
>>> 102: 
>>> 103:     private Reference<AccelSurface> validSrcDataRef = null;
>> 
>> I think that you can wrap these fields into Optional ones to simplify the 
>> state change verification.
>
> For wrapping, you can use the following one-liner 
> `Optional.ofNullable(val).flatMap(e -> Optional.of(new WeakReference(val)));`

I don't really understand how this could simplify the check.
For wrapping, instead of `obj == null ? null : new WeakReference<>(obj)` it 
would become `Optional.ofNullable(obj).flatMap(e -> Optional.of(new 
WeakReference<>(obj)))`.
And for comparing the state, it would need to repeat the same logic with 
special treatment of `null` refs, just replacing the null check with 
`isEmpty()` - is this what you meant?
Overall, it seems easier to me to understand the null-check version, than the 
one wrapped into `Optional`.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/26576#discussion_r2260094434

Reply via email to