IndexVersion { public static int compareSoftwareVersions(String firstVersion, String secondVersion) throws IOException { int GREATER_THAN = 1; int EQUAL = 0; int LESS_THAN = -1; if(firstVersion==null) return LESS_THAN; if(secondVersion==null) return GREATER_THAN; String[] firstParts = firstVersion.split("\\."); String[] secondParts = secondVersion.split("\\."); int firstMajor = firstParts.length>=1 ? Integer.parseInt(firstParts[0]) : 0; int firstMinor = firstParts.length>=2 ? Integer.parseInt(firstParts[1]) : 0; int secondMajor = secondParts.length>=1 ? Integer.parseInt(secondParts[0]) : 0; int secondMinor = secondParts.length>=2 ? Integer.parseInt(secondParts[1]) : 0; if(firstMajor==secondMajor && firstMinor==secondMinor) { return EQUAL; } else if(firstMajor > secondMajor) { return GREATER_THAN; } else if(firstMajor < secondMajor) { return LESS_THAN; } else if(firstMinor > secondMinor) { return GREATER_THAN; } else if(firstMinor < secondMinor) { return LESS_THAN; } else { return EQUAL; } }  static void main(String[] argv); static String getIndexVersion(String indexDirPath); static int compareSoftwareVersions(String firstVersion, String secondVersion); static String getLatestVersion();  }
@Test public void testCompareSoftwareVersions() throws Exception { assertEquals(IndexVersion.compareSoftwareVersions("5", "6"), -1); assertEquals(IndexVersion.compareSoftwareVersions("4.1", "6"), -1); assertEquals(IndexVersion.compareSoftwareVersions("4.1", "4.4"), -1); assertEquals(IndexVersion.compareSoftwareVersions("4.1", "4.10"), -1); assertEquals(IndexVersion.compareSoftwareVersions("4.4", "4.10"), -1); assertEquals(IndexVersion.compareSoftwareVersions("4.4", "5.1"), -1); assertEquals(IndexVersion.compareSoftwareVersions("6", "5"), 1); assertEquals(IndexVersion.compareSoftwareVersions("6.10", "6.4"), 1); assertEquals(IndexVersion.compareSoftwareVersions("6.10", "6.1"), 1); assertEquals(IndexVersion.compareSoftwareVersions("5.3", "2.4"), 1); assertEquals(IndexVersion.compareSoftwareVersions("5", "5.0"), 0); assertEquals(IndexVersion.compareSoftwareVersions("6", "6"), 0); assertEquals(IndexVersion.compareSoftwareVersions("4.2", "4.2"), 0); assertEquals(IndexVersion.compareSoftwareVersions("4.2.1", "4.2"), 0); }