01: package org.claros.commons.comparators;
02:
03: import java.util.Comparator;
04:
05: /**
06: * @author Umut Gokbayrak
07: */
08: public class StringComparator implements Comparator<String> {
09: private int left = 1;
10: private int right = -1;
11:
12: public StringComparator(boolean ascending) {
13: if (ascending) {
14: left = -1;
15: right = 1;
16: }
17: }
18:
19: /* (non-Javadoc)
20: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
21: */
22: public int compare(String arg0, String arg1) {
23: if (arg0 != null && arg1 != null && arg0 instanceof String
24: && arg1 instanceof String) {
25: String s1 = (String) arg0;
26: String s2 = (String) arg1;
27:
28: if (s1.compareTo(s2) > 1) {
29: return right;
30: } else if (s1.compareTo(s2) < 1) {
31: return left;
32: } else {
33: return 0;
34: }
35: }
36: return right;
37: }
38: }
|