View Javadoc

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