Skip to content

Commit 86217a4

Browse files
committed
Slightly refactored VersionComparator (WIP 2/3)
1 parent 3092e03 commit 86217a4

File tree

2 files changed

+33
-37
lines changed

2 files changed

+33
-37
lines changed

Diff for: arduino-core/src/cc/arduino/contributions/VersionComparator.java

+19-29
Original file line numberDiff line numberDiff line change
@@ -34,41 +34,32 @@
3434
import cc.arduino.contributions.libraries.ContributedLibrary;
3535

3636
import java.util.Comparator;
37+
import java.util.Optional;
3738

3839
public class VersionComparator implements Comparator<String> {
3940

40-
@Override
41-
public int compare(String a, String b) {
42-
// null is always less than any other value
43-
if (a == null && b == null)
44-
return 0;
45-
if (a == null)
46-
return -1;
47-
if (b == null)
48-
return 1;
49-
50-
Version versionA = VersionHelper.valueOf(a);
51-
Version versionB = VersionHelper.valueOf(b);
52-
53-
return versionA.compareTo(versionB);
54-
}
55-
56-
public static boolean greaterThan(String a, String b) {
57-
// null is always less than any other value
58-
if (a == null && b == null) {
59-
return false;
41+
public static int compareTo(String a, String b) {
42+
Optional<Version> versionA = VersionHelper.valueOf(a);
43+
Optional<Version> versionB = VersionHelper.valueOf(b);
44+
if (versionA.isPresent() && versionB.isPresent()) {
45+
return versionA.get().compareTo(versionB.get());
6046
}
61-
if (a == null) {
62-
return false;
47+
if (versionA.isPresent()) {
48+
return 1;
6349
}
64-
if (b == null) {
65-
return true;
50+
if (versionB.isPresent()) {
51+
return -1;
6652
}
53+
return 0;
54+
}
6755

68-
Version versionA = VersionHelper.valueOf(a);
69-
Version versionB = VersionHelper.valueOf(b);
56+
@Override
57+
public int compare(String a, String b) {
58+
return compareTo(a, b);
59+
}
7060

71-
return versionA.greaterThan(versionB);
61+
public static boolean greaterThan(String a, String b) {
62+
return compareTo(a, b) > 0;
7263
}
7364

7465
public static String max(String a, String b) {
@@ -79,8 +70,7 @@ public static ContributedLibrary max(ContributedLibrary a, ContributedLibrary b)
7970
return greaterThan(a, b) ? a : b;
8071
}
8172

82-
public static boolean greaterThan(ContributedLibrary a,
83-
ContributedLibrary b) {
73+
public static boolean greaterThan(ContributedLibrary a, ContributedLibrary b) {
8474
return greaterThan(a.getParsedVersion(), b.getParsedVersion());
8575
}
8676
}

Diff for: arduino-core/src/processing/app/packages/UserLibrary.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,24 @@
2828
*/
2929
package processing.app.packages;
3030

31-
import cc.arduino.Constants;
32-
import cc.arduino.contributions.VersionHelper;
33-
import cc.arduino.contributions.libraries.ContributedLibraryReference;
34-
import processing.app.helpers.PreferencesMap;
35-
import processing.app.packages.UserLibraryFolder.Location;
36-
3731
import java.io.File;
3832
import java.io.IOException;
3933
import java.nio.file.Files;
4034
import java.nio.file.Paths;
4135
import java.util.ArrayList;
4236
import java.util.LinkedList;
4337
import java.util.List;
38+
import java.util.Optional;
4439

4540
import com.github.zafarkhaja.semver.Version;
4641

42+
import cc.arduino.Constants;
43+
import cc.arduino.contributions.VersionHelper;
44+
import cc.arduino.contributions.libraries.ContributedLibraryReference;
45+
import processing.app.I18n;
46+
import processing.app.helpers.PreferencesMap;
47+
import processing.app.packages.UserLibraryFolder.Location;
48+
4749
public class UserLibrary {
4850

4951
private String name;
@@ -148,12 +150,16 @@ public static UserLibrary create(UserLibraryFolder libFolderDesc) throws IOExcep
148150
}
149151

150152
String declaredVersion = properties.get("version").trim();
151-
Version version = VersionHelper.valueOf(declaredVersion);
153+
Optional<Version> version = VersionHelper.valueOf(declaredVersion);
154+
if (!version.isPresent()) {
155+
System.err.println(
156+
I18n.format("Invalid version '{0}' for library in: {1}", declaredVersion, libFolder.getAbsolutePath()));
157+
}
152158

153159
UserLibrary res = new UserLibrary();
154160
res.installedFolder = libFolder;
155161
res.name = properties.get("name").trim();
156-
res.version = version.toString();
162+
res.version = version.isPresent() ? version.get().toString() : declaredVersion;
157163
res.author = properties.get("author").trim();
158164
res.maintainer = properties.get("maintainer").trim();
159165
res.sentence = properties.get("sentence").trim();

0 commit comments

Comments
 (0)