1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.betwixt.strategy; 18 19 import java.util.ArrayList; 20 import java.util.Collection; 21 import java.util.Iterator; 22 import java.util.List; 23 24 /** 25 * Pluggable strategy specifying whether property's should be suppressed. 26 * Implementations can be used to give rules about which properties 27 * should be ignored by Betwixt when introspecting. 28 * @since 0.7 29 * @author <a href='http://commons.apache.org'>Apache Commons Team</a>, <a href='http://www.apache.org'>Apache Software Foundation</a> 30 */ 31 public abstract class PropertySuppressionStrategy { 32 33 /** 34 * Default implementation. 35 * @see #DEFAULT 36 */ 37 public static class Default extends PropertySuppressionStrategy { 38 public boolean suppressProperty(Class clazz, Class propertyType, String propertyName) { 39 boolean result = false; 40 // ignore class properties 41 if ( Class.class.equals( propertyType) && "class".equals( propertyName ) ) { 42 result = true; 43 } 44 // ignore isEmpty for collection subclasses 45 if ( "empty".equals( propertyName ) && Collection.class.isAssignableFrom( clazz )) { 46 result = true; 47 } 48 49 return result; 50 } 51 52 public String toString() { 53 return "Default Properties Suppressed"; 54 } 55 } 56 57 /** 58 * Implementation delegates to a list of strategies 59 */ 60 public static class Chain extends PropertySuppressionStrategy { 61 62 private final List strategies = new ArrayList(); 63 64 /** 65 * @see #suppressProperty(Class, Class, String) 66 */ 67 public boolean suppressProperty(Class classContainingTheProperty, Class propertyType, String propertyName) { 68 boolean result = false; 69 for (Iterator it=strategies.iterator(); it.hasNext();) { 70 PropertySuppressionStrategy strategy = (PropertySuppressionStrategy) it.next(); 71 if (strategy.suppressProperty(classContainingTheProperty, propertyType, propertyName)) { 72 result = true; 73 break; 74 } 75 76 } 77 return result; 78 } 79 80 /** 81 * Adds a strategy to the list 82 * @param strategy <code>PropertySuppressionStrategy</code>, not null 83 */ 84 public void addStrategy(PropertySuppressionStrategy strategy) { 85 strategies.add(strategy); 86 } 87 } 88 89 /** 90 * Default implementation suppresses the class property 91 * found on every object. Also, the <code>isEmpty</code> 92 * property is supressed for implementations of <code>Collection</code>. 93 */ 94 public static final PropertySuppressionStrategy DEFAULT = new Default(); 95 96 /** 97 * Should the given property be suppressed? 98 * @param classContainingTheProperty <code>Class</code> giving the type of the bean containing the property <code>propertyName</code> 99 * @param propertyType <code>Class</code> giving the type of the property, not null 100 * @param propertyName the name of the property, not null 101 * @return true when the given property should be suppressed 102 */ 103 public abstract boolean suppressProperty(Class classContainingTheProperty, Class propertyType, String propertyName); 104 }