1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package javax.mail.search;
19
20 import java.util.Arrays;
21 import javax.mail.Message;
22
23 /**
24 * Term that implements a logical AND across terms.
25 *
26 * @version $Rev: 126550 $ $Date: 2005-01-26 14:27:45 -0800 (Wed, 26 Jan 2005) $
27 */
28 public final class AndTerm extends SearchTerm {
29 /**
30 * Terms to which the AND operator should be applied.
31 */
32 protected SearchTerm[] terms;
33
34 /**
35 * Constructor for performing a binary AND.
36 *
37 * @param a the first term
38 * @param b the second ter,
39 */
40 public AndTerm(SearchTerm a, SearchTerm b) {
41 terms = new SearchTerm[]{a, b};
42 }
43
44 /**
45 * Constructor for performing and AND across an arbitraty number of terms.
46 * @param terms the terms to AND together
47 */
48 public AndTerm(SearchTerm[] terms) {
49 this.terms = terms;
50 }
51
52 /**
53 * Return the terms.
54 * @return the terms
55 */
56 public SearchTerm[] getTerms() {
57 return terms;
58 }
59
60 /**
61 * Match by applying the terms, in order, to the Message and performing an AND operation
62 * to the result. Comparision will stop immediately if one of the terms returns false.
63 *
64 * @param message the Message to apply the terms to
65 * @return true if all terms match
66 */
67 public boolean match(Message message) {
68 for (int i = 0; i < terms.length; i++) {
69 SearchTerm term = terms[i];
70 if (!term.match(message)) {
71 return false;
72 }
73 }
74 return true;
75 }
76
77 public boolean equals(Object other) {
78 if (other == this) return true;
79 if (other instanceof AndTerm == false) return false;
80 return Arrays.equals(terms, ((AndTerm) other).terms);
81 }
82
83 public int hashCode() {
84 int hash = 0;
85 for (int i = 0; i < terms.length; i++) {
86 hash = hash * 37 + terms[i].hashCode();
87 }
88 return hash;
89 }
90 }