Skip to content

Instantly share code, notes, and snippets.

@robbdimitrov
Created November 22, 2021 09:45
Show Gist options
  • Save robbdimitrov/1ab1c5a0bb685ccbdf253a2d99e95266 to your computer and use it in GitHub Desktop.
Save robbdimitrov/1ab1c5a0bb685ccbdf253a2d99e95266 to your computer and use it in GitHub Desktop.
Mobile platform and version discovery
package com.robbdimitrov.utils;
import java.util.regex.Pattern;
public class Platform {
public static boolean isIOS(String systemName) {
Pattern pattern = Pattern.compile("^(ios|iphone)", Pattern.CASE_INSENSITIVE);
return pattern.matcher(systemName).find();
}
public static boolean isAndroid(String systemName) {
Pattern pattern = Pattern.compile("^android", Pattern.CASE_INSENSITIVE);
return pattern.matcher(systemName).find();
}
}
package com.robbdimitrov.utils;
import com.robbdimitrov.utils.Platform;
import org.junit.Test;
import static org.junit.Assert.*;
public class PlatformTest {
@Test
public void testIsIOS() {
assertTrue(Platform.isIOS("ios"));
assertTrue(Platform.isIOS("iPhone 10.0"));
assertTrue(Platform.isIOS("iphone 10.0"));
assertTrue(Platform.isIOS("iOS 14"));
assertFalse(Platform.isIOS("Nintendo 10.0"));
assertFalse(Platform.isIOS("droid 10.0"));
assertFalse(Platform.isIOS(""));
}
@Test
public void testIsAndroid() {
assertTrue(Platform.isAndroid("Android"));
assertTrue(Platform.isAndroid("Android 10.0"));
assertTrue(Platform.isAndroid("android 10.0"));
assertFalse(Platform.isAndroid("Nintendo 10.0"));
assertFalse(Platform.isAndroid("droid 10.0"));
assertFalse(Platform.isAndroid(""));
}
}
package com.robbdimitrov.utils;
import static java.lang.Integer.min;
public class Version {
public static boolean matches(String current, String required) {
String sign = required.substring(0, 1);
String[] currentParts = current.split("\\.");
String[] requiredParts = required.substring(2).split("\\.");
if (sign.equals(">")) {
return greaterThan(currentParts, requiredParts);
} else if (sign.equals("<")) {
return lessThan(currentParts, requiredParts);
}
return equals(currentParts, requiredParts);
}
private static boolean equals(String[] leftParts, String[] rightParts) {
if (leftParts.length != rightParts.length) {
return false;
}
for (int i = 0; i < leftParts.length; i++) {
int left = Integer.parseInt(leftParts[i]);
int right = Integer.parseInt(rightParts[i]);
if (left != right) {
return false;
}
}
return true;
}
private static boolean greaterThan(String[] leftParts, String[] rightParts) {
int partsCount = min(leftParts.length, rightParts.length);
for (int i = 0; i < partsCount; i++) {
int left = Integer.parseInt(leftParts[i]);
int right = Integer.parseInt(rightParts[i]);
if (left == right) {
continue;
}
return left > right;
}
return false;
}
private static boolean lessThan(String[] leftParts, String[] rightParts) {
int partsCount = min(leftParts.length, rightParts.length);
for (int i = 0; i < partsCount; i++) {
int left = Integer.parseInt(leftParts[i]);
int right = Integer.parseInt(rightParts[i]);
if (left == right) {
continue;
}
return left < right;
}
return false;
}
}
package com.robbdimitrov.utils;
import com.robbdimitrov.utils.Version;
import org.junit.Test;
import static org.junit.Assert.*;
public class VersionTest {
@Test
public void testEquals() {
assertTrue(Version.matches("10.0.0", "= 10.0.0"));
assertTrue(Version.matches("10.2", "= 10.2"));
assertFalse(Version.matches("10.0.0", "= 9.0.0"));
assertFalse(Version.matches("10.2", "= 10.2.2"));
}
@Test
public void testGreaterThan() {
assertTrue(Version.matches("10.1.0", "> 10.0.0"));
assertTrue(Version.matches("11.0.0", "> 10.5.2"));
assertTrue(Version.matches("10.5.3", "> 10.2"));
assertFalse(Version.matches("9.0.0", "> 9.0.0"));
assertFalse(Version.matches("8.9.1", "> 9.0.0"));
assertFalse(Version.matches("1.0", "> 10.2.2"));
}
@Test
public void testLessThan() {
assertTrue(Version.matches("10.0.0", "< 10.1.0"));
assertTrue(Version.matches("10.5.2", "< 11.0.0"));
assertTrue(Version.matches("10.2", "< 10.5.3"));
assertFalse(Version.matches("9.0.0", "< 9.0.0"));
assertFalse(Version.matches("9.0.0", "< 8.9.1"));
assertFalse(Version.matches("10.2.2", "< 1.0"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment