001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 021 package net.sourceforge.pmd.util.filter; 022 023 import java.util.ArrayList; 024 import java.util.List; 025 026 /** 027 * A base class for Filters which implements behavior using a List of other 028 * Filters. 029 * 030 * @param <T> 031 * The underlying type on which the filter applies. 032 */ 033 public abstract class AbstractCompoundFilter<T> implements Filter<T> { 034 035 protected List<Filter<T>> filters; 036 037 public AbstractCompoundFilter() { 038 filters = new ArrayList<Filter<T>>(2); 039 } 040 041 public AbstractCompoundFilter(Filter<T>... filters) { 042 this.filters = new ArrayList<Filter<T>>(filters.length); 043 for (Filter<T> filter : filters) { 044 this.filters.add(filter); 045 } 046 } 047 048 public List<Filter<T>> getFilters() { 049 return filters; 050 } 051 052 public void setFilters(List<Filter<T>> filters) { 053 this.filters = filters; 054 } 055 056 public void addFilter(Filter<T> filter) { 057 filters.add(filter); 058 } 059 060 protected abstract String getOperator(); 061 062 public String toString() { 063 StringBuilder builder = new StringBuilder(); 064 builder.append("("); 065 for (int i = 0; i < filters.size(); i++) { 066 if (i > 0) { 067 builder.append(" "); 068 builder.append(getOperator()); 069 builder.append(" "); 070 } 071 builder.append(filters.get(i)); 072 } 073 builder.append(")"); 074 return builder.toString(); 075 } 076 }