View Javadoc

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