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 ComparatorFrom implements Comparator {
10: private int left = 1;
11: private int right = -1;
12: private Collator coll;
13:
14: public ComparatorFrom(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 from1 = org.claros.commons.mail.utility.Utility
31: .addressArrToString(h1.getFrom());
32: String from2 = org.claros.commons.mail.utility.Utility
33: .addressArrToString(h2.getFrom());
34:
35: if (from1 == null) {
36: return right;
37: } else if (from2 == null) {
38: return left;
39: } else if (coll.compare(from1, from2) > 0) {
40: return right;
41: } else if (coll.compare(from1, from2) < 0) {
42: return left;
43: } else {
44: return 0;
45: }
46: }
47: return right;
48: }
49: }
|