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