1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package javax.mail.search;
19
20 /**
21 * A Term that provides matching criteria for Strings.
22 *
23 * @version $Rev: 125583 $ $Date: 2005-01-18 19:44:27 -0800 (Tue, 18 Jan 2005) $
24 */
25 public abstract class StringTerm extends SearchTerm {
26 /**
27 * If true, case should be ignored during matching.
28 */
29 protected boolean ignoreCase;
30
31 /**
32 * The pattern associated with this term.
33 */
34 protected String pattern;
35
36 /**
37 * Constructor specifying a pattern.
38 * Defaults to case insensitive matching.
39 * @param pattern the pattern for this term
40 */
41 protected StringTerm(String pattern) {
42 this(pattern, true);
43 }
44
45 /**
46 * Constructor specifying pattern and case sensitivity.
47 * @param pattern the pattern for this term
48 * @param ignoreCase if true, case should be ignored during matching
49 */
50 protected StringTerm(String pattern, boolean ignoreCase) {
51 this.pattern = pattern;
52 this.ignoreCase = ignoreCase;
53 }
54
55 /**
56 * Return the pattern associated with this term.
57 * @return the pattern associated with this term
58 */
59 public String getPattern() {
60 return pattern;
61 }
62
63 /**
64 * Indicate if case should be ignored when matching.
65 * @return if true, case should be ignored during matching
66 */
67 public boolean getIgnoreCase() {
68 return ignoreCase;
69 }
70
71 /**
72 * Determine if the pattern associated with this term is a substring of the
73 * supplied String. If ignoreCase is true then case will be ignored.
74 *
75 * @param match the String to compare to
76 * @return true if this patter is a substring of the supplied String
77 */
78 protected boolean match(String match) {
79 match: for (int length = match.length() - pattern.length(); length > 0; length--) {
80 for (int i = 0; i < pattern.length(); i++) {
81 char c1 = match.charAt(length + i);
82 char c2 = match.charAt(i);
83 if (c1 == c2) {
84 continue;
85 }
86 if (ignoreCase) {
87 if (Character.toLowerCase(c1) == Character.toLowerCase(c2)) {
88 continue;
89 }
90 if (Character.toUpperCase(c1) == Character.toUpperCase(c2)) {
91 continue;
92 }
93 }
94 continue match;
95 }
96 return true;
97 }
98 return false;
99 }
100
101 public boolean equals(Object other) {
102 return super.equals(other)
103 && ((StringTerm) other).pattern.equals(pattern)
104 && ((StringTerm) other).ignoreCase == ignoreCase;
105 }
106
107 public int hashCode() {
108 return super.hashCode() + pattern.hashCode() + (ignoreCase ? 32 : 79);
109 }
110 }