]> git.argeo.org Git - gpl/argeo-slc.git/blob - utils/NameVersionComparator.java
Prepare next development cycle
[gpl/argeo-slc.git] / utils / NameVersionComparator.java
1 package org.argeo.slc.client.ui.dist.utils;
2
3 import org.argeo.eclipse.ui.TreeParent;
4 import org.eclipse.jface.viewers.Viewer;
5 import org.eclipse.jface.viewers.ViewerComparator;
6
7 /**
8 * Enable comparison of two names version string with form org.argeo.slc-1.2.x.
9 * with following rules and assumptions:
10 * <ul>
11 * <li>
12 * Names are ordered using Lexicographical order</li>
13 * <li>
14 * Version are parsed and compared segment by segment; doing best effort to
15 * convert major, minor and micro to integer and compare them as such (to have
16 * 0.1 < 0.9 < 0.10 not 0.1 < 0.10 < 0.9).</li>
17 * <li>Version should not contain any dash (-), version segments should be
18 * separated by dots (.)</li>
19 * </ul>
20 */
21
22 public class NameVersionComparator extends ViewerComparator {
23 private static final long serialVersionUID = 8290130681918221197L;
24
25 private VersionComparator vc = new VersionComparator();
26
27 @Override
28 public int category(Object element) {
29 if (element instanceof String) {
30 int lastInd = ((String) element).lastIndexOf('-');
31 if (lastInd > 0)
32 return 10;
33 }
34 // unvalid names always last
35 return 5;
36 }
37
38 @Override
39 public int compare(Viewer viewer, Object e1, Object e2) {
40 int cat1 = category(e1);
41 int cat2 = category(e2);
42
43 if (cat1 != cat2) {
44 return cat1 - cat2;
45 }
46
47 int result = 0;
48
49 String s1, s2;
50
51 if (e1 instanceof TreeParent) {
52 s1 = ((TreeParent) e1).getName();
53 s2 = ((TreeParent) e2).getName();
54 } else {
55 s1 = e1.toString();
56 s2 = e2.toString();
57 }
58
59 int i1 = s1.lastIndexOf('-');
60 int i2 = s2.lastIndexOf('-');
61
62 // Specific cases, unvalid Strings
63 if (i1 < 0)
64 if (i2 < 0)
65 return s1.compareTo(s2);
66 else
67 return 1;
68 else if (i2 < 0)
69 return -1;
70
71 String aName = s1.substring(0, s1.lastIndexOf('-'));
72 String aVersion = s1.substring(s1.lastIndexOf('-'));
73
74 String bName = s2.substring(0, s2.lastIndexOf('-'));
75 String bVersion = s2.substring(s2.lastIndexOf('-'));
76
77 result = aName.compareTo(bName);
78 if (result != 0)
79 return result;
80 else
81 return vc.compare(viewer, aVersion, bVersion);
82 }
83 }