1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

VersionHelper.valueOf now returns an Optional (WIP 1/3)

This helps to avoid bugs similar to #7917
This commit is contained in:
Cristian Maglie 2018-08-24 14:10:25 +02:00 committed by Cristian Maglie
parent 97f11945c6
commit 3092e03bec
2 changed files with 12 additions and 10 deletions

View File

@ -32,6 +32,7 @@ package cc.arduino.contributions;
import com.github.zafarkhaja.semver.Version;
import java.io.File;
import java.util.Optional;
public abstract class DownloadableContribution {
@ -66,10 +67,10 @@ public abstract class DownloadableContribution {
}
public String getParsedVersion() {
Version version = VersionHelper.valueOf(getVersion());
if (version == null) {
return null;
Optional<Version> version = VersionHelper.valueOf(getVersion());
if (version.isPresent()) {
return version.get().toString();
}
return version.toString();
return null;
}
}

View File

@ -29,13 +29,15 @@
package cc.arduino.contributions;
import java.util.Optional;
import com.github.zafarkhaja.semver.Version;
public class VersionHelper {
public static Version valueOf(String ver) {
public static Optional<Version> valueOf(String ver) {
if (ver == null) {
return null;
return Optional.empty();
}
try {
// Allow x.y-something, assuming x.y.0-something
@ -49,7 +51,7 @@ public class VersionHelper {
}
String[] parts = version.split("\\.");
if (parts.length >= 3) {
return Version.valueOf(ver);
return Optional.of(Version.valueOf(ver));
}
if (parts.length == 2) {
version += ".0";
@ -57,10 +59,9 @@ public class VersionHelper {
if (parts.length == 1) {
version += ".0.0";
}
return Version.valueOf(version + extra);
return Optional.of(Version.valueOf(version + extra));
} catch (Exception e) {
System.err.println("Invalid version found: " + ver);
return null;
return Optional.empty();
}
}