mirror of
https://github.com/arduino/Arduino.git
synced 2024-11-29 10:24:12 +01:00
Slightly refactored VersionComparator (WIP 2/3)
This commit is contained in:
parent
3092e03bec
commit
86217a4fb4
@ -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<String> {
|
||||
|
||||
public static int compareTo(String a, String b) {
|
||||
Optional<Version> versionA = VersionHelper.valueOf(a);
|
||||
Optional<Version> 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<String> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -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> 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();
|
||||
|
Loading…
Reference in New Issue
Block a user