1 package org
.argeo
.slc
.client
.ui
.dist
.utils
;
3 import org
.eclipse
.jface
.viewers
.Viewer
;
4 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
7 * Enable comparison of two version string with form "1.2.5.qualifier" with
8 * following rules and assumptions:
11 * Version are parsed and compared segment by segment; doing best effort to
12 * convert major, minor and micro to integer and compare them as such (to have
13 * 0.1 < 0.9 < 0.10 not 0.1 < 0.10 < 0.9).</li>
14 * <li>Version should not contain any dash (-), version segments should be
15 * separated by dots (.)</li>
19 public class VersionComparator
extends ViewerComparator
{
20 private static final long serialVersionUID
= 3760077835650538982L;
23 public int compare(Viewer viewer
, Object e1
, Object e2
) {
24 String s1
= (String
) e1
;
25 String s2
= (String
) e2
;
26 return compareVersion(s1
, s2
);
30 * Enable comparison of two versions of the form
31 * "major.minor.micro.qualifier". We assume the separator is always a "."
32 * and make best effort to convert major, minor and micro to int.
34 private int compareVersion(String v1
, String v2
) {
35 String
[] t1
= v1
.split("\\.");
36 String
[] t2
= v2
.split("\\.");
38 for (int i
= 0; i
< t1
.length
&& i
< t2
.length
; i
++) {
39 int result
= compareToken(t1
[i
], t2
[i
]);
43 if (t1
.length
> t2
.length
)
45 else if (t1
.length
< t2
.length
)
51 private int compareToken(String t1
, String t2
) {
52 if (t1
== null && t2
== null)
59 Integer i1
= null, i2
= null;
63 } catch (NumberFormatException nfe
) {
64 // the format is not valid we silently compare as String
65 return t1
.compareTo(t2
);
67 return i1
.compareTo(i2
);