diff --git a/src/main/java/com/fizzed/jne/JavaHomeFinder.java b/src/main/java/com/fizzed/jne/JavaHomeFinder.java index f8f12df..359a5d9 100644 --- a/src/main/java/com/fizzed/jne/JavaHomeFinder.java +++ b/src/main/java/com/fizzed/jne/JavaHomeFinder.java @@ -34,6 +34,7 @@ public class JavaHomeFinder { JavaDistribution.CORRETTO }; + private boolean sorted; private JavaImageType imageType; private HardwareArchitecture hardwareArchitecture; private Integer minVersion; @@ -41,6 +42,20 @@ public class JavaHomeFinder { private JavaDistribution distribution; private JavaDistribution[] preferredDistributions; + public boolean isSorted() { + return sorted; + } + + public JavaHomeFinder sorted() { + this.sorted = true; + return this; + } + + public JavaHomeFinder sorted(boolean sorted) { + this.sorted = sorted; + return this; + } + public JavaImageType getImageType() { return imageType; } @@ -123,7 +138,8 @@ public JavaHomeFinder preferredDistributions(JavaDistribution... preferredDistri @Override public String toString() { - return "imageType=" + imageType + + return "sorted=" + sorted + + ", imageType=" + imageType + ", minVersion=" + minVersion + ", maxVersion=" + maxVersion + ", hwArch=" + hardwareArchitecture + @@ -162,6 +178,9 @@ public Optional tryFind(List javaHomes) { return Optional.empty(); } + // the first java home is important (it should be the one running this JVM) + final JavaHome firstJavaHome = javaHomes.get(0); + // filter our list down by image type, version, etc. (concrete criteria) final List filteredJavaHomes = javaHomes.stream() .filter(v -> this.minVersion == null || v.getVersion().getMajor() >= this.minVersion) @@ -169,10 +188,13 @@ public Optional tryFind(List javaHomes) { .filter(v -> this.imageType == null || v.getImageType() == this.imageType) .filter(v -> this.hardwareArchitecture == null || v.getHardwareArchitecture() == this.hardwareArchitecture) .filter(v -> this.distribution == null || v.getDistribution() == this.distribution) - // sort what's left by the most recent version (descending) - .sorted((a, b) -> b.getVersion().compareTo(a.getVersion())) .collect(Collectors.toList()); + if (this.sorted) { + // sort what's left by the most recent version (descending) + filteredJavaHomes.sort((a, b) -> b.getVersion().compareTo(a.getVersion())); + } + // by preferred distribution? if (this.preferredDistributions != null) { for (JavaDistribution d : this.preferredDistributions) {