KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > search > StringTerm


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)StringTerm.java 1.8 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.search;
29
30 /**
31  * This class implements the match method for Strings. The current
32  * implementation provides only for substring matching. We
33  * could add comparisons (like strcmp ...).
34  *
35  * @author Bill Shannon
36  * @author John Mani
37  */

38 public abstract class StringTerm extends SearchTerm JavaDoc {
39     /**
40      * The pattern.
41      *
42      * @serial
43      */

44     protected String JavaDoc pattern;
45
46     /**
47      * Ignore case when comparing?
48      *
49      * @serial
50      */

51     protected boolean ignoreCase;
52
53     private static final long serialVersionUID = 1274042129007696269L;
54
55     protected StringTerm(String JavaDoc pattern) {
56     this.pattern = pattern;
57     ignoreCase = true;
58     }
59
60     protected StringTerm(String JavaDoc pattern, boolean ignoreCase) {
61     this.pattern = pattern;
62     this.ignoreCase = ignoreCase;
63     }
64
65     /**
66      * Return the string to match with.
67      */

68     public String JavaDoc getPattern() {
69     return pattern;
70     }
71
72     /**
73      * Return true if we should ignore case when matching.
74      */

75     public boolean getIgnoreCase() {
76     return ignoreCase;
77     }
78
79     protected boolean match(String JavaDoc s) {
80     int len = s.length() - pattern.length();
81     for (int i=0; i <= len; i++) {
82         if (s.regionMatches(ignoreCase, i,
83                 pattern, 0, pattern.length()))
84         return true;
85     }
86     return false;
87     }
88
89     /**
90      * Equality comparison.
91      */

92     public boolean equals(Object JavaDoc obj) {
93     if (!(obj instanceof StringTerm JavaDoc))
94         return false;
95     StringTerm JavaDoc st = (StringTerm JavaDoc)obj;
96     if (ignoreCase)
97         return st.pattern.equalsIgnoreCase(this.pattern) &&
98             st.ignoreCase == this.ignoreCase;
99     else
100         return st.pattern.equals(this.pattern) &&
101             st.ignoreCase == this.ignoreCase;
102     }
103
104     /**
105      * Compute a hashCode for this object.
106      */

107     public int hashCode() {
108     return ignoreCase ? pattern.hashCode() : ~pattern.hashCode();
109     }
110 }
111
Popular Tags