@@ -34,13 +34,28 @@ public class JavaHomeFinder {
34
34
JavaDistribution .CORRETTO
35
35
};
36
36
37
+ private boolean sorted ;
37
38
private JavaImageType imageType ;
38
39
private HardwareArchitecture hardwareArchitecture ;
39
40
private Integer minVersion ;
40
41
private Integer maxVersion ;
41
42
private JavaDistribution distribution ;
42
43
private JavaDistribution [] preferredDistributions ;
43
44
45
+ public boolean isSorted () {
46
+ return sorted ;
47
+ }
48
+
49
+ public JavaHomeFinder sorted () {
50
+ this .sorted = true ;
51
+ return this ;
52
+ }
53
+
54
+ public JavaHomeFinder sorted (boolean sorted ) {
55
+ this .sorted = sorted ;
56
+ return this ;
57
+ }
58
+
44
59
public JavaImageType getImageType () {
45
60
return imageType ;
46
61
}
@@ -123,7 +138,8 @@ public JavaHomeFinder preferredDistributions(JavaDistribution... preferredDistri
123
138
124
139
@ Override
125
140
public String toString () {
126
- return "imageType=" + imageType +
141
+ return "sorted=" + sorted +
142
+ ", imageType=" + imageType +
127
143
", minVersion=" + minVersion +
128
144
", maxVersion=" + maxVersion +
129
145
", hwArch=" + hardwareArchitecture +
@@ -162,17 +178,23 @@ public Optional<JavaHome> tryFind(List<JavaHome> javaHomes) {
162
178
return Optional .empty ();
163
179
}
164
180
181
+ // the first java home is important (it should be the one running this JVM)
182
+ final JavaHome firstJavaHome = javaHomes .get (0 );
183
+
165
184
// filter our list down by image type, version, etc. (concrete criteria)
166
185
final List <JavaHome > filteredJavaHomes = javaHomes .stream ()
167
186
.filter (v -> this .minVersion == null || v .getVersion ().getMajor () >= this .minVersion )
168
187
.filter (v -> this .maxVersion == null || v .getVersion ().getMajor () <= this .maxVersion )
169
188
.filter (v -> this .imageType == null || v .getImageType () == this .imageType )
170
189
.filter (v -> this .hardwareArchitecture == null || v .getHardwareArchitecture () == this .hardwareArchitecture )
171
190
.filter (v -> this .distribution == null || v .getDistribution () == this .distribution )
172
- // sort what's left by the most recent version (descending)
173
- .sorted ((a , b ) -> b .getVersion ().compareTo (a .getVersion ()))
174
191
.collect (Collectors .toList ());
175
192
193
+ if (this .sorted ) {
194
+ // sort what's left by the most recent version (descending)
195
+ filteredJavaHomes .sort ((a , b ) -> b .getVersion ().compareTo (a .getVersion ()));
196
+ }
197
+
176
198
// by preferred distribution?
177
199
if (this .preferredDistributions != null ) {
178
200
for (JavaDistribution d : this .preferredDistributions ) {
0 commit comments