Coverage Report - net.sf.json.filters.MappingPropertyFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
MappingPropertyFilter
0%
0/29
0%
0/14
2.333
 
 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.HashMap;
 20  
 import java.util.Iterator;
 21  
 import java.util.Map;
 22  
 
 23  
 import net.sf.json.util.PropertyFilter;
 24  
 
 25  
 /**
 26  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 27  
  */
 28  
 public abstract class MappingPropertyFilter implements PropertyFilter {
 29  0
    private Map filters = new HashMap();
 30  
 
 31  
    public MappingPropertyFilter() {
 32  0
       this( null );
 33  0
    }
 34  
 
 35  0
    public MappingPropertyFilter( Map filters ) {
 36  0
       if( filters != null ){
 37  0
          for( Iterator i = filters.entrySet()
 38  0
                .iterator(); i.hasNext(); ){
 39  0
             Map.Entry entry = (Map.Entry) i.next();
 40  0
             Object key = entry.getKey();
 41  0
             Object filter = entry.getValue();
 42  0
             if( filter instanceof PropertyFilter ){
 43  0
                this.filters.put( key, filter );
 44  
             }
 45  0
          }
 46  
       }
 47  0
    }
 48  
 
 49  
    public void addPropertyFilter( Object target, PropertyFilter filter ) {
 50  0
       if( filter != null ){
 51  0
          filters.put( target, filter );
 52  
       }
 53  0
    }
 54  
 
 55  
    public boolean apply( Object source, String name, Object value ) {
 56  0
       for( Iterator i = filters.entrySet()
 57  0
             .iterator(); i.hasNext(); ){
 58  0
          Map.Entry entry = (Map.Entry) i.next();
 59  0
          Object key = entry.getKey();
 60  0
          if( keyMatches( key, source, name, value ) ){
 61  0
             PropertyFilter filter = (PropertyFilter) entry.getValue();
 62  0
             return filter.apply( source, name, value );
 63  
          }
 64  0
       }
 65  0
       return false;
 66  
    }
 67  
 
 68  
    public void removePropertyFilter( Object target ) {
 69  0
       if( target != null ){
 70  0
          filters.remove( target );
 71  
       }
 72  0
    }
 73  
 
 74  
    protected abstract boolean keyMatches( Object key, Object source, String name, Object value );
 75  
 }