01: /**
02: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
03: *
04: * Permission is hereby granted, free of charge, to any person obtaining a copy
05: * of this software and associated documentation files (the "Software"), to deal
06: * in the Software without restriction, including without limitation the rights
07: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
08: * copies of the Software, and to permit persons to whom the Software is
09: * furnished to do so, subject to the following conditions:
10: *
11: * The above copyright notice and this permission notice shall be included in
12: * all copies or substantial portions of the Software.
13: *
14: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20: * SOFTWARE.
21: */package com.liferay.portlet.mail.util.comparator;
22:
23: import com.liferay.portal.kernel.util.DateUtil;
24: import com.liferay.portal.kernel.util.GetterUtil;
25: import com.liferay.portlet.mail.model.MailEnvelope;
26:
27: import java.util.Comparator;
28:
29: /**
30: * <a href="SubjectComparator.java.html"><b><i>View Source</i></b></a>
31: *
32: * @author Alexander Chow
33: *
34: */
35: public class SubjectComparator implements Comparator {
36:
37: public SubjectComparator(boolean asc) {
38: _asc = asc;
39: }
40:
41: public int compare(Object obj1, Object obj2) {
42: MailEnvelope mailEvenlope1 = (MailEnvelope) obj1;
43: MailEnvelope mailEvenlope2 = (MailEnvelope) obj2;
44:
45: String subject1 = _getSubject(mailEvenlope1.getSubject());
46: String subject2 = _getSubject(mailEvenlope2.getSubject());
47:
48: int value = subject1.compareTo(subject2);
49:
50: if (value == 0) {
51: value = DateUtil.compareTo(mailEvenlope1.getDate(),
52: mailEvenlope2.getDate());
53: }
54:
55: if (value == 0) {
56: Long messageId1 = new Long(mailEvenlope1.getMessageId());
57: Long messageId2 = new Long(mailEvenlope2.getMessageId());
58:
59: value = messageId1.compareTo(messageId2);
60: }
61:
62: if (_asc) {
63: return value;
64: } else {
65: return -value;
66: }
67: }
68:
69: private String _getSubject(String subject) {
70: subject = GetterUtil.getString(subject).toLowerCase();
71:
72: while (subject.startsWith("re:") || subject.startsWith("re>")
73: || subject.startsWith("fw:")
74: || subject.startsWith("fw>")) {
75:
76: subject = subject.substring(3).trim();
77: }
78:
79: return subject;
80: }
81:
82: private boolean _asc;
83:
84: }
|