Skip to content

Commit 36be536

Browse files
author
jantje
committed
Moved version compare methods to its own class
1 parent 28ecda7 commit 36be536

File tree

5 files changed

+70
-63
lines changed

5 files changed

+70
-63
lines changed

io.sloeber.core/src/io/sloeber/core/managers/LibraryIndex.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.regex.Pattern;
1212

1313
import io.sloeber.core.api.Defaults;
14+
import io.sloeber.core.tools.Version;
1415

1516
public class LibraryIndex {
1617
private String indexName;
@@ -40,7 +41,7 @@ public void resolve() {
4041

4142
Library current = this.latestLibs.get(name);
4243
if (current != null) {
43-
if (Manager.compareVersions(library.getVersion(), current.getVersion()) > 0) {
44+
if (Version.compare(library.getVersion(), current.getVersion()) > 0) {
4445
this.latestLibs.put(name, library);
4546
}
4647
} else {

io.sloeber.core/src/io/sloeber/core/managers/Manager.java

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static public List<LibraryIndex> getLibraryIndices() {
281281
}
282282
return libraryIndices;
283283
}
284-
284+
285285
static public LibraryIndex getLibraryIndex(String name) {
286286
for (LibraryIndex index : getLibraryIndices()) {
287287
if (index.getName().equals(name)) {
@@ -745,60 +745,6 @@ private static void copyStreamToFile(InputStream in, long size, File outputFile)
745745
}
746746
}
747747

748-
/**
749-
* compares 2 strings as if they are version numbers if version1<version2
750-
* returns -1 if version1==version2(also if both are null) returns 0 else
751-
* return 1 This method caters for the null case
752-
*
753-
* @param version1
754-
* @param version2
755-
* @return
756-
*/
757-
public static int compareVersions(String version1, String version2) {
758-
if (version1 == null) {
759-
return version2 == null ? 0 : -1;
760-
}
761-
762-
if (version2 == null) {
763-
return 1;
764-
}
765-
766-
String[] v1 = version1.split("\\."); //$NON-NLS-1$
767-
String[] v2 = version2.split("\\."); //$NON-NLS-1$
768-
for (int i = 0; i < Math.max(v1.length, v2.length); ++i) {
769-
if (v1.length <= i) {
770-
return v2.length < i ? 0 : -1;
771-
}
772-
773-
if (v2.length <= i) {
774-
return 1;
775-
}
776-
777-
try {
778-
int vi1 = Integer.parseInt(v1[i]);
779-
int vi2 = Integer.parseInt(v2[i]);
780-
if (vi1 < vi2) {
781-
return -1;
782-
}
783-
784-
if (vi1 > vi2) {
785-
return 1;
786-
}
787-
} catch (@SuppressWarnings("unused") NumberFormatException e) {
788-
// not numbers, do string compares
789-
int c = v1[i].compareTo(v2[i]);
790-
if (c < 0) {
791-
return -1;
792-
}
793-
if (c > 0) {
794-
return 1;
795-
}
796-
}
797-
}
798-
799-
return 0;
800-
}
801-
802748
/**
803749
* This method removes the json files from disk and removes memory
804750
* references to these files or their content

io.sloeber.core/src/io/sloeber/core/managers/Package.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.util.List;
1414
import java.util.Map;
1515

16+
import io.sloeber.core.tools.Version;
17+
1618
public class Package implements Comparable<Package> {
1719

1820
private String name;
@@ -51,7 +53,7 @@ public String getWebsiteURL() {
5153
public String getEmail() {
5254
return this.email;
5355
}
54-
56+
5557
public Help getHelp() {
5658
return this.help;
5759
}
@@ -69,7 +71,7 @@ public Collection<ArduinoPlatform> getLatestPlatforms() {
6971
Map<String, ArduinoPlatform> platformMap = new HashMap<>();
7072
for (ArduinoPlatform platform : this.platforms) {
7173
ArduinoPlatform p = platformMap.get(platform.getName());
72-
if (p == null || Manager.compareVersions(platform.getVersion(), p.getVersion()) > 0) {
74+
if (p == null || Version.compare(platform.getVersion(), p.getVersion()) > 0) {
7375
platformMap.put(platform.getName(), platform);
7476
}
7577
}
@@ -89,7 +91,7 @@ public Collection<ArduinoPlatform> getInstalledPlatforms() {
8991
for (ArduinoPlatform platform : this.platforms) {
9092
if (platform.isInstalled()) {
9193
ArduinoPlatform p = platformMap.get(platform.getName());
92-
if (p == null || Manager.compareVersions(platform.getVersion(), p.getVersion()) > 0) {
94+
if (p == null || Version.compare(platform.getVersion(), p.getVersion()) > 0) {
9395
platformMap.put(platform.getName(), platform);
9496
}
9597
}
@@ -104,7 +106,7 @@ public ArduinoPlatform getLatestPlatform(String platformName) {
104106
if (foundPlatform == null) {
105107
foundPlatform = platform;
106108
} else {
107-
if (Manager.compareVersions(platform.getVersion(), foundPlatform.getVersion()) > 0) {
109+
if (Version.compare(platform.getVersion(), foundPlatform.getVersion()) > 0) {
108110
foundPlatform = platform;
109111
}
110112
}
@@ -117,7 +119,7 @@ public ArduinoPlatform getPlatform(String platformName, String version) {
117119

118120
for (ArduinoPlatform platform : this.platforms) {
119121
if (platform.getName().equals(platformName)) {
120-
if (Manager.compareVersions(platform.getVersion(), version) == 0) {
122+
if (Version.compare(platform.getVersion(), version) == 0) {
121123
return platform;
122124
}
123125
}
@@ -142,7 +144,7 @@ public Tool getLatestTool(String toolName) {
142144
Tool latestTool = null;
143145
for (Tool tool : this.tools) {
144146
if (tool.getName().equals(toolName)) {
145-
if (latestTool == null || Manager.compareVersions(tool.getVersion(), latestTool.getVersion()) > 0) {
147+
if (latestTool == null || Version.compare(tool.getVersion(), latestTool.getVersion()) > 0) {
146148
latestTool = tool;
147149
}
148150
}

io.sloeber.core/src/io/sloeber/core/tools/Helpers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ private static void setTheEnvironmentVariablesAddThePlatformInfo(IContributedEnv
706706
addPlatformFileTools(curPlatform, contribEnv, confDesc);
707707
if (curPlatform.isInstalled() && "avr".equalsIgnoreCase(curPlatform.getArchitecture())
708708
&& "arduino".equalsIgnoreCase(curPlatform.getPackage().getMaintainer())) {
709-
if (Manager.compareVersions(curPlatform.getVersion(), curversion) > 0) {
709+
if (Version.compare(curPlatform.getVersion(), curversion) > 0) {
710710
curversion = curPlatform.getVersion();
711711
platform = curPlatform;
712712
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.sloeber.core.tools;
2+
3+
public class Version {
4+
/**
5+
* compares 2 strings as if they are version numbers if version1<version2
6+
* returns -1 if version1==version2(also if both are null) returns 0 else
7+
* return 1 This method caters for the null case
8+
*
9+
* @param version1
10+
* @param version2
11+
* @return
12+
*/
13+
public static int compare(String version1, String version2) {
14+
if (version1 == null) {
15+
return version2 == null ? 0 : -1;
16+
}
17+
18+
if (version2 == null) {
19+
return 1;
20+
}
21+
22+
String[] v1 = version1.split("\\."); //$NON-NLS-1$
23+
String[] v2 = version2.split("\\."); //$NON-NLS-1$
24+
for (int i = 0; i < Math.max(v1.length, v2.length); ++i) {
25+
if (v1.length <= i) {
26+
return v2.length < i ? 0 : -1;
27+
}
28+
29+
if (v2.length <= i) {
30+
return 1;
31+
}
32+
33+
try {
34+
int vi1 = Integer.parseInt(v1[i]);
35+
int vi2 = Integer.parseInt(v2[i]);
36+
if (vi1 < vi2) {
37+
return -1;
38+
}
39+
40+
if (vi1 > vi2) {
41+
return 1;
42+
}
43+
} catch (NumberFormatException e) {
44+
// not numbers, do string compares
45+
int c = v1[i].compareTo(v2[i]);
46+
if (c < 0) {
47+
return -1;
48+
}
49+
if (c > 0) {
50+
return 1;
51+
}
52+
}
53+
}
54+
55+
return 0;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)