KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > BeanPredicate


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
17 package org.apache.commons.beanutils;
18
19 import org.apache.commons.collections.Predicate;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24
25 /**
26  * <p>Predicate implementation that applies the given <code>Predicate</code>
27  * to the result of calling the given property getter.
28  * </p>
29  */

30 public class BeanPredicate implements Predicate {
31    
32     private final Log log = LogFactory.getLog(this.getClass());
33     
34     /** Name of the property whose value will be predicated */
35     private String JavaDoc propertyName;
36     /** <code>Predicate</code> to be applied to the property value */
37     private Predicate predicate;
38
39     /**
40      * Constructs a <code>BeanPredicate</code> that applies the given
41      * <code>Predicate</code> to the named property value.
42      * @param propertyName the name of the property whose value is to be predicated,
43      * not null
44      * @param predicate the <code>Predicate</code> to be applied,
45      * not null
46      */

47     public BeanPredicate(String JavaDoc propertyName, Predicate predicate) {
48         this.propertyName = propertyName;
49         this.predicate = predicate;
50     }
51
52     /**
53      * Evaluates the given object by applying the {@link #getPredicate()}
54      * to a property value named by {@link #getPropertyName()}.
55      * @throws IllegalAccessException when the property cannot be evaluated
56      */

57     public boolean evaluate(Object JavaDoc object) {
58        
59         boolean evaluation = false;
60
61         try {
62             Object JavaDoc propValue = PropertyUtils.getProperty( object, propertyName );
63             evaluation = predicate.evaluate(propValue);
64         } catch (IllegalArgumentException JavaDoc e) {
65             final String JavaDoc errorMsg = "Problem during evaluation.";
66             log.error("ERROR: " + errorMsg, e);
67             throw e;
68         } catch (IllegalAccessException JavaDoc e) {
69             final String JavaDoc errorMsg = "Unable to access the property provided.";
70             log.error(errorMsg, e);
71             throw new IllegalArgumentException JavaDoc(errorMsg);
72         } catch (InvocationTargetException JavaDoc e) {
73             final String JavaDoc errorMsg = "Exception occurred in property's getter";
74             log.error(errorMsg, e);
75             throw new IllegalArgumentException JavaDoc(errorMsg);
76         } catch (NoSuchMethodException JavaDoc e) {
77             final String JavaDoc errorMsg = "Property not found.";
78             log.error(errorMsg, e);
79             throw new IllegalArgumentException JavaDoc(errorMsg);
80         }
81
82         return evaluation;
83     }
84
85     /**
86      * Gets the name of the property whose value is to be predicated.
87      * in the evaluation.
88      * @return the property name, not null
89      */

90     public String JavaDoc getPropertyName() {
91         return propertyName;
92     }
93
94     /**
95      * Sets the name of the property whose value is to be predicated.
96      * @param propertyName the name of the property whose value is to be predicated,
97      * not null
98      */

99     public void setPropertyName(String JavaDoc propertyName) {
100         this.propertyName = propertyName;
101     }
102
103     /**
104      * Gets the <code>Predicate</code> to be applied to the value of the named property
105      * during {@link #evaluate}.
106      * @return <code>Predicate</code>, not null
107      */

108     public Predicate getPredicate() {
109         return predicate;
110     }
111
112     /**
113      * Sets the <code>Predicate</code> to be applied to the value of the named property
114      * during {@link evaluate}.
115      * @param predicate <code>Predicate</code>, not null
116      */

117     public void setPredicate(Predicate predicate) {
118         this.predicate = predicate;
119     }
120
121 }
122
Popular Tags