001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.api; 020 021import com.google.common.collect.Sets; 022import java.util.Set; 023 024/** 025 * A filter set applies filters to AuditEvents. 026 * If a filter in the set rejects an AuditEvent, then the 027 * AuditEvent is rejected. Otherwise, the AuditEvent is accepted. 028 * @author Rick Giles 029 */ 030public class FilterSet 031 implements Filter 032{ 033 /** filter set */ 034 private final Set<Filter> filters = Sets.newHashSet(); 035 036 /** 037 * Adds a Filter to the set. 038 * @param filter the Filter to add. 039 */ 040 public void addFilter(Filter filter) 041 { 042 filters.add(filter); 043 } 044 045 /** 046 * Removes filter. 047 * @param filter filter to remove. 048 */ 049 public void removeFilter(Filter filter) 050 { 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 protected Set<Filter> getFilters() 059 { 060 return filters; 061 } 062 063 @Override 064 public String toString() 065 { 066 return filters.toString(); 067 } 068 069 @Override 070 public int hashCode() 071 { 072 return filters.hashCode(); 073 } 074 075 @Override 076 public boolean equals(Object object) 077 { 078 if (object instanceof FilterSet) { 079 final FilterSet other = (FilterSet) object; 080 return this.filters.equals(other.filters); 081 } 082 return false; 083 } 084 085 /** {@inheritDoc} */ 086 @Override 087 public boolean accept(AuditEvent event) 088 { 089 for (Filter filter : filters) { 090 if (!filter.accept(event)) { 091 return false; 092 } 093 } 094 return true; 095 } 096 097 /** Clears the FilterSet. */ 098 public void clear() 099 { 100 filters.clear(); 101 } 102}