001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package javax.mail.search;
021    
022    /**
023     * A Term that provides comparisons for integers.
024     *
025     * @version $Rev: 920714 $ $Date: 2010-03-09 01:55:49 -0500 (Tue, 09 Mar 2010) $
026     */
027    public abstract class IntegerComparisonTerm extends ComparisonTerm {
028            
029            private static final long serialVersionUID = -6963571240154302484L;
030            
031        protected int number;
032    
033        protected IntegerComparisonTerm(int comparison, int number) {
034            super();
035            this.comparison = comparison;
036            this.number = number;
037        }
038    
039        public int getNumber() {
040            return number;
041        }
042    
043        public int getComparison() {
044            return comparison;
045        }
046    
047        protected boolean match(int match) {
048            switch (comparison) {
049            case EQ:
050                return match == number;
051            case NE:
052                return match != number;
053            case GT:
054                return match > number;
055            case GE:
056                return match >= number;
057            case LT:
058                return match < number;
059            case LE:
060                return match <= number;
061            default:
062                return false;
063            }
064        }
065    
066        public boolean equals(Object other) {
067            if (other == this) return true;
068            if (other instanceof IntegerComparisonTerm == false) return false;
069            final IntegerComparisonTerm term = (IntegerComparisonTerm) other;
070            return this.comparison == term.comparison && this.number == term.number;
071        }
072    
073        public int hashCode() {
074            return number + super.hashCode();
075        }
076    }