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    import sun.security.provider.Sun;
029    
030    /**
031     * A representation of an RFC1036 Internet newsgroup address.
032     *
033     * @version $Rev: 467553 $ $Date: 2006-10-25 00:01:51 -0400 (Wed, 25 Oct 2006) $
034     */
035    public class NewsAddress extends Address {
036        /**
037         * The host for this newsgroup
038         */
039        protected String host;
040    
041        /**
042         * The name of this newsgroup
043         */
044        protected String newsgroup;
045    
046        public NewsAddress() {
047        }
048    
049        public NewsAddress(String newsgroup) {
050            this.newsgroup = newsgroup;
051        }
052    
053        public NewsAddress(String newsgroup, String host) {
054            this.newsgroup = newsgroup;
055            this.host = host;
056        }
057    
058        /**
059         * The type of this address; always "news".
060         * @return "news"
061         */
062        public String getType() {
063            return "news";
064        }
065    
066        public void setNewsgroup(String newsgroup) {
067            this.newsgroup = newsgroup;
068        }
069    
070        public String getNewsgroup() {
071            return newsgroup;
072        }
073    
074        public void setHost(String host) {
075            this.host = host;
076        }
077    
078        public String getHost() {
079            return host;
080        }
081    
082        public String toString() {
083            // Sun impl only appears to return the newsgroup name, no host.
084            return newsgroup;
085        }
086    
087        public boolean equals(Object o) {
088            if (this == o) return true;
089            if (!(o instanceof NewsAddress)) return false;
090    
091            final NewsAddress newsAddress = (NewsAddress) o;
092    
093            if (host != null ? !host.equals(newsAddress.host) : newsAddress.host != null) return false;
094            if (newsgroup != null ? !newsgroup.equals(newsAddress.newsgroup) : newsAddress.newsgroup != null) return false;
095    
096            return true;
097        }
098    
099        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    }