1 package org.robolectric.util; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Scanner; 6 7 public class JavaVersion implements Comparable<JavaVersion> { 8 private final List<Integer> versions; 9 JavaVersion(String version)10 public JavaVersion(String version) { 11 versions = new ArrayList<>(); 12 Scanner s = new Scanner(version).useDelimiter("[^\\d]+"); 13 while (s.hasNext()) { 14 versions.add(s.nextInt()); 15 } 16 } 17 compareTo(JavaVersion o)18 @Override public int compareTo(JavaVersion o) { 19 List<Integer> versions2 = o.versions; 20 int max = Math.min(versions.size(), versions2.size()); 21 for (int i = 0; i < max; i++) { 22 int compare = versions.get(i).compareTo(versions2.get(i)); 23 if (compare != 0) { 24 return compare; 25 } 26 } 27 28 // Assume longer is newer 29 return Integer.compare(versions.size(), versions2.size()); 30 } 31 } 32