001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail.util;
009:
010: import java.util.*;
011:
012: import dtw.webmail.model.*;
013:
014: /**
015: * Utility class providing <tt>Comparator</tt>'s for
016: * sorting messages applying different criterias.
017: *
018: * @author Dieter Wimberger
019: * @version 0.9.7 07/02/2003
020: */
021: public class MessageSortingUtil {
022:
023: /**
024: * Defines a <tt>Comparator</tt> that sorts
025: * the receive or sent date chronological (i.e. oldest first).
026: */
027: public static final Comparator DATE_CHRONOLOGICAL = new Comparator() {
028:
029: public int compare(Object o1, Object o2) {
030: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
031: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
032: return msg1.getDate().compareTo(msg2.getDate());
033: }//compare(Object,Object)
034: };
035:
036: /**
037: * Defines a <tt>Comparator</tt> that sorts
038: * the receive or sent date reverse to chronological
039: * (i.e. most recent first).
040: */
041: public static final Comparator DATE_REVERSE_CHRONOLOGICAL = new Comparator() {
042:
043: public int compare(Object o1, Object o2) {
044: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
045: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
046: return msg2.getDate().compareTo(msg1.getDate());
047: }//compare(Object,Object)
048: };
049:
050: /**
051: * Defines a <tt>Comparator</tt> that sorts
052: * the receive date chronological (i.e. oldest first).
053: */
054: public static final Comparator RECEIVE_CHRONOLOGICAL = new Comparator() {
055:
056: public int compare(Object o1, Object o2) {
057: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
058: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
059: return msg1.getReceivedDate().compareTo(
060: msg2.getReceivedDate());
061: }//compare(Object,Object)
062: };
063: /**
064: * Defines a <tt>Comparator</tt> that sorts
065: * the receive date reverse to chronological
066: * (i.e. most recent first).
067: */
068: public static final Comparator RECEIVE_REVERSE_CHRONOLOGICAL = new Comparator() {
069:
070: public int compare(Object o1, Object o2) {
071: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
072: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
073: return msg2.getReceivedDate().compareTo(
074: msg1.getReceivedDate());
075: }//compare(Object,Object)
076: };
077:
078: /**
079: * Defines a <tt>Comparator</tt> that sorts
080: * the send date chronological (i.e. oldest first).
081: */
082: public static final Comparator SEND_CHRONOLOGICAL = new Comparator() {
083:
084: public int compare(Object o1, Object o2) {
085: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
086: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
087: return msg1.getSentDate().compareTo(msg2.getSentDate());
088: }//compare(Object,Object)
089: };
090:
091: /**
092: * Defines a <tt>Comparator</tt> that sorts
093: * the send date reverse to chronological
094: * (i.e. most recent first).
095: */
096: public static final Comparator SEND_REVERSE_CHRONOLOGICAL = new Comparator() {
097:
098: public int compare(Object o1, Object o2) {
099: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
100: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
101: return msg2.getSentDate().compareTo(msg1.getSentDate());
102: }//compare(Object,Object)
103: };
104:
105: /**
106: * Defines a <tt>Comparator</tt> that sorts
107: * the sender or receiver string lexographical.
108: */
109: public static final Comparator WHO_LEXOGRAPHICAL = new Comparator() {
110:
111: public int compare(Object o1, Object o2) {
112: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
113: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
114: return msg1.getWho().compareTo(msg2.getWho());
115: }//compare(Object,Object)
116: };
117:
118: /**
119: * Defines a <tt>Comparator</tt> that sorts
120: * the sender or receiver string reverse lexographical.
121: */
122: public static final Comparator WHO_REVERSE_LEXOGRAPHICAL = new Comparator() {
123:
124: public int compare(Object o1, Object o2) {
125: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
126: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
127: return msg2.getWho().compareTo(msg1.getWho());
128: }//compare(Object,Object)
129: };
130:
131: /**
132: * Defines a <tt>Comparator</tt> that sorts
133: * the sender string lexographical.
134: */
135: public static final Comparator SENDER_LEXOGRAPHICAL = new Comparator() {
136:
137: public int compare(Object o1, Object o2) {
138: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
139: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
140: return msg1.getFrom().compareTo(msg2.getFrom());
141: }//compare(Object,Object)
142: };
143:
144: /**
145: * Defines a <tt>Comparator</tt> that sorts
146: * the sender string reverse lexographical.
147: */
148: public static final Comparator SENDER_REVERSE_LEXOGRAPHICAL = new Comparator() {
149:
150: public int compare(Object o1, Object o2) {
151: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
152: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
153: return msg2.getFrom().compareTo(msg1.getFrom());
154: }//compare(Object,Object)
155: };
156:
157: /**
158: * Defines a <tt>Comparator</tt> that sorts
159: * the receiver string lexographical.
160: */
161: public static final Comparator RECEIVER_LEXOGRAPHICAL = new Comparator() {
162:
163: public int compare(Object o1, Object o2) {
164: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
165: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
166: return msg1.getTo().compareTo(msg2.getTo());
167: }//compare(Object,Object)
168: };
169:
170: /**
171: * Defines a <tt>Comparator</tt> that sorts
172: * the receiver string reverse lexographical.
173: */
174: public static final Comparator RECEIVER_REVERSE_LEXOGRAPHICAL = new Comparator() {
175:
176: public int compare(Object o1, Object o2) {
177: JwmaMessageInfo msg1 = (JwmaMessageInfo) o1;
178: JwmaMessageInfo msg2 = (JwmaMessageInfo) o2;
179: return msg2.getTo().compareTo(msg1.getTo());
180: }//compare(Object,Object)
181: };
182:
183: /**
184: * Defines a <tt>Comparator</tt> that sorts
185: * the message number numerical (i.e. lowest first).
186: */
187: public static final Comparator NUMBER_NUMERICAL = new Comparator() {
188:
189: public int compare(Object o1, Object o2) {
190: JwmaMessageInfoImpl msg1 = (JwmaMessageInfoImpl) o1;
191: JwmaMessageInfoImpl msg2 = (JwmaMessageInfoImpl) o2;
192: return msg1.getNumberForSort().compareTo(
193: msg2.getNumberForSort());
194: }//compare(Object,Object)
195: };
196:
197: /**
198: * Defines a <tt>Comparator</tt> that sorts
199: * the message number reverse numerical (i.e. highest first).
200: */
201: public static final Comparator NUMBER_REVERSE_NUMERICAL = new Comparator() {
202:
203: public int compare(Object o1, Object o2) {
204: JwmaMessageInfoImpl msg1 = (JwmaMessageInfoImpl) o1;
205: JwmaMessageInfoImpl msg2 = (JwmaMessageInfoImpl) o2;
206: return msg2.getNumberForSort().compareTo(
207: msg1.getNumberForSort());
208: }//compare(Object,Object)
209: };
210:
211: /**
212: * Defines the relation between a given criteria and a
213: * comparator.
214: */
215: public static final Comparator[] CRITERIA_COMPARATOR = {
216: RECEIVE_CHRONOLOGICAL, //0
217: RECEIVE_REVERSE_CHRONOLOGICAL, //1
218: SEND_CHRONOLOGICAL, //2
219: SEND_REVERSE_CHRONOLOGICAL, //3
220: RECEIVER_LEXOGRAPHICAL, //4
221: RECEIVER_REVERSE_LEXOGRAPHICAL, //5
222: SENDER_LEXOGRAPHICAL, //6
223: SENDER_REVERSE_LEXOGRAPHICAL, //7
224: NUMBER_NUMERICAL, //8
225: NUMBER_REVERSE_NUMERICAL, //9
226: DATE_CHRONOLOGICAL, //10
227: DATE_REVERSE_CHRONOLOGICAL, //11
228: WHO_LEXOGRAPHICAL, //12
229: WHO_REVERSE_LEXOGRAPHICAL //13
230: };
231:
232: }//class MessageSortingUtil
|