KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#)OrTerm.java 1.11 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 OR operator on individual SearchTerms.
34  *
35  * @author Bill Shannon
36  * @author John Mani
37  */

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

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

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

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

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

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 true;
95     return false;
96     }
97
98     /**
99      * Equality comparison.
100      */

101     public boolean equals(Object JavaDoc obj) {
102     if (!(obj instanceof OrTerm JavaDoc))
103         return false;
104     OrTerm JavaDoc ot = (OrTerm JavaDoc)obj;
105     if (ot.terms.length != terms.length)
106         return false;
107     for (int i=0; i < terms.length; i++)
108         if (!terms[i].equals(ot.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