View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.Set;
25  
26  /**
27   * A filter set applies filters to AuditEvents.
28   * If a filter in the set rejects an AuditEvent, then the
29   * AuditEvent is rejected. Otherwise, the AuditEvent is accepted.
30   */
31  public class FilterSet
32      implements Filter {
33  
34      /** Filter set. */
35      private final Set<Filter> filters = new HashSet<>();
36  
37      /**
38       * Creates a new {@code FilterSet} instance.
39       */
40      public FilterSet() {
41          // no code by default
42      }
43  
44      /**
45       * Adds a Filter to the set.
46       *
47       * @param filter the Filter to add.
48       */
49      public void addFilter(Filter filter) {
50          filters.add(filter);
51      }
52  
53      /**
54       * Removes filter.
55       *
56       * @param filter filter to remove.
57       */
58      public void removeFilter(Filter filter) {
59          filters.remove(filter);
60      }
61  
62      /**
63       * Returns the Filters of the filter set.
64       *
65       * @return the Filters of the filter set.
66       */
67      public Set<Filter> getFilters() {
68          return Collections.unmodifiableSet(filters);
69      }
70  
71      @Override
72      public String toString() {
73          return filters.toString();
74      }
75  
76      @Override
77      public boolean accept(AuditEvent event) {
78          boolean result = true;
79          for (Filter filter : filters) {
80              if (!filter.accept(event)) {
81                  result = false;
82                  break;
83              }
84          }
85          return result;
86      }
87  
88      /** Clears the FilterSet. */
89      public void clear() {
90          filters.clear();
91      }
92  
93  }