Skip to content

Commit d285b7f

Browse files
committedMay 17, 2018
Improved VersionComparator API
1 parent 27b9986 commit d285b7f

File tree

7 files changed

+21
-23
lines changed

7 files changed

+21
-23
lines changed
 

‎app/src/cc/arduino/contributions/BuiltInCoreIsNewerCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private void builtInPackageIsNewerCheck() throws InterruptedException {
8484
Thread.sleep(100);
8585
}
8686

87-
if (VersionHelper.valueOf(installedBuiltIn.getParsedVersion()).greaterThan(VersionHelper.valueOf(installedNotBuiltIn.getParsedVersion()))) {
87+
if (VersionComparator.greaterThan(installedBuiltIn.getParsedVersion(), installedNotBuiltIn.getParsedVersion())) {
8888
SwingUtilities.invokeLater(() -> {
8989
PreferencesData.setInteger("builtin_platform_is_newer", BaseNoGui.REVISION);
9090
assert base.hasActiveEditor();

‎app/src/cc/arduino/contributions/libraries/filters/UpdatableLibraryPredicate.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@
3939

4040
public class UpdatableLibraryPredicate implements Predicate<ContributedLibrary> {
4141

42-
private final VersionComparator versionComparator;
43-
44-
public UpdatableLibraryPredicate() {
45-
this.versionComparator = new VersionComparator();
46-
}
47-
4842
@Override
4943
public boolean test(ContributedLibrary contributedLibrary) {
5044
String libraryName = contributedLibrary.getName();
@@ -54,7 +48,7 @@ public boolean test(ContributedLibrary contributedLibrary) {
5448
}
5549
List<ContributedLibrary> libraries = BaseNoGui.librariesIndexer.getIndex().find(libraryName);
5650
return libraries.stream()
57-
.filter(library -> versionComparator.greaterThan(library.getParsedVersion(), installed.getParsedVersion()))
51+
.filter(library -> VersionComparator.greaterThan(library.getParsedVersion(), installed.getParsedVersion()))
5852
.count() > 0;
5953
}
6054
}

‎app/src/cc/arduino/contributions/libraries/ui/ContributedLibraryTableCellEditor.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,9 @@ public Component getTableCellEditorComponent(JTable table, Object value,
110110
final List<ContributedLibrary> uninstalledPreviousReleases = new LinkedList<>();
111111
final List<ContributedLibrary> uninstalledNewerReleases = new LinkedList<>();
112112

113-
final VersionComparator versionComparator = new VersionComparator();
114113
uninstalledReleases.stream().forEach(input -> {
115114
if (installed == null
116-
|| versionComparator.greaterThan(installed.getParsedVersion(),
117-
input.getParsedVersion())) {
115+
|| VersionComparator.greaterThan(installed, input)) {
118116
uninstalledPreviousReleases.add(input);
119117
} else {
120118
uninstalledNewerReleases.add(input);

‎app/src/cc/arduino/contributions/packages/filters/UpdatablePlatformPredicate.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@
3838

3939
public class UpdatablePlatformPredicate implements Predicate<ContributedPlatform> {
4040

41-
private final VersionComparator versionComparator;
42-
43-
public UpdatablePlatformPredicate() {
44-
this.versionComparator = new VersionComparator();
45-
}
46-
4741
@Override
4842
public boolean test(ContributedPlatform contributedPlatform) {
4943
String packageName = contributedPlatform.getParentPackage().getName();
@@ -56,7 +50,7 @@ public boolean test(ContributedPlatform contributedPlatform) {
5650

5751
List<ContributedPlatform> platforms = BaseNoGui.indexer.getIndex().findPlatforms(packageName, architecture);
5852
return platforms.stream()
59-
.filter(platform -> versionComparator.greaterThan(platform.getParsedVersion(), installed.getParsedVersion()))
53+
.filter(platform -> VersionComparator.greaterThan(platform.getParsedVersion(), installed.getParsedVersion()))
6054
.count() > 0;
6155
}
6256
}

‎app/src/cc/arduino/contributions/packages/ui/ContributedPlatformTableCellEditor.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ public Component getTableCellEditorComponent(JTable table, Object _value,
106106
final List<ContributedPlatform> uninstalledPreviousReleases = new LinkedList<>();
107107
final List<ContributedPlatform> uninstalledNewerReleases = new LinkedList<>();
108108

109-
final VersionComparator versionComparator = new VersionComparator();
110109
uninstalledReleases.stream().forEach(input -> {
111110
if (installed == null
112-
|| versionComparator.greaterThan(installed.getParsedVersion(),
111+
|| VersionComparator.greaterThan(installed.getParsedVersion(),
113112
input.getParsedVersion())) {
114113
uninstalledPreviousReleases.add(input);
115114
} else {

‎arduino-core/src/cc/arduino/contributions/VersionComparator.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
import com.github.zafarkhaja.semver.Version;
3333

34+
import cc.arduino.contributions.libraries.ContributedLibrary;
35+
3436
import java.util.Comparator;
3537

3638
public class VersionComparator implements Comparator<String> {
@@ -51,7 +53,7 @@ public int compare(String a, String b) {
5153
return versionA.compareTo(versionB);
5254
}
5355

54-
public boolean greaterThan(String a, String b) {
56+
public static boolean greaterThan(String a, String b) {
5557
// null is always less than any other value
5658
if (a == null && b == null) {
5759
return false;
@@ -69,4 +71,16 @@ public boolean greaterThan(String a, String b) {
6971
return versionA.greaterThan(versionB);
7072
}
7173

74+
public static String max(String a, String b) {
75+
return greaterThan(a, b) ? a : b;
76+
}
77+
78+
public static ContributedLibrary max(ContributedLibrary a, ContributedLibrary b) {
79+
return greaterThan(a, b) ? a : b;
80+
}
81+
82+
public static boolean greaterThan(ContributedLibrary a,
83+
ContributedLibrary b) {
84+
return greaterThan(a.getParsedVersion(), b.getParsedVersion());
85+
}
7286
}

‎arduino-core/src/processing/app/BaseNoGui.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,6 @@ public static void createToolPreferences(Collection<ContributedTool> installedTo
699699
}
700700

701701
Map<String, String> latestVersions = new HashMap<>();
702-
VersionComparator comparator = new VersionComparator();
703702
for (ContributedTool tool : installedTools) {
704703
File installedFolder = tool.getDownloadableContribution(getPlatform()).getInstalledFolder();
705704
String toolPath;
@@ -714,7 +713,7 @@ public static void createToolPreferences(Collection<ContributedTool> installedTo
714713
PreferencesData.set(prefix + tool.getPackager() + "-" + toolName + "-" + toolVersion + ".path", toolPath);
715714
// In the generic tool property put the path of the latest version if more are available
716715
try {
717-
if (!latestVersions.containsKey(toolName) || comparator.greaterThan(toolVersion, latestVersions.get(toolName))) {
716+
if (!latestVersions.containsKey(toolName) || VersionComparator.greaterThan(toolVersion, latestVersions.get(toolName))) {
718717
latestVersions.put(toolName, toolVersion);
719718
PreferencesData.set(prefix + toolName + ".path", toolPath);
720719
}

0 commit comments

Comments
 (0)