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: 1
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: 31 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: 62 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, 2021 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 java.util.ArrayList;
20: import java.util.List;
21: import java.util.StringTokenizer;
22: import java.util.Locale;
23: import jakarta.mail.*;
24:
25: /**
26: * This class models an RFC1036 newsgroup address.
27: *
28: * @author Bill Shannon
29: * @author John Mani
30: */
31:
32: public class NewsAddress extends Address {
33:
34: protected String newsgroup;
35: protected String host;        // may be null
36:
37: private static final long serialVersionUID = -4203797299824684143L;
38:
39: /**
40: * Default constructor.
41: */
42: public NewsAddress() { }
43:
44: /**
45: * Construct a NewsAddress with the given newsgroup.
46: *
47: * @param newsgroup        the newsgroup
48: */
49: public NewsAddress(String newsgroup) {
50:         this(newsgroup, null);
51: }
52:
53: /**
54: * Construct a NewsAddress with the given newsgroup and host.
55: *
56: * @param newsgroup        the newsgroup
57: * @param host        the host
58: */
59: public NewsAddress(String newsgroup, String host) {
60:         // XXX - this method should throw an exception so we can report
61:         // illegal addresses, but for now just remove whitespace
62:         this.newsgroup = newsgroup.replaceAll("\\s+", "");
63:         this.host = host;
64: }
65:
66: /**
67: * Return the type of this address. The type of a NewsAddress
68: * is "news".
69: */
70: @Override
71: public String getType() {
72:         return "news";
73: }
74:
75: /**
76: * Set the newsgroup.
77: *
78: * @param        newsgroup        the newsgroup
79: */
80: public void setNewsgroup(String newsgroup) {
81:         this.newsgroup = newsgroup;
82: }
83:
84: /**
85: * Get the newsgroup.
86: *
87: * @return        newsgroup
88: */
89: public String getNewsgroup() {
90:         return newsgroup;
91: }
92:
93: /**
94: * Set the host.
95: *
96: * @param        host        the host
97: */
98: public void setHost(String host) {
99:         this.host = host;
100: }
101:
102: /**
103: * Get the host.
104: *
105: * @return        host
106: */
107: public String getHost() {
108:         return host;
109: }
110:
111: /**
112: * Convert this address into a RFC 1036 address.
113: *
114: * @return                newsgroup
115: */
116: @Override
117: public String toString() {
118:         return newsgroup;
119: }
120:
121: /**
122: * The equality operator.
123: */
124: @Override
125: public boolean equals(Object a) {
126:•        if (!(a instanceof NewsAddress))
127:          return false;
128:
129:         NewsAddress s = (NewsAddress)a;
130:•        return ((newsgroup == null && s.newsgroup == null) ||
131:•         (newsgroup != null && newsgroup.equals(s.newsgroup))) &&
132:          ((host == null && s.host == null) ||
133:•         (host != null && s.host != null && host.equalsIgnoreCase(s.host)));
134: }
135:
136: /**
137: * Compute a hash code for the address.
138: */
139: @Override
140: public int hashCode() {
141:         int hash = 0;
142:•        if (newsgroup != null)
143:          hash += newsgroup.hashCode();
144:•        if (host != null)
145:          hash += host.toLowerCase(Locale.ENGLISH).hashCode();
146:         return hash;
147: }
148:
149: /**
150: * Convert the given array of NewsAddress objects into
151: * a comma separated sequence of address strings. The
152: * resulting string contains only US-ASCII characters, and
153: * hence is mail-safe.
154: *
155: * @param addresses        array of NewsAddress objects
156: * @exception         ClassCastException if any address object in the
157: *         given array is not a NewsAddress objects. Note
158: *         that this is a RuntimeException.
159: * @return                 comma separated address strings
160: */
161: public static String toString(Address[] addresses) {
162:•        if (addresses == null || addresses.length == 0)
163:          return null;
164:
165:         StringBuilder s =
166:                 new StringBuilder(((NewsAddress)addresses[0]).toString());
167:         int used = s.length();
168:•        for (int i = 1; i < addresses.length; i++) {
169:          s.append(",");
170:          used++;
171:          String ng = ((NewsAddress)addresses[i]).toString();
172:•         if (used + ng.length() > 76) {
173:                 s.append("\r\n\t");
174:                 used = 8;
175:          }
176:          s.append(ng);
177:          used += ng.length();
178:         }
179:         
180:         return s.toString();
181: }
182:
183: /**
184: * Parse the given comma separated sequence of newsgroups into
185: * NewsAddress objects.
186: *
187: * @param newsgroups        comma separated newsgroup string
188: * @return                        array of NewsAddress objects
189: * @exception                AddressException if the parse failed
190: */
191: public static NewsAddress[] parse(String newsgroups)
192:                                 throws AddressException {
193:         // XXX - verify format of newsgroup name?
194:         StringTokenizer st = new StringTokenizer(newsgroups, ",");
195:         List<NewsAddress> nglist = new ArrayList<>();
196:•        while (st.hasMoreTokens()) {
197:          String ng = st.nextToken();
198:          nglist.add(new NewsAddress(ng));
199:         }
200:         return nglist.toArray(new NewsAddress[nglist.size()]);
201: }
202: }