<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
Hi,<br>
<br>
Also, Null checks in the VM are very cheap, likely cheaper than a
virtual dispatch.<br>
Adding calls to requireNonNull is almost always a no-op, except for
adding a variable name to the exception it still throws NPE. A
frequently used class like Optional will be compiled to machine code
and the number of byte codes is not an important metric.<br>
<br>
Regardless, Project Valhalla will be converting and optimizing
Optional to a value class as the project matures.<br>
<br>
Thanks, Roger<br>
<br>
<br>
<div class="moz-cite-prefix">On 8/23/23 11:56 AM, Remi Forax wrote:<br>
</div>
<blockquote type="cite" cite="mid:2142535309.16863634.1692806202964.JavaMail.zimbra@univ-eiffel.fr">
<div style="font-family: arial, helvetica, sans-serif; font-size:
12pt; color: #000000">
<div>Sorry, you can not do that.</div>
<div>Optional is a value based class [1] (see the javadoc) so
the class has to be final.<br data-mce-bogus="1">
</div>
<div><br data-mce-bogus="1">
</div>
<div>And more generally, the API of a classes of OpenJDK will
not change based on some stylistic issue, OOP or not. <br data-mce-bogus="1">
</div>
<div><br data-mce-bogus="1">
</div>
<div>regards,<br data-mce-bogus="1">
</div>
<div>RĂ©mi Forax<br data-mce-bogus="1">
</div>
<div><br data-mce-bogus="1">
</div>
<div>[1]
<a class="moz-txt-link-freetext" href="https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/doc-files/ValueBased.html">https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/doc-files/ValueBased.html</a><br data-mce-bogus="1">
</div>
<div><br data-mce-bogus="1">
</div>
<div><br>
</div>
<hr id="zwchr" data-marker="__DIVIDER__">
<div data-marker="__HEADERS__">
<blockquote style="border-left:2px solid
#1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From:
</b>"Oleksii Kucheruk" <a class="moz-txt-link-rfc2396E" href="mailto:iselo+openjdk@raccoons.co"><iselo+openjdk@raccoons.co></a><br>
<b>To: </b>"core-libs-dev"
<a class="moz-txt-link-rfc2396E" href="mailto:core-libs-dev@openjdk.org"><core-libs-dev@openjdk.org></a><br>
<b>Sent: </b>Wednesday, August 23, 2023 4:43:17 PM<br>
<b>Subject: </b>RFE: Refactor java.util.Optional and add
NonNull checks<br>
</blockquote>
</div>
<div data-marker="__QUOTED_TEXT__">
<blockquote style="border-left:2px solid
#1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">
<div dir="ltr">Hi there.
<div>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.</div>
<br>
<div>I have two solutions:</div>
<div>1. Completely dynamic that avoids single static
`EMPTY` instance and unchecked casting of each
`Optional.empty()`</div>
<div>2. Preserving original single static `EMPTY` per VM.</div>
<br>
<div>Also there are couple methods that throws NPE due
to calling methods on null-objects and requires to add
`Objects.requireNonNull(...)`.</div>
<br>
<div>OptionalInt, OptionalDouble, OptionalLong could be
refactored same way even with remove of additional
boolean variable `isPresent`.</div>
<br>
<div>Since I'm new here any guidance will be helpful.</div>
<div>Thank you in advance.</div>
</div>
<br>
</blockquote>
</div>
</div>
</blockquote>
<br>
</body>
</html>