1 | |
package org.kuali.maven.plugins.dnsme.beans; |
2 | |
|
3 | |
import java.util.Comparator; |
4 | |
|
5 | |
import org.apache.commons.lang.StringUtils; |
6 | |
|
7 | 1602 | public class RecordComparator implements Comparator<Record> { |
8 | |
|
9 | |
@Override |
10 | |
public int compare(Record record1, Record record2) { |
11 | 1600 | String name1 = record1.getName(); |
12 | 1600 | String name2 = record2.getName(); |
13 | |
|
14 | 1600 | Integer nullCompare = getNullCompare(name1, name2); |
15 | 1600 | if (nullCompare != null) { |
16 | 0 | return nullCompare; |
17 | |
} |
18 | |
|
19 | 1600 | String[] tokens1 = StringUtils.split(name1, "."); |
20 | 1600 | String[] tokens2 = StringUtils.split(name2, "."); |
21 | |
|
22 | 1600 | String s1 = getReversedString(tokens1); |
23 | 1600 | String s2 = getReversedString(tokens2); |
24 | |
|
25 | 1600 | return s1.compareTo(s2); |
26 | |
} |
27 | |
|
28 | |
protected String getReversedString(String[] tokens) { |
29 | 3200 | StringBuilder sb = new StringBuilder(); |
30 | 8410 | for (int i = tokens.length - 1; i >= 0; i--) { |
31 | 5210 | sb.append(tokens[i]); |
32 | |
} |
33 | 3200 | return sb.toString(); |
34 | |
} |
35 | |
|
36 | |
protected Integer getNullCompare(String s1, String s2) { |
37 | 1600 | if (s1 == null && s2 == null) { |
38 | 0 | return 0; |
39 | 1600 | } else if (s1 != null && s2 == null) { |
40 | 0 | return 1; |
41 | 1600 | } else if (s1 == null && s2 != null) { |
42 | 0 | return -1; |
43 | |
} else { |
44 | 1600 | return null; |
45 | |
} |
46 | |
} |
47 | |
|
48 | |
} |