View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.maildir.uid;
14  
15  import javax.mail.Folder;
16  import javax.mail.MessagingException;
17  import javax.mail.internet.MimeMessage;
18  
19  import org.abstracthorizon.mercury.maildir.util.MessageWrapper;
20  
21  /**
22   * Message wrapper for UID messages created by
23   * {@link org.abstracthorizon.mercury.maildir.uid.UIDMaildirFolderData}.
24   *
25   * @author Daniel Sendula
26   */
27  public class UIDMessageWrapper extends /* ReadOnlyMessageWrapper */ MessageWrapper implements UIDMessage, Comparable<UIDMessage> {
28  
29      /**
30       * Constructor.
31       * @param folder folder
32       * @param message message this message is based on
33       * @param msgnum message number
34       * @throws MessagingException
35       */
36      protected UIDMessageWrapper(Folder folder, UIDMaildirMessage message, int msgnum) throws MessagingException {
37          super(folder, message, msgnum);
38      }
39  
40      /**
41       * Returns wrapped message
42       * @return wrapped message
43       */
44      public MimeMessage getMessage() {
45          return message;
46      }
47  
48      /**
49       * Returns supplied message's UID.
50       * @return supplied message's UID.
51       * @throws MessagingException never
52       * @throws MessagingException if supplied message's getUID method throws it
53       * @throws IllegalStateException if supplied message is <code>null</code> or not <code>UIDMessage</code>
54       */
55      public UID getUID() throws MessagingException {
56          if (message == null) {
57              throw new IllegalStateException("Supplied message is empty");
58          }
59          if (message instanceof UIDMaildirMessage) {
60              return ((UIDMaildirMessage)message).getUID();
61          }
62          throw new IllegalStateException("Supplied message is not UIDMessage");
63      }
64  
65  
66      /**
67       * Checks supplied message's uid against wrapped message's uid
68       * @param o message to be compared with
69       * @return <code>true</code> if two messages have same uid and belong to folder with same full names
70       */
71      public boolean equals(Object o) {
72          if (o instanceof UIDMessageWrapper) {
73              try {
74                  long u1 = getUID().getUID();
75                  long u2 = ((UIDMessageWrapper)o).getUID().getUID();
76                  if ((u1 == u2) && (getFolder().getFullName().equals(((UIDMessageWrapper)o).getFolder().getFullName()))) {
77                      return true;
78                  }
79              } catch (MessagingException ignore) {
80              }
81          }
82          return false;
83      }
84  
85      /**
86       * Compares wrapped message's uid with supplied message's uid.
87       *
88       * @param o message to be compared with
89       * @return -1, 0 or 1. If supplied object is not of <code>UIDMessage</code> type it will return -1.
90       */
91      public int compareTo(UIDMessage o) {
92          if (o instanceof UIDMessage) {
93              try {
94                  long u1 = getUID().getUID();
95                  long u2 = o.getUID().getUID();
96                  if (u1 == u2) {
97                      return 0;
98                  } else if (u1 < u2) {
99                      return -1;
100                 } else {
101                     return 1;
102                 }
103             } catch (MessagingException ignore) {
104             }
105         }
106         return -1;
107     }
108 }