Skip to content

Package: HeaderTerm

HeaderTerm

nameinstructionbranchcomplexitylinemethod
HeaderTerm(String, String)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
equals(Object)
M: 3 C: 19
86%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 1 C: 3
75%
M: 0 C: 1
100%
getHeaderName()
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%
hashCode()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
match(Message)
M: 31 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 10 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.search;
18:
19: import java.util.Locale;
20: import jakarta.mail.Message;
21:
22: /**
23: * This class implements comparisons for Message headers.
24: * The comparison is case-insensitive.
25: *
26: * @author Bill Shannon
27: * @author John Mani
28: */
29: public final class HeaderTerm extends StringTerm {
30: /**
31: * The name of the header.
32: *
33: * @serial
34: */
35: private String headerName;
36:
37: private static final long serialVersionUID = 8342514650333389122L;
38:
39: /**
40: * Constructor.
41: *
42: * @param headerName The name of the header
43: * @param pattern The pattern to search for
44: */
45: public HeaderTerm(String headerName, String pattern) {
46:         super(pattern);
47:         this.headerName = headerName;
48: }
49:
50: /**
51: * Return the name of the header to compare with.
52: *
53: * @return        the name of the header
54: */
55: public String getHeaderName() {
56:         return headerName;
57: }
58:
59: /**
60: * The header match method.
61: *
62: * @param msg        The match is applied to this Message's header
63: * @return                true if the match succeeds, otherwise false
64: */
65: @Override
66: public boolean match(Message msg) {
67:         String[] headers;
68:
69:         try {
70:          headers = msg.getHeader(headerName);
71:         } catch (Exception e) {
72:          return false;
73:         }
74:
75:•        if (headers == null)
76:          return false;
77:
78:•        for (int i=0; i < headers.length; i++)
79:•         if (super.match(headers[i]))
80:                 return true;
81:         return false;
82: }
83:
84: /**
85: * Equality comparison.
86: */
87: @Override
88: public boolean equals(Object obj) {
89:•        if (!(obj instanceof HeaderTerm))
90:          return false;
91:         HeaderTerm ht = (HeaderTerm)obj;
92:         // XXX - depends on header comparisons being case independent
93:•        return ht.headerName.equalsIgnoreCase(headerName) && super.equals(ht);
94: }
95:
96: /**
97: * Compute a hashCode for this object.
98: */
99: @Override
100: public int hashCode() {
101:         // XXX - depends on header comparisons being case independent
102:         return headerName.toLowerCase(Locale.ENGLISH).hashCode() +
103:                                         super.hashCode();
104: }
105: }