01: package org.claros.commons.mail.comparator;
02:
03: import java.text.Collator;
04: import java.util.Comparator;
05: import java.util.Locale;
06:
07: import org.claros.commons.mail.models.EmailHeader;
08:
09: public class ComparatorSubject implements Comparator {
10: private int left = 1;
11: private int right = -1;
12: private Collator coll;
13:
14: public ComparatorSubject(boolean ascending, Locale loc) {
15: if (ascending) {
16: left = -1;
17: right = 1;
18: }
19: coll = Collator.getInstance(loc);
20: }
21:
22: /* (non-Javadoc)
23: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
24: */
25: public int compare(Object arg0, Object arg1) {
26: if (arg0 != null && arg1 != null && arg0 instanceof EmailHeader
27: && arg1 instanceof EmailHeader) {
28: EmailHeader h1 = (EmailHeader) arg0;
29: EmailHeader h2 = (EmailHeader) arg1;
30: String str1 = h1.getSubject();
31: String str2 = h2.getSubject();
32:
33: if (str1 == null) {
34: return right;
35: } else if (str2 == null) {
36: return left;
37: } else if (coll.compare(str1, str2) > 0) {
38: return right;
39: } else if (coll.compare(str1, str2) < 0) {
40: return left;
41: } else {
42: return 0;
43: }
44: }
45: return right;
46: }
47: }
|