001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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 */
017package org.apache.commons.functor.core.comparator;
018
019import org.apache.commons.functor.Predicate;
020import org.apache.commons.lang3.Validate;
021
022/**
023 * A {@link Predicate} that tests whether a {@link Comparable} object is
024 * within a range. The range is defined in the constructor.
025 *
026 * @since 1.0
027 * @param <A> the predicate argument type.
028 * @version $Revision: 1537901 $ $Date: 2013-11-01 12:30:19 +0100 (Fr, 01 Nov 2013) $
029 */
030public class IsWithinRange<A extends Comparable<A>> implements Predicate<A> {
031
032    /** Hashcode of the name of this Predicate. */
033    private static final int NAME_HASH_CODE = "IsWithinRange".hashCode();
034
035    /** Base Hashcode. */
036    private static final int BASE_HASH_CODE = 29;
037
038    /***************************************************
039     *  Instance variables
040     ***************************************************/
041
042    /** The minimum value of the range. */
043    private final A min;
044    /** The maximum value of the range. */
045    private final A max;
046
047    /***************************************************
048     *  Constructors
049     ***************************************************/
050
051    /**
052     * Create a new IsWithinRange by passing in the range that will
053     * be used in the {@link #test}.
054     * @param min Comparable
055     * @param max Comparable
056     */
057    public IsWithinRange(A min, A max) {
058        Validate.notNull(min, "min must not be null");
059        Validate.notNull(max, "max must not be null");
060        if (min.compareTo(max) > 0) {
061            throw new IllegalArgumentException("min must be <= max");
062        }
063        this.min = min;
064        this.max = max;
065    }
066
067    /***************************************************
068     *  Instance methods
069     ***************************************************/
070
071    /**
072     * {@inheritDoc}
073     * Test if the passed in object is within the specified range.
074     */
075    public final boolean test(A o) {
076        return o.compareTo(min) >= 0 && o.compareTo(max) <= 0;
077    }
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public final boolean equals(Object o) {
084        if (this == o) {
085            return true;
086        }
087        if (!(o instanceof IsWithinRange<?>)) {
088            return false;
089        }
090        final IsWithinRange<?> isWithinRange = (IsWithinRange<?>) o;
091        return max.equals(isWithinRange.max) && min.equals(isWithinRange.min);
092    }
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
098    public int hashCode() {
099        return BASE_HASH_CODE * min.hashCode() + max.hashCode() + NAME_HASH_CODE;
100    }
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public String toString() {
107        return "IsWithinRange(" + min + ", " + max + ")";
108    }
109
110    /**
111     * Obtain an IsWithinRange instance.
112     * @param <A> the argument type.
113     * @param min the min value
114     * @param max the max value
115     * @return IsWithinRange<A>
116     */
117    public static <A extends Comparable<A>> IsWithinRange<A> instance(A min, A max) {
118        return new IsWithinRange<A>(min, max);
119    }
120}