Skip to content

Commit

Permalink
7903451: JMH: Refactor internal Optional usages
Browse files Browse the repository at this point in the history
Passing an empty Optional to method orAnother() of class
org.openjdk.jmh.util.Optional is a no-op.  Stop doing it in getters
getJvmArgs(), getJvmArgsAppend(), and getJvmArgsPrepend() of class
OptionsBuilder.

Refactor usage of method org.openjdk.jmh.util.Optional#orAnother in
class Runner using new method orElseGet() with lazy `Supplier`s as per
comment added in commit db1bc36 (7902576: Lazy query for current JVM
args in Runner, 2020-01-20).  Clean up imports, while we're here.

Reviewed-by: shade
  • Loading branch information
rybak authored and shipilev committed Apr 4, 2023
1 parent 2f21ef2 commit e88ed82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
15 changes: 6 additions & 9 deletions jmh-core/src/main/java/org/openjdk/jmh/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

import java.io.*;
import java.lang.management.ManagementFactory;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.file.Files;
Expand Down Expand Up @@ -475,14 +474,12 @@ private BenchmarkParams newBenchmarkParams(BenchmarkListEntry benchmark, ActionM
benchmark.getJvmArgsPrepend().orElse(Collections.<String>emptyList())));

// We want to be extra lazy when accessing ManagementFactory, because security manager
// may prevent us doing so. When JMH upgrades to Java 8, replaces this with proper Optional
// and lazy Supplier.
Optional<Collection<String>> jvmArgsMid = options.getJvmArgs().orAnother(benchmark.getJvmArgs());
if (jvmArgsMid.hasValue()) {
jvmArgs.addAll(jvmArgsMid.get());
} else {
jvmArgs.addAll(ManagementFactory.getRuntimeMXBean().getInputArguments());
}
// may prevent us doing so.
jvmArgs.addAll(options.getJvmArgs()
.orElseGet(() -> benchmark.getJvmArgs()
.orElseGet(() -> ManagementFactory.getRuntimeMXBean().getInputArguments())
));


jvmArgs.addAll(options.getJvmArgsAppend().orElse(
benchmark.getJvmArgsAppend().orElse(Collections.<String>emptyList())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,9 @@ public ChainedOptionsBuilder jvmArgs(String... value) {
@Override
public Optional<Collection<String>> getJvmArgs() {
if (otherOptions != null) {
return jvmArgs.orAnother(otherOptions.getJvmArgs().orAnother(Optional.<Collection<String>>none()));
return jvmArgs.orAnother(otherOptions.getJvmArgs());
} else {
return jvmArgs.orAnother(Optional.<Collection<String>>none());
return jvmArgs;
}
}

Expand All @@ -639,9 +639,9 @@ public ChainedOptionsBuilder jvmArgsAppend(String... value) {
@Override
public Optional<Collection<String>> getJvmArgsAppend() {
if (otherOptions != null) {
return jvmArgsAppend.orAnother(otherOptions.getJvmArgsAppend().orAnother(Optional.<Collection<String>>none()));
return jvmArgsAppend.orAnother(otherOptions.getJvmArgsAppend());
} else {
return jvmArgsAppend.orAnother(Optional.<Collection<String>>none());
return jvmArgsAppend;
}
}

Expand All @@ -658,9 +658,9 @@ public ChainedOptionsBuilder jvmArgsPrepend(String... value) {
@Override
public Optional<Collection<String>> getJvmArgsPrepend() {
if (otherOptions != null) {
return jvmArgsPrepend.orAnother(otherOptions.getJvmArgsPrepend().orAnother(Optional.<Collection<String>>none()));
return jvmArgsPrepend.orAnother(otherOptions.getJvmArgsPrepend());
} else {
return jvmArgsPrepend.orAnother(Optional.<Collection<String>>none());
return jvmArgsPrepend;
}
}

Expand Down
5 changes: 5 additions & 0 deletions jmh-core/src/main/java/org/openjdk/jmh/util/Optional.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.Serializable;
import java.util.Objects;
import java.util.function.Supplier;

/**
* Option class
Expand All @@ -51,6 +52,10 @@ public T orElse(T elseVal) {
return (val == null) ? elseVal : val;
}

public T orElseGet(Supplier<T> alternativeSupplier) {
return (val == null) ? alternativeSupplier.get() : val;
}

public Optional<T> orAnother(Optional<T> alternative) {
return (val == null) ? alternative : this;
}
Expand Down

0 comments on commit e88ed82

Please sign in to comment.