001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package javax.mail.internet;
021
022 import java.util.ArrayList;
023 import java.util.List;
024 import java.util.StringTokenizer;
025
026 import javax.mail.Address;
027
028 /**
029 * A representation of an RFC1036 Internet newsgroup address.
030 *
031 * @version $Rev: 920714 $ $Date: 2010-03-09 01:55:49 -0500 (Tue, 09 Mar 2010) $
032 */
033 public class NewsAddress extends Address {
034
035 private static final long serialVersionUID = -4203797299824684143L;
036
037 /**
038 * The host for this newsgroup
039 */
040 protected String host;
041
042 /**
043 * The name of this newsgroup
044 */
045 protected String newsgroup;
046
047 public NewsAddress() {
048 }
049
050 public NewsAddress(String newsgroup) {
051 this.newsgroup = newsgroup;
052 }
053
054 public NewsAddress(String newsgroup, String host) {
055 this.newsgroup = newsgroup;
056 this.host = host;
057 }
058
059 /**
060 * The type of this address; always "news".
061 * @return "news"
062 */
063 public String getType() {
064 return "news";
065 }
066
067 public void setNewsgroup(String newsgroup) {
068 this.newsgroup = newsgroup;
069 }
070
071 public String getNewsgroup() {
072 return newsgroup;
073 }
074
075 public void setHost(String host) {
076 this.host = host;
077 }
078
079 public String getHost() {
080 return host;
081 }
082
083 public String toString() {
084 // Sun impl only appears to return the newsgroup name, no host.
085 return newsgroup;
086 }
087
088 public boolean equals(Object o) {
089 if (this == o) return true;
090 if (!(o instanceof NewsAddress)) return false;
091
092 final NewsAddress newsAddress = (NewsAddress) o;
093
094 if (host != null ? !host.equals(newsAddress.host) : newsAddress.host != null) return false;
095 if (newsgroup != null ? !newsgroup.equals(newsAddress.newsgroup) : newsAddress.newsgroup != null) return false;
096
097 return true;
098 }
099
100 public int hashCode() {
101 int result;
102 result = (host != null ? host.toLowerCase().hashCode() : 0);
103 result = 29 * result + (newsgroup != null ? newsgroup.hashCode() : 0);
104 return result;
105 }
106
107 /**
108 * Parse a comma-spearated list of addresses.
109 *
110 * @param addresses the list to parse
111 * @return the array of extracted addresses
112 * @throws AddressException if one of the addresses is invalid
113 */
114 public static NewsAddress[] parse(String addresses) throws AddressException {
115 List result = new ArrayList();
116 StringTokenizer tokenizer = new StringTokenizer(addresses, ",");
117 while (tokenizer.hasMoreTokens()) {
118 String address = tokenizer.nextToken().trim();
119 int index = address.indexOf('@');
120 if (index == -1) {
121 result.add(new NewsAddress(address));
122 } else {
123 String newsgroup = address.substring(0, index).trim();
124 String host = address.substring(index+1).trim();
125 result.add(new NewsAddress(newsgroup, host));
126 }
127 }
128 return (NewsAddress[]) result.toArray(new NewsAddress[result.size()]);
129 }
130
131 /**
132 * Convert the supplied addresses to a comma-separated String.
133 * If addresses is null, returns null; if empty, returns an empty string.
134 *
135 * @param addresses the addresses to convert
136 * @return a comma-separated list of addresses
137 */
138 public static String toString(Address[] addresses) {
139 if (addresses == null) {
140 return null;
141 }
142 if (addresses.length == 0) {
143 return "";
144 }
145
146 StringBuffer result = new StringBuffer(addresses.length * 32);
147 result.append(addresses[0]);
148 for (int i = 1; i < addresses.length; i++) {
149 result.append(',').append(addresses[i].toString());
150 }
151 return result.toString();
152 }
153 }