1
2
3
4
5
6 package org.repoweb.model;
7 import java.util.Comparator;
8 /***
9 * Comparator for version.
10 */
11 public class VersionComparator implements Comparator {
12 public VersionComparator() {}
13
14 public int compare(Object versionA, Object versionB) {
15 return compareVersion(((String)versionA).toLowerCase(),
16 ((String)versionB).toLowerCase());
17 }
18
19
20 private int compareVersion(String valA, String valB) {
21
22 if (valA.length() == 0) {
23 return -1;
24 }
25 else if (valB.length() == 0) {
26 return 1;
27 }
28
29 if (valB.startsWith(valA) && isSnap(valB.substring(valA.length()))) {
30 return 1;
31 }
32 else if (valA.startsWith(valB) && isSnap(valA.substring(valB.length()))) {
33 return -1;
34 }
35
36 return valA.compareTo(valB);
37 }
38
39
40 private boolean isSnap(String version) {
41 if (version.length() > 2) {
42 return version.charAt(0) == '-'
43 || (!Character.isDigit(version.charAt(0))
44 && !Character.isDigit(version.charAt(1)));
45 }
46 else {
47 return false;
48 }
49 }
50 }