001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.filters;
021
022import java.util.Objects;
023
024/**
025 * This filter element is immutable and accepts an Integer in a range.
026 */
027class IntRangeFilterElement implements IntFilterElement {
028
029    /** Lower bound of the range. */
030    private final Integer lowerBound;
031
032    /** Upper bound of the range. */
033    private final Integer upperBound;
034
035    /**
036     * Constructs a {@code IntRangeFilterElement} with a
037     * lower bound and an upper bound for the range.
038     *
039     * @param lowerBound the lower bound of the range.
040     * @param upperBound the upper bound of the range.
041     */
042    /* package */ IntRangeFilterElement(int lowerBound, int upperBound) {
043        this.lowerBound = lowerBound;
044        this.upperBound = upperBound;
045    }
046
047    @Override
048    public boolean accept(int intValue) {
049        return lowerBound.compareTo(intValue) <= 0
050            && upperBound.compareTo(intValue) >= 0;
051    }
052
053    @Override
054    public int hashCode() {
055        return Objects.hash(lowerBound, upperBound);
056    }
057
058    @Override
059    public boolean equals(Object other) {
060        if (this == other) {
061            return true;
062        }
063        if (other == null || getClass() != other.getClass()) {
064            return false;
065        }
066        final IntRangeFilterElement intRangeFilter = (IntRangeFilterElement) other;
067        return Objects.equals(lowerBound, intRangeFilter.lowerBound)
068                && Objects.equals(upperBound, intRangeFilter.upperBound);
069    }
070
071}