1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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://jakarta.apache.org/commons'>Jakarta 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
41 if ( Class.class.equals( propertyType) && "class".equals( propertyName ) ) {
42 result = true;
43 }
44
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 }