001    /**
002     *
003     * Copyright 2003-2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package javax.mail.search;
019    
020    import java.util.Arrays;
021    import javax.mail.Message;
022    
023    /**
024     * Term that implements a logical AND across terms.
025     *
026     * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
027     */
028    public final class AndTerm extends SearchTerm {
029        /**
030         * Terms to which the AND operator should be applied.
031         */
032        protected SearchTerm[] terms;
033    
034        /**
035         * Constructor for performing a binary AND.
036         *
037         * @param a the first term
038         * @param b the second ter,
039         */
040        public AndTerm(SearchTerm a, SearchTerm b) {
041            terms = new SearchTerm[]{a, b};
042        }
043    
044        /**
045         * Constructor for performing and AND across an arbitraty number of terms.
046         * @param terms the terms to AND together
047         */
048        public AndTerm(SearchTerm[] terms) {
049            this.terms = terms;
050        }
051    
052        /**
053         * Return the terms.
054         * @return the terms
055         */
056        public SearchTerm[] getTerms() {
057            return terms;
058        }
059    
060        /**
061         * Match by applying the terms, in order, to the Message and performing an AND operation
062         * to the result. Comparision will stop immediately if one of the terms returns false.
063         *
064         * @param message the Message to apply the terms to
065         * @return true if all terms match
066         */
067        public boolean match(Message message) {
068            for (int i = 0; i < terms.length; i++) {
069                SearchTerm term = terms[i];
070                if (!term.match(message)) {
071                    return false;
072                }
073            }
074            return true;
075        }
076    
077        public boolean equals(Object other) {
078            if (other == this) return true;
079            if (other instanceof AndTerm == false) return false;
080            return Arrays.equals(terms, ((AndTerm) other).terms);
081        }
082    
083        public int hashCode() {
084            int hash = 0;
085            for (int i = 0; i < terms.length; i++) {
086                hash = hash * 37 + terms[i].hashCode();
087            }
088            return hash;
089        }
090    }