1 | |
package org.kuali.maven.mojo.s3; |
2 | |
|
3 | |
import java.util.Comparator; |
4 | |
|
5 | |
import org.apache.commons.lang.StringUtils; |
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | 19 | public class DisplayRowComparator implements Comparator<DisplayRow> { |
14 | |
public static final String DEFAULT_SEPARATORS = ".-"; |
15 | 9 | String separators = DEFAULT_SEPARATORS; |
16 | |
|
17 | |
@Override |
18 | |
public int compare(DisplayRow one, DisplayRow two) { |
19 | 10 | String show1 = one.getShow(); |
20 | 10 | String show2 = two.getShow(); |
21 | |
|
22 | 10 | if (show1 == null && show2 == null) { |
23 | 2 | return 0; |
24 | |
} |
25 | |
|
26 | 8 | if (show1 == null) { |
27 | 1 | return -1; |
28 | |
} |
29 | |
|
30 | 7 | if (show2 == null) { |
31 | 2 | return 1; |
32 | |
} |
33 | 5 | String[] tokens1 = StringUtils.split(show1, separators); |
34 | 5 | String[] tokens2 = StringUtils.split(show2, separators); |
35 | 5 | int len = Math.min(tokens1.length, tokens2.length); |
36 | |
|
37 | 20 | for (int i = 0; i < len; i++) { |
38 | 17 | String token1 = tokens1[i]; |
39 | 17 | String token2 = tokens2[i]; |
40 | 17 | int compareTo = compareTokens(token1, token2); |
41 | 17 | if (compareTo != 0) { |
42 | 2 | return compareTo; |
43 | |
} |
44 | |
} |
45 | |
|
46 | 3 | if (tokens1.length > tokens2.length) { |
47 | 1 | return -1; |
48 | 2 | } else if (tokens1.length < tokens2.length) { |
49 | 1 | return 1; |
50 | |
} else { |
51 | 1 | return 0; |
52 | |
} |
53 | |
} |
54 | |
|
55 | |
protected int compareTokens(String token1, String token2) { |
56 | |
try { |
57 | 17 | Double d1 = new Double(token1); |
58 | 15 | Double d2 = new Double(token2); |
59 | 15 | return d1.compareTo(d2); |
60 | 2 | } catch (NumberFormatException e) { |
61 | |
|
62 | |
} |
63 | 2 | return token1.compareTo(token2); |
64 | |
|
65 | |
} |
66 | |
|
67 | |
public String getSeparators() { |
68 | 1 | return separators; |
69 | |
} |
70 | |
|
71 | |
public void setSeparators(String separators) { |
72 | 1 | this.separators = separators; |
73 | 1 | } |
74 | |
|
75 | |
} |