Skip to content

Package: NewsAddress

NewsAddress

nameinstructionbranchcomplexitylinemethod
NewsAddress()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
NewsAddress(String)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
NewsAddress(String, String)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
equals(Object)
M: 3 C: 42
93%
M: 8 C: 12
60%
M: 8 C: 3
27%
M: 1 C: 5
83%
M: 0 C: 1
100%
getHost()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getNewsgroup()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getType()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hashCode()
M: 24 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
parse(String)
M: 30 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
setHost(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setNewsgroup(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toString()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toString(Address[])
M: 60 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package jakarta.mail.internet;
18:
19: import jakarta.mail.Address;
20:
21: import java.util.ArrayList;
22: import java.util.List;
23: import java.util.Locale;
24: import java.util.StringTokenizer;
25:
26: /**
27: * This class models an RFC1036 newsgroup address.
28: *
29: * @author Bill Shannon
30: * @author John Mani
31: */
32:
33: public class NewsAddress extends Address {
34:
35: /**
36: * The newsgroup.
37: */
38: protected String newsgroup;
39:
40: /**
41: * The host. May be {@code null}.
42: */
43: protected String host;
44:
45: private static final long serialVersionUID = -4203797299824684143L;
46:
47: /**
48: * Default constructor.
49: */
50: public NewsAddress() {
51: }
52:
53: /**
54: * Construct a NewsAddress with the given newsgroup.
55: *
56: * @param newsgroup the newsgroup
57: */
58: public NewsAddress(String newsgroup) {
59: this(newsgroup, null);
60: }
61:
62: /**
63: * Construct a NewsAddress with the given newsgroup and host.
64: *
65: * @param newsgroup the newsgroup
66: * @param host the host
67: */
68: public NewsAddress(String newsgroup, String host) {
69: // XXX - this method should throw an exception so we can report
70: // illegal addresses, but for now just remove whitespace
71: this.newsgroup = newsgroup.replaceAll("\\s+", "");
72: this.host = host;
73: }
74:
75: /**
76: * Return the type of this address. The type of a NewsAddress
77: * is "news".
78: */
79: @Override
80: public String getType() {
81: return "news";
82: }
83:
84: /**
85: * Set the newsgroup.
86: *
87: * @param newsgroup the newsgroup
88: */
89: public void setNewsgroup(String newsgroup) {
90: this.newsgroup = newsgroup;
91: }
92:
93: /**
94: * Get the newsgroup.
95: *
96: * @return newsgroup
97: */
98: public String getNewsgroup() {
99: return newsgroup;
100: }
101:
102: /**
103: * Set the host.
104: *
105: * @param host the host
106: */
107: public void setHost(String host) {
108: this.host = host;
109: }
110:
111: /**
112: * Get the host.
113: *
114: * @return host
115: */
116: public String getHost() {
117: return host;
118: }
119:
120: /**
121: * Convert this address into a RFC 1036 address.
122: *
123: * @return newsgroup
124: */
125: @Override
126: public String toString() {
127: return newsgroup;
128: }
129:
130: /**
131: * The equality operator.
132: */
133: @Override
134: public boolean equals(Object a) {
135:• if (!(a instanceof NewsAddress))
136: return false;
137:
138: NewsAddress s = (NewsAddress) a;
139:• return ((newsgroup == null && s.newsgroup == null) ||
140:• (newsgroup != null && newsgroup.equals(s.newsgroup))) &&
141: ((host == null && s.host == null) ||
142:• (host != null && s.host != null && host.equalsIgnoreCase(s.host)));
143: }
144:
145: /**
146: * Compute a hash code for the address.
147: */
148: @Override
149: public int hashCode() {
150: int hash = 0;
151:• if (newsgroup != null)
152: hash += newsgroup.hashCode();
153:• if (host != null)
154: hash += host.toLowerCase(Locale.ENGLISH).hashCode();
155: return hash;
156: }
157:
158: /**
159: * Convert the given array of NewsAddress objects into
160: * a comma separated sequence of address strings. The
161: * resulting string contains only US-ASCII characters, and
162: * hence is mail-safe.
163: *
164: * @param addresses array of NewsAddress objects
165: * @return comma separated address strings
166: * @throws ClassCastException if any address object in the
167: * given array is not a NewsAddress objects. Note
168: * that this is a RuntimeException.
169: */
170: public static String toString(Address[] addresses) {
171:• if (addresses == null || addresses.length == 0)
172: return null;
173:
174: StringBuilder s =
175: new StringBuilder(addresses[0].toString());
176: int used = s.length();
177:• for (int i = 1; i < addresses.length; i++) {
178: s.append(",");
179: used++;
180: String ng = addresses[i].toString();
181:• if (used + ng.length() > 76) {
182: s.append("\r\n\t");
183: used = 8;
184: }
185: s.append(ng);
186: used += ng.length();
187: }
188:
189: return s.toString();
190: }
191:
192: /**
193: * Parse the given comma separated sequence of newsgroups into
194: * NewsAddress objects.
195: *
196: * @param newsgroups comma separated newsgroup string
197: * @return array of NewsAddress objects
198: * @throws AddressException if the parse failed
199: */
200: public static NewsAddress[] parse(String newsgroups)
201: throws AddressException {
202: // XXX - verify format of newsgroup name?
203: StringTokenizer st = new StringTokenizer(newsgroups, ",");
204: List<NewsAddress> nglist = new ArrayList<>();
205:• while (st.hasMoreTokens()) {
206: String ng = st.nextToken();
207: nglist.add(new NewsAddress(ng));
208: }
209: return nglist.toArray(new NewsAddress[0]);
210: }
211: }