1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33
34
35 public class NewsAddress extends Address {
36
37
38
39 protected String host;
40
41
42
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
60
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
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
108
109
110
111
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
132
133
134
135
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 }