View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package javax.mail.internet;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.StringTokenizer;
25  
26  import javax.mail.Address;
27  
28  import sun.security.provider.Sun;
29  
30  /**
31   * A representation of an RFC1036 Internet newsgroup address.
32   *
33   * @version $Rev: 467553 $ $Date: 2006-10-25 00:01:51 -0400 (Wed, 25 Oct 2006) $
34   */
35  public class NewsAddress extends Address {
36      /**
37       * The host for this newsgroup
38       */
39      protected String host;
40  
41      /**
42       * The name of this newsgroup
43       */
44      protected String newsgroup;
45  
46      public NewsAddress() {
47      }
48  
49      public NewsAddress(String newsgroup) {
50          this.newsgroup = newsgroup;
51      }
52  
53      public NewsAddress(String newsgroup, String host) {
54          this.newsgroup = newsgroup;
55          this.host = host;
56      }
57  
58      /**
59       * The type of this address; always "news".
60       * @return "news"
61       */
62      public String getType() {
63          return "news";
64      }
65  
66      public void setNewsgroup(String newsgroup) {
67          this.newsgroup = newsgroup;
68      }
69  
70      public String getNewsgroup() {
71          return newsgroup;
72      }
73  
74      public void setHost(String host) {
75          this.host = host;
76      }
77  
78      public String getHost() {
79          return host;
80      }
81  
82      public String toString() {
83          // Sun impl only appears to return the newsgroup name, no host.
84          return newsgroup;
85      }
86  
87      public boolean equals(Object o) {
88          if (this == o) return true;
89          if (!(o instanceof NewsAddress)) return false;
90  
91          final NewsAddress newsAddress = (NewsAddress) o;
92  
93          if (host != null ? !host.equals(newsAddress.host) : newsAddress.host != null) return false;
94          if (newsgroup != null ? !newsgroup.equals(newsAddress.newsgroup) : newsAddress.newsgroup != null) return false;
95  
96          return true;
97      }
98  
99      public int hashCode() {
100         int result;
101         result = (host != null ? host.toLowerCase().hashCode() : 0);
102         result = 29 * result + (newsgroup != null ? newsgroup.hashCode() : 0);
103         return result;
104     }
105 
106     /**
107      * Parse a comma-spearated list of addresses.
108      *
109      * @param addresses the list to parse
110      * @return the array of extracted addresses
111      * @throws AddressException if one of the addresses is invalid
112      */
113     public static NewsAddress[] parse(String addresses) throws AddressException {
114         List result = new ArrayList();
115         StringTokenizer tokenizer = new StringTokenizer(addresses, ",");
116         while (tokenizer.hasMoreTokens()) {
117             String address = tokenizer.nextToken().trim();
118             int index = address.indexOf('@');
119             if (index == -1) {
120                 result.add(new NewsAddress(address));
121             } else {
122                 String newsgroup = address.substring(0, index).trim();
123                 String host = address.substring(index+1).trim();
124                 result.add(new NewsAddress(newsgroup, host));
125             }
126         }
127         return (NewsAddress[]) result.toArray(new NewsAddress[result.size()]);
128     }
129 
130     /**
131      * Convert the supplied addresses to a comma-separated String.
132      * If addresses is null, returns null; if empty, returns an empty string.
133      *
134      * @param addresses the addresses to convert
135      * @return a comma-separated list of addresses
136      */
137     public static String toString(Address[] addresses) {
138         if (addresses == null) {
139             return null;
140         }
141         if (addresses.length == 0) {
142             return "";
143         }
144 
145         StringBuffer result = new StringBuffer(addresses.length * 32);
146         result.append(addresses[0]);
147         for (int i = 1; i < addresses.length; i++) {
148             result.append(',').append(addresses[i].toString());
149         }
150         return result.toString();
151     }
152 }