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: 467553 $ $Date: 2006-10-25 06:01:51 +0200 (Mi, 25. Okt 2006) $ 026 */ 027 public abstract class StringTerm extends SearchTerm { 028 /** 029 * If true, case should be ignored during matching. 030 */ 031 protected boolean ignoreCase; 032 033 /** 034 * The pattern associated with this term. 035 */ 036 protected String pattern; 037 038 /** 039 * Constructor specifying a pattern. 040 * Defaults to case insensitive matching. 041 * @param pattern the pattern for this term 042 */ 043 protected StringTerm(String pattern) { 044 this(pattern, true); 045 } 046 047 /** 048 * Constructor specifying pattern and case sensitivity. 049 * @param pattern the pattern for this term 050 * @param ignoreCase if true, case should be ignored during matching 051 */ 052 protected StringTerm(String pattern, boolean ignoreCase) { 053 this.pattern = pattern; 054 this.ignoreCase = ignoreCase; 055 } 056 057 /** 058 * Return the pattern associated with this term. 059 * @return the pattern associated with this term 060 */ 061 public String getPattern() { 062 return pattern; 063 } 064 065 /** 066 * Indicate if case should be ignored when matching. 067 * @return if true, case should be ignored during matching 068 */ 069 public boolean getIgnoreCase() { 070 return ignoreCase; 071 } 072 073 /** 074 * Determine if the pattern associated with this term is a substring of the 075 * supplied String. If ignoreCase is true then case will be ignored. 076 * 077 * @param match the String to compare to 078 * @return true if this patter is a substring of the supplied String 079 */ 080 protected boolean match(String match) { 081 match: for (int length = match.length() - pattern.length(); length > 0; length--) { 082 for (int i = 0; i < pattern.length(); i++) { 083 char c1 = match.charAt(length + i); 084 char c2 = match.charAt(i); 085 if (c1 == c2) { 086 continue; 087 } 088 if (ignoreCase) { 089 if (Character.toLowerCase(c1) == Character.toLowerCase(c2)) { 090 continue; 091 } 092 if (Character.toUpperCase(c1) == Character.toUpperCase(c2)) { 093 continue; 094 } 095 } 096 continue match; 097 } 098 return true; 099 } 100 return false; 101 } 102 103 public boolean equals(Object other) { 104 return super.equals(other) 105 && ((StringTerm) other).pattern.equals(pattern) 106 && ((StringTerm) other).ignoreCase == ignoreCase; 107 } 108 109 public int hashCode() { 110 return super.hashCode() + pattern.hashCode() + (ignoreCase ? 32 : 79); 111 } 112 }