Skip to content

Commit

Permalink
Add sorted() value for JavaHomeFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlauer committed Nov 3, 2023
1 parent f38bce7 commit 0105d73
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/main/java/com/fizzed/jne/JavaHomeFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,28 @@ public class JavaHomeFinder {
JavaDistribution.CORRETTO
};

private boolean sorted;
private JavaImageType imageType;
private HardwareArchitecture hardwareArchitecture;
private Integer minVersion;
private Integer maxVersion;
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;
}
Expand Down Expand Up @@ -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 +
Expand Down Expand Up @@ -162,17 +178,23 @@ public Optional<JavaHome> tryFind(List<JavaHome> 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<JavaHome> filteredJavaHomes = javaHomes.stream()
.filter(v -> this.minVersion == null || v.getVersion().getMajor() >= this.minVersion)
.filter(v -> this.maxVersion == null || v.getVersion().getMajor() <= this.maxVersion)
.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) {
Expand Down

0 comments on commit 0105d73

Please sign in to comment.