1 29 30 package echo2example.email.faux; 31 32 import java.util.Comparator ; 33 import java.util.SortedSet ; 34 import java.util.TreeSet ; 35 36 import javax.mail.Flags ; 37 import javax.mail.Folder ; 38 import javax.mail.Message ; 39 import javax.mail.MessagingException ; 40 41 45 public class FauxFolder extends Folder { 46 47 private static final int MESSAGE_COUNT = 140; 48 49 private static final Message [] INBOX_MESSAGES; 50 static { 51 try { 52 MessageGenerator messageGenerator = new MessageGenerator(); 53 SortedSet sortingSet = new TreeSet (new Comparator (){ 54 public int compare(Object a, Object b) { 55 try { 56 Message message1 = (Message ) a; 57 Message message2 = (Message ) b; 58 int dateDelta = message1.getSentDate().compareTo(message2.getSentDate()); 59 if (dateDelta != 0) { 60 return dateDelta; 61 } 62 return message1.toString().compareTo(message2.toString()); 63 } catch (MessagingException ex) { 64 throw new RuntimeException (ex); 65 } 66 } 67 }); 68 for (int i = 0; i < MESSAGE_COUNT; ++i) { 69 sortingSet.add(messageGenerator.generateMessage()); 70 } 71 INBOX_MESSAGES = (Message []) sortingSet.toArray(new Message [sortingSet.size()]); 72 } catch (MessagingException ex) { 73 throw new RuntimeException (ex); 74 } 75 } 76 77 private int type; 78 private FauxStore store; 79 80 86 public static final FauxFolder createInbox(FauxStore store) { 87 return new FauxFolder(store, HOLDS_MESSAGES); 88 } 89 90 96 public static final FauxFolder createRoot(FauxStore store) { 97 return new FauxFolder(store, HOLDS_FOLDERS); 98 } 99 100 106 private FauxFolder(FauxStore store, int type) { 107 super(store); 108 this.store = store; 109 this.type = type; 110 } 111 112 115 public void appendMessages(Message [] messages) 116 throws MessagingException { 117 throw new UnsupportedOperationException (); 118 } 119 120 123 public void close(boolean expunge) 124 throws MessagingException { } 125 126 129 public boolean create(int type) throws MessagingException { 130 return true; 131 } 132 133 136 public boolean delete(boolean recurse) 137 throws MessagingException { 138 return false; 139 } 140 141 144 public boolean exists() throws MessagingException { 145 return type == HOLDS_MESSAGES; 146 } 147 148 151 public Message [] expunge() throws MessagingException { 152 return new Message [0]; 153 } 154 155 158 public Folder getFolder(String name) 159 throws MessagingException { 160 if (type == HOLDS_FOLDERS) { 161 return store.inboxFolder; 162 } else { 163 throw new MessagingException (); 164 } 165 } 166 167 170 public String getFullName() { 171 return getName(); 172 } 173 174 177 public Message getMessage(int index) throws MessagingException { 178 return INBOX_MESSAGES[index - 1]; 179 } 180 181 184 public int getMessageCount() throws MessagingException { 185 return INBOX_MESSAGES.length; 186 } 187 188 191 public String getName() { 192 switch (type) { 193 case HOLDS_FOLDERS: 194 return "/"; 195 case HOLDS_MESSAGES: 196 return "INBOX"; 197 default: 198 return "Unknown"; 199 } 200 } 201 202 205 public Folder getParent() throws MessagingException { 206 return type == HOLDS_MESSAGES ? store.rootFolder : null; 207 } 208 209 212 public Flags getPermanentFlags() { 213 return new Flags (); 214 } 215 216 219 public char getSeparator() throws MessagingException { 220 return '\u0000'; 221 } 222 223 226 public int getType() throws MessagingException { 227 return type; 228 } 229 230 233 public boolean hasNewMessages() throws MessagingException { 234 return false; 235 } 236 237 240 public boolean isOpen() { 241 return true; 242 } 243 244 247 public Folder [] list(String pattern) throws MessagingException { 248 if (type == HOLDS_FOLDERS) { 249 return new Folder []{store.inboxFolder}; 250 } else { 251 throw new MessagingException (); 252 } 253 } 254 255 258 public void open(int mode) throws MessagingException { 259 } 260 261 264 public boolean renameTo(Folder folder) throws MessagingException { 265 return false; 266 } 267 } | Popular Tags |