KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#)RecipientTerm.java 1.10 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 import javax.mail.Address JavaDoc;
32
33 /**
34  * This class implements comparisons for the Recipient Address headers.
35  *
36  * @author Bill Shannon
37  * @author John Mani
38  */

39 public final class RecipientTerm extends AddressTerm JavaDoc {
40
41     /**
42      * The recipient type.
43      *
44      * @serial
45      */

46     protected Message.RecipientType JavaDoc type;
47
48     private static final long serialVersionUID = 6548700653122680468L;
49
50     /**
51      * Constructor.
52      *
53      * @param type the recipient type
54      * @param address the address to match for
55      */

56     public RecipientTerm(Message.RecipientType JavaDoc type, Address JavaDoc address) {
57     super(address);
58     this.type = type;
59     }
60
61     /**
62      * Return the type of recipient to match with.
63      */

64     public Message.RecipientType JavaDoc getRecipientType() {
65     return type;
66     }
67
68     /**
69      * The match method.
70      *
71      * @param msg The address match is applied to this Message's recepient
72      * address
73      * @return true if the match succeeds, otherwise false
74      */

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

96     public boolean equals(Object JavaDoc obj) {
97     if (!(obj instanceof RecipientTerm JavaDoc))
98         return false;
99     RecipientTerm JavaDoc rt = (RecipientTerm JavaDoc)obj;
100     return rt.type.equals(this.type) && super.equals(obj);
101     }
102
103     /**
104      * Compute a hashCode for this object.
105      */

106     public int hashCode() {
107     return type.hashCode() + super.hashCode();
108     }
109 }
110
Popular Tags