KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > internet > NewsAddress


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)NewsAddress.java 1.8 05/08/29
24  *
25  * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.internet;
29
30 import java.util.Vector JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import javax.mail.*;
33
34 /**
35  * This class models an RFC1036 newsgroup address.
36  *
37  * @author Bill Shannon
38  * @author John Mani
39  */

40
41 public class NewsAddress extends Address {
42
43     protected String JavaDoc newsgroup;
44     protected String JavaDoc host; // may be null
45

46     private static final long serialVersionUID = -4203797299824684143L;
47
48     /**
49      * Default constructor.
50      */

51     public NewsAddress() { }
52
53     /**
54      * Construct a NewsAddress with the given newsgroup.
55      *
56      * @param newsgroup the newsgroup
57      */

58     public NewsAddress(String JavaDoc newsgroup) {
59     this(newsgroup, null);
60     }
61
62     /**
63      * Construct a NewsAddress with the given newsgroup and host.
64      *
65      * @param newsgroup the newsgroup
66      * @param host the host
67      */

68     public NewsAddress(String JavaDoc newsgroup, String JavaDoc host) {
69     this.newsgroup = newsgroup;
70     this.host = host;
71     }
72
73     /**
74      * Return the type of this address. The type of a NewsAddress
75      * is "news".
76      */

77     public String JavaDoc getType() {
78     return "news";
79     }
80
81     /**
82      * Set the newsgroup.
83      *
84      * @param newsgroup the newsgroup
85      */

86     public void setNewsgroup(String JavaDoc newsgroup) {
87     this.newsgroup = newsgroup;
88     }
89
90     /**
91      * Get the newsgroup.
92      *
93      * @return newsgroup
94      */

95     public String JavaDoc getNewsgroup() {
96     return newsgroup;
97     }
98
99     /**
100      * Set the host.
101      *
102      * @param host the host
103      */

104     public void setHost(String JavaDoc host) {
105     this.host = host;
106     }
107
108     /**
109      * Get the host.
110      *
111      * @return host
112      */

113     public String JavaDoc getHost() {
114     return host;
115     }
116
117     /**
118      * Convert this address into a RFC 1036 address.
119      *
120      * @return newsgroup
121      */

122     public String JavaDoc toString() {
123     return newsgroup;
124     }
125
126     /**
127      * The equality operator.
128      */

129     public boolean equals(Object JavaDoc a) {
130     if (!(a instanceof NewsAddress JavaDoc))
131         return false;
132
133     NewsAddress JavaDoc s = (NewsAddress JavaDoc)a;
134     return newsgroup.equals(s.newsgroup) &&
135         ((host == null && s.host == null) ||
136          (host != null && s.host != null && host.equalsIgnoreCase(s.host)));
137     }
138
139     /**
140      * Compute a hash code for the address.
141      */

142     public int hashCode() {
143     int hash = 0;
144     if (newsgroup != null)
145         hash += newsgroup.hashCode();
146     if (host != null)
147         hash += host.toLowerCase().hashCode();
148     return hash;
149     }
150
151     /**
152      * Convert the given array of NewsAddress objects into
153      * a comma separated sequence of address strings. The
154      * resulting string contains only US-ASCII characters, and
155      * hence is mail-safe.
156      *
157      * @param addresses array of NewsAddress objects
158      * @exception ClassCastException, if any address object in the
159      * given array is not a NewsAddress objects. Note
160      * that this is a RuntimeException.
161      * @return comma separated address strings
162      */

163     public static String JavaDoc toString(Address[] addresses) {
164     if (addresses == null || addresses.length == 0)
165         return null;
166
167     StringBuffer JavaDoc s =
168         new StringBuffer JavaDoc(((NewsAddress JavaDoc)addresses[0]).toString());
169     for (int i = 1; i < addresses.length; i++)
170         s.append(",").append(((NewsAddress JavaDoc)addresses[i]).toString());
171     
172     return s.toString();
173     }
174
175     /**
176      * Parse the given comma separated sequence of newsgroup into
177      * NewsAddress objects.
178      *
179      * @param newsgroups comma separated newsgroup string
180      * @return array of NewsAddress objects
181      * @exception AddressException if the parse failed
182      */

183     public static NewsAddress JavaDoc[] parse(String JavaDoc newsgroups)
184                 throws AddressException JavaDoc {
185     // XXX - verify format of newsgroup name?
186
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(newsgroups, ",");
187     Vector JavaDoc nglist = new Vector JavaDoc();
188     while (st.hasMoreTokens()) {
189         String JavaDoc ng = st.nextToken();
190         nglist.addElement(new NewsAddress JavaDoc(ng));
191     }
192     int size = nglist.size();
193     NewsAddress JavaDoc[] na = new NewsAddress JavaDoc[size];
194     if (size > 0)
195         nglist.copyInto(na);
196     return na;
197     }
198 }
199
Popular Tags