KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#)AndTerm.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 the logical AND operator on individual
34  * SearchTerms.
35  *
36  * @author Bill Shannon
37  * @author John Mani
38  */

39 public final class AndTerm extends SearchTerm JavaDoc {
40
41     /**
42      * The array of terms on which the AND operator should be
43      * applied.
44      *
45      * @serial
46      */

47     protected SearchTerm JavaDoc[] terms;
48
49     private static final long serialVersionUID = -3583274505380989582L;
50
51     /**
52      * Constructor that takes two terms.
53      *
54      * @param t1 first term
55      * @param t2 second term
56      */

57     public AndTerm(SearchTerm JavaDoc t1, SearchTerm JavaDoc t2) {
58     terms = new SearchTerm JavaDoc[2];
59     terms[0] = t1;
60     terms[1] = t2;
61     }
62
63     /**
64      * Constructor that takes an array of SearchTerms.
65      *
66      * @param t array of terms
67      */

68     public AndTerm(SearchTerm JavaDoc[] t) {
69     terms = new SearchTerm JavaDoc[t.length]; // clone the array
70
for (int i = 0; i < t.length; i++)
71         terms[i] = t[i];
72     }
73
74     /**
75      * Return the search terms.
76      */

77     public SearchTerm JavaDoc[] getTerms() {
78     return (SearchTerm JavaDoc[])terms.clone();
79     }
80
81     /**
82      * The AND operation. <p>
83      *
84      * The terms specified in the constructor are applied to
85      * the given object and the AND operator is applied to their results.
86      *
87      * @param msg The specified SearchTerms are applied to this Message
88      * and the AND operator is applied to their results.
89      * @return true if the AND succeds, otherwise false
90      */

91     public boolean match(Message JavaDoc msg) {
92     for (int i=0; i < terms.length; i++)
93         if (!terms[i].match(msg))
94         return false;
95     return true;
96     }
97
98     /**
99      * Equality comparison.
100      */

101     public boolean equals(Object JavaDoc obj) {
102     if (!(obj instanceof AndTerm JavaDoc))
103         return false;
104     AndTerm JavaDoc at = (AndTerm JavaDoc)obj;
105     if (at.terms.length != terms.length)
106         return false;
107     for (int i=0; i < terms.length; i++)
108         if (!terms[i].equals(at.terms[i]))
109         return false;
110     return true;
111     }
112
113     /**
114      * Compute a hashCode for this object.
115      */

116     public int hashCode() {
117     int hash = 0;
118     for (int i=0; i < terms.length; i++)
119         hash += terms[i].hashCode();
120     return hash;
121     }
122 }
123
Popular Tags