From 3092e03becc932073566cb82f4d9bb7cdf3ce13d Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 24 Aug 2018 14:10:25 +0200 Subject: [PATCH] VersionHelper.valueOf now returns an Optional (WIP 1/3) This helps to avoid bugs similar to #7917 --- .../contributions/DownloadableContribution.java | 9 +++++---- .../src/cc/arduino/contributions/VersionHelper.java | 13 +++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/arduino-core/src/cc/arduino/contributions/DownloadableContribution.java b/arduino-core/src/cc/arduino/contributions/DownloadableContribution.java index 531ad5b3d..87ef9cee4 100644 --- a/arduino-core/src/cc/arduino/contributions/DownloadableContribution.java +++ b/arduino-core/src/cc/arduino/contributions/DownloadableContribution.java @@ -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 = VersionHelper.valueOf(getVersion()); + if (version.isPresent()) { + return version.get().toString(); } - return version.toString(); + return null; } } diff --git a/arduino-core/src/cc/arduino/contributions/VersionHelper.java b/arduino-core/src/cc/arduino/contributions/VersionHelper.java index 0f108aed5..caf98c8f1 100644 --- a/arduino-core/src/cc/arduino/contributions/VersionHelper.java +++ b/arduino-core/src/cc/arduino/contributions/VersionHelper.java @@ -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 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(); } }