| 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 | |
public static final String DEFAULT_DELIMITER = "/"; |
| 16 | 9 | String separators = DEFAULT_SEPARATORS; |
| 17 | 9 | String delimiter = DEFAULT_DELIMITER; |
| 18 | |
|
| 19 | |
@Override |
| 20 | |
public int compare(DisplayRow one, DisplayRow two) { |
| 21 | 10 | String show1 = one.getShow(); |
| 22 | 10 | String show2 = two.getShow(); |
| 23 | |
|
| 24 | 10 | if (show1 == null && show2 == null) { |
| 25 | 2 | return 0; |
| 26 | |
} |
| 27 | |
|
| 28 | 8 | if (show1 == null) { |
| 29 | 1 | return -1; |
| 30 | |
} |
| 31 | |
|
| 32 | 7 | if (show2 == null) { |
| 33 | 2 | return 1; |
| 34 | |
} |
| 35 | |
|
| 36 | 5 | show1 = StringUtils.strip(show1, delimiter); |
| 37 | 5 | show2 = StringUtils.strip(show2, delimiter); |
| 38 | |
|
| 39 | 5 | String[] tokens1 = StringUtils.split(show1, separators); |
| 40 | 5 | String[] tokens2 = StringUtils.split(show2, separators); |
| 41 | |
|
| 42 | 5 | int len = Math.min(tokens1.length, tokens2.length); |
| 43 | |
|
| 44 | 20 | for (int i = 0; i < len; i++) { |
| 45 | 17 | String token1 = tokens1[i]; |
| 46 | 17 | String token2 = tokens2[i]; |
| 47 | 17 | int compareTo = compareTokens(token1, token2); |
| 48 | 17 | if (compareTo != 0) { |
| 49 | 2 | return compareTo; |
| 50 | |
} |
| 51 | |
} |
| 52 | |
|
| 53 | 3 | if (tokens1.length > tokens2.length) { |
| 54 | 1 | return -1; |
| 55 | 2 | } else if (tokens1.length < tokens2.length) { |
| 56 | 1 | return 1; |
| 57 | |
} else { |
| 58 | 1 | return 0; |
| 59 | |
} |
| 60 | |
} |
| 61 | |
|
| 62 | |
protected int compareTokens(String token1, String token2) { |
| 63 | |
try { |
| 64 | 17 | Double d1 = new Double(token1); |
| 65 | 15 | Double d2 = new Double(token2); |
| 66 | 15 | return d1.compareTo(d2); |
| 67 | 2 | } catch (NumberFormatException e) { |
| 68 | |
|
| 69 | |
} |
| 70 | 2 | return token1.compareTo(token2); |
| 71 | |
|
| 72 | |
} |
| 73 | |
|
| 74 | |
public String getSeparators() { |
| 75 | 1 | return separators; |
| 76 | |
} |
| 77 | |
|
| 78 | |
public void setSeparators(String separators) { |
| 79 | 1 | this.separators = separators; |
| 80 | 1 | } |
| 81 | |
|
| 82 | |
public String getDelimiter() { |
| 83 | 0 | return delimiter; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public void setDelimiter(String delimiter) { |
| 87 | 0 | this.delimiter = delimiter; |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
} |