001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package javax.mail.search;
021
022 /**
023 * A Term that provides matching criteria for Strings.
024 *
025 * @version $Rev: 920714 $ $Date: 2010-03-09 01:55:49 -0500 (Tue, 09 Mar 2010) $
026 */
027 public abstract class StringTerm extends SearchTerm {
028
029 private static final long serialVersionUID = 1274042129007696269L;
030
031 /**
032 * If true, case should be ignored during matching.
033 */
034 protected boolean ignoreCase;
035
036 /**
037 * The pattern associated with this term.
038 */
039 protected String pattern;
040
041 /**
042 * Constructor specifying a pattern.
043 * Defaults to case insensitive matching.
044 * @param pattern the pattern for this term
045 */
046 protected StringTerm(String pattern) {
047 this(pattern, true);
048 }
049
050 /**
051 * Constructor specifying pattern and case sensitivity.
052 * @param pattern the pattern for this term
053 * @param ignoreCase if true, case should be ignored during matching
054 */
055 protected StringTerm(String pattern, boolean ignoreCase) {
056 this.pattern = pattern;
057 this.ignoreCase = ignoreCase;
058 }
059
060 /**
061 * Return the pattern associated with this term.
062 * @return the pattern associated with this term
063 */
064 public String getPattern() {
065 return pattern;
066 }
067
068 /**
069 * Indicate if case should be ignored when matching.
070 * @return if true, case should be ignored during matching
071 */
072 public boolean getIgnoreCase() {
073 return ignoreCase;
074 }
075
076 /**
077 * Determine if the pattern associated with this term is a substring of the
078 * supplied String. If ignoreCase is true then case will be ignored.
079 *
080 * @param match the String to compare to
081 * @return true if this patter is a substring of the supplied String
082 */
083 protected boolean match(String match) {
084 int matchLength = pattern.length();
085 int length = match.length() - matchLength;
086
087 for (int i = 0; i <= length; i++) {
088 if (match.regionMatches(ignoreCase, i, pattern, 0, matchLength)) {
089 return true;
090 }
091 }
092 return false;
093 }
094
095 public boolean equals(Object other) {
096 if (this == other) return true;
097 if (other instanceof StringTerm == false) return false;
098
099 StringTerm term = (StringTerm)other;
100
101 if (ignoreCase) {
102 return term.pattern.equalsIgnoreCase(pattern) && term.ignoreCase == ignoreCase;
103 }
104 else {
105 return term.pattern.equals(pattern) && term.ignoreCase == ignoreCase;
106 }
107 }
108
109 public int hashCode() {
110 return pattern.hashCode() + (ignoreCase ? 32 : 79);
111 }
112 }