View Javadoc

1   /*
2    * Copyright 2002-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.json.filters;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import net.sf.json.util.PropertyFilter;
24  
25  /**
26   * @author Andres Almiray <aalmiray@users.sourceforge.net>
27   */
28  public class CompositePropertyFilter implements PropertyFilter {
29     private List filters = new ArrayList();
30  
31     public CompositePropertyFilter() {
32        this( null );
33     }
34  
35     public CompositePropertyFilter( List filters ) {
36        if( filters != null ){
37           for( Iterator i = filters.iterator(); i.hasNext(); ){
38              Object filter = i.next();
39              if( filter instanceof PropertyFilter ){
40                 this.filters.add( filter );
41              }
42           }
43        }
44     }
45  
46     public void addPropertyFilter( PropertyFilter filter ) {
47        if( filter != null ){
48           filters.add( filter );
49        }
50     }
51  
52     public boolean apply( Object source, String name, Object value ) {
53        for( Iterator i = filters.iterator(); i.hasNext(); ){
54           PropertyFilter filter = (PropertyFilter) i.next();
55           if( filter.apply( source, name, value ) ){
56              return true;
57           }
58        }
59        return false;
60     }
61  
62     public void removePropertyFilter( PropertyFilter filter ) {
63        if( filter != null ){
64           filters.remove( filter );
65        }
66     }
67  }