Makes the version comparator more generic
This commit is contained in:
parent
85febdf41b
commit
73faa4408e
@ -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;
|
||||
}
|
||||
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user