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