001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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.Objects;
025import java.util.Set;
026
027/**
028 * A filter set applies filters to AuditEvents.
029 * If a filter in the set rejects an AuditEvent, then the
030 * AuditEvent is rejected. Otherwise, the AuditEvent is accepted.
031 * @author Rick Giles
032 */
033public class FilterSet
034    implements Filter {
035
036    /** Filter set. */
037    private final Set<Filter> filters = new HashSet<>();
038
039    /**
040     * Adds a Filter to the set.
041     * @param filter the Filter to add.
042     */
043    public void addFilter(Filter filter) {
044        filters.add(filter);
045    }
046
047    /**
048     * Removes filter.
049     * @param filter filter to remove.
050     */
051    public void removeFilter(Filter filter) {
052        filters.remove(filter);
053    }
054
055    /**
056     * Returns the Filters of the filter set.
057     * @return the Filters of the filter set.
058     */
059    public Set<Filter> getFilters() {
060        return Collections.unmodifiableSet(filters);
061    }
062
063    @Override
064    public String toString() {
065        return filters.toString();
066    }
067
068    @Override
069    public boolean equals(Object other) {
070        if (this == other) {
071            return true;
072        }
073        if (other == null || getClass() != other.getClass()) {
074            return false;
075        }
076        final FilterSet filterSet = (FilterSet) other;
077        return Objects.equals(filters, filterSet.filters);
078    }
079
080    @Override
081    public int hashCode() {
082        return Objects.hash(filters);
083    }
084
085    @Override
086    public boolean accept(AuditEvent event) {
087        boolean result = true;
088        for (Filter filter : filters) {
089            if (!filter.accept(event)) {
090                result = false;
091                break;
092            }
093        }
094        return result;
095    }
096
097    /** Clears the FilterSet. */
098    public void clear() {
099        filters.clear();
100    }
101
102}