View Javadoc

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