001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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
024import com.puppycrawl.tools.checkstyle.api.AuditEvent;
025import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
026import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
027import com.puppycrawl.tools.checkstyle.api.Filter;
028import com.puppycrawl.tools.checkstyle.api.FilterSet;
029
030/**
031 * <p>
032 * This filter accepts AuditEvents according to file, check, line, and
033 * column, as specified in a suppression file.
034 * </p>
035 * @author Rick Giles
036 */
037public class SuppressionFilter
038    extends AutomaticBean
039    implements Filter {
040    /** Set of individual suppresses. */
041    private FilterSet filters = new FilterSet();
042
043    /**
044     * Loads the suppressions for a file.
045     * @param fileName name of the suppressions file.
046     * @throws CheckstyleException if there is an error.
047     */
048    public void setFile(String fileName)
049        throws CheckstyleException {
050        filters = SuppressionsLoader.loadSuppressions(fileName);
051    }
052
053    @Override
054    public boolean accept(AuditEvent event) {
055        return filters.accept(event);
056    }
057
058    @Override
059    public boolean equals(Object obj) {
060        if (this == obj) {
061            return true;
062        }
063        if (obj == null || getClass() != obj.getClass()) {
064            return false;
065        }
066        final SuppressionFilter suppressionFilter = (SuppressionFilter) obj;
067        return Objects.equals(filters, suppressionFilter.filters);
068    }
069
070    @Override
071    public int hashCode() {
072        return Objects.hash(filters);
073    }
074}