- /*
- * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
-
- package javax.mail.search;
-
- /**
- * This class implements the match method for Strings. The current
- * implementation provides only for substring matching. We
- * could add comparisons (like strcmp ...).
- *
- * @author Bill Shannon
- * @author John Mani
- */
- public abstract class StringTerm extends SearchTerm {
- /**
- * The pattern.
- *
- * @serial
- */
- protected String pattern;
-
- /**
- * Ignore case when comparing?
- *
- * @serial
- */
- protected boolean ignoreCase;
-
- protected StringTerm(String pattern) {
- this.pattern = pattern;
- ignoreCase = true;
- }
-
- protected StringTerm(String pattern, boolean ignoreCase) {
- this.pattern = pattern;
- this.ignoreCase = ignoreCase;
- }
-
- /**
- * Return the string to match with.
- */
- public String getPattern() {
- return pattern;
- }
-
- /**
- * Return true if we should ignore case when matching.
- */
- public boolean getIgnoreCase() {
- return ignoreCase;
- }
-
- protected boolean match(String s) {
- int len = s.length() - pattern.length();
- for (int i=0; i <= len; i++) {
- if (s.regionMatches(ignoreCase, i,
- pattern, 0, pattern.length()))
- return true;
- }
- return false;
- }
-
- /**
- * Equality comparison.
- */
- public boolean equals(Object obj) {
- if (!(obj instanceof StringTerm))
- return false;
- StringTerm st = (StringTerm)obj;
- if (ignoreCase)
- return st.pattern.equalsIgnoreCase(this.pattern) &&
- st.ignoreCase == this.ignoreCase;
- else
- return st.pattern.equals(this.pattern) &&
- st.ignoreCase == this.ignoreCase;
- }
-
- /**
- * Compute a hashCode for this object.
- */
- public int hashCode() {
- return ignoreCase ? pattern.hashCode() : ~pattern.hashCode();
- }
- }