KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#)FromStringTerm.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 From Address
35  * header. <p>
36  *
37  * Note that this class differs from the <code>FromTerm</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 FromStringTerm extends AddressStringTerm JavaDoc {
45
46     private static final long serialVersionUID = 5801127523826772788L;
47
48     /**
49      * Constructor.
50      *
51      * @param pattern the address pattern to be compared.
52      */

53     public FromStringTerm(String JavaDoc pattern) {
54     super(pattern);
55     }
56
57     /**
58      * Check whether the address string specified in the constructor is
59      * a substring of the From address of this Message.
60      *
61      * @param msg The comparison is applied to this Message's From
62      * address.
63      * @return true if the match succeeds, otherwise false.
64      */

65     public boolean match(Message JavaDoc msg) {
66     Address JavaDoc[] from;
67
68     try {
69         from = msg.getFrom();
70     } catch (Exception JavaDoc e) {
71         return false;
72     }
73
74     if (from == null)
75         return false;
76     
77     for (int i=0; i < from.length; i++)
78         if (super.match(from[i]))
79         return true;
80     return false;
81     }
82
83     /**
84      * Equality comparison.
85      */

86     public boolean equals(Object JavaDoc obj) {
87     if (!(obj instanceof FromStringTerm JavaDoc))
88         return false;
89     return super.equals(obj);
90     }
91 }
92
Popular Tags