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.api;
021
022import java.util.Collections;
023import java.util.HashSet;
024import java.util.Set;
025
026/**
027 * A before execution file filter set applies filters to events.
028 * If a before execution file filter in the set rejects an event, then the
029 * event is rejected. Otherwise, the event is accepted.
030 */
031public final class BeforeExecutionFileFilterSet
032    implements BeforeExecutionFileFilter {
033
034    /** Filter set. */
035    private final Set<BeforeExecutionFileFilter> beforeExecutionFileFilters = new HashSet<>();
036
037    /**
038     * Adds a Filter to the set.
039     *
040     * @param filter the Filter to add.
041     */
042    public void addBeforeExecutionFileFilter(BeforeExecutionFileFilter filter) {
043        beforeExecutionFileFilters.add(filter);
044    }
045
046    /**
047     * Removes filter.
048     *
049     * @param filter filter to remove.
050     */
051    public void removeBeforeExecutionFileFilter(BeforeExecutionFileFilter filter) {
052        beforeExecutionFileFilters.remove(filter);
053    }
054
055    /**
056     * Returns the Filters of the filter set.
057     *
058     * @return the Filters of the filter set.
059     */
060    public Set<BeforeExecutionFileFilter> getBeforeExecutionFileFilters() {
061        return Collections.unmodifiableSet(beforeExecutionFileFilters);
062    }
063
064    @Override
065    public String toString() {
066        return beforeExecutionFileFilters.toString();
067    }
068
069    @Override
070    public boolean accept(String uri) {
071        boolean result = true;
072        for (BeforeExecutionFileFilter filter : beforeExecutionFileFilters) {
073            if (!filter.accept(uri)) {
074                result = false;
075                break;
076            }
077        }
078        return result;
079    }
080
081    /** Clears the BeforeExecutionFileFilterSet. */
082    public void clear() {
083        beforeExecutionFileFilters.clear();
084    }
085
086}