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: 593593 $ $Date: 2007-11-09 12:04:20 -0500 (Fri, 09 Nov 2007) $
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            int matchLength = pattern.length(); 
082            int length = match.length() - matchLength;        
083            
084            for (int i = 0; i <= length; i++) {
085                if (match.regionMatches(ignoreCase, i, pattern, 0, matchLength)) {
086                    return true; 
087                }
088            }
089            return false;
090        }
091    
092        public boolean equals(Object other) {
093            if (this == other) return true;
094            if (other instanceof StringTerm == false) return false;
095            
096            StringTerm term = (StringTerm)other; 
097            
098            if (ignoreCase) {
099                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    }