View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Sort version numbers in the correct order.
25   * 
26   * <pre>
27   * 1.1.10          <  1.1.2
28   * 1.1.10-SNAPSHOT <  1.1.10
29   * </pre>
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  			; // Intentionally ignore this
83  		}
84  
85  		// Fall through to sorting things as regular strings
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 }