RFE: Refactor java.util.Optional and add NonNull checks
Oleksii Kucheruk
iselo+openjdk at raccoons.co
Wed Aug 23 14:43:17 UTC 2023
Hi there.
I have found that `java.util.Optional` is written procedural style and has
`ifnonnull` checks in each method. I propose to refactor `Optional` in
accordance to OOP-style. This will eliminates all unnecessary
`if`-statements, removes duplications and reduces bytecode size more then
twice.
I have two solutions:
1. Completely dynamic that avoids single static `EMPTY` instance and
unchecked casting of each `Optional.empty()`
2. Preserving original single static `EMPTY` per VM.
Also there are couple methods that throws NPE due to calling methods on
null-objects and requires to add `Objects.requireNonNull(...)`.
OptionalInt, OptionalDouble, OptionalLong could be refactored same way even
with remove of additional boolean variable `isPresent`.
Since I'm new here any guidance will be helpful.
Thank you in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20230823/192a6868/attachment.htm>
More information about the core-libs-dev
mailing list