KickJava   Java API By Example, From Geeks To Geeks.

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


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

27
28 package javax.mail.search;
29
30 import javax.mail.Message JavaDoc;
31 import javax.mail.Address JavaDoc;
32
33 /**
34  * This class implements string comparisons for the Recipient Address
35  * headers. <p>
36  *
37  * Note that this class differs from the <code>RecipientTerm</code> class
38  * in that this class does comparisons on address strings rather than Address
39  * objects. The string comparisons are case-insensitive.
40  *
41  * @since JavaMail 1.1
42  */

43
44 public final class RecipientStringTerm extends AddressStringTerm JavaDoc {
45
46     /**
47      * The recipient type.
48      *
49      * @serial
50      */

51     private Message.RecipientType JavaDoc type;
52
53     private static final long serialVersionUID = -8293562089611618849L;
54
55     /**
56      * Constructor.
57      *
58      * @param type the recipient type
59      * @param pattern the address pattern to be compared.
60      */

61     public RecipientStringTerm(Message.RecipientType JavaDoc type, String JavaDoc pattern) {
62     super(pattern);
63     this.type = type;
64     }
65
66     /**
67      * Return the type of recipient to match with.
68      */

69     public Message.RecipientType JavaDoc getRecipientType() {
70     return type;
71     }
72
73     /**
74      * Check whether the address specified in the constructor is
75      * a substring of the recipient address of this Message.
76      *
77      * @param msg The comparison is applied to this Message's recepient
78      * address.
79      * @return true if the match succeeds, otherwise false.
80      */

81     public boolean match(Message JavaDoc msg) {
82     Address JavaDoc[] recipients;
83
84     try {
85         recipients = msg.getRecipients(type);
86     } catch (Exception JavaDoc e) {
87         return false;
88     }
89
90     if (recipients == null)
91         return false;
92     
93     for (int i=0; i < recipients.length; i++)
94         if (super.match(recipients[i]))
95         return true;
96     return false;
97     }
98
99     /**
100      * Equality comparison.
101      */

102     public boolean equals(Object JavaDoc obj) {
103     if (!(obj instanceof RecipientStringTerm JavaDoc))
104         return false;
105     RecipientStringTerm JavaDoc rst = (RecipientStringTerm JavaDoc)obj;
106     return rst.type.equals(this.type) && super.equals(obj);
107     }
108
109     /**
110      * Compute a hashCode for this object.
111      */

112     public int hashCode() {
113     return type.hashCode() + super.hashCode();
114     }
115 }
116
Popular Tags