@Test public void testCompare() { assertEquals(0, new Version("1.0").compareTo("1.0")); assertEquals(0, new Version("1.0.1").compareTo("1.0.1")); assertTrue(new Version("1.0.1").compareTo("1.0.2") < 0); assertTrue(new Version("1.0.2").compareTo("1.0.1") > 0); assertTrue(new Version("1.0.1.1").compareTo("1.0.1") > 0); }
public int compareTo(Object o) { if (o instanceof CharSequence) { o = new Version(o.toString()); } else if (!(o instanceof Version)) { throw new IllegalArgumentException( "Can only compare to Versions and Strings, got: " + o); } int[] otherVersion = ((Version) o).versionParts; int len = Math.min(otherVersion.length, versionParts.length); for (int i = 0; i < len; i++) { int delta = versionParts[i] - otherVersion[i]; if (delta != 0) { return delta; } } if (versionParts.length < otherVersion.length) { return -1; } else if (versionParts.length > otherVersion.length) { return 1; } else { return 0; } }
Version implements Comparable<Object> { public int compareTo(Object o) { if (o instanceof CharSequence) { o = new Version(o.toString()); } else if (!(o instanceof Version)) { throw new IllegalArgumentException( "Can only compare to Versions and Strings, got: " + o); } int[] otherVersion = ((Version) o).versionParts; int len = Math.min(otherVersion.length, versionParts.length); for (int i = 0; i < len; i++) { int delta = versionParts[i] - otherVersion[i]; if (delta != 0) { return delta; } } if (versionParts.length < otherVersion.length) { return -1; } else if (versionParts.length > otherVersion.length) { return 1; } else { return 0; } } }
Version implements Comparable<Object> { public int compareTo(Object o) { if (o instanceof CharSequence) { o = new Version(o.toString()); } else if (!(o instanceof Version)) { throw new IllegalArgumentException( "Can only compare to Versions and Strings, got: " + o); } int[] otherVersion = ((Version) o).versionParts; int len = Math.min(otherVersion.length, versionParts.length); for (int i = 0; i < len; i++) { int delta = versionParts[i] - otherVersion[i]; if (delta != 0) { return delta; } } if (versionParts.length < otherVersion.length) { return -1; } else if (versionParts.length > otherVersion.length) { return 1; } else { return 0; } } Version(String versionString); }
Version implements Comparable<Object> { public int compareTo(Object o) { if (o instanceof CharSequence) { o = new Version(o.toString()); } else if (!(o instanceof Version)) { throw new IllegalArgumentException( "Can only compare to Versions and Strings, got: " + o); } int[] otherVersion = ((Version) o).versionParts; int len = Math.min(otherVersion.length, versionParts.length); for (int i = 0; i < len; i++) { int delta = versionParts[i] - otherVersion[i]; if (delta != 0) { return delta; } } if (versionParts.length < otherVersion.length) { return -1; } else if (versionParts.length > otherVersion.length) { return 1; } else { return 0; } } Version(String versionString); int compareTo(Object o); String getVersionString(); }
Version implements Comparable<Object> { public int compareTo(Object o) { if (o instanceof CharSequence) { o = new Version(o.toString()); } else if (!(o instanceof Version)) { throw new IllegalArgumentException( "Can only compare to Versions and Strings, got: " + o); } int[] otherVersion = ((Version) o).versionParts; int len = Math.min(otherVersion.length, versionParts.length); for (int i = 0; i < len; i++) { int delta = versionParts[i] - otherVersion[i]; if (delta != 0) { return delta; } } if (versionParts.length < otherVersion.length) { return -1; } else if (versionParts.length > otherVersion.length) { return 1; } else { return 0; } } Version(String versionString); int compareTo(Object o); String getVersionString();  }