KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#)HeaderTerm.java 1.9 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.search;
29
30 import javax.mail.Message JavaDoc;
31
32 /**
33  * This class implements comparisons for Message headers.
34  * The comparison is case-insensitive.
35  *
36  * @author Bill Shannon
37  * @author John Mani
38  */

39 public final class HeaderTerm extends StringTerm JavaDoc {
40     /**
41      * The name of the header.
42      *
43      * @serial
44      */

45     protected String JavaDoc headerName;
46
47     private static final long serialVersionUID = 8342514650333389122L;
48
49     /**
50      * Constructor.
51      *
52      * @param headerName The name of the header
53      * @param pattern The pattern to search for
54      */

55     public HeaderTerm(String JavaDoc headerName, String JavaDoc pattern) {
56     super(pattern);
57     this.headerName = headerName;
58     }
59
60     /**
61      * Return the name of the header to compare with.
62      */

63     public String JavaDoc getHeaderName() {
64     return headerName;
65     }
66
67     /**
68      * The header match method.
69      *
70      * @param msg The match is applied to this Message's header
71      * @return true if the match succeeds, otherwise false
72      */

73     public boolean match(Message JavaDoc msg) {
74     String JavaDoc[] headers;
75
76     try {
77         headers = msg.getHeader(headerName);
78     } catch (Exception JavaDoc e) {
79         return false;
80     }
81
82     if (headers == null)
83         return false;
84
85     for (int i=0; i < headers.length; i++)
86         if (super.match(headers[i]))
87         return true;
88     return false;
89     }
90
91     /**
92      * Equality comparison.
93      */

94     public boolean equals(Object JavaDoc obj) {
95     if (!(obj instanceof HeaderTerm JavaDoc))
96         return false;
97     HeaderTerm JavaDoc ht = (HeaderTerm JavaDoc)obj;
98     // XXX - depends on header comparisons being case independent
99
return ht.headerName.equalsIgnoreCase(headerName) && super.equals(ht);
100     }
101
102     /**
103      * Compute a hashCode for this object.
104      */

105     public int hashCode() {
106     // XXX - depends on header comparisons being case independent
107
return headerName.toLowerCase().hashCode() + super.hashCode();
108     }
109 }
110
Popular Tags