From 86217a4fb4febbde10f56de4c77371e5c4b47315 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 24 Aug 2018 14:15:02 +0200 Subject: [PATCH] Slightly refactored VersionComparator (WIP 2/3) --- .../contributions/VersionComparator.java | 48 ++++++++----------- .../processing/app/packages/UserLibrary.java | 22 +++++---- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/arduino-core/src/cc/arduino/contributions/VersionComparator.java b/arduino-core/src/cc/arduino/contributions/VersionComparator.java index ccdca9940..ba0ebb639 100644 --- a/arduino-core/src/cc/arduino/contributions/VersionComparator.java +++ b/arduino-core/src/cc/arduino/contributions/VersionComparator.java @@ -34,41 +34,32 @@ import com.github.zafarkhaja.semver.Version; import cc.arduino.contributions.libraries.ContributedLibrary; import java.util.Comparator; +import java.util.Optional; public class VersionComparator implements Comparator { + public static int compareTo(String a, String b) { + Optional versionA = VersionHelper.valueOf(a); + Optional versionB = VersionHelper.valueOf(b); + if (versionA.isPresent() && versionB.isPresent()) { + return versionA.get().compareTo(versionB.get()); + } + if (versionA.isPresent()) { + return 1; + } + if (versionB.isPresent()) { + return -1; + } + return 0; + } + @Override public int compare(String a, String b) { - // null is always less than any other value - if (a == null && b == null) - return 0; - if (a == null) - return -1; - if (b == null) - return 1; - - Version versionA = VersionHelper.valueOf(a); - Version versionB = VersionHelper.valueOf(b); - - return versionA.compareTo(versionB); + return compareTo(a, b); } public static boolean greaterThan(String a, String b) { - // null is always less than any other value - if (a == null && b == null) { - return false; - } - if (a == null) { - return false; - } - if (b == null) { - return true; - } - - Version versionA = VersionHelper.valueOf(a); - Version versionB = VersionHelper.valueOf(b); - - return versionA.greaterThan(versionB); + return compareTo(a, b) > 0; } public static String max(String a, String b) { @@ -79,8 +70,7 @@ public class VersionComparator implements Comparator { return greaterThan(a, b) ? a : b; } - public static boolean greaterThan(ContributedLibrary a, - ContributedLibrary b) { + public static boolean greaterThan(ContributedLibrary a, ContributedLibrary b) { return greaterThan(a.getParsedVersion(), b.getParsedVersion()); } } diff --git a/arduino-core/src/processing/app/packages/UserLibrary.java b/arduino-core/src/processing/app/packages/UserLibrary.java index ee9c0bd03..f70baafac 100644 --- a/arduino-core/src/processing/app/packages/UserLibrary.java +++ b/arduino-core/src/processing/app/packages/UserLibrary.java @@ -28,12 +28,6 @@ */ package processing.app.packages; -import cc.arduino.Constants; -import cc.arduino.contributions.VersionHelper; -import cc.arduino.contributions.libraries.ContributedLibraryReference; -import processing.app.helpers.PreferencesMap; -import processing.app.packages.UserLibraryFolder.Location; - import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -41,9 +35,17 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import java.util.Optional; import com.github.zafarkhaja.semver.Version; +import cc.arduino.Constants; +import cc.arduino.contributions.VersionHelper; +import cc.arduino.contributions.libraries.ContributedLibraryReference; +import processing.app.I18n; +import processing.app.helpers.PreferencesMap; +import processing.app.packages.UserLibraryFolder.Location; + public class UserLibrary { private String name; @@ -148,12 +150,16 @@ public class UserLibrary { } String declaredVersion = properties.get("version").trim(); - Version version = VersionHelper.valueOf(declaredVersion); + Optional version = VersionHelper.valueOf(declaredVersion); + if (!version.isPresent()) { + System.err.println( + I18n.format("Invalid version '{0}' for library in: {1}", declaredVersion, libFolder.getAbsolutePath())); + } UserLibrary res = new UserLibrary(); res.installedFolder = libFolder; res.name = properties.get("name").trim(); - res.version = version.toString(); + res.version = version.isPresent() ? version.get().toString() : declaredVersion; res.author = properties.get("author").trim(); res.maintainer = properties.get("maintainer").trim(); res.sentence = properties.get("sentence").trim();