Skip to content

Instantly share code, notes, and snippets.

@Karewan
Last active October 22, 2022 20:08
Show Gist options
  • Save Karewan/fab7eb059162626be6cb0923c1d8903e to your computer and use it in GitHub Desktop.
Save Karewan/fab7eb059162626be6cb0923c1d8903e to your computer and use it in GitHub Desktop.
PHP version_compare ported to Java and Android
import androidx.annotation.NonNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Versions {
/**
* Compare two versions
* @param v1 Version 1
* @param v2 Version 2
* @param operator Comparison operator (gt, ge, le, eq, ne, lt)
* @return boolean
*/
public static boolean compare(@NonNull String v1, @NonNull String v2, @NonNull String operator) {
int compare = 0;
List<String> L1 = _prepVersion(v1);
List<String> L2 = _prepVersion(v2);
for(int y = L1.size(); y < L2.size(); y++) L1.add("0");
for(int x = L2.size(); x < L1.size(); x++) L2.add("0");
for(int i = 0; i < L1.size(); i++) {
if(L1.get(i).equalsIgnoreCase(L2.get(i))) continue;
int i1 = _numVersion(L1.get(i));
int i2 = _numVersion(L2.get(i));
if(i1 < i2) {
compare = -1;
break;
} else if(i1 > i2) {
compare = 1;
break;
}
}
switch(operator) {
case "gt":
return compare > 0;
case "ge":
return compare >= 0;
case "le":
return compare <= 0;
case "eq":
return compare == 0;
case "ne":
return compare != 0;
case "lt":
return compare < 0;
default:
return false;
}
}
@NonNull
private static List<String> _prepVersion(@NonNull String v) {
v = v.trim();
if(v.startsWith("v") || v.startsWith("V")) v = v.substring(1);
v = v.replaceAll("[_\\-+]", ".")
.replaceAll("([^.\\d]+)", ".$1.")
.replaceAll("\\.{2,}", ".")
.replaceAll("\\.+$", "");
return new LinkedList<>(Arrays.asList(v.length() == 0 ? new String[] {"-8"} : v.split("\\.")));
}
private static final Map<String, Integer> sVM = new HashMap<String, Integer>() {{
put("dev", -6);
put("alpha", -5);
put("a", -5);
put("beta", -4);
put("b", -4);
put("RC", -3);
put("rc", -3);
put("#", -2);
put("p", 1);
put("pl", 1);
}};
@SuppressWarnings({"ConstantConditions"})
private static int _numVersion(@NonNull String v) {
if(v.matches("\\d+")) {
try {
return Integer.parseInt(v);
} catch (NumberFormatException e) {
return -7;
}
} else if(sVM.containsKey(v)) {
return sVM.get(v);
}
return -7;
}
}
import com.sun.istack.internal.NotNull;
import java.util.*;
public final class Versions {
/**
* Compare two versions
* @param v1 Version 1
* @param v2 Version 2
* @param operator Comparison operator (gt, ge, le, eq, ne, lt)
* @return boolean
*/
public static boolean compare(@NotNull String v1, @NotNull String v2, @NotNull String operator) {
int compare = 0;
List<String> L1 = _prepVersion(v1);
List<String> L2 = _prepVersion(v2);
for(int y = L1.size(); y < L2.size(); y++) L1.add("0");
for(int x = L2.size(); x < L1.size(); x++) L2.add("0");
for(int i = 0; i < L1.size(); i++) {
if(L1.get(i).equalsIgnoreCase(L2.get(i))) continue;
int i1 = _numVersion(L1.get(i));
int i2 = _numVersion(L2.get(i));
if(i1 < i2) {
compare = -1;
break;
} else if(i1 > i2) {
compare = 1;
break;
}
}
switch(operator) {
case "gt":
return compare > 0;
case "ge":
return compare >= 0;
case "le":
return compare <= 0;
case "eq":
return compare == 0;
case "ne":
return compare != 0;
case "lt":
return compare < 0;
default:
return false;
}
}
@NotNull
private static List<String> _prepVersion(@NotNull String v) {
v = v.trim();
if(v.startsWith("v") || v.startsWith("V")) v = v.substring(1);
v = v.replaceAll("[_\\-+]", ".")
.replaceAll("([^.\\d]+)", ".$1.")
.replaceAll("\\.{2,}", ".")
.replaceAll("\\.+$", "");
return new LinkedList<>(Arrays.asList(v.length() == 0 ? new String[] {"-8"} : v.split("\\.")));
}
private static final Map<String, Integer> sVM = new HashMap<String, Integer>() {{
put("dev", -6);
put("alpha", -5);
put("a", -5);
put("beta", -4);
put("b", -4);
put("RC", -3);
put("rc", -3);
put("#", -2);
put("p", 1);
put("pl", 1);
}};
private static int _numVersion(@NotNull String v) {
if(v.chars().allMatch(Character::isDigit)) {
try {
return Integer.parseInt(v);
} catch (NumberFormatException e) {
return -7;
}
} else {
return sVM.getOrDefault(v, -7);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment