1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.aws.cloudfront;
17
18 import java.util.Comparator;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.common.aws.s3.old.BucketConstants;
22
23
24
25
26
27
28
29
30
31 public class DisplayRowComparator implements Comparator<DisplayRow> {
32
33 public static final String DEFAULT_SEPARATORS = ".-";
34
35 String separators = DEFAULT_SEPARATORS;
36 String delimiter = BucketConstants.DEFAULT_DELIMITER;
37
38 @Override
39 public int compare(DisplayRow one, DisplayRow two) {
40
41 String show1 = one.getShow();
42 String show2 = two.getShow();
43
44 if (show1 == null && show2 == null) {
45 return 0;
46 }
47
48 if (show1 == null) {
49 return -1;
50 }
51
52 if (show2 == null) {
53 return 1;
54 }
55
56 show1 = StringUtils.strip(show1, delimiter);
57 show2 = StringUtils.strip(show2, delimiter);
58
59 String[] tokens1 = StringUtils.split(show1, separators);
60 String[] tokens2 = StringUtils.split(show2, separators);
61
62 int len = Math.min(tokens1.length, tokens2.length);
63
64 for (int i = 0; i < len; i++) {
65 String token1 = tokens1[i];
66 String token2 = tokens2[i];
67 int compareTo = compareTokens(token1, token2);
68 if (compareTo != 0) {
69 return compareTo;
70 }
71 }
72
73 return tokens1.length - tokens2.length;
74 }
75
76 protected int compareTokens(String token1, String token2) {
77 try {
78 int i1 = Integer.parseInt(token1);
79 int i2 = Integer.parseInt(token2);
80 return Double.compare(i1, i2);
81 } catch (NumberFormatException e) {
82 ;
83 }
84
85
86 return token1.compareTo(token2);
87
88 }
89
90 public String getSeparators() {
91 return separators;
92 }
93
94 public void setSeparators(String separators) {
95 this.separators = separators;
96 }
97
98 public String getDelimiter() {
99 return delimiter;
100 }
101
102 public void setDelimiter(String delimiter) {
103 this.delimiter = delimiter;
104 }
105
106 }