RFR: Optional refactoring

Andrei Rybak duke at openjdk.org
Tue Mar 28 12:55:00 UTC 2023


On Sun, 26 Mar 2023 08:38:53 GMT, Andrei Rybak <duke at openjdk.org> wrote:

> This is a follow-up to the email https://mail.openjdk.org/pipermail/jmh-dev/2023-January/003562.html
> 
> `java.util.Optional` isn't `Serializeable`. Because of that `org.openjdk.jmh.runner.options.TestOptions` fails with `java.io.NotSerializableException: java.util.Optional`, when  `org.openjdk.jmh.util.Optional` is completely replaced with `java.util.Optional`. See [test results on branch `java-util-optional`](https://github.com/rybak/jmh/actions/runs/4326921418), ([one commit](https://github.com/rybak/jmh/commit/25b9cb382be7285a51446976247d036ac855fc91) on top of initial state of this PR).
> 
> I didn't look further into possibility of removing `Optional`s as fields of class `CommandLineOptions`. I think that the refactoring up until the drop-in replacement to `java.util.Optional` might still be worth it, at least from the point of making it more approachable for people who are familiar with `java.util.Optional`.  So here are these changes for your consideration. Details are in the commit messages.

I force pushed to drop the commits that are directly related to the replacement with `j.l.Optional`, leaving just two minor cleanups around the usage of `org.openjdk.jmh.util.Optional`.

Range-diff:


$ git range-diff 2d8146a...1e84775
1:  da03a011 = 1:  a7762e07 OptionsBuilder: don't pass empty Optional to orAnother()
2:  06a697d9 < -:  -------- OptionsBuilder: extract method getOrOther
3:  e7505b37 ! 2:  1e847757 Runner: replace orAnother() with orElseGet()
    @@ Commit message
         Runner: replace orAnother() with orElseGet()
     
         Refactor usage of method org.openjdk.jmh.util.Optional#orAnother in
    -    class Runner using new method orElseGet().  This is part of preparation
    -    for a possible future drop-in replacement of usages of class
    -    org.openjdk.jmh.util.Optional with class java.util.Optional.
    -
    -    In Java 8, the minimal version that JMH supports at the moment, class
    -    java.util.Optional doesn't have an analogue to JMH's method orAnother().
    -    A similar method (taking a Supplier of Optional as a parameter), which
    -    could be used instead was added to java.util.Optional in Java 9, see
    -    https://bugs.openjdk.org/browse/JDK-8080418.
    +    class Runner using new method orElseGet() with lazy `Supplier`s as per
    +    comment added in commit db1bc365 (7902576: Lazy query for current JVM
    +    args in Runner, 2020-01-20).  Clean up imports, while we're here.
     
      ## jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java ##
     @@ jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java: import org.openjdk.jmh.util.Optional;
4:  8e2f4814 < -:  -------- OptionsBuilder: refactor usage of Optional#orAnother()
5:  b12d5612 < -:  -------- Optional: rename none() to empty()
6:  39b5f628 < -:  -------- Optional: rename eitherOf() to ofNullable()
7:  2d8146a5 < -:  -------- Optional: rename hasValue() to isPresent()

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

PR Comment: https://git.openjdk.org/jmh/pull/98#issuecomment-1486830490


More information about the jmh-dev mailing list