001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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.Collections;
023import java.util.HashSet;
024import java.util.Objects;
025import java.util.Set;
026
027/**
028 * <div>
029 * This filter element is immutable and accepts an integer that matches a CSV value, where
030 * each value is an integer or a range of integers.
031 * </div>
032 */
033final class CsvFilterElement implements IntFilterElement {
034
035    /** Filter set. */
036    private final Set<IntFilterElement> filters = new HashSet<>();
037
038    /**
039     * Constructs a {@code CsvFilterElement} from a CSV, Comma-Separated Values,
040     * string. Each value is an integer, or a range of integers. A range of
041     * integers is of the form integer-integer, such as 1-10.
042     * Note: integers must be non-negative.
043     *
044     * @param pattern the CSV string.
045     * @throws NumberFormatException if a component substring does not contain a parsable integer.
046     */
047    /* package */ CsvFilterElement(String pattern) {
048        for (String token : pattern.split(",", -1)) {
049            if (token.isEmpty()) {
050                continue;
051            }
052            final String trimmedToken = token.trim();
053            final int index = trimmedToken.indexOf('-');
054            if (index == -1) {
055                final int matchValue = Integer.parseInt(trimmedToken);
056                addFilter(new IntMatchFilterElement(matchValue));
057            }
058            else {
059                final int lowerBound =
060                    Integer.parseInt(trimmedToken.substring(0, index));
061                final int upperBound =
062                    Integer.parseInt(trimmedToken.substring(index + 1));
063                addFilter(new IntRangeFilterElement(lowerBound, upperBound));
064            }
065        }
066    }
067
068    /**
069     * Adds a IntFilterElement to the set.
070     *
071     * @param filter the IntFilterElement to add.
072     */
073    private void addFilter(IntFilterElement filter) {
074        filters.add(filter);
075    }
076
077    /**
078     * Returns the IntFilters of the filter set.
079     *
080     * @return the IntFilters of the filter set.
081     */
082    private Set<IntFilterElement> getFilters() {
083        return Collections.unmodifiableSet(filters);
084    }
085
086    /**
087     * Determines whether an Integer matches a CSV integer value.
088     *
089     * @param intValue the Integer to check.
090     * @return true if intValue is an Integer that matches a CSV value.
091     */
092    @Override
093    public boolean accept(int intValue) {
094        boolean result = false;
095        for (IntFilterElement filter : getFilters()) {
096            if (filter.accept(intValue)) {
097                result = true;
098                break;
099            }
100        }
101        return result;
102    }
103
104    @Override
105    public boolean equals(Object object) {
106        if (this == object) {
107            return true;
108        }
109        if (object == null || getClass() != object.getClass()) {
110            return false;
111        }
112        final CsvFilterElement csvFilter = (CsvFilterElement) object;
113        return Objects.equals(filters, csvFilter.filters);
114    }
115
116    @Override
117    public int hashCode() {
118        return Objects.hash(filters);
119    }
120
121}