KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > message > Address


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.message;
37
38 import java.io.IOException JavaDoc;
39 import java.io.Serializable JavaDoc;
40
41 import org.columba.ristretto.parser.AddressParser;
42 import org.columba.ristretto.parser.ParserException;
43
44 /**
45  * Address is the representation of a mail address as specified by RFC 2822.
46  * It consistis of a human readable display name and the technical mail address.
47  *
48  * @author Timo Stich <tstich@users.sourceforge.net>
49  */

50 public class Address implements Serializable JavaDoc, Comparable JavaDoc {
51
52     private String JavaDoc displayName;
53     private String JavaDoc mailAddress;
54
55     static final long serialVersionUID = -7663098020871087578l;
56
57     /**
58      * Construct a Address with a valid mail address that must not be parsed.
59      * <p>
60      * <b>Note:</b> If you want to parse an Address from a String use the <code>static</code> {@link #parse(CharSequence)} instead.
61      *
62      * @param mailAddress a valid mail address (e.g. my@mail.org).
63      */

64     public Address(String JavaDoc mailAddress) {
65         this("", mailAddress);
66     }
67
68     /**
69      * Contruct a Address with a valid mail address and a displayname.
70      * <p>
71      * <b>Note:</b> If you want to parse an Address from a String use the <code>static</code> {@link #parse(CharSequence)} instead.
72      *
73      * @param displayName the name to display for this address, i.e. the real name (e.g. Timo Stich).
74      * @param mailAddress A valid mail address (e.g. my@mail.org).
75      */

76     
77     public Address(String JavaDoc displayName, String JavaDoc mailAddress) {
78         this.displayName = displayName;
79         this.mailAddress = mailAddress;
80     }
81
82     /**
83      * Gets the displayname. The returned String can be used to give a human readable representation of the mail address.
84      * <p>
85      * Example: "Timo Stich"
86      *
87      * @return the human readable String representation or <code>null</code> if not set.
88      */

89     public String JavaDoc getDisplayName() {
90         return displayName;
91     }
92
93     /**
94      * Gets the mail address. This is the technical part of the mail address as defined
95      * in RFC 2822.
96      * <p>
97      * Example: my@mail.org
98      *
99      * @return the mail address
100      */

101     public String JavaDoc getMailAddress() {
102         return mailAddress;
103     }
104
105     /**
106      * Get the canonical form of the mailaddress. This is basically the technical mail address
107      * in brackets and is defined in RFC 2822.
108      * <p>
109      * Example: &lt;my@mail.org&gt;
110      *
111      * @return the canonical form of the mailaddress
112      */

113     public String JavaDoc getCanonicalMailAddress() {
114         return '<' + mailAddress + '>';
115     }
116
117     /**
118      * Gets the short form of the address. This is either the human readable display name if available
119      * or the technical mail address.
120      * <p>
121      * Examples: "Timo Stich" or "my@mail.org"
122      *
123      * @return the short form of the address
124      */

125     public String JavaDoc getShortAddress() {
126         if ((displayName != null) && (displayName.length() > 0)) {
127             return displayName;
128         } else {
129             return mailAddress;
130         }
131     }
132
133     /**
134      * Sets the displayname. This is the human readable part of the address.
135      * <p>
136      * Example: "Timo Stich"
137      *
138      * @param displayName the human readable representation of the address.
139      */

140     public void setDisplayName(String JavaDoc displayName) {
141         this.displayName = displayName;
142     }
143
144     
145     /**
146      * @see java.lang.Object#equals(java.lang.Object)
147      */

148     public boolean equals(Object JavaDoc arg0) {
149         boolean result = false;
150         if ((arg0 != null) && (arg0 instanceof Address)) {
151             Address a = (Address) arg0;
152             result = true;
153             if (mailAddress != null) {
154                 result = result && mailAddress.equals(a.getMailAddress());
155             }
156         }
157
158         return result;
159     }
160
161
162     /**
163      * @see java.lang.Object#toString()
164      */

165     public String JavaDoc toString() {
166         if (displayName == null || displayName.length() == 0) {
167             return mailAddress;
168         } else {
169             return "\"" + displayName + "\" " + getCanonicalMailAddress();
170         }
171     }
172
173     /**
174      * @param out
175      * @throws IOException
176      */

177     private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
178         if (displayName != null) {
179             // write a 1 in front so we know we have a displayname
180
out.writeByte(1);
181             out.writeUTF(displayName);
182             out.writeUTF(mailAddress);
183         } else {
184             // write a 0 in front so we know we have no displayname
185
out.writeByte(0);
186             out.writeUTF(mailAddress);
187         }
188     }
189
190     private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
191         byte mode = in.readByte();
192
193         if (mode == 1) {
194             displayName = in.readUTF();
195         }
196
197         mailAddress = in.readUTF();
198     }
199
200     /**
201      * @see java.lang.Comparable#compareTo(java.lang.Object)
202      */

203     public int compareTo(Object JavaDoc arg0) {
204         return mailAddress.compareTo(((Address) arg0).getMailAddress());
205     }
206
207     /**
208      * @see java.lang.Object#hashCode()
209      */

210     public int hashCode() {
211         return mailAddress.hashCode();
212     }
213
214     /**
215      * Uses the {@link AddressParser} to parse the CharSequence.
216      *
217      * @param source the address that is parsed, e.g. "Timo Stich &lt;my@mail.org&gt;"
218      * @return the Address object that represents the input CharSequence
219      * @throws ParserException
220      */

221     public static Address parse(CharSequence JavaDoc source) throws ParserException {
222         return AddressParser.parseAddress(source);
223     }
224 }
225
Popular Tags