View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.api;
21  
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Objects;
25  import java.util.Set;
26  
27  /**
28   * A filter set applies filters to AuditEvents.
29   * If a filter in the set rejects an AuditEvent, then the
30   * AuditEvent is rejected. Otherwise, the AuditEvent is accepted.
31   * @author Rick Giles
32   */
33  public class FilterSet
34      implements Filter {
35  
36      /** Filter set. */
37      private final Set<Filter> filters = new HashSet<>();
38  
39      /**
40       * Adds a Filter to the set.
41       * @param filter the Filter to add.
42       */
43      public void addFilter(Filter filter) {
44          filters.add(filter);
45      }
46  
47      /**
48       * Removes filter.
49       * @param filter filter to remove.
50       */
51      public void removeFilter(Filter filter) {
52          filters.remove(filter);
53      }
54  
55      /**
56       * Returns the Filters of the filter set.
57       * @return the Filters of the filter set.
58       */
59      public Set<Filter> getFilters() {
60          return Collections.unmodifiableSet(filters);
61      }
62  
63      @Override
64      public String toString() {
65          return filters.toString();
66      }
67  
68      @Override
69      public boolean equals(Object other) {
70          if (this == other) {
71              return true;
72          }
73          if (other == null || getClass() != other.getClass()) {
74              return false;
75          }
76          final FilterSet filterSet = (FilterSet) other;
77          return Objects.equals(filters, filterSet.filters);
78      }
79  
80      @Override
81      public int hashCode() {
82          return Objects.hash(filters);
83      }
84  
85      @Override
86      public boolean accept(AuditEvent event) {
87          boolean result = true;
88          for (Filter filter : filters) {
89              if (!filter.accept(event)) {
90                  result = false;
91                  break;
92              }
93          }
94          return result;
95      }
96  
97      /** Clears the FilterSet. */
98      public void clear() {
99          filters.clear();
100     }
101 
102 }