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 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 * @version $Id$ 32 */ 33 public class BeanPredicate implements Predicate { 34 35 private final Log log = LogFactory.getLog(this.getClass()); 36 37 /** Name of the property whose value will be predicated */ 38 private String propertyName; 39 /** <code>Predicate</code> to be applied to the property value */ 40 private Predicate predicate; 41 42 /** 43 * Constructs a <code>BeanPredicate</code> that applies the given 44 * <code>Predicate</code> to the named property value. 45 * @param propertyName the name of the property whose value is to be predicated, 46 * not null 47 * @param predicate the <code>Predicate</code> to be applied, 48 * not null 49 */ 50 public BeanPredicate(final String propertyName, final Predicate predicate) { 51 this.propertyName = propertyName; 52 this.predicate = predicate; 53 } 54 55 /** 56 * Evaluates the given object by applying the {@link #getPredicate()} 57 * to a property value named by {@link #getPropertyName()}. 58 * 59 * @param object The object being evaluated 60 * @return the result of the predicate evaluation 61 * @throws IllegalArgumentException when the property cannot be evaluated 62 */ 63 public boolean evaluate(final Object object) { 64 65 boolean evaluation = false; 66 67 try { 68 final Object propValue = PropertyUtils.getProperty( object, propertyName ); 69 evaluation = predicate.evaluate(propValue); 70 } catch (final IllegalArgumentException e) { 71 final String errorMsg = "Problem during evaluation."; 72 log.error("ERROR: " + errorMsg, e); 73 throw e; 74 } catch (final IllegalAccessException e) { 75 final String errorMsg = "Unable to access the property provided."; 76 log.error(errorMsg, e); 77 throw new IllegalArgumentException(errorMsg); 78 } catch (final InvocationTargetException e) { 79 final String errorMsg = "Exception occurred in property's getter"; 80 log.error(errorMsg, e); 81 throw new IllegalArgumentException(errorMsg); 82 } catch (final NoSuchMethodException e) { 83 final String errorMsg = "Property not found."; 84 log.error(errorMsg, e); 85 throw new IllegalArgumentException(errorMsg); 86 } 87 88 return evaluation; 89 } 90 91 /** 92 * Gets the name of the property whose value is to be predicated. 93 * in the evaluation. 94 * @return the property name, not null 95 */ 96 public String getPropertyName() { 97 return propertyName; 98 } 99 100 /** 101 * Sets the name of the property whose value is to be predicated. 102 * @param propertyName the name of the property whose value is to be predicated, 103 * not null 104 */ 105 public void setPropertyName(final String propertyName) { 106 this.propertyName = propertyName; 107 } 108 109 /** 110 * Gets the <code>Predicate</code> to be applied to the value of the named property 111 * during {@link #evaluate}. 112 * @return <code>Predicate</code>, not null 113 */ 114 public Predicate getPredicate() { 115 return predicate; 116 } 117 118 /** 119 * Sets the <code>Predicate</code> to be applied to the value of the named property 120 * during {@link #evaluate(Object)}. 121 * @param predicate <code>Predicate</code>, not null 122 */ 123 public void setPredicate(final Predicate predicate) { 124 this.predicate = predicate; 125 } 126 127 }