001    package edu.harvard.deas.hyperenc.gui;
002    
003    import java.io.Serializable;
004    import java.util.Date;
005    
006    
007    import edu.harvard.deas.hyperenc.Contact;
008    import edu.harvard.deas.hyperenc.HyperMessage;
009    
010    /**
011     * A message with some extra fields for user interaction purposes. For example,
012     * a GuiMessage may have different states, as e-mails do in an e-mail client
013     * (read versus unread, and so on).
014     * <p>
015     * GuiMessages are immutable.
016     */
017    public class GuiMessage implements Serializable {
018            private static final long serialVersionUID = 1L;
019            
020            private HyperMessage hm;
021            private ReadStatus read;
022            
023            /**
024             * Construct a new GuiMessage around the given HyperMessage.
025             */
026            public GuiMessage(HyperMessage m, ReadStatus read) {
027              this.hm = m;
028                    this.read = read;
029            }
030            
031            /**
032             * Get the HyperMessage underlying this GuiMessage.
033             * @return the HyperMessage underlying this GuiMessage
034             */
035            public HyperMessage getHyperMessage() {
036              return hm;
037            }
038    
039      /**
040       * Get the unique ID of this GuiMessage. This is identical to the ID of the
041       * underlying HyperMessage.
042       * @return the ID of this message
043       */
044      public int getID() {
045        return hm.getID();
046      }
047      
048      /**
049       * Get the subject of this GuiMessage. This is identical to the subject of the
050       * underlying HyperMessage.
051       * @return the subject of this message
052       */
053      public String getSubject() {
054        return hm.getSubject();
055      }
056      
057      /**
058       * Get the date of this GuiMessage. This is identical to the date of the
059       * underlying HyperMessage.
060       * @return the date of this message
061       */
062      public Date getDate() {
063        return hm.getDate();
064      }
065      
066      /**
067       * Get the sender of this GuiMessage. This is identical to the sender of the
068       * underlying HyperMessage.
069       * @return the sender of this message
070       */
071      public Contact getSender() {
072        return hm.getSender();
073      }
074      
075      /**
076       * Get the recipient of this GuiMessage. This is identical to the recipient
077       * of the underlying HyperMessage.
078       * @return the recipient of this message
079       */
080      public Contact getRecipient() {
081        return hm.getRecipient();
082      }
083      
084      /**
085       * Get the content of this GuiMessage. This is identical to the content of the
086       * underlying HyperMessage.
087       * @return the content of this message
088       */
089      public String getContent() {
090        return hm.getContent();
091      }
092    
093      /**
094       * Whether this message has been read or not. Used to indicate whether or not
095       * the message has been previously viewed by the user.
096       * @return the read status of this message
097       */
098      public ReadStatus isRead() {
099        return read;
100      }
101    
102      /**
103       * Returns a message whose fields are the same as those of <code>gm</code>,
104       * except the message is marked as read. After this method returns,
105       * <code>isRead()</code> returns <code>ReadStatus.READ</code>.
106       * <p>
107       * If <code>gm</code> is already marked as read, it is unspecified whether
108       * the returned GuiMessage is the same object as <code>gm</code>.
109       * 
110       * @param gm
111       *        a message
112       * @return a GuiMessage identical to <code>gm</code> except that it is marked
113       *         as read
114       */
115      public static GuiMessage markRead(GuiMessage gm) {
116        if (gm.isRead() == ReadStatus.READ)
117          return gm;
118        
119        return new GuiMessage(gm.getHyperMessage(), ReadStatus.READ);
120      }
121    
122      /**
123       * Returns a message whose fields are the same as those of <code>gm</code>,
124       * except the message is marked as unread. After this method returns,
125       * <code>isRead()</code> returns <code>ReadStatus.UNREAD</code>.
126       * <p>
127       * If <code>gm</code> is already marked as unread, it is unspecified whether
128       * the returned GuiMessage is the same object as <code>gm</code>.
129       * 
130       * @param gm
131       *        a message
132       * @return a GuiMessage identical to <code>gm</code> except that it is marked
133       *         as unread
134       */
135      public static GuiMessage markUnread(GuiMessage gm) {
136        if (gm.isRead() == ReadStatus.UNREAD)
137          return gm;
138        
139        return new GuiMessage(gm.getHyperMessage(), ReadStatus.UNREAD);
140      }
141    
142      /**
143       * Whether this message is encrypted.
144       * @return <code>true</code> if the message content is encrypted,
145       *         <code>false</code> if not
146       */
147      public boolean isEncrypted() {
148        return (null != hm.getPadsUsed());
149      }
150    
151      /**
152       * Compares this GuiMessage to the specified object. The result is
153       * <code>true</code> if and only if the argument is not <code>null</code> and
154       * is a GuiMessage object whose HyperMessage is equal to this GuiMessage's
155       * HyperMessage. (The ReadStatus field of GuiMessage is ignored in the
156       * equality comparison.)
157       */
158      @Override
159      public boolean equals(Object o) {
160        if (o == null || !(o instanceof GuiMessage)) {
161          return false;
162        }
163        
164        GuiMessage gm = (GuiMessage)o;
165        
166        return gm.getHyperMessage().equals(hm);
167      }
168      
169      /**
170       * Returns the <code>hashCode()</code> of the contained HyperMessage.
171       */
172      @Override
173      public int hashCode() {
174        return hm.hashCode();
175      }
176      
177    }