JavaPlatform[] platforms) {
ExceptionHelper.checkNotNullArgument(specName, "specName");
ExceptionHelper.checkNotNullArgument(versionStr, "versionStr");
SpecificationVersion version;
try {
version = new SpecificationVersion(versionStr);
} catch (NumberFormatException ex) {
LOGGER.log(Level.INFO, "Invalid platform version: " + versionStr, ex);
return JavaPlatform.getDefault();
}
for (JavaPlatform platform: platforms) {
Specification specification = platform.getSpecification();
if (specName.equalsIgnoreCase(specification.getName())
&& version.equals(specification.getVersion())) {
return platform;
}
}
// We could not find an exact match, so try to find the best match:
//
// 1. If there is at least one platform with a version higher than
// requested, choose the one with the lowest version which is still
// higher than the requested (the closest version to the requested
// which is above the requested version).
//
// 2. In case every platform is below the requested, choose the one
// with the highest version number.
JavaPlatform bestMatch = null;
for (JavaPlatform platform: platforms) {
Specification platformSpecification = platform.getSpecification();
if (platformSpecification == null) {
continue;
}
if (!specName.equalsIgnoreCase(platformSpecification.getName())) {
continue;
}
SpecificationVersion thisVersion = platformSpecification.getVersion();
if (thisVersion == null) {
continue;
}
if (bestMatch == null) {
bestMatch = platform;
}
else {
SpecificationVersion bestVersion = bestMatch.getSpecification().getVersion();
// required version is greater than the one we currently have
if (version.compareTo(bestVersion) > 0) {
// Replace if this platform has a greater version number
if (bestVersion.compareTo(thisVersion) < 0) {
bestMatch = platform;
}
}
else {
// Replace if this platform is still above the requirement
// but is below the one we currently have.
if (version.compareTo(thisVersion) < 0
&& thisVersion.compareTo(bestVersion) < 0) {
bestMatch = platform;
}
}
}
}
if (bestMatch == null) {
return JavaPlatform.getDefault();
}
SpecificationVersion bestMatchVersion = bestMatch.getSpecification().getVersion();
String higherOrLower = version.compareTo(bestMatchVersion) < 0
? "higher"
: "lower";