Skip to content

Package: StringTerm

StringTerm

nameinstructionbranchcomplexitylinemethod
StringTerm(String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
StringTerm(String, boolean)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
equals(Object)
M: 18 C: 23
56%
M: 8 C: 4
33%
M: 6 C: 1
14%
M: 2 C: 4
67%
M: 0 C: 1
100%
getIgnoreCase()
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%
getPattern()
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: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
match(String)
M: 30 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 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: /**
20: * This class implements the match method for Strings. The current
21: * implementation provides only for substring matching. We
22: * could add comparisons (like strcmp ...).
23: *
24: * @author Bill Shannon
25: * @author John Mani
26: */
27: public abstract class StringTerm extends SearchTerm {
28: /**
29: * The pattern.
30: *
31: * @serial
32: */
33: protected String pattern;
34:
35: /**
36: * Ignore case when comparing?
37: *
38: * @serial
39: */
40: protected boolean ignoreCase;
41:
42: private static final long serialVersionUID = 1274042129007696269L;
43:
44: /**
45: * Construct a StringTerm with the given pattern.
46: * Case will be ignored.
47: *
48: * @param        pattern                the pattern
49: */
50: protected StringTerm(String pattern) {
51:         this.pattern = pattern;
52:         ignoreCase = true;
53: }
54:
55: /**
56: * Construct a StringTerm with the given pattern and ignoreCase flag.
57: *
58: * @param        pattern                the pattern
59: * @param        ignoreCase        should we ignore case?
60: */
61: protected StringTerm(String pattern, boolean ignoreCase) {
62:         this.pattern = pattern;
63:         this.ignoreCase = ignoreCase;
64: }
65:
66: /**
67: * Return the string to match with.
68: *
69: * @return        the string to match
70: */
71: public String getPattern() {
72:         return pattern;
73: }
74:
75: /**
76: * Return true if we should ignore case when matching.
77: *
78: * @return        true if we should ignore case
79: */
80: public boolean getIgnoreCase() {
81:         return ignoreCase;
82: }
83:
84: protected boolean match(String s) {
85:         int len = s.length() - pattern.length();
86:•        for (int i=0; i <= len; i++) {
87:•         if (s.regionMatches(ignoreCase, i,
88:                                 pattern, 0, pattern.length()))
89:                 return true;
90:         }
91:         return false;
92: }
93:
94: /**
95: * Equality comparison.
96: */
97: @Override
98: public boolean equals(Object obj) {
99:•        if (!(obj instanceof StringTerm))
100:          return false;
101:         StringTerm st = (StringTerm)obj;
102:•        if (ignoreCase)
103:•         return st.pattern.equalsIgnoreCase(this.pattern) &&
104:                  st.ignoreCase == this.ignoreCase;
105:         else
106:•         return st.pattern.equals(this.pattern) &&
107:                  st.ignoreCase == this.ignoreCase;
108: }
109:
110: /**
111: * Compute a hashCode for this object.
112: */
113: @Override
114: public int hashCode() {
115:•        return ignoreCase ? pattern.hashCode() : ~pattern.hashCode();
116: }
117: }