1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.mercury.maildir.uid;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import javax.mail.Message;
20 import javax.mail.MessagingException;
21 import javax.mail.UIDFolder;
22 import javax.mail.internet.MimeMessage;
23
24 import org.abstracthorizon.mercury.maildir.MaildirFolder;
25 import org.abstracthorizon.mercury.maildir.MaildirFolderData;
26 import org.abstracthorizon.mercury.maildir.util.MessageWrapper;
27
28
29
30
31
32
33
34 public class UIDMaildirFolder extends MaildirFolder implements UIDFolder {
35
36
37 protected Map<UID, UIDMessageWrapper> uids;
38
39
40
41
42
43
44 protected UIDMaildirFolder(UIDMaildirStore store, MaildirFolderData folderData) {
45 super(store, folderData);
46 }
47
48
49
50
51
52
53 public void open(int mode) throws MessagingException {
54 uids = new HashMap<UID, UIDMessageWrapper>();
55 super.open(mode);
56 }
57
58
59
60
61
62
63 public void close(boolean expunge) throws MessagingException {
64 super.close(expunge);
65 uids = null;
66 }
67
68
69
70
71
72
73 public void appendMessages(Message[] messages) throws MessagingException {
74
75 super.appendMessages(messages);
76 }
77
78
79
80
81
82
83
84
85 protected MessageWrapper addMessage(MimeMessage msg, int num) throws MessagingException {
86 UIDMessageWrapper wrapper = new UIDMessageWrapper(this, (UIDMaildirMessage)msg, num);
87 UID uid = wrapper.getUID();
88
89
90
91 uids.put(uid, wrapper);
92 map.put(msg, wrapper);
93 return wrapper;
94 }
95
96
97
98
99
100
101
102 protected MessageWrapper removeMessage(MimeMessage msg) throws MessagingException {
103 UIDMessageWrapper wrapper = (UIDMessageWrapper)super.removeMessage(msg);
104 if (wrapper != null) {
105 uids.remove(wrapper.getUID());
106 }
107 return wrapper;
108 }
109
110
111
112
113
114
115
116 public Message getMessageByUID(long uid) throws MessagingException {
117 if (!opened) {
118 throw new IllegalStateException("Folder is not opened; "+getFullName());
119 }
120 UID uidx = new UID(uid);
121 return (Message)uids.get(uidx);
122 }
123
124
125
126
127
128
129
130 public Message[] getMessagesByUID(long[] uids) throws MessagingException {
131 if (!opened) {
132 throw new IllegalStateException("Folder is not opened; "+getFullName());
133 }
134 long maxUid = ((UIDMaildirFolderData)folderData).getMaxUID();
135 ArrayList<Message> list = new ArrayList<Message>();
136 for (int i = 0; i < uids.length; i++) {
137 if (uids[i] <= maxUid) {
138 Message msg = getMessageByUID(uids[i]);
139 if (msg != null) {
140 list.add(msg);
141 }
142 }
143 }
144 Message[] res = new Message[list.size()];
145 return (Message[])list.toArray(res);
146 }
147
148
149
150
151
152
153
154
155 public Message[] getMessagesByUID(long start, long end) throws MessagingException {
156 if (!opened) {
157 throw new IllegalStateException("Folder is not opened; "+getFullName());
158 }
159 long maxUid = ((UIDMaildirFolderData)folderData).getMaxUID();
160 if ((maxUid < 0) && (messages != null) && (messages.size() > 0)) {
161 UIDMessage uid = (UIDMessage)messages.get(messages.size()-1);
162 maxUid = uid.getUID().getUID();
163 }
164 if (end > maxUid) {
165 end = maxUid;
166 }
167 ArrayList<Message> list = new ArrayList<Message>();
168 for (long i = start; i <= end; i++) {
169 Message msg = getMessageByUID(i);
170 if (msg != null) {
171 list.add(msg);
172 }
173 }
174 Message[] res = new Message[list.size()];
175 return (Message[])list.toArray(res);
176 }
177
178
179
180
181
182
183
184 public long getUID(Message message) throws MessagingException {
185 if (!opened) {
186 throw new IllegalStateException("Folder is not opened; "+getFullName());
187 }
188 UIDMessage msg = (UIDMessage)message;
189 return msg.getUID().getUID();
190 }
191
192
193
194
195
196 public long getUIDValidity() {
197 return ((UIDMaildirFolderData)folderData).getUIDValidity();
198 }
199 }
200
201