Coverage Report - org.apache.commons.betwixt.strategy.PropertySuppressionStrategy

Classes in this File Line Coverage Branch Coverage Complexity
PropertySuppressionStrategy
43% 
50% 
1.8

 1  
 /*
 2  
  * Copyright 2004 The Apache Software Foundation.
 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  
 package org.apache.commons.betwixt.strategy;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collection;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 
 23  
 /**
 24  
  * Pluggable strategy specifying whether property's should be suppressed.
 25  
  * Implementations can be used to give rules about which properties 
 26  
  * should be ignored by Betwixt when introspecting.
 27  
  * @since 0.7
 28  
  * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a>
 29  
  */
 30  2131
 public abstract class PropertySuppressionStrategy {
 31  
 
 32  
     /**
 33  
      * Default implementation.
 34  
      * @see #DEFAULT
 35  
      */
 36  1033
     public static class Default extends PropertySuppressionStrategy {
 37  
         public boolean suppressProperty(Class clazz, Class propertyType, String propertyName) {
 38  18840
             boolean result = false;
 39  
             // ignore class properties
 40  18840
             if ( Class.class.equals( propertyType) && "class".equals( propertyName ) ) {
 41  5367
                 result = true;
 42  
             }
 43  
             // ignore isEmpty for collection subclasses
 44  18840
             if ( "empty".equals( propertyName ) && Collection.class.isAssignableFrom( clazz )) {
 45  70
                 result = true;
 46  
             }
 47  
             
 48  18840
             return result;
 49  
         }
 50  
         
 51  
         public String toString() {
 52  0
             return "Default Properties Suppressed";
 53  
         }
 54  
     }
 55  
 
 56  
     /**
 57  
      * Implementation delegates to a list of strategies
 58  
      */
 59  0
     public static class Chain extends PropertySuppressionStrategy {
 60  
 
 61  0
         private final List strategies = new ArrayList();
 62  
         
 63  
         /**
 64  
          * @see #suppressProperty(Class, Class, String)
 65  
          */
 66  
         public boolean suppressProperty(Class classContainingTheProperty, Class propertyType, String propertyName) {
 67  0
             boolean result = false;
 68  0
             for (Iterator it=strategies.iterator(); it.hasNext();) {
 69  0
                 PropertySuppressionStrategy strategy = (PropertySuppressionStrategy) it.next();
 70  0
                 if (strategy.suppressProperty(classContainingTheProperty, propertyType, propertyName)) {
 71  0
                     result = true;
 72  0
                     break;
 73  
                 }
 74  
                 
 75  
             }
 76  0
             return result;
 77  
         }
 78  
         
 79  
         /**
 80  
          * Adds a strategy to the list
 81  
          * @param strategy <code>PropertySuppressionStrategy</code>, not null
 82  
          */
 83  
         public void addStrategy(PropertySuppressionStrategy strategy) {
 84  0
             strategies.add(strategy);
 85  0
         }
 86  
     }
 87  
     
 88  
     /**
 89  
      * Default implementation suppresses the class property
 90  
      * found on every object. Also, the <code>isEmpty</code>
 91  
      * property is supressed for implementations of <code>Collection</code>.
 92  
      */
 93  1033
     public static final PropertySuppressionStrategy DEFAULT = new Default();
 94  
     
 95  
     /**
 96  
      * Should the given property be suppressed?
 97  
      * @param classContainingTheProperty <code>Class</code> giving the type of the bean containing the property <code>propertyName</code>
 98  
      * @param propertyType <code>Class</code> giving the type of the property, not null
 99  
      * @param propertyName the name of the property, not null
 100  
      * @return true when the given property should be suppressed
 101  
      */
 102  
     public abstract boolean suppressProperty(Class classContainingTheProperty, Class propertyType, String propertyName);
 103  
 }