X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.slc.client.ui.dist%2Fsrc%2Forg%2Fargeo%2Fslc%2Fclient%2Fui%2Fdist%2Futils%2FVersionComparator.java;fp=org.argeo.slc.client.ui.dist%2Fsrc%2Forg%2Fargeo%2Fslc%2Fclient%2Fui%2Fdist%2Futils%2FVersionComparator.java;h=4492b06694f81ef3042a07aeb12afffe93949e49;hb=2db415932b071525adb52c6374e021174512a924;hp=0000000000000000000000000000000000000000;hpb=7e2f6c6ae08e97925955184aaa29035ac05de149;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/utils/VersionComparator.java b/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/utils/VersionComparator.java new file mode 100644 index 000000000..4492b0669 --- /dev/null +++ b/org.argeo.slc.client.ui.dist/src/org/argeo/slc/client/ui/dist/utils/VersionComparator.java @@ -0,0 +1,68 @@ +package org.argeo.slc.client.ui.dist.utils; + +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerComparator; + +/** + * Enable comparison of two version string with form "1.2.5.qualifier" with + * following rules and assumptions: + * + */ + +public class VersionComparator extends ViewerComparator { + + @Override + public int compare(Viewer viewer, Object e1, Object e2) { + String s1 = (String) e1; + String s2 = (String) e2; + return compareVersion(s1, s2); + } + + /** + * Enable comparison of two versions of the form + * "major.minor.micro.qualifier". We assume the separator is always a "." + * and make best effort to convert major, minor and micro to int. + */ + private int compareVersion(String v1, String v2) { + String[] t1 = v1.split("\\."); + String[] t2 = v2.split("\\."); + + for (int i = 0; i < t1.length && i < t2.length; i++) { + int result = compareToken(t1[i], t2[i]); + if (result != 0) + return result; + } + if (t1.length > t2.length) + return 1; + else if (t1.length < t2.length) + return -1; + else + return 0; + } + + private int compareToken(String t1, String t2) { + if (t1 == null && t2 == null) + return 0; + else if (t1 == null) + return -1; + else if (t2 == null) + return 1; + + Integer i1 = null, i2 = null; + try { + i1 = new Integer(t1); + i2 = new Integer(t2); + } catch (NumberFormatException nfe) { + // the format is not valid we silently compare as String + return t1.compareTo(t2); + } + return i1.compareTo(i2); + } +} \ No newline at end of file