1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils;
19
20 import org.apache.commons.collections.Predicate;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import java.lang.reflect.InvocationTargetException;
25
26 /***
27 * <p>Predicate implementation that applies the given <code>Predicate</code>
28 * to the result of calling the given property getter.
29 * </p>
30 */
31 public class BeanPredicate implements Predicate {
32
33 private final Log log = LogFactory.getLog(this.getClass());
34
35 /*** Name of the property whose value will be predicated */
36 private String propertyName;
37 /*** <code>Predicate</code> to be applied to the property value */
38 private Predicate predicate;
39
40 /***
41 * Constructs a <code>BeanPredicate</code> that applies the given
42 * <code>Predicate</code> to the named property value.
43 * @param propertyName the name of the property whose value is to be predicated,
44 * not null
45 * @param predicate the <code>Predicate</code> to be applied,
46 * not null
47 */
48 public BeanPredicate(String propertyName, Predicate predicate) {
49 this.propertyName = propertyName;
50 this.predicate = predicate;
51 }
52
53 /***
54 * Evaluates the given object by applying the {@link #getPredicate()}
55 * to a property value named by {@link #getPropertyName()}.
56 *
57 * @param object The object being evaluated
58 * @return the result of the predicate evaluation
59 * @throws IllegalArgumentException when the property cannot be evaluated
60 */
61 public boolean evaluate(Object object) {
62
63 boolean evaluation = false;
64
65 try {
66 Object propValue = PropertyUtils.getProperty( object, propertyName );
67 evaluation = predicate.evaluate(propValue);
68 } catch (IllegalArgumentException e) {
69 final String errorMsg = "Problem during evaluation.";
70 log.error("ERROR: " + errorMsg, e);
71 throw e;
72 } catch (IllegalAccessException e) {
73 final String errorMsg = "Unable to access the property provided.";
74 log.error(errorMsg, e);
75 throw new IllegalArgumentException(errorMsg);
76 } catch (InvocationTargetException e) {
77 final String errorMsg = "Exception occurred in property's getter";
78 log.error(errorMsg, e);
79 throw new IllegalArgumentException(errorMsg);
80 } catch (NoSuchMethodException e) {
81 final String errorMsg = "Property not found.";
82 log.error(errorMsg, e);
83 throw new IllegalArgumentException(errorMsg);
84 }
85
86 return evaluation;
87 }
88
89 /***
90 * Gets the name of the property whose value is to be predicated.
91 * in the evaluation.
92 * @return the property name, not null
93 */
94 public String getPropertyName() {
95 return propertyName;
96 }
97
98 /***
99 * Sets the name of the property whose value is to be predicated.
100 * @param propertyName the name of the property whose value is to be predicated,
101 * not null
102 */
103 public void setPropertyName(String propertyName) {
104 this.propertyName = propertyName;
105 }
106
107 /***
108 * Gets the <code>Predicate</code> to be applied to the value of the named property
109 * during {@link #evaluate}.
110 * @return <code>Predicate</code>, not null
111 */
112 public Predicate getPredicate() {
113 return predicate;
114 }
115
116 /***
117 * Sets the <code>Predicate</code> to be applied to the value of the named property
118 * during {@link #evaluate(Object)}.
119 * @param predicate <code>Predicate</code>, not null
120 */
121 public void setPredicate(Predicate predicate) {
122 this.predicate = predicate;
123 }
124
125 }