On Thu, 7 Aug 2025 11:57:38 GMT, Nikita Gubarkov <ngubar...@openjdk.org> wrote:
>> 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`. Actually, state change is also oneliner, but if you think that it's too complex for understanding, let's keep the original version. !mbRef.flatMap(e-> Optional.ofNullable(e.get())).equals(Optional.ofNullable(obj)); ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/26576#discussion_r2261387813