MessageVersion implements Comparable<MessageVersion> { @Override public int compareTo(MessageVersion that) { if (that == null) { return 1; } Integer[] thisParts = getVersionParts(); Integer[] thatParts = that.getVersionParts(); int length = Math.max(thisParts.length, thatParts.length); for (int i = 0; i < length; i++) { int comp = getVersionPart(thisParts, i) - getVersionPart(thatParts, i); if (comp != 0) { return comp; } } return 0; } MessageVersion(String version); @Override int compareTo(MessageVersion that); @Override boolean equals(Object that); @Override int hashCode(); @Override String toString(); static final MessageVersion NO_VERSION; }
@Test public void testCompareTo() throws Exception { MessageVersion version1 = new MessageVersion("1.0.0"); MessageVersion version2 = new MessageVersion("1.0.0"); MessageVersion version3 = new MessageVersion("2.0.0"); MessageVersion version4 = new MessageVersion("1"); MessageVersion version5 = new MessageVersion("1.5"); MessageVersion version6 = new MessageVersion("1.0.5"); assertTrue(version1.compareTo(version2) == 0); assertTrue(version2.compareTo(version1) == 0); assertTrue(version1.compareTo(version3) < 0); assertTrue(version3.compareTo(version1) > 0); assertTrue(version1.compareTo(version4) == 0); assertTrue(version4.compareTo(version1) == 0); assertTrue(version1.compareTo(version5) < 0); assertTrue(version5.compareTo(version1) > 0); assertTrue(version1.compareTo(version6) < 0); assertTrue(version6.compareTo(version1) > 0); }