Makes the version comparator more generic

This commit is contained in:
Kristian Knarvik 2021-08-03 12:14:59 +02:00
parent 85febdf41b
commit 73faa4408e
2 changed files with 8 additions and 9 deletions

View File

@ -87,17 +87,14 @@ public final class Updater {
public static boolean isVersionHigher(String oldVersion, String newVersion) {
String[] oldVersionParts = oldVersion.split("\\.");
String[] newVersionParts = newVersion.split("\\.");
int[] oldVersionInts = new int[]{Integer.parseInt(oldVersionParts[0]), Integer.parseInt(oldVersionParts[1]),
Integer.parseInt(oldVersionParts[2])};
int[] newVersionInts = new int[]{Integer.parseInt(newVersionParts[0]), Integer.parseInt(newVersionParts[1]),
Integer.parseInt(newVersionParts[2])};
if (newVersionInts[0] > oldVersionInts[0]) {
return true;
} else if (newVersionInts[0] == oldVersionInts[0]) {
if (newVersionInts[1] > oldVersionInts[1]) {
for (int i = 0; i < Math.max(oldVersionParts.length, newVersionParts.length); i++) {
int oldVersionNumber = oldVersionParts.length > i ? Integer.parseInt(oldVersionParts[i]) : 0;
int newVersionNumber = newVersionParts.length > i ? Integer.parseInt(newVersionParts[i]) : 0;
if (newVersionNumber > oldVersionNumber) {
return true;
} else if (newVersionNumber < oldVersionNumber) {
return false;
}
return newVersionInts[1] == oldVersionInts[1] && newVersionInts[2] > oldVersionInts[2];
}
return false;
}

View File

@ -16,6 +16,8 @@ public class UpdaterTest {
assertFalse(Updater.isVersionHigher("1.2.1", "1.1.6"));
assertTrue(Updater.isVersionHigher("1.2.1", "2.1.6"));
assertTrue(Updater.isVersionHigher("1.2.1", "2.0.0"));
assertTrue(Updater.isVersionHigher("1.2", "1.2.1"));
assertTrue(Updater.isVersionHigher("1.1.2", "1.2"));
}
}