01: package org.claros.commons.mail.comparator;
02:
03: import java.util.Comparator;
04: import java.util.Date;
05:
06: import org.claros.commons.mail.models.EmailHeader;
07:
08: /**
09: * @author Umut Gokbayrak
10: */
11: public class ComparatorDate implements Comparator {
12: private int left = 1;
13: private int right = -1;
14:
15: public ComparatorDate(boolean ascending) {
16: if (ascending) {
17: left = -1;
18: right = 1;
19: }
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: Date dt1 = h1.getDate();
31: Date dt2 = h2.getDate();
32:
33: if (dt1 == null) {
34: return right;
35: } else if (dt2 == null) {
36: return left;
37: } else if (dt1.after(dt2)) {
38: return right;
39: } else if (dt1.before(dt2)) {
40: return left;
41: } else {
42: return 0;
43: }
44: }
45: return right;
46: }
47: }
|