1 package net.sourceforge.pmd.util.filter;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 /**
7 * A base class for Filters which implements behavior using a List of other
8 * Filters.
9 *
10 * @param <T>
11 * The underlying type on which the filter applies.
12 */
13 public abstract class AbstractCompoundFilter<T> implements Filter<T> {
14
15 protected List<Filter<T>> filters;
16
17 public AbstractCompoundFilter() {
18 filters = new ArrayList<Filter<T>>(2);
19 }
20
21 public AbstractCompoundFilter(Filter<T>... filters) {
22 this.filters = new ArrayList<Filter<T>>(filters.length);
23 for (Filter<T> filter : filters) {
24 this.filters.add(filter);
25 }
26 }
27
28 public List<Filter<T>> getFilters() {
29 return filters;
30 }
31
32 public void setFilters(List<Filter<T>> filters) {
33 this.filters = filters;
34 }
35
36 public void addFilter(Filter<T> filter) {
37 filters.add(filter);
38 }
39
40 protected abstract String getOperator();
41
42 public String toString() {
43 StringBuilder builder = new StringBuilder();
44 builder.append("(");
45 for (int i = 0; i < filters.size(); i++) {
46 if (i > 0) {
47 builder.append(" ");
48 builder.append(getOperator());
49 builder.append(" ");
50 }
51 builder.append(filters.get(i));
52 }
53 builder.append(")");
54 return builder.toString();
55 }
56 }